3b3d794c3531c79945ad8e9f84435ba3ac5d6973
[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 #include <time.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #include <limits.h>
36
37 #include <libcitadel.h>
38
39 #include "server.h"
40 #include "sysdep_decls.h"
41 #include "msgbase.h"
42 #include "threads.h"
43 #include "citadel_dirs.h"
44 #include "context.h"
45
46 /*
47  * define macros for module init stuff
48  */
49  
50 #define CTDL_MODULE_INIT(module_name) char *ctdl_module_##module_name##_init (int threading)
51
52 #define CTDL_INIT_CALL(module_name) ctdl_module_##module_name##_init (threading)
53
54 #define CTDL_MODULE_UPGRADE(module_name) char *ctdl_module_##module_name##_upgrade (void)
55
56 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
57
58 #define CtdlAideMessage(TEXT, SUBJECT)          \
59         quickie_message(                        \
60                 "Citadel",                      \
61                 NULL,                           \
62                 NULL,                           \
63                 AIDEROOM,                       \
64                 TEXT,                           \
65                 FMT_CITADEL,                    \
66                 SUBJECT) 
67
68 /*
69  * Hook functions available to modules.
70  */
71 /* Priorities for  */
72 #define PRIO_QUEUE 500
73 #define PRIO_AGGR 1000
74 #define PRIO_SEND 1500
75 #define PRIO_CLEANUP 2000
76 /* Priorities for EVT_HOUSE */
77 #define PRIO_HOUSE 3000
78 /* Priorities for EVT_LOGIN */
79 #define PRIO_CREATE 10000
80 /* Priorities for EVT_LOGOUT */
81 #define PRIO_LOGOUT 15000
82 /* Priorities for EVT_LOGIN */
83 #define PRIO_LOGIN 20000
84 /* Priorities for EVT_START */
85 #define PRIO_START 25000
86 /* Priorities for EVT_STOP */
87 #define PRIO_STOP 30000
88 /* Priorities for EVT_ASYNC */
89 #define PRIO_ASYNC 35000
90 /* Priorities for EVT_SHUTDOWN */
91 #define PRIO_SHUTDOWN 40000
92 /* Priorities for EVT_UNSTEALTH */
93 #define PRIO_UNSTEALTH 45000
94 /* Priorities for EVT_STEALTH */
95 #define PRIO_STEALTH 50000
96
97
98 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType, int Priority);
99 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
100 void CtdlShutdownServiceHooks(void);
101
102 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
103 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
104
105 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
106 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
107
108 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes*),
109                                                         int EventType);
110 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
111                                int EventType);
112
113 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
114 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
115
116 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
117 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
118
119 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
120 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
121
122 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
123 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
124
125 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
126 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
127
128 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
129
130 void CtdlRegisterServiceHook(int tcp_port,
131                              char *sockpath,
132                              void (*h_greeting_function) (void),
133                              void (*h_command_function) (void),
134                              void (*h_async_function) (void),
135                              const char *ServiceName
136 );
137 void CtdlUnregisterServiceHook(int tcp_port,
138                         char *sockpath,
139                         void (*h_greeting_function) (void),
140                         void (*h_command_function) (void),
141                         void (*h_async_function) (void)
142 );
143
144 void CtdlRegisterFixedOutputHook(char *content_type,
145                         void (*output_function) (char *supplied_data, int len)
146 );
147 void CtdlUnRegisterFixedOutputHook(char *content_type);
148
149 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
150
151 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
152
153 /*
154  * if you say a) (which may take a while)
155  * don't forget to say b)
156  */
157 void CtdlDisableHouseKeeping(void);
158 void CtdlEnableHouseKeeping(void);
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 typedef struct CfgLineType CfgLineType;
214 typedef struct RoomNetCfgLine RoomNetCfgLine;
215 typedef struct OneRoomNetCfg OneRoomNetCfg;
216
217 unsigned CtdlCreateRoom(char *new_room_name,
218                         int new_room_type,
219                         char *new_room_pass,
220                         int new_room_floor,
221                         int really_create,
222                         int avoid_access,
223                         int new_room_view);
224 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name);
225 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name);
226 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
227 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view);
228 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
229 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
230 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
231 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
232 char *LoadRoomNetConfigFile(long roomnum);
233 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data);
234 void SaveChangedConfigs(void);
235 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
236 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
237 void CtdlUserGoto (char *where, int display_result, int transiently, int *msgs, int *new, long *oldest, long *newest);
238 struct floor *CtdlGetCachedFloor(int floor_num);
239 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
240 void CtdlGetFloor (struct floor *flbuf, int floor_num);
241 void CtdlPutFloor (struct floor *flbuf, int floor_num);
242 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
243 int CtdlGetFloorByName(const char *floor_name);
244 int CtdlGetFloorByNameLock(const char *floor_name);
245 int CtdlGetAvailableFloor(void);
246 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
247 void CtdlPutRoom(struct ctdlroom *);
248
249 /*
250  * Possible return values for CtdlRenameRoom()
251  */
252 enum {
253         crr_ok,                         /* success */
254         crr_room_not_found,             /* room not found */
255         crr_already_exists,             /* new name already exists */
256         crr_noneditable,                /* cannot edit this room */
257         crr_invalid_floor,              /* target floor does not exist */
258         crr_access_denied               /* not allowed to edit this room */
259 };
260
261
262
263 /*
264  * API declarations from citserver.h
265  */
266 int CtdlAccessCheck(int);
267 /* 'required access level' values which may be passed to CtdlAccessCheck()
268  */
269 enum {
270         ac_none,
271         ac_logged_in_or_guest,
272         ac_logged_in,
273         ac_room_aide,
274         ac_aide,
275         ac_internal,
276 };
277
278
279
280 /*
281  * API declarations from serv_extensions.h
282  */
283 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
284
285 #define NODENAME                CtdlGetConfigStr("c_nodename")
286 #define FQDN                    CtdlGetConfigStr("c_fqdn")
287 #define CTDLUID                 ctdluid
288 #define CREATAIDE               CtdlGetConfigInt("c_creataide")
289 #define REGISCALL               CtdlGetConfigInt("c_regiscall")
290 #define TWITDETECT              CtdlGetConfigInt("c_twitdetect")
291 #define TWITROOM                CtdlGetConfigStr("c_twitroom")
292 #define RESTRICT_INTERNET       CtdlGetConfigInt("c_restrict")
293
294 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
295 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
296 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
297
298 struct CfgLineType {
299         RoomNetCfg C;
300         CfgLineParser Parser;
301         CfgLineSerializer Serializer;
302         CfgLineDeAllocator DeAllocator;
303         ConstStr Str;
304         int IsSingleLine;
305         int nSegments;
306 };
307
308 struct RoomNetCfgLine {
309         RoomNetCfgLine *next;
310         int nValues;
311         StrBuf **Value;
312 };
313
314 struct OneRoomNetCfg {
315         long lastsent;
316         // long changed;
317         StrBuf *Sender;
318         StrBuf *RoomInfo;
319         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
320         StrBuf *misc;
321 };
322
323
324 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
325 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
326 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
327 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
328 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
329 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
330 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
331
332 OneRoomNetCfg *CtdlGetNetCfgForRoom(long QRNumber);
333 void SaveRoomNetConfigFile(OneRoomNetCfg *, long);
334 void FreeRoomNetworkStruct(OneRoomNetCfg **);
335
336 typedef struct _nodeconf {
337         int DeleteMe;
338         StrBuf *NodeName;
339         StrBuf *Secret;
340         StrBuf *Host;
341         StrBuf *Port;
342 }CtdlNodeConf;
343
344 HashList* CtdlLoadIgNetCfg(void);
345
346
347 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
348                                  size_t n,
349                                  const char* RemoteIdentifier);
350
351
352 typedef struct __NetMap {
353         StrBuf *NodeName;
354         time_t lastcontact;
355         StrBuf *NextHop;
356 }CtdlNetMap;
357
358 HashList* CtdlReadNetworkMap(void);
359 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
360
361
362
363
364 /*
365  * Expose API calls from user_ops.c
366  */
367 int CtdlGetUser(struct ctdluser *usbuf, char *name);
368 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
369 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
370 void CtdlPutUser(struct ctdluser *usbuf);
371 void CtdlPutUserLock(struct ctdluser *usbuf);
372 int CtdlLockGetCurrentUser(void);
373 void CtdlPutCurrentUserLock(void);
374 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
375 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room);
376 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room);
377 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
378 int CtdlLoginExistingUser(char *authname, const char *username);
379
380 /*
381  * Values which may be returned by CtdlLoginExistingUser()
382  */
383 enum {
384         pass_ok,
385         pass_already_logged_in,
386         pass_no_user,
387         pass_internal_error,
388         pass_wrong_password
389 };
390
391 int CtdlTryPassword(const char *password, long len);
392 /*
393  * Values which may be returned by CtdlTryPassword()
394  */
395 enum {
396         login_ok,
397         login_already_logged_in,
398         login_too_many_users,
399         login_not_found
400 };
401
402 void CtdlUserLogout(void);
403
404 /*
405  * Expose API calls from msgbase.c
406  */
407
408
409 /*
410  * Expose API calls from euidindex.c
411  */
412 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
413
414
415 /*
416  * Expose API calls from modules/openid/serv_openid_rp.c in order to turn it into a generic external authentication driver
417  */
418 int attach_extauth(struct ctdluser *who, StrBuf *claimed_id);
419
420 #endif /* CTDL_MODULE_H */