4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
24 #include <sys/types.h>
32 #include "dynloader.h"
33 #include "sysdep_decls.h"
43 struct LogFunctionHook *LogHookTable = NULL;
44 struct CleanupFunctionHook *CleanupHookTable = NULL;
45 struct SessionFunctionHook *SessionHookTable = NULL;
46 struct UserFunctionHook *UserHookTable = NULL;
47 struct XmsgFunctionHook *XmsgHookTable = NULL;
48 struct MessageFunctionHook *MessageHookTable = NULL;
49 struct ServiceFunctionHook *ServiceHookTable = NULL;
51 struct ProtoFunctionHook {
52 void (*handler) (char *cmdbuf);
55 struct ProtoFunctionHook *next;
56 } *ProtoHookList = NULL;
58 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
60 struct ProtoFunctionHook *p;
62 p = (struct ProtoFunctionHook *)
63 mallok(sizeof(struct ProtoFunctionHook));
66 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
72 p->next = ProtoHookList;
74 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
78 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
80 struct ProtoFunctionHook *cur, *p;
82 for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
83 /* This will also remove duplicates if any */
85 handler == cur->handler &&
86 !strcmp(cmd, cur->cmd)) {
87 lprintf(5, "Unregistered server command %s (%s)\n",
90 if (cur == ProtoHookList) {
100 int DLoader_Exec_Cmd(char *cmdbuf)
102 struct ProtoFunctionHook *p;
104 for (p = ProtoHookList; p; p = p->next) {
105 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
106 p->handler(&cmdbuf[5]);
113 void DLoader_Init(char *pathname)
120 char *(*h_init_fcn) (void);
123 char pathbuf[PATH_MAX];
125 if ((dir = opendir(pathname)) == NULL) {
129 while ((dptr = readdir(dir)) != NULL) {
130 if (strlen(dptr->d_name) < 4)
133 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
135 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-4], ".dll"))
139 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
140 lprintf(7, "Initializing %s...\n", pathbuf);
143 if (!(fcn_handle = dlopen(pathbuf, RTLD_LAZY)))
145 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
148 safestrncpy(dl_error, dlerror(), sizeof dl_error);
149 for (i=0; i<strlen(dl_error); ++i)
150 if (!isprint(dl_error[i]))
152 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
156 h_init_fcn = (char * (*)(void))
158 dlsym(fcn_handle, "Dynamic_Module_Init");
160 dlsym(fcn_handle, "_Dynamic_Module_Init");
163 if (dlerror() != NULL) {
164 fprintf(stderr, "DLoader_Init dlsym failed\n");
167 dl_info = h_init_fcn();
169 lprintf(3, "Loaded module: %s\n", dl_info);
175 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
178 struct LogFunctionHook *newfcn;
180 newfcn = (struct LogFunctionHook *)
181 mallok(sizeof(struct LogFunctionHook));
182 newfcn->next = LogHookTable;
183 newfcn->h_function_pointer = fcn_ptr;
184 newfcn->loglevel = loglevel;
185 LogHookTable = newfcn;
187 lprintf(5, "Registered a new logging function\n");
191 void CtdlUnregisterLogHook(void (*fcn_ptr) (char *), int loglevel)
193 struct LogFunctionHook *cur, *p;
195 for (cur = LogHookTable; cur != NULL; cur = cur->next) {
196 /* This will also remove duplicates if any */
197 while (cur != NULL &&
198 fcn_ptr == cur->h_function_pointer &&
199 loglevel == cur->loglevel) {
200 lprintf(5, "Unregistered logging function\n");
202 if (cur == LogHookTable) {
212 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
215 struct CleanupFunctionHook *newfcn;
217 newfcn = (struct CleanupFunctionHook *)
218 mallok(sizeof(struct CleanupFunctionHook));
219 newfcn->next = CleanupHookTable;
220 newfcn->h_function_pointer = fcn_ptr;
221 CleanupHookTable = newfcn;
223 lprintf(5, "Registered a new cleanup function\n");
227 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
229 struct CleanupFunctionHook *cur, *p;
231 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
232 /* This will also remove duplicates if any */
233 while (cur != NULL &&
234 fcn_ptr == cur->h_function_pointer) {
235 lprintf(5, "Unregistered cleanup function\n");
237 if (cur == CleanupHookTable) {
238 CleanupHookTable = p;
247 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
250 struct SessionFunctionHook *newfcn;
252 newfcn = (struct SessionFunctionHook *)
253 mallok(sizeof(struct SessionFunctionHook));
254 newfcn->next = SessionHookTable;
255 newfcn->h_function_pointer = fcn_ptr;
256 newfcn->eventtype = EventType;
257 SessionHookTable = newfcn;
259 lprintf(5, "Registered a new session function (type %d)\n",
264 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
266 struct SessionFunctionHook *cur, *p;
268 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
269 /* This will also remove duplicates if any */
270 while (cur != NULL &&
271 fcn_ptr == cur->h_function_pointer &&
272 EventType == cur->eventtype) {
273 lprintf(5, "Unregistered session function (type %d)\n",
276 if (cur == SessionHookTable) {
277 SessionHookTable = p;
286 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
289 struct UserFunctionHook *newfcn;
291 newfcn = (struct UserFunctionHook *)
292 mallok(sizeof(struct UserFunctionHook));
293 newfcn->next = UserHookTable;
294 newfcn->h_function_pointer = fcn_ptr;
295 newfcn->eventtype = EventType;
296 UserHookTable = newfcn;
298 lprintf(5, "Registered a new user function (type %d)\n",
303 void CtdlUnregisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
305 struct UserFunctionHook *cur, *p;
307 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
308 /* This will also remove duplicates if any */
309 while (cur != NULL &&
310 fcn_ptr == cur->h_function_pointer &&
311 EventType == cur->eventtype) {
312 lprintf(5, "Unregistered user function (type %d)\n",
315 if (cur == UserHookTable) {
325 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
329 struct MessageFunctionHook *newfcn;
331 newfcn = (struct MessageFunctionHook *)
332 mallok(sizeof(struct MessageFunctionHook));
333 newfcn->next = MessageHookTable;
334 newfcn->h_function_pointer = handler;
335 newfcn->eventtype = EventType;
336 MessageHookTable = newfcn;
338 lprintf(5, "Registered a new message function (type %d)\n",
343 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
346 struct MessageFunctionHook *cur, *p;
348 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
349 /* This will also remove duplicates if any */
350 while (cur != NULL &&
351 handler == cur->h_function_pointer &&
352 EventType == cur->eventtype) {
353 lprintf(5, "Unregistered message function (type %d)\n",
356 if (cur == MessageHookTable) {
357 MessageHookTable = p;
366 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
369 struct XmsgFunctionHook *newfcn;
371 newfcn = (struct XmsgFunctionHook *)
372 mallok(sizeof(struct XmsgFunctionHook));
373 newfcn->next = XmsgHookTable;
374 newfcn->order = order;
375 newfcn->h_function_pointer = fcn_ptr;
376 XmsgHookTable = newfcn;
377 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
381 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
383 struct XmsgFunctionHook *cur, *p;
385 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
386 /* This will also remove duplicates if any */
387 while (cur != NULL &&
388 fcn_ptr == cur->h_function_pointer &&
389 order == cur->order) {
390 lprintf(5, "Unregistered x-msg function "
391 "(priority %d)\n", order);
393 if (cur == XmsgHookTable) {
403 void CtdlRegisterServiceHook(int tcp_port,
405 void (*h_greeting_function) (void),
406 void (*h_command_function) (void) )
408 struct ServiceFunctionHook *newfcn;
411 newfcn = (struct ServiceFunctionHook *)
412 mallok(sizeof(struct ServiceFunctionHook));
413 newfcn->next = ServiceHookTable;
414 newfcn->tcp_port = tcp_port;
415 newfcn->sockpath = sockpath;
416 newfcn->h_greeting_function = h_greeting_function;
417 newfcn->h_command_function = h_command_function;
419 if (sockpath != NULL) {
420 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
421 sprintf(message, "Unix domain socket '%s': ", sockpath);
423 else if (tcp_port <= 0) { /* port -1 to disable */
424 lprintf(7, "Service has been manually disabled, skipping\n");
429 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
430 sprintf(message, "TCP port %d: ", tcp_port);
433 if (newfcn->msock > 0) {
434 ServiceHookTable = newfcn;
435 strcat(message, "registered.");
436 lprintf(5, "%s\n", message);
439 strcat(message, "FAILED.");
440 lprintf(2, "%s\n", message);
446 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
447 void (*h_greeting_function) (void),
448 void (*h_command_function) (void) )
450 struct ServiceFunctionHook *cur, *p;
452 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
453 /* This will also remove duplicates if any */
454 while (cur != NULL &&
455 !(sockpath && cur->sockpath &&
456 strcmp(sockpath, cur->sockpath)) &&
457 h_greeting_function == cur->h_greeting_function &&
458 h_command_function == cur->h_command_function &&
459 tcp_port == cur->tcp_port) {
462 lprintf(5, "Closed UNIX domain socket %s\n",
464 } else if (tcp_port) {
465 lprintf(5, "Closed TCP port %d\n", tcp_port);
467 lprintf(5, "Unregistered unknown service\n");
470 if (cur == ServiceHookTable) {
471 ServiceHookTable = p;
480 void PerformSessionHooks(int EventType)
482 struct SessionFunctionHook *fcn;
484 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
485 if (fcn->eventtype == EventType) {
486 (*fcn->h_function_pointer) ();
491 void PerformLogHooks(int loglevel, char *logmsg)
493 struct LogFunctionHook *fcn;
495 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
496 if (fcn->loglevel >= loglevel) {
497 (*fcn->h_function_pointer) (logmsg);
502 void PerformUserHooks(char *username, long usernum, int EventType)
504 struct UserFunctionHook *fcn;
506 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
507 if (fcn->eventtype == EventType) {
508 (*fcn->h_function_pointer) (username, usernum);
513 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
515 struct MessageFunctionHook *fcn;
516 int total_retval = 0;
518 /* Other code may elect to protect this message from server-side
519 * handlers; if this is the case, don't do anything.
520 lprintf(9, "** Event type is %d, flags are %d\n",
521 EventType, msg->cm_flags);
523 if (msg->cm_flags & CM_SKIP_HOOKS) {
524 lprintf(9, "Skipping hooks\n");
528 /* Otherwise, run all the hooks appropriate to this event type.
530 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
531 if (fcn->eventtype == EventType) {
532 total_retval = total_retval +
533 (*fcn->h_function_pointer) (msg);
537 /* Return the sum of the return codes from the hook functions. If
538 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
539 * the save operation to abort.
546 int PerformXmsgHooks(char *sender, char *recp, char *msg)
548 struct XmsgFunctionHook *fcn;
552 for (p=0; p<MAX_XMSG_PRI; ++p) {
553 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
554 if (fcn->order == p) {
556 (*fcn->h_function_pointer)
560 /* Break out of the loop if a higher-priority function
561 * successfully delivered the message. This prevents duplicate
562 * deliveries to local users simultaneously signed onto
565 if (total_sent) break;