4a92f9bab49bbf300ded5d1b86ed85333553f814
[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  * if you say a) (which may take a while)
173  * don't forget to say b)
174  */
175 void CtdlDisableHouseKeeping(void);
176 void CtdlEnableHouseKeeping(void);
177
178 /*
179  * Directory services hooks for LDAP etc
180  */
181
182 #define DIRECTORY_USER_DEL 1    // Delete a user entry
183 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
184 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
185 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
186 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
187 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
188
189 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
190 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
191
192 /* TODODRW: This needs to be changed into a hook type interface
193  * for now we have this horrible hack
194  */
195 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
196
197 /* return the current context list as an array and do it in a safe manner
198  * The returned data is a copy so only reading is useful
199  * The number of contexts is returned in count.
200  * Beware, this does not copy any of the data pointed to by the context.
201  * This means that you can not rely on things like the redirect buffer being valid.
202  * You must free the returned pointer when done.
203  */
204 struct CitContext *CtdlGetContextArray (int *count);
205 void CtdlFillSystemContext(struct CitContext *context, char *name);
206 int CtdlTrySingleUser(void);
207 void CtdlEndSingleUser(void);
208 int CtdlWantSingleUser(void);
209 int CtdlIsSingleUser(void);
210
211
212 int CtdlIsUserLoggedIn (char *user_name);
213 int CtdlIsUserLoggedInByNum (long usernum);
214 void CtdlBumpNewMailCounter(long which_user);
215
216
217 /*
218  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
219  * This provides a quick way to initialise a variable that might be used to indicate
220  * messages that should not be processed. EG. a new Sieve script will use this
221  * to record determine that messages older than this should not be processed.
222  * This function is defined in control.c
223  */
224 long CtdlGetCurrentMessageNumber(void);
225
226
227
228 /*
229  * Expose various room operation functions from room_ops.c to the modules API
230  */
231 typedef struct CfgLineType CfgLineType;
232 typedef struct RoomNetCfgLine RoomNetCfgLine;
233 typedef struct OneRoomNetCfg OneRoomNetCfg;
234
235 unsigned CtdlCreateRoom(char *new_room_name,
236                         int new_room_type,
237                         char *new_room_pass,
238                         int new_room_floor,
239                         int really_create,
240                         int avoid_access,
241                         int new_room_view);
242 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name);
243 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
244 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
245 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view);
246 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
247 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
248 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
249 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
250 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data, RoomNetCfg filter);
251 void SaveChangedConfigs(void);
252 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
253 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
254 void CtdlUserGoto (char *where, int display_result, int transiently, int *msgs, int *new, long *oldest, long *newest);
255 struct floor *CtdlGetCachedFloor(int floor_num);
256 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
257 void CtdlGetFloor (struct floor *flbuf, int floor_num);
258 void CtdlPutFloor (struct floor *flbuf, int floor_num);
259 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
260 int CtdlGetFloorByName(const char *floor_name);
261 int CtdlGetFloorByNameLock(const char *floor_name);
262 int CtdlGetAvailableFloor(void);
263 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
264 void CtdlPutRoom(struct ctdlroom *);
265
266 /*
267  * Possible return values for CtdlRenameRoom()
268  */
269 enum {
270         crr_ok,                         /* success */
271         crr_room_not_found,             /* room not found */
272         crr_already_exists,             /* new name already exists */
273         crr_noneditable,                /* cannot edit this room */
274         crr_invalid_floor,              /* target floor does not exist */
275         crr_access_denied               /* not allowed to edit this room */
276 };
277
278
279
280 /*
281  * API declarations from citserver.h
282  */
283 int CtdlAccessCheck(int);
284 /* 'required access level' values which may be passed to CtdlAccessCheck()
285  */
286 enum {
287         ac_none,
288         ac_logged_in_or_guest,
289         ac_logged_in,
290         ac_room_aide,
291         ac_aide,
292         ac_internal,
293 };
294
295
296
297 /*
298  * API declarations from serv_extensions.h
299  */
300 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
301
302 /* 
303  * Global system configuration
304  */
305 struct config {
306         char c_nodename[16];            /* short name of this node on a Citadel network */
307         char c_fqdn[64];                /* this site's fully qualified domain name */
308         char c_humannode[21];           /* human-readable site name */
309         char c_phonenum[16];            /* telephone number */
310         uid_t c_ctdluid;                /* uid of posix account under which Citadel will run */
311         char c_creataide;               /* 1 = creating a room auto-grants room aide privileges */
312         int c_sleeping;                 /* watchdog timer (seconds) */
313         char c_initax;                  /* initial access level for new users */
314         char c_regiscall;               /* after c_regiscall logins user will be asked to register */
315         char c_twitdetect;              /* automatically move messages from problem users to trashcan */
316         char c_twitroom[ROOMNAMELEN];   /* name of trashcan */
317         char c_moreprompt[80];          /* paginator prompt */
318         char c_restrict;                /* require per-user permission to send Internet mail */
319         long c_niu_1;
320         char c_site_location[32];       /* geographic location of this Citadel site */
321         char c_sysadm[26];              /* name of system administrator */
322         char c_niu_2[15];
323         int c_niu_3;
324         int c_maxsessions;              /* maximum number of concurrent sessions allowed */
325         char c_ip_addr[20];             /* bind address for listening sockets */
326         int c_port_number;              /* port number for Citadel protocol (usually 504) */
327         int c_niu_4;
328         struct ExpirePolicy c_ep;       /* default expire policy for the entire site */
329         int c_userpurge;                /* user purge time (in days) */
330         int c_roompurge;                /* room purge time (in days) */
331         char c_logpages[ROOMNAMELEN];
332         char c_createax;
333         long c_maxmsglen;
334         int c_min_workers;
335         int c_max_workers;
336         int c_pop3_port;
337         int c_smtp_port;
338         int c_rfc822_strict_from;
339         int c_aide_zap;
340         int c_imap_port;
341         time_t c_net_freq;
342         char c_disable_newu;
343         char c_enable_fulltext;
344         char c_baseroom[ROOMNAMELEN];
345         char c_aideroom[ROOMNAMELEN];
346         int c_purge_hour;
347         struct ExpirePolicy c_mbxep;
348         char c_ldap_host[128];
349         int c_ldap_port;
350         char c_ldap_base_dn[256];
351         char c_ldap_bind_dn[256];
352         char c_ldap_bind_pw[256];
353         int c_msa_port;
354         int c_imaps_port;
355         int c_pop3s_port;
356         int c_smtps_port;
357         char c_auto_cull;
358         char c_niu_5;
359         char c_allow_spoofing;
360         char c_journal_email;
361         char c_journal_pubmsgs;
362         char c_journal_dest[128];
363         char c_default_cal_zone[128];
364         int c_pftcpdict_port;
365         int c_managesieve_port;
366         int c_auth_mode;
367         char c_funambol_host[256];
368         int c_funambol_port;
369         char c_funambol_source[256];
370         char c_funambol_auth[256];
371         char c_rbl_at_greeting;
372         char c_master_user[32];
373         char c_master_pass[32];
374         char c_pager_program[256];
375         char c_imap_keep_from;
376         int c_xmpp_c2s_port;
377         int c_xmpp_s2s_port;
378         time_t c_pop3_fetch;
379         time_t c_pop3_fastest;
380         int c_spam_flag_only;
381         int c_guest_logins;
382         int c_nntp_port;
383         int c_nntps_port;
384 };
385 struct configlen {
386         long c_nodename;
387         long c_fqdn;
388         long c_humannode;
389         long c_phonenum;
390         long c_twitroom;
391         long c_moreprompt;
392         long c_site_location;
393         long c_sysadm;
394         long c_niu_2;
395         long c_ip_addr;
396         long c_logpages;
397         long c_baseroom;
398         long c_aideroom;
399         long c_ldap_host;
400         long c_ldap_base_dn;
401         long c_ldap_bind_dn;
402         long c_ldap_bind_pw;
403         long c_journal_dest;
404         long c_default_cal_zone;
405         long c_funambol_host;
406         long c_funambol_source;
407         long c_funambol_auth;
408         long c_master_user;
409         long c_master_pass;
410         long c_pager_program;
411 };
412
413 #define SET_CFGSTRBUF(which, buffer) configlen.which = safestrncpy(config.which, ChrPtr(buffer), sizeof(config.which))
414 #define SET_CFGSTR(which, buffer) configlen.which = safestrncpy(config.which, buffer, sizeof(config.which))
415
416 extern struct config config;
417 extern struct configlen configlen;
418
419
420 #define NODENAME                config.c_nodename
421 #define FQDN                    config.c_fqdn
422 #define CTDLUID                 config.c_ctdluid
423 #define CREATAIDE               config.c_creataide
424 #define REGISCALL               config.c_regiscall
425 #define TWITDETECT              config.c_twitdetect
426 #define TWITROOM                config.c_twitroom
427 #define RESTRICT_INTERNET       config.c_restrict
428
429 #define CFG_KEY(which) config.which, configlen.which
430
431 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
432 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
433 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
434
435 struct CfgLineType {
436         RoomNetCfg C;
437         CfgLineParser Parser;
438         CfgLineSerializer Serializer;
439         CfgLineDeAllocator DeAllocator;
440         ConstStr Str;
441         int IsSingleLine;
442         int nSegments;
443 };
444
445 struct RoomNetCfgLine {
446         RoomNetCfgLine *next;
447         int nValues;
448         StrBuf **Value;
449 };
450
451 struct OneRoomNetCfg {
452         long lastsent;
453         long changed;
454         StrBuf *Sender;
455         StrBuf *RoomInfo;
456         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
457         StrBuf *misc;
458 };
459
460
461 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
462 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
463 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
464 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
465 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
466 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
467 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
468
469 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber);
470
471 typedef struct _nodeconf {
472         int DeleteMe;
473         StrBuf *NodeName;
474         StrBuf *Secret;
475         StrBuf *Host;
476         StrBuf *Port;
477 }CtdlNodeConf;
478
479 HashList* CtdlLoadIgNetCfg(void);
480
481
482 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
483                                  size_t n,
484                                  const char* RemoteIdentifier);
485
486
487 typedef struct __NetMap {
488         StrBuf *NodeName;
489         time_t lastcontact;
490         StrBuf *NextHop;
491 }CtdlNetMap;
492
493 HashList* CtdlReadNetworkMap(void);
494 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
495 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed);
496 int CtdlIsValidNode(const StrBuf **nexthop,
497                     const StrBuf **secret,
498                     StrBuf *node,
499                     HashList *IgnetCfg,
500                     HashList *the_netmap);
501
502
503
504
505 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation);
506
507 /*
508  * Operations that can be performed by network_talking_to()
509  */
510 enum {
511         NTT_ADD,
512         NTT_REMOVE,
513         NTT_CHECK
514 };
515
516 /*
517  * Expose API calls from user_ops.c
518  */
519 int CtdlGetUser(struct ctdluser *usbuf, char *name);
520 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
521 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
522 void CtdlPutUser(struct ctdluser *usbuf);
523 void CtdlPutUserLock(struct ctdluser *usbuf);
524
525 int CtdlLockGetCurrentUser(void);
526 void CtdlPutCurrentUserLock(void);
527
528 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
529 void CtdlGetRelationship(visit *vbuf,
530                         struct ctdluser *rel_user,
531                         struct ctdlroom *rel_room);
532 void CtdlSetRelationship(visit *newvisit,
533                         struct ctdluser *rel_user,
534                         struct ctdlroom *rel_room);
535 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
536
537 int CtdlLoginExistingUser(char *authname, const char *username);
538
539 /*
540  * Values which may be returned by CtdlLoginExistingUser()
541  */
542 enum {
543         pass_ok,
544         pass_already_logged_in,
545         pass_no_user,
546         pass_internal_error,
547         pass_wrong_password
548 };
549
550 int CtdlTryPassword(const char *password, long len);
551 /*
552  * Values which may be returned by CtdlTryPassword()
553  */
554 enum {
555         login_ok,
556         login_already_logged_in,
557         login_too_many_users,
558         login_not_found
559 };
560
561 void CtdlUserLogout(void);
562
563
564
565
566 /*
567  * Expose API calls from msgbase.c
568  */
569
570
571
572 /*
573  * Expose API calls from euidindex.c
574  */
575 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
576
577
578
579 #endif /* CTDL_MODULE_H */