4d26b594526294387d3a4e628cf30bc67b62797a
[citadel.git] / citadel / dynloader.c
1 /*******************************************************
2  *
3  * Citadel Dynamic Loading Module
4  * Written by Brian Costello
5  * btx@calyx.net
6  *
7  * $Id$
8  *
9  ******************************************************/
10
11
12 #include "sysdep.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <dlfcn.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <strings.h>
19 #include <syslog.h>
20 #include <limits.h>
21 #include <ctype.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include "dynloader.h"
25 #include "sysdep_decls.h"
26 #include "msgbase.h"
27 #include "tools.h"
28
29 #ifndef HAVE_SNPRINTF
30 #include <stdarg.h>
31 #include "snprintf.h"
32 #endif
33
34 struct LogFunctionHook *LogHookTable = NULL;
35 struct CleanupFunctionHook *CleanupHookTable = NULL;
36 struct SessionFunctionHook *SessionHookTable = NULL;
37 struct UserFunctionHook *UserHookTable = NULL;
38 struct XmsgFunctionHook *XmsgHookTable = NULL;
39 struct MessageFunctionHook *MessageHookTable = NULL;
40 struct ServiceFunctionHook *ServiceHookTable = NULL;
41
42 struct ProtoFunctionHook {
43         void (*handler) (char *cmdbuf);
44         char *cmd;
45         char *desc;
46         struct ProtoFunctionHook *next;
47 } *ProtoHookList = NULL;
48
49 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
50 {
51         struct ProtoFunctionHook *p = mallok(sizeof *p);
52
53         if (p == NULL) {
54                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
55                 exit(EXIT_FAILURE);
56         }
57         p->handler = handler;
58         p->cmd = cmd;
59         p->desc = desc;
60         p->next = ProtoHookList;
61         ProtoHookList = p;
62         lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
63 }
64
65 int DLoader_Exec_Cmd(char *cmdbuf)
66 {
67         struct ProtoFunctionHook *p;
68
69         for (p = ProtoHookList; p; p = p->next) {
70                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
71                         p->handler(&cmdbuf[5]);
72                         return 1;
73                 }
74         }
75         return 0;
76 }
77
78 void DLoader_Init(char *pathname)
79 {
80         void *fcn_handle;
81         char dl_error[256];
82         DIR *dir;
83         int i;
84         struct dirent *dptr;
85         char *(*h_init_fcn) (void);
86         char *dl_info;
87
88         char pathbuf[PATH_MAX];
89
90         if ((dir = opendir(pathname)) == NULL) {
91                 perror("opendir");
92                 exit(1);
93         }
94         while ((dptr = readdir(dir)) != NULL) {
95                 if (strlen(dptr->d_name) < 4)
96                         continue;
97                 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
98                         continue;
99
100                 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
101 #ifdef RTLD_NOW
102                 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
103 #else                           /* OpenBSD */
104                 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
105 #endif
106                 {
107                         safestrncpy(dl_error, dlerror(), sizeof dl_error);
108                         for (i=0; i<strlen(dl_error); ++i)
109                                 if (!isprint(dl_error[i]))
110                                         dl_error[i]='.';
111                         fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
112                                 dl_error);
113                         continue;
114                 }
115                 h_init_fcn = (char * (*)(void))
116 #ifndef __OpenBSD__
117                     dlsym(fcn_handle, "Dynamic_Module_Init");
118 #else
119                     dlsym(fcn_handle, "_Dynamic_Module_Init");
120 #endif
121
122                 if (dlerror() != NULL) {
123                         fprintf(stderr, "DLoader_Init dlsym failed\n");
124                         continue;
125                 }
126                 dl_info = h_init_fcn();
127
128                 lprintf(3, "Loaded module: %s\n", dl_info);
129         }       /* While */
130 }
131
132
133
134 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
135 {
136
137         struct LogFunctionHook *newfcn;
138
139         newfcn = (struct LogFunctionHook *)
140             mallok(sizeof(struct LogFunctionHook));
141         newfcn->next = LogHookTable;
142         newfcn->h_function_pointer = fcn_ptr;
143         newfcn->loglevel = loglevel;
144         LogHookTable = newfcn;
145
146         lprintf(5, "Registered a new logging function\n");
147 }
148
149
150 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
151 {
152
153         struct CleanupFunctionHook *newfcn;
154
155         newfcn = (struct CleanupFunctionHook *)
156             mallok(sizeof(struct CleanupFunctionHook));
157         newfcn->next = CleanupHookTable;
158         newfcn->h_function_pointer = fcn_ptr;
159         CleanupHookTable = newfcn;
160
161         lprintf(5, "Registered a new cleanup function\n");
162 }
163
164
165 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
166 {
167
168         struct SessionFunctionHook *newfcn;
169
170         newfcn = (struct SessionFunctionHook *)
171             mallok(sizeof(struct SessionFunctionHook));
172         newfcn->next = SessionHookTable;
173         newfcn->h_function_pointer = fcn_ptr;
174         newfcn->eventtype = EventType;
175         SessionHookTable = newfcn;
176
177         lprintf(5, "Registered a new session function (type %d)\n",
178                 EventType);
179 }
180
181
182 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
183 {
184
185         struct UserFunctionHook *newfcn;
186
187         newfcn = (struct UserFunctionHook *)
188             mallok(sizeof(struct UserFunctionHook));
189         newfcn->next = UserHookTable;
190         newfcn->h_function_pointer = fcn_ptr;
191         newfcn->eventtype = EventType;
192         UserHookTable = newfcn;
193
194         lprintf(5, "Registered a new user function (type %d)\n",
195                 EventType);
196 }
197
198
199 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
200                                 int EventType)
201 {
202
203         struct MessageFunctionHook *newfcn;
204
205         newfcn = (struct MessageFunctionHook *)
206             mallok(sizeof(struct MessageFunctionHook));
207         newfcn->next = MessageHookTable;
208         newfcn->h_function_pointer = handler;
209         newfcn->eventtype = EventType;
210         MessageHookTable = newfcn;
211
212         lprintf(5, "Registered a new message function (type %d)\n",
213                 EventType);
214 }
215
216
217 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
218 {
219
220         struct XmsgFunctionHook *newfcn;
221
222         newfcn = (struct XmsgFunctionHook *)
223             mallok(sizeof(struct XmsgFunctionHook));
224         newfcn->next = XmsgHookTable;
225         newfcn->order = order;
226         newfcn->h_function_pointer = fcn_ptr;
227         XmsgHookTable = newfcn;
228         lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
229 }
230
231 void CtdlRegisterServiceHook(int tcp_port,
232                         void (*h_greeting_function) (void),
233                         void (*h_command_function) (void) )
234 {
235         struct ServiceFunctionHook *newfcn;
236
237         newfcn = (struct ServiceFunctionHook *)
238             mallok(sizeof(struct ServiceFunctionHook));
239         newfcn->next = ServiceHookTable;
240         newfcn->tcp_port = tcp_port;
241         newfcn->h_greeting_function = h_greeting_function;
242         newfcn->h_command_function = h_command_function;
243         newfcn->msock = (-1);
244         ServiceHookTable = newfcn;
245         lprintf(5, "Registered a new service (TCP port %d)\n", tcp_port);
246 }
247
248
249
250 void PerformSessionHooks(int EventType)
251 {
252         struct SessionFunctionHook *fcn;
253
254         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
255                 if (fcn->eventtype == EventType) {
256                         (*fcn->h_function_pointer) ();
257                 }
258         }
259 }
260
261 void PerformLogHooks(int loglevel, char *logmsg)
262 {
263         struct LogFunctionHook *fcn;
264
265         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
266                 if (fcn->loglevel >= loglevel) {
267                         (*fcn->h_function_pointer) (logmsg);
268                 }
269         }
270 }
271
272 void PerformUserHooks(char *username, long usernum, int EventType)
273 {
274         struct UserFunctionHook *fcn;
275
276         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
277                 if (fcn->eventtype == EventType) {
278                         (*fcn->h_function_pointer) (username, usernum);
279                 }
280         }
281 }
282
283 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
284 {
285         struct MessageFunctionHook *fcn;
286         int total_retval = 0;
287
288         /* Other code may elect to protect this message from server-side
289          * handlers; if this is the case, don't do anything.
290          */
291         lprintf(9, "** Event type is %d, flags are %d\n",
292                 EventType, msg->cm_flags);
293         if (msg->cm_flags & CM_SKIP_HOOKS) {
294                 lprintf(9, "Skipping hooks\n");
295                 return(0);
296         }
297
298         /* Otherwise, run all the hooks appropriate to this event type.
299          */
300         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
301                 if (fcn->eventtype == EventType) {
302                         total_retval = total_retval +
303                                 (*fcn->h_function_pointer) (msg);
304                 }
305         }
306
307         /* Return the sum of the return codes from the hook functions.  If
308          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
309          * the save operation to abort.
310          */
311         return total_retval;
312 }
313
314
315
316 int PerformXmsgHooks(char *sender, char *recp, char *msg)
317 {
318         struct XmsgFunctionHook *fcn;
319         int total_sent = 0;
320         int p;
321
322         for (p=0; p<MAX_XMSG_PRI; ++p) {
323                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
324                         if (fcn->order == p) {
325                                 total_sent +=
326                                         (*fcn->h_function_pointer)
327                                                 (sender, recp, msg);
328                         }
329                 }
330                 /* Break out of the loop if a higher-priority function
331                  * successfully delivered the message.  This prevents duplicate
332                  * deliveries to local users simultaneously signed onto
333                  * remote services.
334                  */
335                 if (total_sent) goto DONE;
336         }
337 DONE:   return total_sent;
338 }