68577186373401dc521a5fe5d372853c24c537ed
[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 typedef struct CfgLineType CfgLineType;
230 typedef struct RoomNetCfgLine RoomNetCfgLine;
231 typedef struct OneRoomNetCfg OneRoomNetCfg;
232
233 unsigned CtdlCreateRoom(char *new_room_name,
234                         int new_room_type,
235                         char *new_room_pass,
236                         int new_room_floor,
237                         int really_create,
238                         int avoid_access,
239                         int new_room_view);
240 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name);
241 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
242 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
243 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
244                 int *result, int *view);
245 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
246 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
247 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
248 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
249 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB,
250                            void *in_data,
251                            RoomNetCfg filter);
252 void SaveChangedConfigs(void);
253
254 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
255 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
256 void CtdlUserGoto (char *where, int display_result, int transiently,
257                         int *msgs, int *new);
258 struct floor *CtdlGetCachedFloor(int floor_num);
259 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
260 void CtdlGetFloor (struct floor *flbuf, int floor_num);
261 void CtdlPutFloor (struct floor *flbuf, int floor_num);
262 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
263 int CtdlGetFloorByName(const char *floor_name);
264 int CtdlGetFloorByNameLock(const char *floor_name);
265 int CtdlGetAvailableFloor(void);
266 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
267 void CtdlPutRoom(struct ctdlroom *);
268
269 /*
270  * Possible return values for CtdlRenameRoom()
271  */
272 enum {
273         crr_ok,                         /* success */
274         crr_room_not_found,             /* room not found */
275         crr_already_exists,             /* new name already exists */
276         crr_noneditable,                /* cannot edit this room */
277         crr_invalid_floor,              /* target floor does not exist */
278         crr_access_denied               /* not allowed to edit this room */
279 };
280
281
282
283 /*
284  * API declarations from citserver.h
285  */
286 int CtdlAccessCheck(int);
287 /* 'required access level' values which may be passed to CtdlAccessCheck()
288  */
289 enum {
290         ac_none,
291         ac_logged_in_or_guest,
292         ac_logged_in,
293         ac_room_aide,
294         ac_aide,
295         ac_internal,
296 };
297
298
299
300 /*
301  * API declarations from serv_extensions.h
302  */
303 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
304
305 /* 
306  * Global system configuration
307  */
308 struct config {
309         char c_nodename[16];            /* short name of this node on a Citadel network */
310         char c_fqdn[64];                /* this site's fully qualified domain name */
311         char c_humannode[21];           /* human-readable site name */
312         char c_phonenum[16];            /* telephone number */
313         uid_t c_ctdluid;                /* uid of posix account under which Citadel will run */
314         char c_creataide;               /* 1 = creating a room auto-grants room aide privileges */
315         int c_sleeping;                 /* watchdog timer (seconds) */
316         char c_initax;                  /* initial access level for new users */
317         char c_regiscall;               /* after c_regiscall logins user will be asked to register */
318         char c_twitdetect;              /* automatically move messages from problem users to trashcan */
319         char c_twitroom[ROOMNAMELEN];   /* name of trashcan */
320         char c_moreprompt[80];          /* paginator prompt */
321         char c_restrict;                /* require per-user permission to send Internet mail */
322         long c_niu_1;
323         char c_site_location[32];       /* geographic location of this Citadel site */
324         char c_sysadm[26];              /* name of system administrator */
325         char c_niu_2[15];
326         int c_niu_3;
327         int c_maxsessions;              /* maximum number of concurrent sessions allowed */
328         char c_ip_addr[20];             /* bind address for listening sockets */
329         int c_port_number;              /* port number for Citadel protocol (usually 504) */
330         int c_niu_4;
331         struct ExpirePolicy c_ep;       /* default expire policy for the entire site */
332         int c_userpurge;                /* user purge time (in days) */
333         int c_roompurge;                /* room purge time (in days) */
334         char c_logpages[ROOMNAMELEN];
335         char c_createax;
336         long c_maxmsglen;
337         int c_min_workers;
338         int c_max_workers;
339         int c_pop3_port;
340         int c_smtp_port;
341         int c_rfc822_strict_from;
342         int c_aide_zap;
343         int c_imap_port;
344         time_t c_net_freq;
345         char c_disable_newu;
346         char c_enable_fulltext;
347         char c_baseroom[ROOMNAMELEN];
348         char c_aideroom[ROOMNAMELEN];
349         int c_purge_hour;
350         struct ExpirePolicy c_mbxep;
351         char c_ldap_host[128];
352         int c_ldap_port;
353         char c_ldap_base_dn[256];
354         char c_ldap_bind_dn[256];
355         char c_ldap_bind_pw[256];
356         int c_msa_port;
357         int c_imaps_port;
358         int c_pop3s_port;
359         int c_smtps_port;
360         char c_auto_cull;
361         char c_instant_expunge;
362         char c_allow_spoofing;
363         char c_journal_email;
364         char c_journal_pubmsgs;
365         char c_journal_dest[128];
366         char c_default_cal_zone[128];
367         int c_pftcpdict_port;
368         int c_managesieve_port;
369         int c_auth_mode;
370         char c_funambol_host[256];
371         int c_funambol_port;
372         char c_funambol_source[256];
373         char c_funambol_auth[256];
374         char c_rbl_at_greeting;
375         char c_master_user[32];
376         char c_master_pass[32];
377         char c_pager_program[256];
378         char c_imap_keep_from;
379         int c_xmpp_c2s_port;
380         int c_xmpp_s2s_port;
381         time_t c_pop3_fetch;
382         time_t c_pop3_fastest;
383         int c_spam_flag_only;
384         int c_guest_logins;
385 };
386
387 extern struct config config;
388
389
390 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
391 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
392 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
393
394 struct CfgLineType {
395         RoomNetCfg C;
396         CfgLineParser Parser;
397         CfgLineSerializer Serializer;
398         CfgLineDeAllocator DeAllocator;
399         ConstStr Str;
400         int IsSingleLine;
401         int nSegments;
402 };
403
404 struct RoomNetCfgLine {
405         RoomNetCfgLine *next;
406         int nValues;
407         StrBuf **Value;
408 };
409
410 struct OneRoomNetCfg {
411         long lastsent;
412         long changed;
413         StrBuf *Sender;
414         StrBuf *RoomInfo;
415         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
416         StrBuf *misc;
417 };
418
419
420 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
421 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
422 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
423 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
424 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
425 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
426 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
427
428 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber);
429
430 typedef struct _nodeconf {
431         int DeleteMe;
432         StrBuf *NodeName;
433         StrBuf *Secret;
434         StrBuf *Host;
435         StrBuf *Port;
436 }CtdlNodeConf;
437
438 HashList* CtdlLoadIgNetCfg(void);
439
440
441 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
442                                  size_t n,
443                                  const char* RemoteIdentifier);
444
445
446 typedef struct __NetMap {
447         StrBuf *NodeName;
448         time_t lastcontact;
449         StrBuf *NextHop;
450 }CtdlNetMap;
451
452 HashList* CtdlReadNetworkMap(void);
453 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
454 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed);
455 int CtdlIsValidNode(const StrBuf **nexthop,
456                     const StrBuf **secret,
457                     StrBuf *node,
458                     HashList *IgnetCfg,
459                     HashList *the_netmap);
460
461
462
463
464 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation);
465
466 /*
467  * Operations that can be performed by network_talking_to()
468  */
469 enum {
470         NTT_ADD,
471         NTT_REMOVE,
472         NTT_CHECK
473 };
474
475 /*
476  * Expose API calls from user_ops.c
477  */
478 int CtdlGetUser(struct ctdluser *usbuf, char *name);
479 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
480 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
481 void CtdlPutUser(struct ctdluser *usbuf);
482 void CtdlPutUserLock(struct ctdluser *usbuf);
483 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
484 void CtdlGetRelationship(visit *vbuf,
485                         struct ctdluser *rel_user,
486                         struct ctdlroom *rel_room);
487 void CtdlSetRelationship(visit *newvisit,
488                         struct ctdluser *rel_user,
489                         struct ctdlroom *rel_room);
490 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
491
492 int CtdlLoginExistingUser(char *authname, const char *username);
493
494 /*
495  * Values which may be returned by CtdlLoginExistingUser()
496  */
497 enum {
498         pass_ok,
499         pass_already_logged_in,
500         pass_no_user,
501         pass_internal_error,
502         pass_wrong_password
503 };
504
505 int CtdlTryPassword(const char *password, long len);
506 /*
507  * Values which may be returned by CtdlTryPassword()
508  */
509 enum {
510         login_ok,
511         login_already_logged_in,
512         login_too_many_users,
513         login_not_found
514 };
515
516 void CtdlUserLogout(void);
517
518
519
520
521 /*
522  * Expose API calls from msgbase.c
523  */
524 char *CtdlGetSysConfig(char *sysconfname);
525 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata);
526
527
528
529
530 /*
531  * Expose API calls from euidindex.c
532  */
533 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
534
535
536 #endif /* CTDL_MODULE_H */