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