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