eddcbeffcc92df2cfe961eb23cb9001200a8518b
[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) \
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 /*
93  * Hook functions available to modules.
94  */
95 /* Priorities for  */
96 #define PRIO_QUEUE 500
97 #define PRIO_AGGR 1000
98 #define PRIO_SEND 1500
99 #define PRIO_CLEANUP 2000
100 /* Priorities for EVT_HOUSE */
101 #define PRIO_HOUSE 3000
102 /* Priorities for EVT_LOGIN */
103 #define PRIO_CREATE 10000
104 /* Priorities for EVT_LOGOUT */
105 #define PRIO_LOGOUT 15000
106 /* Priorities for EVT_LOGIN */
107 #define PRIO_LOGIN 20000
108 /* Priorities for EVT_START */
109 #define PRIO_START 25000
110 /* Priorities for EVT_STOP */
111 #define PRIO_STOP 30000
112 /* Priorities for EVT_ASYNC */
113 #define PRIO_ASYNC 35000
114 /* Priorities for EVT_SHUTDOWN */
115 #define PRIO_SHUTDOWN 40000
116 /* Priorities for EVT_UNSTEALTH */
117 #define PRIO_UNSTEALTH 45000
118 /* Priorities for EVT_STEALTH */
119 #define PRIO_STEALTH 50000
120 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType, int Priority);
121 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
122 void CtdlShutdownServiceHooks(void);
123
124 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
125 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
126
127 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
128 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
129
130 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
131                                                         int EventType);
132 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
133                                                         int EventType);
134
135 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
136 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
137
138 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
139 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
140
141 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
142 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
143
144 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
145 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
146
147 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
148 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
149
150 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
151
152 void CtdlRegisterServiceHook(int tcp_port,
153                              char *sockpath,
154                              void (*h_greeting_function) (void),
155                              void (*h_command_function) (void),
156                              void (*h_async_function) (void),
157                              const char *ServiceName
158 );
159 void CtdlUnregisterServiceHook(int tcp_port,
160                         char *sockpath,
161                         void (*h_greeting_function) (void),
162                         void (*h_command_function) (void),
163                         void (*h_async_function) (void)
164 );
165
166 void CtdlRegisterFixedOutputHook(char *content_type,
167                         void (*output_function) (char *supplied_data, int len)
168 );
169 void CtdlUnRegisterFixedOutputHook(char *content_type);
170
171 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
172
173 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
174
175
176 /*
177  * Directory services hooks for LDAP etc
178  */
179
180 #define DIRECTORY_USER_DEL 1    // Delete a user entry
181 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
182 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
183 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
184 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
185 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
186
187 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
188 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
189
190 /* TODODRW: This needs to be changed into a hook type interface
191  * for now we have this horrible hack
192  */
193 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
194
195 /* return the current context list as an array and do it in a safe manner
196  * The returned data is a copy so only reading is useful
197  * The number of contexts is returned in count.
198  * Beware, this does not copy any of the data pointed to by the context.
199  * This means that you can not rely on things like the redirect buffer being valid.
200  * You must free the returned pointer when done.
201  */
202 struct CitContext *CtdlGetContextArray (int *count);
203 void CtdlFillSystemContext(struct CitContext *context, char *name);
204 int CtdlTrySingleUser(void);
205 void CtdlEndSingleUser(void);
206 int CtdlWantSingleUser(void);
207 int CtdlIsSingleUser(void);
208
209
210 int CtdlIsUserLoggedIn (char *user_name);
211 int CtdlIsUserLoggedInByNum (long usernum);
212 void CtdlBumpNewMailCounter(long which_user);
213
214
215 /*
216  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
217  * This provides a quick way to initialise a variable that might be used to indicate
218  * messages that should not be processed. EG. a new Sieve script will use this
219  * to record determine that messages older than this should not be processed.
220  * This function is defined in control.c
221  */
222 long CtdlGetCurrentMessageNumber(void);
223
224
225
226 /*
227  * Expose various room operation functions from room_ops.c to the modules API
228  */
229
230 unsigned CtdlCreateRoom(char *new_room_name,
231                         int new_room_type,
232                         char *new_room_pass,
233                         int new_room_floor,
234                         int really_create,
235                         int avoid_access,
236                         int new_room_view);
237 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name);
238 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
239 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
240 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
241                 int *result, int *view);
242 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
243 void CtdlForEachRoom(void (*CallBack)(struct ctdlroom *EachRoom, void *out_data),
244         void *in_data);
245 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
246 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
247 void CtdlUserGoto (char *where, int display_result, int transiently,
248                         int *msgs, int *new);
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_instant_expunge;
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 };
377
378 extern struct config config;
379
380
381 typedef struct CfgLineType CfgLineType;
382 typedef struct RoomNetCfgLine RoomNetCfgLine;
383 typedef struct OneRoomNetCfg OneRoomNetCfg;
384
385 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
386 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
387 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
388
389 struct CfgLineType {
390         RoomNetCfg C;
391         CfgLineParser Parser;
392         CfgLineSerializer Serializer;
393         CfgLineDeAllocator DeAllocator;
394         ConstStr Str;
395         int IsSingleLine;
396 };
397
398 struct RoomNetCfgLine {
399         RoomNetCfgLine *next;
400         StrBuf *Value;
401 };
402
403 struct OneRoomNetCfg {
404         long lastsent;
405         StrBuf *Sender;
406         StrBuf *RoomInfo;
407         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
408         StrBuf *misc;
409 };
410
411
412 #define CtdlREGISTERRoomCfgType(a, p, uniq, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, s, d);
413 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, CfgLineSerializer s, CfgLineDeAllocator d);
414 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
415 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
416 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
417
418
419
420
421 /*
422  * Expose API calls from user_ops.c
423  */
424 int CtdlGetUser(struct ctdluser *usbuf, char *name);
425 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
426 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
427 void CtdlPutUser(struct ctdluser *usbuf);
428 void CtdlPutUserLock(struct ctdluser *usbuf);
429 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
430 void CtdlGetRelationship(visit *vbuf,
431                         struct ctdluser *rel_user,
432                         struct ctdlroom *rel_room);
433 void CtdlSetRelationship(visit *newvisit,
434                         struct ctdluser *rel_user,
435                         struct ctdlroom *rel_room);
436 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
437
438 int CtdlLoginExistingUser(char *authname, const char *username);
439
440 /*
441  * Values which may be returned by CtdlLoginExistingUser()
442  */
443 enum {
444         pass_ok,
445         pass_already_logged_in,
446         pass_no_user,
447         pass_internal_error,
448         pass_wrong_password
449 };
450
451 int CtdlTryPassword(const char *password, long len);
452 /*
453  * Values which may be returned by CtdlTryPassword()
454  */
455 enum {
456         login_ok,
457         login_already_logged_in,
458         login_too_many_users,
459         login_not_found
460 };
461
462 void CtdlUserLogout(void);
463
464
465
466
467 /*
468  * Expose API calls from msgbase.c
469  */
470 char *CtdlGetSysConfig(char *sysconfname);
471 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata);
472
473
474
475
476 /*
477  * Expose API calls from euidindex.c
478  */
479 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
480
481
482 #endif /* CTDL_MODULE_H */