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