7005c1c38a8df3368b7c2a1032cabaed093b8883
[citadel.git] / citadel / include / ctdl_module.h
1
2 #ifndef CTDL_MODULE_H
3 #define CTDL_MODULE_H
4
5 #include "sysdep.h"
6
7 #ifdef HAVE_GC
8 #define GC_THREADS
9 #define GC_REDIRECT_TO_LOCAL
10 #include <gc/gc_local_alloc.h>
11 #else
12 #define GC_MALLOC malloc
13 #define GC_MALLOC_ATOMIC malloc
14 #define GC_FREE free
15 #define GC_REALLOC realloc
16 #endif
17
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <signal.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <syslog.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #include <limits.h>
36
37 #include <libcitadel.h>
38
39 #include "server.h"
40 #include "sysdep_decls.h"
41 #include "msgbase.h"
42 #include "threads.h"
43 #include "citadel_dirs.h"
44 #include "context.h"
45
46 /*
47  * define macros for module init stuff
48  */
49  
50 #define CTDL_MODULE_INIT(module_name) char *ctdl_module_##module_name##_init (int threading)
51
52 #define CTDL_INIT_CALL(module_name) ctdl_module_##module_name##_init (threading)
53
54 #define CTDL_MODULE_UPGRADE(module_name) char *ctdl_module_##module_name##_upgrade (void)
55
56 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
57
58 #define CtdlAideMessage(TEXT, SUBJECT)          \
59         quickie_message(                        \
60                 "Citadel",                      \
61                 NULL,                           \
62                 NULL,                           \
63                 AIDEROOM,                       \
64                 TEXT,                           \
65                 FMT_CITADEL,                    \
66                 SUBJECT) 
67
68
69 #define CtdlAideFPMessage(TEXT, SUBJECT, N, STR, STRLEN, ccid, ioid, TIME) \
70         flood_protect_quickie_message(                   \
71                 "Citadel",                               \
72                 NULL,                                    \
73                 NULL,                                    \
74                 AIDEROOM,                                \
75                 TEXT,                                    \
76                 FMT_CITADEL,                             \
77                 SUBJECT,                                 \
78                 N,                                       \
79                 STR,                                     \
80                 STRLEN,                                  \
81                 ccid,                                    \
82                 ioid,                                    \
83                 TIME)
84 /*
85  * Hook functions available to modules.
86  */
87 /* Priorities for  */
88 #define PRIO_QUEUE 500
89 #define PRIO_AGGR 1000
90 #define PRIO_SEND 1500
91 #define PRIO_CLEANUP 2000
92 /* Priorities for EVT_HOUSE */
93 #define PRIO_HOUSE 3000
94 /* Priorities for EVT_LOGIN */
95 #define PRIO_CREATE 10000
96 /* Priorities for EVT_LOGOUT */
97 #define PRIO_LOGOUT 15000
98 /* Priorities for EVT_LOGIN */
99 #define PRIO_LOGIN 20000
100 /* Priorities for EVT_START */
101 #define PRIO_START 25000
102 /* Priorities for EVT_STOP */
103 #define PRIO_STOP 30000
104 /* Priorities for EVT_ASYNC */
105 #define PRIO_ASYNC 35000
106 /* Priorities for EVT_SHUTDOWN */
107 #define PRIO_SHUTDOWN 40000
108 /* Priorities for EVT_UNSTEALTH */
109 #define PRIO_UNSTEALTH 45000
110 /* Priorities for EVT_STEALTH */
111 #define PRIO_STEALTH 50000
112 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr)(StrBuf*), int EventType, int Priority);
113 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType);
114
115
116 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType, int Priority);
117 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
118 void CtdlShutdownServiceHooks(void);
119
120 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
121 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
122
123 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
124 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
125
126 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes*),
127                                                         int EventType);
128 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
129                                int EventType);
130
131 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
132 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
133
134 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
135 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
136
137 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
138 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
139
140 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
141 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
142
143 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
144 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
145
146 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
147
148 void CtdlRegisterServiceHook(int tcp_port,
149                              char *sockpath,
150                              void (*h_greeting_function) (void),
151                              void (*h_command_function) (void),
152                              void (*h_async_function) (void),
153                              const char *ServiceName
154 );
155 void CtdlUnregisterServiceHook(int tcp_port,
156                         char *sockpath,
157                         void (*h_greeting_function) (void),
158                         void (*h_command_function) (void),
159                         void (*h_async_function) (void)
160 );
161
162 void CtdlRegisterFixedOutputHook(char *content_type,
163                         void (*output_function) (char *supplied_data, int len)
164 );
165 void CtdlUnRegisterFixedOutputHook(char *content_type);
166
167 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
168
169 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
170
171
172 /*
173  * Directory services hooks for LDAP etc
174  */
175
176 #define DIRECTORY_USER_DEL 1    // Delete a user entry
177 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
178 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
179 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
180 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
181 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
182
183 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
184 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
185
186 /* TODODRW: This needs to be changed into a hook type interface
187  * for now we have this horrible hack
188  */
189 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
190
191 /* return the current context list as an array and do it in a safe manner
192  * The returned data is a copy so only reading is useful
193  * The number of contexts is returned in count.
194  * Beware, this does not copy any of the data pointed to by the context.
195  * This means that you can not rely on things like the redirect buffer being valid.
196  * You must free the returned pointer when done.
197  */
198 struct CitContext *CtdlGetContextArray (int *count);
199 void CtdlFillSystemContext(struct CitContext *context, char *name);
200 int CtdlTrySingleUser(void);
201 void CtdlEndSingleUser(void);
202 int CtdlWantSingleUser(void);
203 int CtdlIsSingleUser(void);
204
205
206 int CtdlIsUserLoggedIn (char *user_name);
207 int CtdlIsUserLoggedInByNum (long usernum);
208 void CtdlBumpNewMailCounter(long which_user);
209
210
211 /*
212  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
213  * This provides a quick way to initialise a variable that might be used to indicate
214  * messages that should not be processed. EG. a new Sieve script will use this
215  * to record determine that messages older than this should not be processed.
216  * This function is defined in control.c
217  */
218 long CtdlGetCurrentMessageNumber(void);
219
220
221
222 /*
223  * Expose various room operation functions from room_ops.c to the modules API
224  */
225 typedef struct CfgLineType CfgLineType;
226 typedef struct RoomNetCfgLine RoomNetCfgLine;
227 typedef struct OneRoomNetCfg OneRoomNetCfg;
228
229 unsigned CtdlCreateRoom(char *new_room_name,
230                         int new_room_type,
231                         char *new_room_pass,
232                         int new_room_floor,
233                         int really_create,
234                         int avoid_access,
235                         int new_room_view);
236 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name);
237 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
238 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
239 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view);
240 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
241 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
242 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
243 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
244 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data, RoomNetCfg filter);
245 void SaveChangedConfigs(void);
246 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
247 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
248 void CtdlUserGoto (char *where, int display_result, int transiently, int *msgs, int *new, long *oldest, long *newest);
249 struct floor *CtdlGetCachedFloor(int floor_num);
250 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
251 void CtdlGetFloor (struct floor *flbuf, int floor_num);
252 void CtdlPutFloor (struct floor *flbuf, int floor_num);
253 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
254 int CtdlGetFloorByName(const char *floor_name);
255 int CtdlGetFloorByNameLock(const char *floor_name);
256 int CtdlGetAvailableFloor(void);
257 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
258 void CtdlPutRoom(struct ctdlroom *);
259
260 /*
261  * Possible return values for CtdlRenameRoom()
262  */
263 enum {
264         crr_ok,                         /* success */
265         crr_room_not_found,             /* room not found */
266         crr_already_exists,             /* new name already exists */
267         crr_noneditable,                /* cannot edit this room */
268         crr_invalid_floor,              /* target floor does not exist */
269         crr_access_denied               /* not allowed to edit this room */
270 };
271
272
273
274 /*
275  * API declarations from citserver.h
276  */
277 int CtdlAccessCheck(int);
278 /* 'required access level' values which may be passed to CtdlAccessCheck()
279  */
280 enum {
281         ac_none,
282         ac_logged_in_or_guest,
283         ac_logged_in,
284         ac_room_aide,
285         ac_aide,
286         ac_internal,
287 };
288
289
290
291 /*
292  * API declarations from serv_extensions.h
293  */
294 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
295
296 /* 
297  * Global system configuration
298  */
299 struct config {
300         char c_nodename[16];            /* short name of this node on a Citadel network */
301         char c_fqdn[64];                /* this site's fully qualified domain name */
302         char c_humannode[21];           /* human-readable site name */
303         char c_phonenum[16];            /* telephone number */
304         uid_t c_ctdluid;                /* uid of posix account under which Citadel will run */
305         char c_creataide;               /* 1 = creating a room auto-grants room aide privileges */
306         int c_sleeping;                 /* watchdog timer (seconds) */
307         char c_initax;                  /* initial access level for new users */
308         char c_regiscall;               /* after c_regiscall logins user will be asked to register */
309         char c_twitdetect;              /* automatically move messages from problem users to trashcan */
310         char c_twitroom[ROOMNAMELEN];   /* name of trashcan */
311         char c_moreprompt[80];          /* paginator prompt */
312         char c_restrict;                /* require per-user permission to send Internet mail */
313         long c_niu_1;
314         char c_site_location[32];       /* geographic location of this Citadel site */
315         char c_sysadm[26];              /* name of system administrator */
316         char c_niu_2[15];
317         int c_niu_3;
318         int c_maxsessions;              /* maximum number of concurrent sessions allowed */
319         char c_ip_addr[20];             /* bind address for listening sockets */
320         int c_port_number;              /* port number for Citadel protocol (usually 504) */
321         int c_niu_4;
322         struct ExpirePolicy c_ep;       /* default expire policy for the entire site */
323         int c_userpurge;                /* user purge time (in days) */
324         int c_roompurge;                /* room purge time (in days) */
325         char c_logpages[ROOMNAMELEN];
326         char c_createax;
327         long c_maxmsglen;
328         int c_min_workers;
329         int c_max_workers;
330         int c_pop3_port;
331         int c_smtp_port;
332         int c_rfc822_strict_from;
333         int c_aide_zap;
334         int c_imap_port;
335         time_t c_net_freq;
336         char c_disable_newu;
337         char c_enable_fulltext;
338         char c_baseroom[ROOMNAMELEN];
339         char c_aideroom[ROOMNAMELEN];
340         int c_purge_hour;
341         struct ExpirePolicy c_mbxep;
342         char c_ldap_host[128];
343         int c_ldap_port;
344         char c_ldap_base_dn[256];
345         char c_ldap_bind_dn[256];
346         char c_ldap_bind_pw[256];
347         int c_msa_port;
348         int c_imaps_port;
349         int c_pop3s_port;
350         int c_smtps_port;
351         char c_auto_cull;
352         char c_niu_5;
353         char c_allow_spoofing;
354         char c_journal_email;
355         char c_journal_pubmsgs;
356         char c_journal_dest[128];
357         char c_default_cal_zone[128];
358         int c_pftcpdict_port;
359         int c_managesieve_port;
360         int c_auth_mode;
361         char c_funambol_host[256];
362         int c_funambol_port;
363         char c_funambol_source[256];
364         char c_funambol_auth[256];
365         char c_rbl_at_greeting;
366         char c_master_user[32];
367         char c_master_pass[32];
368         char c_pager_program[256];
369         char c_imap_keep_from;
370         int c_xmpp_c2s_port;
371         int c_xmpp_s2s_port;
372         time_t c_pop3_fetch;
373         time_t c_pop3_fastest;
374         int c_spam_flag_only;
375         int c_guest_logins;
376         int c_nntp_port;
377         int c_nntps_port;
378 };
379 struct configlen {
380         long c_nodename;
381         long c_fqdn;
382         long c_humannode;
383         long c_phonenum;
384         long c_twitroom;
385         long c_moreprompt;
386         long c_site_location;
387         long c_sysadm;
388         long c_niu_2;
389         long c_ip_addr;
390         long c_logpages;
391         long c_baseroom;
392         long c_aideroom;
393         long c_ldap_host;
394         long c_ldap_base_dn;
395         long c_ldap_bind_dn;
396         long c_ldap_bind_pw;
397         long c_journal_dest;
398         long c_default_cal_zone;
399         long c_funambol_host;
400         long c_funambol_source;
401         long c_funambol_auth;
402         long c_master_user;
403         long c_master_pass;
404         long c_pager_program;
405 };
406
407 #define SET_CFGSTRBUF(which, buffer) configlen.which = safestrncpy(config.which, ChrPtr(buffer), sizeof(config.which))
408 #define SET_CFGSTR(which, buffer) configlen.which = safestrncpy(config.which, buffer, sizeof(config.which))
409
410 extern struct config config;
411 extern struct configlen configlen;
412
413
414 #define NODENAME                config.c_nodename
415 #define FQDN                    config.c_fqdn
416 #define CTDLUID                 config.c_ctdluid
417 #define CREATAIDE               config.c_creataide
418 #define REGISCALL               config.c_regiscall
419 #define TWITDETECT              config.c_twitdetect
420 #define TWITROOM                config.c_twitroom
421 #define RESTRICT_INTERNET       config.c_restrict
422
423 #define CFG_KEY(which) config.which, configlen.which
424
425 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
426 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
427 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
428
429 struct CfgLineType {
430         RoomNetCfg C;
431         CfgLineParser Parser;
432         CfgLineSerializer Serializer;
433         CfgLineDeAllocator DeAllocator;
434         ConstStr Str;
435         int IsSingleLine;
436         int nSegments;
437 };
438
439 struct RoomNetCfgLine {
440         RoomNetCfgLine *next;
441         int nValues;
442         StrBuf **Value;
443 };
444
445 struct OneRoomNetCfg {
446         long lastsent;
447         long changed;
448         StrBuf *Sender;
449         StrBuf *RoomInfo;
450         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
451         StrBuf *misc;
452 };
453
454
455 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
456 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
457 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
458 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
459 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
460 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
461 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
462
463 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber);
464
465 typedef struct _nodeconf {
466         int DeleteMe;
467         StrBuf *NodeName;
468         StrBuf *Secret;
469         StrBuf *Host;
470         StrBuf *Port;
471 }CtdlNodeConf;
472
473 HashList* CtdlLoadIgNetCfg(void);
474
475
476 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
477                                  size_t n,
478                                  const char* RemoteIdentifier);
479
480
481 typedef struct __NetMap {
482         StrBuf *NodeName;
483         time_t lastcontact;
484         StrBuf *NextHop;
485 }CtdlNetMap;
486
487 HashList* CtdlReadNetworkMap(void);
488 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
489 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed);
490 int CtdlIsValidNode(const StrBuf **nexthop,
491                     const StrBuf **secret,
492                     StrBuf *node,
493                     HashList *IgnetCfg,
494                     HashList *the_netmap);
495
496
497
498
499 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation);
500
501 /*
502  * Operations that can be performed by network_talking_to()
503  */
504 enum {
505         NTT_ADD,
506         NTT_REMOVE,
507         NTT_CHECK
508 };
509
510 /*
511  * Expose API calls from user_ops.c
512  */
513 int CtdlGetUser(struct ctdluser *usbuf, char *name);
514 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
515 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
516 void CtdlPutUser(struct ctdluser *usbuf);
517 void CtdlPutUserLock(struct ctdluser *usbuf);
518 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
519 void CtdlGetRelationship(visit *vbuf,
520                         struct ctdluser *rel_user,
521                         struct ctdlroom *rel_room);
522 void CtdlSetRelationship(visit *newvisit,
523                         struct ctdluser *rel_user,
524                         struct ctdlroom *rel_room);
525 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
526
527 int CtdlLoginExistingUser(char *authname, const char *username);
528
529 /*
530  * Values which may be returned by CtdlLoginExistingUser()
531  */
532 enum {
533         pass_ok,
534         pass_already_logged_in,
535         pass_no_user,
536         pass_internal_error,
537         pass_wrong_password
538 };
539
540 int CtdlTryPassword(const char *password, long len);
541 /*
542  * Values which may be returned by CtdlTryPassword()
543  */
544 enum {
545         login_ok,
546         login_already_logged_in,
547         login_too_many_users,
548         login_not_found
549 };
550
551 void CtdlUserLogout(void);
552
553
554
555
556 /*
557  * Expose API calls from msgbase.c
558  */
559
560
561
562 /*
563  * Expose API calls from euidindex.c
564  */
565 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
566
567
568
569 #endif /* CTDL_MODULE_H */