9b7f8e2f96ae460f671a8cddfb8c39561987abfb
[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
49 #ifndef HAVE_SNPRINTF
50 #include "snprintf.h"
51 #endif
52
53
54 #include <libcitadel.h>
55
56 #include "server.h"
57 #include "sysdep_decls.h"
58 #include "msgbase.h"
59 #include "threads.h"
60 #include "citadel_dirs.h"
61 #include "context.h"
62
63 /*
64  * define macros for module init stuff
65  */
66  
67 #define CTDL_MODULE_INIT(module_name) char *ctdl_module_##module_name##_init (int threading)
68
69 #define CTDL_INIT_CALL(module_name) ctdl_module_##module_name##_init (threading)
70
71 #define CTDL_MODULE_UPGRADE(module_name) char *ctdl_module_##module_name##_upgrade (void)
72
73 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
74
75 #define CtdlAideMessage(TEXT, SUBJECT) quickie_message("Citadel",NULL,NULL,AIDEROOM,TEXT,FMT_CITADEL,SUBJECT) 
76 /*
77  * Hook functions available to modules.
78  */
79 /* Priorities for  */
80 #define PRIO_QUEUE 500
81 #define PRIO_AGGR 1000
82 #define PRIO_SEND 1500
83 #define PRIO_CLEANUP 2000
84 /* Priorities for EVT_HOUSE */
85 #define PRIO_HOUSE 3000
86 /* Priorities for EVT_LOGIN */
87 #define PRIO_CREATE 10000
88 /* Priorities for EVT_LOGOUT */
89 #define PRIO_LOGOUT 15000
90 /* Priorities for EVT_LOGIN */
91 #define PRIO_LOGIN 20000
92 /* Priorities for EVT_START */
93 #define PRIO_START 25000
94 /* Priorities for EVT_STOP */
95 #define PRIO_STOP 30000
96 /* Priorities for EVT_ASYNC */
97 #define PRIO_ASYNC 35000
98 /* Priorities for EVT_SHUTDOWN */
99 #define PRIO_SHUTDOWN 40000
100 /* Priorities for EVT_UNSTEALTH */
101 #define PRIO_UNSTEALTH 45000
102 /* Priorities for EVT_STEALTH */
103 #define PRIO_STEALTH 50000
104 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType, int Priority);
105 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
106 void CtdlShutdownServiceHooks(void);
107
108 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
109 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
110
111 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
112 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
113
114 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
115                                                         int EventType);
116 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
117                                                         int EventType);
118
119 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
120 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
121
122 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
123 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
124
125 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
126 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
127
128 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
129 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
130
131 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
132 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
133
134 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
135
136 void CtdlRegisterServiceHook(int tcp_port,
137                              char *sockpath,
138                              void (*h_greeting_function) (void),
139                              void (*h_command_function) (void),
140                              void (*h_async_function) (void),
141                              const char *ServiceName
142 );
143 void CtdlUnregisterServiceHook(int tcp_port,
144                         char *sockpath,
145                         void (*h_greeting_function) (void),
146                         void (*h_command_function) (void),
147                         void (*h_async_function) (void)
148 );
149
150 void CtdlRegisterFixedOutputHook(char *content_type,
151                         void (*output_function) (char *supplied_data, int len)
152 );
153 void CtdlUnRegisterFixedOutputHook(char *content_type);
154
155 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
156
157 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
158
159
160 /*
161  * Directory services hooks for LDAP etc
162  */
163
164 #define DIRECTORY_USER_DEL 1    // Delete a user entry
165 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
166 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
167 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
168 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
169 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
170
171 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
172 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
173
174 /* TODODRW: This needs to be changed into a hook type interface
175  * for now we have this horrible hack
176  */
177 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
178
179 /* return the current context list as an array and do it in a safe manner
180  * The returned data is a copy so only reading is useful
181  * The number of contexts is returned in count.
182  * Beware, this does not copy any of the data pointed to by the context.
183  * This means that you can not rely on things like the redirect buffer being valid.
184  * You must free the returned pointer when done.
185  */
186 struct CitContext *CtdlGetContextArray (int *count);
187 void CtdlFillSystemContext(struct CitContext *context, char *name);
188 int CtdlTrySingleUser(void);
189 void CtdlEndSingleUser(void);
190 int CtdlWantSingleUser(void);
191 int CtdlIsSingleUser(void);
192
193
194 int CtdlIsUserLoggedIn (char *user_name);
195 int CtdlIsUserLoggedInByNum (long usernum);
196 void CtdlBumpNewMailCounter(long which_user);
197
198
199 /*
200  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
201  * This provides a quick way to initialise a variable that might be used to indicate
202  * messages that should not be processed. EG. a new Sieve script will use this
203  * to record determine that messages older than this should not be processed.
204  * This function is defined in control.c
205  */
206 long CtdlGetCurrentMessageNumber(void);
207
208
209
210 /*
211  * Expose various room operation functions from room_ops.c to the modules API
212  */
213
214 unsigned CtdlCreateRoom(char *new_room_name,
215                         int new_room_type,
216                         char *new_room_pass,
217                         int new_room_floor,
218                         int really_create,
219                         int avoid_access,
220                         int new_room_view);
221 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name);
222 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
223 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
224 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
225                 int *result, int *view);
226 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
227 void CtdlForEachRoom(void (*CallBack)(struct ctdlroom *EachRoom, void *out_data),
228         void *in_data);
229 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
230 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
231 void CtdlUserGoto (char *where, int display_result, int transiently,
232                         int *msgs, int *new);
233 struct floor *CtdlGetCachedFloor(int floor_num);
234 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
235 void CtdlGetFloor (struct floor *flbuf, int floor_num);
236 void CtdlPutFloor (struct floor *flbuf, int floor_num);
237 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
238 int CtdlGetFloorByName(const char *floor_name);
239 int CtdlGetFloorByNameLock(const char *floor_name);
240 int CtdlGetAvailableFloor(void);
241 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
242 void CtdlPutRoom(struct ctdlroom *);
243
244 /*
245  * Possible return values for CtdlRenameRoom()
246  */
247 enum {
248         crr_ok,                         /* success */
249         crr_room_not_found,             /* room not found */
250         crr_already_exists,             /* new name already exists */
251         crr_noneditable,                /* cannot edit this room */
252         crr_invalid_floor,              /* target floor does not exist */
253         crr_access_denied               /* not allowed to edit this room */
254 };
255
256
257
258 /*
259  * API declarations from citserver.h
260  */
261 int CtdlAccessCheck(int);
262 /* 'required access level' values which may be passed to CtdlAccessCheck()
263  */
264 enum {
265         ac_none,
266         ac_logged_in_or_guest,
267         ac_logged_in,
268         ac_room_aide,
269         ac_aide,
270         ac_internal,
271 };
272
273
274
275 /*
276  * API declarations from serv_extensions.h
277  */
278 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
279
280 /* 
281  * Global system configuration
282  */
283 struct config {
284         char c_nodename[16];            /* short name of this node on a Citadel network */
285         char c_fqdn[64];                /* this site's fully qualified domain name */
286         char c_humannode[21];           /* human-readable site name */
287         char c_phonenum[16];            /* telephone number */
288         uid_t c_ctdluid;                /* uid of posix account under which Citadel will run */
289         char c_creataide;               /* 1 = creating a room auto-grants room aide privileges */
290         int c_sleeping;                 /* watchdog timer (seconds) */
291         char c_initax;                  /* initial access level for new users */
292         char c_regiscall;               /* after c_regiscall logins user will be asked to register */
293         char c_twitdetect;              /* automatically move messages from problem users to trashcan */
294         char c_twitroom[ROOMNAMELEN];   /* name of trashcan */
295         char c_moreprompt[80];          /* paginator prompt */
296         char c_restrict;                /* require per-user permission to send Internet mail */
297         long c_niu_1;
298         char c_site_location[32];       /* geographic location of this Citadel site */
299         char c_sysadm[26];              /* name of system administrator */
300         char c_niu_2[15];
301         int c_niu_3;
302         int c_maxsessions;              /* maximum number of concurrent sessions allowed */
303         char c_ip_addr[20];             /* bind address for listening sockets */
304         int c_port_number;              /* port number for Citadel protocol (usually 504) */
305         int c_niu_4;
306         struct ExpirePolicy c_ep;       /* default expire policy for the entire site */
307         int c_userpurge;                /* user purge time (in days) */
308         int c_roompurge;                /* room purge time (in days) */
309         char c_logpages[ROOMNAMELEN];
310         char c_createax;
311         long c_maxmsglen;
312         int c_min_workers;
313         int c_max_workers;
314         int c_pop3_port;
315         int c_smtp_port;
316         int c_rfc822_strict_from;
317         int c_aide_zap;
318         int c_imap_port;
319         time_t c_net_freq;
320         char c_disable_newu;
321         char c_enable_fulltext;
322         char c_baseroom[ROOMNAMELEN];
323         char c_aideroom[ROOMNAMELEN];
324         int c_purge_hour;
325         struct ExpirePolicy c_mbxep;
326         char c_ldap_host[128];
327         int c_ldap_port;
328         char c_ldap_base_dn[256];
329         char c_ldap_bind_dn[256];
330         char c_ldap_bind_pw[256];
331         int c_msa_port;
332         int c_imaps_port;
333         int c_pop3s_port;
334         int c_smtps_port;
335         char c_auto_cull;
336         char c_instant_expunge;
337         char c_allow_spoofing;
338         char c_journal_email;
339         char c_journal_pubmsgs;
340         char c_journal_dest[128];
341         char c_default_cal_zone[128];
342         int c_pftcpdict_port;
343         int c_managesieve_port;
344         int c_auth_mode;
345         char c_funambol_host[256];
346         int c_funambol_port;
347         char c_funambol_source[256];
348         char c_funambol_auth[256];
349         char c_rbl_at_greeting;
350         char c_master_user[32];
351         char c_master_pass[32];
352         char c_pager_program[256];
353         char c_imap_keep_from;
354         int c_xmpp_c2s_port;
355         int c_xmpp_s2s_port;
356         time_t c_pop3_fetch;
357         time_t c_pop3_fastest;
358         int c_spam_flag_only;
359         int c_guest_logins;
360 };
361
362 extern struct config config;
363
364
365
366 /*
367  * Expose API calls from user_ops.c
368  */
369 int CtdlGetUser(struct ctdluser *usbuf, char *name);
370 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
371 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
372 void CtdlPutUser(struct ctdluser *usbuf);
373 void CtdlPutUserLock(struct ctdluser *usbuf);
374 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
375 void CtdlGetRelationship(visit *vbuf,
376                         struct ctdluser *rel_user,
377                         struct ctdlroom *rel_room);
378 void CtdlSetRelationship(visit *newvisit,
379                         struct ctdluser *rel_user,
380                         struct ctdlroom *rel_room);
381 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
382
383 int CtdlLoginExistingUser(char *authname, const char *username);
384
385 /*
386  * Values which may be returned by CtdlLoginExistingUser()
387  */
388 enum {
389         pass_ok,
390         pass_already_logged_in,
391         pass_no_user,
392         pass_internal_error,
393         pass_wrong_password
394 };
395
396 int CtdlTryPassword(const char *password, long len);
397 /*
398  * Values which may be returned by CtdlTryPassword()
399  */
400 enum {
401         login_ok,
402         login_already_logged_in,
403         login_too_many_users,
404         login_not_found
405 };
406
407 void CtdlUserLogout(void);
408
409
410
411
412 /*
413  * Expose API calls from msgbase.c
414  */
415 char *CtdlGetSysConfig(char *sysconfname);
416 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata);
417
418
419
420
421 /*
422  * Expose API calls from euidindex.c
423  */
424 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
425
426
427 #endif /* CTDL_MODULE_H */