6d16624b584d93dcd9d45e6f8d2ce6f32a8b76cc
[citadel.git] / citadel / 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 CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
114 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
115
116 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
117 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
118
119 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
120 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
121
122 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
123 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
124
125 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
126
127 void CtdlRegisterServiceHook(int tcp_port,
128                              char *sockpath,
129                              void (*h_greeting_function) (void),
130                              void (*h_command_function) (void),
131                              void (*h_async_function) (void),
132                              const char *ServiceName
133 );
134 void CtdlUnregisterServiceHook(int tcp_port,
135                         char *sockpath,
136                         void (*h_greeting_function) (void),
137                         void (*h_command_function) (void),
138                         void (*h_async_function) (void)
139 );
140
141 void CtdlRegisterFixedOutputHook(char *content_type,
142                         void (*output_function) (char *supplied_data, int len)
143 );
144 void CtdlUnRegisterFixedOutputHook(char *content_type);
145
146 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
147
148 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
149
150 /*
151  * if you say a) (which may take a while)
152  * don't forget to say b)
153  */
154 void CtdlDisableHouseKeeping(void);
155 void CtdlEnableHouseKeeping(void);
156
157 /*
158  * Directory services hooks for LDAP etc
159  */
160
161 #define DIRECTORY_USER_DEL 1    // Delete a user entry
162 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
163 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
164 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
165 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
166 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
167
168 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
169 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
170
171 /* TODODRW: This needs to be changed into a hook type interface
172  * for now we have this horrible hack
173  */
174 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
175
176 /* return the current context list as an array and do it in a safe manner
177  * The returned data is a copy so only reading is useful
178  * The number of contexts is returned in count.
179  * Beware, this does not copy any of the data pointed to by the context.
180  * This means that you can not rely on things like the redirect buffer being valid.
181  * You must free the returned pointer when done.
182  */
183 struct CitContext *CtdlGetContextArray (int *count);
184 void CtdlFillSystemContext(struct CitContext *context, char *name);
185 int CtdlTrySingleUser(void);
186 void CtdlEndSingleUser(void);
187 int CtdlWantSingleUser(void);
188 int CtdlIsSingleUser(void);
189
190
191 int CtdlIsUserLoggedIn (char *user_name);
192 int CtdlIsUserLoggedInByNum (long usernum);
193 void CtdlBumpNewMailCounter(long which_user);
194
195
196 /*
197  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
198  * This provides a quick way to initialise a variable that might be used to indicate
199  * messages that should not be processed.  For example, a new inbox script will use this
200  * to record determine that messages older than this should not be processed.
201  * This function is defined in control.c
202  */
203 long CtdlGetCurrentMessageNumber(void);
204
205
206
207 /*
208  * Expose various room operation functions from room_ops.c to the modules API
209  */
210 typedef struct CfgLineType CfgLineType;
211 typedef struct RoomNetCfgLine RoomNetCfgLine;
212 typedef struct OneRoomNetCfg OneRoomNetCfg;
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, const char *room_name);
222 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name);
223 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
224 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view);
225 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
226 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
227 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
228 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
229 char *LoadRoomNetConfigFile(long roomnum);
230 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data);
231 void SaveChangedConfigs(void);
232 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
233 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
234 void CtdlUserGoto (char *where, int display_result, int transiently, int *msgs, int *new, long *oldest, long *newest);
235 struct floor *CtdlGetCachedFloor(int floor_num);
236 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
237 void CtdlGetFloor (struct floor *flbuf, int floor_num);
238 void CtdlPutFloor (struct floor *flbuf, int floor_num);
239 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
240 int CtdlGetFloorByName(const char *floor_name);
241 int CtdlGetFloorByNameLock(const char *floor_name);
242 int CtdlGetAvailableFloor(void);
243 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
244 void CtdlPutRoom(struct ctdlroom *);
245
246 /*
247  * Possible return values for CtdlRenameRoom()
248  */
249 enum {
250         crr_ok,                         /* success */
251         crr_room_not_found,             /* room not found */
252         crr_already_exists,             /* new name already exists */
253         crr_noneditable,                /* cannot edit this room */
254         crr_invalid_floor,              /* target floor does not exist */
255         crr_access_denied               /* not allowed to edit this room */
256 };
257
258
259
260 /*
261  * API declarations from citserver.h
262  */
263 int CtdlAccessCheck(int);
264 /* 'required access level' values which may be passed to CtdlAccessCheck()
265  */
266 enum {
267         ac_none,
268         ac_logged_in_or_guest,
269         ac_logged_in,
270         ac_room_aide,
271         ac_aide,
272         ac_internal,
273 };
274
275
276
277 /*
278  * API declarations from serv_extensions.h
279  */
280 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
281
282 #define NODENAME                CtdlGetConfigStr("c_nodename")
283 #define FQDN                    CtdlGetConfigStr("c_fqdn")
284 #define CTDLUID                 ctdluid
285 #define CREATAIDE               CtdlGetConfigInt("c_creataide")
286 #define REGISCALL               CtdlGetConfigInt("c_regiscall")
287 #define TWITDETECT              CtdlGetConfigInt("c_twitdetect")
288 #define TWITROOM                CtdlGetConfigStr("c_twitroom")
289 #define RESTRICT_INTERNET       CtdlGetConfigInt("c_restrict")
290
291 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
292 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
293 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
294
295 struct CfgLineType {
296         RoomNetCfg C;
297         CfgLineParser Parser;
298         CfgLineSerializer Serializer;
299         CfgLineDeAllocator DeAllocator;
300         ConstStr Str;
301         int IsSingleLine;
302         int nSegments;
303 };
304
305 struct RoomNetCfgLine {
306         RoomNetCfgLine *next;
307         int nValues;
308         StrBuf **Value;
309 };
310
311 struct OneRoomNetCfg {
312         long lastsent;
313         // long changed;
314         StrBuf *Sender;
315         StrBuf *RoomInfo;
316         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
317         StrBuf *misc;
318 };
319
320
321 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
322 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
323 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
324 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
325 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
326 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
327 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
328
329 OneRoomNetCfg *CtdlGetNetCfgForRoom(long QRNumber);
330 void SaveRoomNetConfigFile(OneRoomNetCfg *, long);
331 void FreeRoomNetworkStruct(OneRoomNetCfg **);
332
333 typedef struct _nodeconf {
334         int DeleteMe;
335         StrBuf *NodeName;
336         StrBuf *Secret;
337         StrBuf *Host;
338         StrBuf *Port;
339 }CtdlNodeConf;
340
341 HashList* CtdlLoadIgNetCfg(void);
342
343
344 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
345                                  size_t n,
346                                  const char* RemoteIdentifier);
347
348
349 typedef struct __NetMap {
350         StrBuf *NodeName;
351         time_t lastcontact;
352         StrBuf *NextHop;
353 }CtdlNetMap;
354
355 HashList* CtdlReadNetworkMap(void);
356 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
357
358
359
360
361 /*
362  * Expose API calls from user_ops.c
363  */
364 int CtdlGetUser(struct ctdluser *usbuf, char *name);
365 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
366 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
367 void CtdlPutUser(struct ctdluser *usbuf);
368 void CtdlPutUserLock(struct ctdluser *usbuf);
369 int CtdlLockGetCurrentUser(void);
370 void CtdlPutCurrentUserLock(void);
371 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
372 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room);
373 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room);
374 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
375 int CtdlLoginExistingUser(const char *username);
376
377 /*
378  * Values which may be returned by CtdlLoginExistingUser()
379  */
380 enum {
381         pass_ok,
382         pass_already_logged_in,
383         pass_no_user,
384         pass_internal_error,
385         pass_wrong_password
386 };
387
388 int CtdlTryPassword(const char *password, long len);
389 /*
390  * Values which may be returned by CtdlTryPassword()
391  */
392 enum {
393         login_ok,
394         login_already_logged_in,
395         login_too_many_users,
396         login_not_found
397 };
398
399 void CtdlUserLogout(void);
400
401 /*
402  * Expose API calls from msgbase.c
403  */
404
405
406 /*
407  * Expose API calls from euidindex.c
408  */
409 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
410
411
412 /*
413  * Expose API calls from modules/openid/serv_openid_rp.c in order to turn it into a generic external authentication driver
414  */
415 int attach_extauth(struct ctdluser *who, StrBuf *claimed_id);
416
417 #endif /* CTDL_MODULE_H */