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, *lastcur;
175 for (cur = ProtoHookList;
177 cur = (cur != NULL)? cur->next: NULL) {
178 /* This will also remove duplicates if any */
179 while (cur != NULL &&
180 handler == cur->handler &&
181 !strcmp(cmd, cur->cmd)) {
182 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
185 if (cur == ProtoHookList) {
188 else if (lastcur != NULL)
199 void CtdlDestroyProtoHooks(void)
201 struct ProtoFunctionHook *cur, *p;
206 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
207 cur->cmd, cur->desc);
212 ProtoHookList = NULL;
216 int DLoader_Exec_Cmd(char *cmdbuf)
218 struct ProtoFunctionHook *p;
220 for (p = ProtoHookList; p; p = p->next) {
221 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
222 p->handler(&cmdbuf[5]);
229 void initialize_server_extensions(void)
233 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
234 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
235 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
236 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
237 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
238 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
239 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
240 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
241 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
242 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
243 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
244 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
245 lprintf(CTDL_INFO, "%s\n", serv_network_init());
246 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
247 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
248 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
249 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
250 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
251 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
252 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
253 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
254 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
255 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
256 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
257 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
258 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
259 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
260 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
261 for (filter = 1; filter != 0; filter = filter << 1)
262 if ((filter & DetailErrorFlags) != 0)
263 LogPrintMessages(filter);
268 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
271 struct CleanupFunctionHook *newfcn;
273 newfcn = (struct CleanupFunctionHook *)
274 malloc(sizeof(struct CleanupFunctionHook));
275 newfcn->next = CleanupHookTable;
276 newfcn->h_function_pointer = fcn_ptr;
277 CleanupHookTable = newfcn;
279 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
283 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
285 struct CleanupFunctionHook *cur, *p;
287 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
288 /* This will also remove duplicates if any */
289 while (cur != NULL &&
290 fcn_ptr == cur->h_function_pointer) {
291 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
293 if (cur == CleanupHookTable) {
294 CleanupHookTable = p;
302 void CtdlDestroyCleanupHooks(void)
304 struct CleanupFunctionHook *cur, *p;
306 cur = CleanupHookTable;
309 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
314 CleanupHookTable = NULL;
318 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
321 struct SessionFunctionHook *newfcn;
323 newfcn = (struct SessionFunctionHook *)
324 malloc(sizeof(struct SessionFunctionHook));
325 newfcn->next = SessionHookTable;
326 newfcn->h_function_pointer = fcn_ptr;
327 newfcn->eventtype = EventType;
328 SessionHookTable = newfcn;
330 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
335 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
337 struct SessionFunctionHook *cur, *p;
339 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
340 /* This will also remove duplicates if any */
341 while (cur != NULL &&
342 fcn_ptr == cur->h_function_pointer &&
343 EventType == cur->eventtype) {
344 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
347 if (cur == SessionHookTable) {
348 SessionHookTable = p;
356 void CtdlDestroySessionHooks(void)
358 struct SessionFunctionHook *cur, *p;
360 cur = SessionHookTable;
363 lprintf(CTDL_INFO, "Destroyed session function\n");
368 SessionHookTable = NULL;
372 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
375 struct UserFunctionHook *newfcn;
377 newfcn = (struct UserFunctionHook *)
378 malloc(sizeof(struct UserFunctionHook));
379 newfcn->next = UserHookTable;
380 newfcn->h_function_pointer = fcn_ptr;
381 newfcn->eventtype = EventType;
382 UserHookTable = newfcn;
384 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
389 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
391 struct UserFunctionHook *cur, *p;
393 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
394 /* This will also remove duplicates if any */
395 while (cur != NULL &&
396 fcn_ptr == cur->h_function_pointer &&
397 EventType == cur->eventtype) {
398 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
401 if (cur == UserHookTable) {
410 void CtdlDestroyUserHooks(void)
412 struct UserFunctionHook *cur, *p;
417 lprintf(CTDL_INFO, "Destroyed user function \n");
422 UserHookTable = NULL;
426 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
430 struct MessageFunctionHook *newfcn;
432 newfcn = (struct MessageFunctionHook *)
433 malloc(sizeof(struct MessageFunctionHook));
434 newfcn->next = MessageHookTable;
435 newfcn->h_function_pointer = handler;
436 newfcn->eventtype = EventType;
437 MessageHookTable = newfcn;
439 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
444 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
447 struct MessageFunctionHook *cur, *p;
449 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
450 /* This will also remove duplicates if any */
451 while (cur != NULL &&
452 handler == cur->h_function_pointer &&
453 EventType == cur->eventtype) {
454 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
457 if (cur == MessageHookTable) {
458 MessageHookTable = p;
466 void CtdlDestroyMessageHook(void)
468 struct MessageFunctionHook *cur, *p;
470 cur = MessageHookTable;
473 lprintf(CTDL_INFO, "Destroyed message function \n");
478 MessageHookTable = NULL;
482 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
484 struct NetprocFunctionHook *newfcn;
486 newfcn = (struct NetprocFunctionHook *)
487 malloc(sizeof(struct NetprocFunctionHook));
488 newfcn->next = NetprocHookTable;
489 newfcn->h_function_pointer = handler;
490 NetprocHookTable = newfcn;
492 lprintf(CTDL_INFO, "Registered a new netproc function\n");
496 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
498 struct NetprocFunctionHook *cur, *p;
500 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
501 /* This will also remove duplicates if any */
502 while (cur != NULL &&
503 handler == cur->h_function_pointer ) {
504 lprintf(CTDL_INFO, "Unregistered netproc function\n");
506 if (cur == NetprocHookTable) {
507 NetprocHookTable = p;
515 void CtdlDestroyNetprocHooks(void)
517 struct NetprocFunctionHook *cur, *p;
519 cur = NetprocHookTable;
522 lprintf(CTDL_INFO, "Unregistered netproc function\n");
527 NetprocHookTable = NULL;
531 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
533 struct DeleteFunctionHook *newfcn;
535 newfcn = (struct DeleteFunctionHook *)
536 malloc(sizeof(struct DeleteFunctionHook));
537 newfcn->next = DeleteHookTable;
538 newfcn->h_function_pointer = handler;
539 DeleteHookTable = newfcn;
541 lprintf(CTDL_INFO, "Registered a new netproc function\n");
545 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
547 struct DeleteFunctionHook *cur, *p;
549 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
550 /* This will also remove duplicates if any */
551 while (cur != NULL &&
552 handler == cur->h_function_pointer ) {
553 lprintf(CTDL_INFO, "Unregistered netproc function\n");
555 if (cur == DeleteHookTable) {
563 void CtdlDestroyDeleteHooks(void)
565 struct DeleteFunctionHook *cur, *p;
567 cur = DeleteHookTable;
570 lprintf(CTDL_INFO, "Destroyed netproc function\n");
575 DeleteHookTable = NULL;
581 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
583 struct FixedOutputHook *newfcn;
585 newfcn = (struct FixedOutputHook *)
586 malloc(sizeof(struct FixedOutputHook));
587 newfcn->next = FixedOutputTable;
588 newfcn->h_function_pointer = handler;
589 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
590 FixedOutputTable = newfcn;
592 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
596 void CtdlUnregisterFixedOutputHook(char *content_type)
598 struct FixedOutputHook *cur, *p;
600 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
601 /* This will also remove duplicates if any */
602 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
603 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
605 if (cur == FixedOutputTable) {
606 FixedOutputTable = p;
614 void CtdlDestroyFixedOutputHooks(void)
616 struct FixedOutputHook *cur, *p;
618 cur = FixedOutputTable;
621 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
627 FixedOutputTable = NULL;
630 /* returns nonzero if we found a hook and used it */
631 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
633 struct FixedOutputHook *fcn;
635 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
636 if (!strcasecmp(content_type, fcn->content_type)) {
637 (*fcn->h_function_pointer) (content, content_length);
648 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
651 struct XmsgFunctionHook *newfcn;
653 newfcn = (struct XmsgFunctionHook *)
654 malloc(sizeof(struct XmsgFunctionHook));
655 newfcn->next = XmsgHookTable;
656 newfcn->order = order;
657 newfcn->h_function_pointer = fcn_ptr;
658 XmsgHookTable = newfcn;
659 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
663 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
665 struct XmsgFunctionHook *cur, *p;
667 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
668 /* This will also remove duplicates if any */
669 while (cur != NULL &&
670 fcn_ptr == cur->h_function_pointer &&
671 order == cur->order) {
672 lprintf(CTDL_INFO, "Unregistered x-msg function "
673 "(priority %d)\n", order);
675 if (cur == XmsgHookTable) {
684 void CtdlDestroyXmsgHooks(void)
686 struct XmsgFunctionHook *cur, *p;
691 lprintf(CTDL_INFO, "Destroyed x-msg function "
692 "(priority %d)\n", cur->order);
698 XmsgHookTable = NULL;
702 void CtdlRegisterServiceHook(int tcp_port,
704 void (*h_greeting_function) (void),
705 void (*h_command_function) (void),
706 void (*h_async_function) (void)
709 struct ServiceFunctionHook *newfcn;
714 newfcn = (struct ServiceFunctionHook *)
715 malloc(sizeof(struct ServiceFunctionHook));
716 message = (char*) malloc (SIZ);
718 newfcn->next = ServiceHookTable;
719 newfcn->tcp_port = tcp_port;
720 newfcn->sockpath = sockpath;
721 newfcn->h_greeting_function = h_greeting_function;
722 newfcn->h_command_function = h_command_function;
723 newfcn->h_async_function = h_async_function;
725 if (sockpath != NULL) {
726 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
727 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
729 else if (tcp_port <= 0) { /* port -1 to disable */
730 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
736 newfcn->msock = ig_tcp_server(config.c_ip_addr,
738 config.c_maxsessions,
740 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
743 if (newfcn->msock > 0) {
744 ServiceHookTable = newfcn;
745 strcat(message, "registered.");
746 lprintf(CTDL_INFO, "%s\n", message);
749 AddPortError(message, error);
750 strcat(message, "FAILED.");
751 lprintf(CTDL_CRIT, "%s\n", message);
760 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
761 void (*h_greeting_function) (void),
762 void (*h_command_function) (void),
763 void (*h_async_function) (void)
766 struct ServiceFunctionHook *cur, *p;
768 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
769 /* This will also remove duplicates if any */
770 while (cur != NULL &&
771 !(sockpath && cur->sockpath &&
772 strcmp(sockpath, cur->sockpath)) &&
773 h_greeting_function == cur->h_greeting_function &&
774 h_command_function == cur->h_command_function &&
775 h_async_function == cur->h_async_function &&
776 tcp_port == cur->tcp_port) {
779 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
781 } else if (tcp_port) {
782 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
784 lprintf(CTDL_INFO, "Unregistered unknown service\n");
787 if (cur == ServiceHookTable) {
788 ServiceHookTable = p;
796 void CtdlDestroyServiceHook(void)
798 struct ServiceFunctionHook *cur, *p;
800 cur = ServiceHookTable;
805 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
807 } else if (cur->tcp_port) {
808 lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
810 lprintf(CTDL_INFO, "Unregistered unknown service\n");
816 ServiceHookTable = NULL;
820 void PerformSessionHooks(int EventType)
822 struct SessionFunctionHook *fcn;
824 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
825 if (fcn->eventtype == EventType) {
826 (*fcn->h_function_pointer) ();
831 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
833 struct UserFunctionHook *fcn;
835 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
836 if (fcn->eventtype == EventType) {
837 (*fcn->h_function_pointer) (usbuf);
842 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
844 struct MessageFunctionHook *fcn;
845 int total_retval = 0;
847 /* Other code may elect to protect this message from server-side
848 * handlers; if this is the case, don't do anything.
849 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
850 EventType, msg->cm_flags);
852 if (msg->cm_flags & CM_SKIP_HOOKS) {
853 lprintf(CTDL_DEBUG, "Skipping hooks\n");
857 /* Otherwise, run all the hooks appropriate to this event type.
859 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
860 if (fcn->eventtype == EventType) {
861 total_retval = total_retval +
862 (*fcn->h_function_pointer) (msg);
866 /* Return the sum of the return codes from the hook functions. If
867 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
868 * the save operation to abort.
875 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
877 struct NetprocFunctionHook *fcn;
878 int total_retval = 0;
880 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
881 total_retval = total_retval +
882 (*fcn->h_function_pointer) (msg, target_room);
885 /* Return the sum of the return codes from the hook functions.
886 * A nonzero return code will cause the message to *not* be imported.
892 void PerformDeleteHooks(char *room, long msgnum)
894 struct DeleteFunctionHook *fcn;
896 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
897 (*fcn->h_function_pointer) (room, msgnum);
905 int PerformXmsgHooks(char *sender, char *recp, char *msg)
907 struct XmsgFunctionHook *fcn;
911 for (p=0; p<MAX_XMSG_PRI; ++p) {
912 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
913 if (fcn->order == p) {
915 (*fcn->h_function_pointer)
919 /* Break out of the loop if a higher-priority function
920 * successfully delivered the message. This prevents duplicate
921 * deliveries to local users simultaneously signed onto
924 if (total_sent) break;