]> code.citadel.org Git - citadel.git/blob - citadel/dynloader.c
* Merged in IO ERROR's diffs to make Citadel work with HP/UX
[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
242         newfcn = (struct ServiceFunctionHook *)
243             mallok(sizeof(struct ServiceFunctionHook));
244         newfcn->next = ServiceHookTable;
245         newfcn->tcp_port = tcp_port;
246         newfcn->sockpath = sockpath;
247         newfcn->h_greeting_function = h_greeting_function;
248         newfcn->h_command_function = h_command_function;
249
250         if (sockpath != NULL) {
251                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
252         }
253         else if (tcp_port < 0) {        /* port -1 to disable */
254                 lprintf(7, "Service has been manually disabled, skipping\n");
255                 phree(newfcn);
256                 return;
257         }
258         else {
259                 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
260         }
261
262         if (newfcn->msock >= 0) {
263                 ServiceHookTable = newfcn;
264                 lprintf(5, "Registered a new service (TCP port %d)\n",
265                         tcp_port);
266         }
267         else {
268                 lprintf(2, "ERROR: could not bind to TCP port %d.\n",
269                         tcp_port);
270                 phree(newfcn);
271         }
272 }
273
274
275
276 void PerformSessionHooks(int EventType)
277 {
278         struct SessionFunctionHook *fcn;
279
280         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
281                 if (fcn->eventtype == EventType) {
282                         (*fcn->h_function_pointer) ();
283                 }
284         }
285 }
286
287 void PerformLogHooks(int loglevel, char *logmsg)
288 {
289         struct LogFunctionHook *fcn;
290
291         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
292                 if (fcn->loglevel >= loglevel) {
293                         (*fcn->h_function_pointer) (logmsg);
294                 }
295         }
296 }
297
298 void PerformUserHooks(char *username, long usernum, int EventType)
299 {
300         struct UserFunctionHook *fcn;
301
302         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
303                 if (fcn->eventtype == EventType) {
304                         (*fcn->h_function_pointer) (username, usernum);
305                 }
306         }
307 }
308
309 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
310 {
311         struct MessageFunctionHook *fcn;
312         int total_retval = 0;
313
314         /* Other code may elect to protect this message from server-side
315          * handlers; if this is the case, don't do anything.
316          */
317         lprintf(9, "** Event type is %d, flags are %d\n",
318                 EventType, msg->cm_flags);
319         if (msg->cm_flags & CM_SKIP_HOOKS) {
320                 lprintf(9, "Skipping hooks\n");
321                 return(0);
322         }
323
324         /* Otherwise, run all the hooks appropriate to this event type.
325          */
326         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
327                 if (fcn->eventtype == EventType) {
328                         total_retval = total_retval +
329                                 (*fcn->h_function_pointer) (msg);
330                 }
331         }
332
333         /* Return the sum of the return codes from the hook functions.  If
334          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
335          * the save operation to abort.
336          */
337         return total_retval;
338 }
339
340
341
342 int PerformXmsgHooks(char *sender, char *recp, char *msg)
343 {
344         struct XmsgFunctionHook *fcn;
345         int total_sent = 0;
346         int p;
347
348         for (p=0; p<MAX_XMSG_PRI; ++p) {
349                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
350                         if (fcn->order == p) {
351                                 total_sent +=
352                                         (*fcn->h_function_pointer)
353                                                 (sender, recp, msg);
354                         }
355                 }
356                 /* Break out of the loop if a higher-priority function
357                  * successfully delivered the message.  This prevents duplicate
358                  * deliveries to local users simultaneously signed onto
359                  * remote services.
360                  */
361                 if (total_sent) goto DONE;
362         }
363 DONE:   return total_sent;
364 }