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