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