4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
13 #include <sys/types.h>
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
31 struct CleanupFunctionHook *CleanupHookTable = NULL;
32 struct SessionFunctionHook *SessionHookTable = NULL;
33 struct UserFunctionHook *UserHookTable = NULL;
34 struct XmsgFunctionHook *XmsgHookTable = NULL;
35 struct MessageFunctionHook *MessageHookTable = NULL;
36 struct NetprocFunctionHook *NetprocHookTable = NULL;
37 struct DeleteFunctionHook *DeleteHookTable = NULL;
38 struct ServiceFunctionHook *ServiceHookTable = NULL;
39 struct FixedOutputHook *FixedOutputTable = NULL;
41 struct ProtoFunctionHook {
42 void (*handler) (char *cmdbuf);
45 struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
49 #define ERR_PORT (1 << 1)
52 static char *portlist = NULL;
53 static size_t nSizPort = 0;
55 static char *errormessages = NULL;
56 static size_t nSizErrmsg = 0;
59 static long DetailErrorFlags;
62 char *ErrSubject = "Startup Problems";
63 char *ErrGeneral = "Citadel had trouble on starting up. %s This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
64 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
65 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
66 "The errors returned by the system were:\n%s\n";
69 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
70 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
71 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
72 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
73 " and stop the programm, that binds these ports. You should eventually remove "
74 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
77 void LogPrintMessages(long err)
79 char *List, *DetailList, *Short, *Where, *Hint, *Message;
80 int n = nSizPort + nSizErrmsg + 5;
82 Message = (char*) malloc(n * SIZ);
91 DetailList = errormessages;
102 snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
104 quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
110 void AppendString(char **target, char *append, size_t *len, size_t rate)
116 AddLen = strlen(append);
122 *target = (char*)malloc (*len * SIZ);
126 oLen = strlen(*target);
127 RelPtr = strlen(*target);
128 if (oLen + AddLen + 2 > *len * SIZ)
130 char *Buff = *target;
131 size_t NewSiz = *len + 10;
132 *target = malloc (NewSiz * SIZ);
133 memcpy (*target, Buff, NewSiz * SIZ);
137 memcpy (*target + oLen, append, AddLen);
138 (*target)[AddLen + 1] = '\n';
139 (*target)[AddLen + 2] = '\0';
142 void AddPortError(char *Port, char *ErrorMessage)
144 DetailErrorFlags |= ERR_PORT;
146 AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
147 AppendString(&portlist, Port, &nSizPort, 2);
151 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
153 struct ProtoFunctionHook *p;
155 p = (struct ProtoFunctionHook *)
156 malloc(sizeof(struct ProtoFunctionHook));
159 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
162 p->handler = handler;
165 p->next = ProtoHookList;
167 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
171 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
173 struct ProtoFunctionHook *cur, *p;
175 for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
176 /* This will also remove duplicates if any */
177 while (cur != NULL &&
178 handler == cur->handler &&
179 !strcmp(cmd, cur->cmd)) {
180 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
183 if (cur == ProtoHookList) {
193 int DLoader_Exec_Cmd(char *cmdbuf)
195 struct ProtoFunctionHook *p;
197 for (p = ProtoHookList; p; p = p->next) {
198 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
199 p->handler(&cmdbuf[5]);
206 void initialize_server_extensions(void)
210 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
211 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
212 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
213 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
214 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
215 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
216 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
217 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
218 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
219 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
220 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
221 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
222 lprintf(CTDL_INFO, "%s\n", serv_network_init());
223 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
224 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
225 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
226 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
227 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
228 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
229 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
230 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
231 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
232 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
233 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
234 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
235 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
236 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
237 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
238 for (filter = 1; filter != 0; filter = filter << 1)
239 if ((filter & DetailErrorFlags) != 0)
240 LogPrintMessages(filter);
245 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
248 struct CleanupFunctionHook *newfcn;
250 newfcn = (struct CleanupFunctionHook *)
251 malloc(sizeof(struct CleanupFunctionHook));
252 newfcn->next = CleanupHookTable;
253 newfcn->h_function_pointer = fcn_ptr;
254 CleanupHookTable = newfcn;
256 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
260 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
262 struct CleanupFunctionHook *cur, *p;
264 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
265 /* This will also remove duplicates if any */
266 while (cur != NULL &&
267 fcn_ptr == cur->h_function_pointer) {
268 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
270 if (cur == CleanupHookTable) {
271 CleanupHookTable = p;
280 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
283 struct SessionFunctionHook *newfcn;
285 newfcn = (struct SessionFunctionHook *)
286 malloc(sizeof(struct SessionFunctionHook));
287 newfcn->next = SessionHookTable;
288 newfcn->h_function_pointer = fcn_ptr;
289 newfcn->eventtype = EventType;
290 SessionHookTable = newfcn;
292 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
297 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
299 struct SessionFunctionHook *cur, *p;
301 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
302 /* This will also remove duplicates if any */
303 while (cur != NULL &&
304 fcn_ptr == cur->h_function_pointer &&
305 EventType == cur->eventtype) {
306 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
309 if (cur == SessionHookTable) {
310 SessionHookTable = p;
319 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
322 struct UserFunctionHook *newfcn;
324 newfcn = (struct UserFunctionHook *)
325 malloc(sizeof(struct UserFunctionHook));
326 newfcn->next = UserHookTable;
327 newfcn->h_function_pointer = fcn_ptr;
328 newfcn->eventtype = EventType;
329 UserHookTable = newfcn;
331 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
336 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
338 struct UserFunctionHook *cur, *p;
340 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
341 /* This will also remove duplicates if any */
342 while (cur != NULL &&
343 fcn_ptr == cur->h_function_pointer &&
344 EventType == cur->eventtype) {
345 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
348 if (cur == UserHookTable) {
358 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
362 struct MessageFunctionHook *newfcn;
364 newfcn = (struct MessageFunctionHook *)
365 malloc(sizeof(struct MessageFunctionHook));
366 newfcn->next = MessageHookTable;
367 newfcn->h_function_pointer = handler;
368 newfcn->eventtype = EventType;
369 MessageHookTable = newfcn;
371 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
376 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
379 struct MessageFunctionHook *cur, *p;
381 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
382 /* This will also remove duplicates if any */
383 while (cur != NULL &&
384 handler == cur->h_function_pointer &&
385 EventType == cur->eventtype) {
386 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
389 if (cur == MessageHookTable) {
390 MessageHookTable = p;
399 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
401 struct NetprocFunctionHook *newfcn;
403 newfcn = (struct NetprocFunctionHook *)
404 malloc(sizeof(struct NetprocFunctionHook));
405 newfcn->next = NetprocHookTable;
406 newfcn->h_function_pointer = handler;
407 NetprocHookTable = newfcn;
409 lprintf(CTDL_INFO, "Registered a new netproc function\n");
413 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
415 struct NetprocFunctionHook *cur, *p;
417 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
418 /* This will also remove duplicates if any */
419 while (cur != NULL &&
420 handler == cur->h_function_pointer ) {
421 lprintf(CTDL_INFO, "Unregistered netproc function\n");
423 if (cur == NetprocHookTable) {
424 NetprocHookTable = p;
433 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
435 struct DeleteFunctionHook *newfcn;
437 newfcn = (struct DeleteFunctionHook *)
438 malloc(sizeof(struct DeleteFunctionHook));
439 newfcn->next = DeleteHookTable;
440 newfcn->h_function_pointer = handler;
441 DeleteHookTable = newfcn;
443 lprintf(CTDL_INFO, "Registered a new netproc function\n");
447 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
449 struct DeleteFunctionHook *cur, *p;
451 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
452 /* This will also remove duplicates if any */
453 while (cur != NULL &&
454 handler == cur->h_function_pointer ) {
455 lprintf(CTDL_INFO, "Unregistered netproc function\n");
457 if (cur == DeleteHookTable) {
469 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
471 struct FixedOutputHook *newfcn;
473 newfcn = (struct FixedOutputHook *)
474 malloc(sizeof(struct FixedOutputHook));
475 newfcn->next = FixedOutputTable;
476 newfcn->h_function_pointer = handler;
477 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
478 FixedOutputTable = newfcn;
480 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
484 void CtdlUnregisterFixedOutputHook(char *content_type)
486 struct FixedOutputHook *cur, *p;
488 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
489 /* This will also remove duplicates if any */
490 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
491 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
493 if (cur == FixedOutputTable) {
494 FixedOutputTable = p;
502 /* returns nonzero if we found a hook and used it */
503 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
505 struct FixedOutputHook *fcn;
507 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
508 if (!strcasecmp(content_type, fcn->content_type)) {
509 (*fcn->h_function_pointer) (content, content_length);
520 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
523 struct XmsgFunctionHook *newfcn;
525 newfcn = (struct XmsgFunctionHook *)
526 malloc(sizeof(struct XmsgFunctionHook));
527 newfcn->next = XmsgHookTable;
528 newfcn->order = order;
529 newfcn->h_function_pointer = fcn_ptr;
530 XmsgHookTable = newfcn;
531 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
535 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
537 struct XmsgFunctionHook *cur, *p;
539 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
540 /* This will also remove duplicates if any */
541 while (cur != NULL &&
542 fcn_ptr == cur->h_function_pointer &&
543 order == cur->order) {
544 lprintf(CTDL_INFO, "Unregistered x-msg function "
545 "(priority %d)\n", order);
547 if (cur == XmsgHookTable) {
557 void CtdlRegisterServiceHook(int tcp_port,
559 void (*h_greeting_function) (void),
560 void (*h_command_function) (void),
561 void (*h_async_function) (void)
564 struct ServiceFunctionHook *newfcn;
569 newfcn = (struct ServiceFunctionHook *)
570 malloc(sizeof(struct ServiceFunctionHook));
571 message = (char*) malloc (SIZ);
573 newfcn->next = ServiceHookTable;
574 newfcn->tcp_port = tcp_port;
575 newfcn->sockpath = sockpath;
576 newfcn->h_greeting_function = h_greeting_function;
577 newfcn->h_command_function = h_command_function;
578 newfcn->h_async_function = h_async_function;
580 if (sockpath != NULL) {
581 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
582 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
584 else if (tcp_port <= 0) { /* port -1 to disable */
585 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
590 newfcn->msock = ig_tcp_server(config.c_ip_addr,
592 config.c_maxsessions,
594 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
597 if (newfcn->msock > 0) {
598 ServiceHookTable = newfcn;
599 strcat(message, "registered.");
600 lprintf(CTDL_INFO, "%s\n", message);
603 AddPortError(message, error);
604 strcat(message, "FAILED.");
605 lprintf(CTDL_CRIT, "%s\n", message);
613 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
614 void (*h_greeting_function) (void),
615 void (*h_command_function) (void),
616 void (*h_async_function) (void)
619 struct ServiceFunctionHook *cur, *p;
621 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
622 /* This will also remove duplicates if any */
623 while (cur != NULL &&
624 !(sockpath && cur->sockpath &&
625 strcmp(sockpath, cur->sockpath)) &&
626 h_greeting_function == cur->h_greeting_function &&
627 h_command_function == cur->h_command_function &&
628 h_async_function == cur->h_async_function &&
629 tcp_port == cur->tcp_port) {
632 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
634 } else if (tcp_port) {
635 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
637 lprintf(CTDL_INFO, "Unregistered unknown service\n");
640 if (cur == ServiceHookTable) {
641 ServiceHookTable = p;
650 void PerformSessionHooks(int EventType)
652 struct SessionFunctionHook *fcn;
654 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
655 if (fcn->eventtype == EventType) {
656 (*fcn->h_function_pointer) ();
661 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
663 struct UserFunctionHook *fcn;
665 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
666 if (fcn->eventtype == EventType) {
667 (*fcn->h_function_pointer) (usbuf);
672 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
674 struct MessageFunctionHook *fcn;
675 int total_retval = 0;
677 /* Other code may elect to protect this message from server-side
678 * handlers; if this is the case, don't do anything.
679 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
680 EventType, msg->cm_flags);
682 if (msg->cm_flags & CM_SKIP_HOOKS) {
683 lprintf(CTDL_DEBUG, "Skipping hooks\n");
687 /* Otherwise, run all the hooks appropriate to this event type.
689 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
690 if (fcn->eventtype == EventType) {
691 total_retval = total_retval +
692 (*fcn->h_function_pointer) (msg);
696 /* Return the sum of the return codes from the hook functions. If
697 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
698 * the save operation to abort.
705 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
707 struct NetprocFunctionHook *fcn;
708 int total_retval = 0;
710 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
711 total_retval = total_retval +
712 (*fcn->h_function_pointer) (msg, target_room);
715 /* Return the sum of the return codes from the hook functions.
716 * A nonzero return code will cause the message to *not* be imported.
722 void PerformDeleteHooks(char *room, long msgnum)
724 struct DeleteFunctionHook *fcn;
726 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
727 (*fcn->h_function_pointer) (room, msgnum);
735 int PerformXmsgHooks(char *sender, char *recp, char *msg)
737 struct XmsgFunctionHook *fcn;
741 for (p=0; p<MAX_XMSG_PRI; ++p) {
742 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
743 if (fcn->order == p) {
745 (*fcn->h_function_pointer)
749 /* Break out of the loop if a higher-priority function
750 * successfully delivered the message. This prevents duplicate
751 * deliveries to local users simultaneously signed onto
754 if (total_sent) break;