8a0f265ff8dba1b7198572bca6f3f2600d1fa752
[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 struct configlen {
394         long c_nodename;
395         long c_fqdn;
396         long c_humannode;
397         long c_phonenum;
398         long c_twitroom;
399         long c_moreprompt;
400         long c_site_location;
401         long c_sysadm;
402         long c_niu_2;
403         long c_ip_addr;
404         long c_logpages;
405         long c_baseroom;
406         long c_aideroom;
407         long c_ldap_host;
408         long c_ldap_base_dn;
409         long c_ldap_bind_dn;
410         long c_ldap_bind_pw;
411         long c_journal_dest;
412         long c_default_cal_zone;
413         long c_funambol_host;
414         long c_funambol_source;
415         long c_funambol_auth;
416         long c_master_user;
417         long c_master_pass;
418         long c_pager_program;
419 };
420
421 extern struct config config;
422 extern struct configlen configlen;
423
424
425 #define NODENAME                config.c_nodename
426 #define FQDN                    config.c_fqdn
427 #define CTDLUID                 config.c_ctdluid
428 #define CREATAIDE               config.c_creataide
429 #define REGISCALL               config.c_regiscall
430 #define TWITDETECT              config.c_twitdetect
431 #define TWITROOM                config.c_twitroom
432 #define RESTRICT_INTERNET       config.c_restrict
433
434 #define CFG_KEY(which) config.which, configlen.which
435
436 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
437 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
438 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
439
440 struct CfgLineType {
441         RoomNetCfg C;
442         CfgLineParser Parser;
443         CfgLineSerializer Serializer;
444         CfgLineDeAllocator DeAllocator;
445         ConstStr Str;
446         int IsSingleLine;
447         int nSegments;
448 };
449
450 struct RoomNetCfgLine {
451         RoomNetCfgLine *next;
452         int nValues;
453         StrBuf **Value;
454 };
455
456 struct OneRoomNetCfg {
457         long lastsent;
458         long changed;
459         StrBuf *Sender;
460         StrBuf *RoomInfo;
461         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
462         StrBuf *misc;
463 };
464
465
466 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
467 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
468 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
469 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
470 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
471 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
472 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
473
474 OneRoomNetCfg* CtdlGetNetCfgForRoom(long QRNumber);
475
476 typedef struct _nodeconf {
477         int DeleteMe;
478         StrBuf *NodeName;
479         StrBuf *Secret;
480         StrBuf *Host;
481         StrBuf *Port;
482 }CtdlNodeConf;
483
484 HashList* CtdlLoadIgNetCfg(void);
485
486
487 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
488                                  size_t n,
489                                  const char* RemoteIdentifier);
490
491
492 typedef struct __NetMap {
493         StrBuf *NodeName;
494         time_t lastcontact;
495         StrBuf *NextHop;
496 }CtdlNetMap;
497
498 HashList* CtdlReadNetworkMap(void);
499 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
500 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed);
501 int CtdlIsValidNode(const StrBuf **nexthop,
502                     const StrBuf **secret,
503                     StrBuf *node,
504                     HashList *IgnetCfg,
505                     HashList *the_netmap);
506
507
508
509
510 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation);
511
512 /*
513  * Operations that can be performed by network_talking_to()
514  */
515 enum {
516         NTT_ADD,
517         NTT_REMOVE,
518         NTT_CHECK
519 };
520
521 /*
522  * Expose API calls from user_ops.c
523  */
524 int CtdlGetUser(struct ctdluser *usbuf, char *name);
525 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
526 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
527 void CtdlPutUser(struct ctdluser *usbuf);
528 void CtdlPutUserLock(struct ctdluser *usbuf);
529 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
530 void CtdlGetRelationship(visit *vbuf,
531                         struct ctdluser *rel_user,
532                         struct ctdlroom *rel_room);
533 void CtdlSetRelationship(visit *newvisit,
534                         struct ctdluser *rel_user,
535                         struct ctdlroom *rel_room);
536 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
537
538 int CtdlLoginExistingUser(char *authname, const char *username);
539
540 /*
541  * Values which may be returned by CtdlLoginExistingUser()
542  */
543 enum {
544         pass_ok,
545         pass_already_logged_in,
546         pass_no_user,
547         pass_internal_error,
548         pass_wrong_password
549 };
550
551 int CtdlTryPassword(const char *password, long len);
552 /*
553  * Values which may be returned by CtdlTryPassword()
554  */
555 enum {
556         login_ok,
557         login_already_logged_in,
558         login_too_many_users,
559         login_not_found
560 };
561
562 void CtdlUserLogout(void);
563
564
565
566
567 /*
568  * Expose API calls from msgbase.c
569  */
570
571
572
573 /*
574  * Expose API calls from euidindex.c
575  */
576 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
577
578
579
580 #endif /* CTDL_MODULE_H */