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;
61 char *ErrSubject = "Startup Problems";
62 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"
63 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
64 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
65 "The errors returned by the system were:\n%s\n"
66 "You can recheck the above if you follow this faq item:\n"
67 "http://www.citadel.org/doku.php/faq:mastering_your_os:net#netstat";
70 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
71 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
72 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
73 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
74 " and stop the programm, that binds these ports. You should eventually remove "
75 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
78 void LogPrintMessages(long err)
80 char *List, *DetailList, *Short, *Where, *Hint, *Message;
81 int n = nSizPort + nSizErrmsg + 5;
83 Message = (char*) malloc(n * SIZ);
92 DetailList = errormessages;
103 snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
105 quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
111 void AppendString(char **target, char *append, size_t *len, size_t rate)
117 AddLen = strlen(append);
123 *target = (char*)malloc (*len * SIZ);
127 oLen = strlen(*target);
128 RelPtr = strlen(*target);
129 if (oLen + AddLen + 2 > *len * SIZ)
131 char *Buff = *target;
132 size_t NewSiz = *len + 10;
133 *target = malloc (NewSiz * SIZ);
134 memcpy (*target, Buff, NewSiz * SIZ);
138 memcpy (*target + oLen, append, AddLen);
139 (*target)[oLen + AddLen + 1] = '\n';
140 (*target)[oLen + AddLen + 2] = '\0';
143 void AddPortError(char *Port, char *ErrorMessage)
148 DetailErrorFlags |= ERR_PORT;
150 AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
151 AppendString(&portlist, Port, &nSizPort, 2);
153 pos = strchr (portlist, ':');
154 if (pos != NULL) *pos = ';';
156 len = strlen (errormessages);
157 if (nSizErrmsg * SIZ > len + 3)
159 errormessages[len] = ';';
160 errormessages[len+1] = ' ';
161 errormessages[len+2] = '\0';
166 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
168 struct ProtoFunctionHook *p;
170 p = (struct ProtoFunctionHook *)
171 malloc(sizeof(struct ProtoFunctionHook));
174 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
177 p->handler = handler;
180 p->next = ProtoHookList;
182 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
186 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
188 struct ProtoFunctionHook *cur = NULL;
189 struct ProtoFunctionHook *p = NULL;
190 struct ProtoFunctionHook *lastcur = NULL;
192 for (cur = ProtoHookList;
194 cur = (cur != NULL)? cur->next: NULL) {
195 /* This will also remove duplicates if any */
196 while (cur != NULL &&
197 handler == cur->handler &&
198 !strcmp(cmd, cur->cmd)) {
199 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
202 if (cur == ProtoHookList) {
205 else if (lastcur != NULL)
216 void CtdlDestroyProtoHooks(void)
218 struct ProtoFunctionHook *cur, *p;
223 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
224 cur->cmd, cur->desc);
229 ProtoHookList = NULL;
233 int DLoader_Exec_Cmd(char *cmdbuf)
235 struct ProtoFunctionHook *p;
237 for (p = ProtoHookList; p; p = p->next) {
238 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
239 p->handler(&cmdbuf[5]);
246 void initialize_server_extensions(void)
250 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
251 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
252 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
253 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
254 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
255 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
256 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
257 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
258 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
259 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
260 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
261 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
262 lprintf(CTDL_INFO, "%s\n", serv_network_init());
263 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
264 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
265 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
266 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
267 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
268 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
269 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
270 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
271 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
272 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
273 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
274 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
275 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
276 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
277 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
278 for (filter = 1; filter != 0; filter = filter << 1)
279 if ((filter & DetailErrorFlags) != 0)
280 LogPrintMessages(filter);
285 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
288 struct CleanupFunctionHook *newfcn;
290 newfcn = (struct CleanupFunctionHook *)
291 malloc(sizeof(struct CleanupFunctionHook));
292 newfcn->next = CleanupHookTable;
293 newfcn->h_function_pointer = fcn_ptr;
294 CleanupHookTable = newfcn;
296 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
300 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
302 struct CleanupFunctionHook *cur, *p;
304 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
305 /* This will also remove duplicates if any */
306 while (cur != NULL &&
307 fcn_ptr == cur->h_function_pointer) {
308 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
310 if (cur == CleanupHookTable) {
311 CleanupHookTable = p;
319 void CtdlDestroyCleanupHooks(void)
321 struct CleanupFunctionHook *cur, *p;
323 cur = CleanupHookTable;
326 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
331 CleanupHookTable = NULL;
335 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
338 struct SessionFunctionHook *newfcn;
340 newfcn = (struct SessionFunctionHook *)
341 malloc(sizeof(struct SessionFunctionHook));
342 newfcn->next = SessionHookTable;
343 newfcn->h_function_pointer = fcn_ptr;
344 newfcn->eventtype = EventType;
345 SessionHookTable = newfcn;
347 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
352 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
354 struct SessionFunctionHook *cur, *p;
356 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
357 /* This will also remove duplicates if any */
358 while (cur != NULL &&
359 fcn_ptr == cur->h_function_pointer &&
360 EventType == cur->eventtype) {
361 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
364 if (cur == SessionHookTable) {
365 SessionHookTable = p;
373 void CtdlDestroySessionHooks(void)
375 struct SessionFunctionHook *cur, *p;
377 cur = SessionHookTable;
380 lprintf(CTDL_INFO, "Destroyed session function\n");
385 SessionHookTable = NULL;
389 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
392 struct UserFunctionHook *newfcn;
394 newfcn = (struct UserFunctionHook *)
395 malloc(sizeof(struct UserFunctionHook));
396 newfcn->next = UserHookTable;
397 newfcn->h_function_pointer = fcn_ptr;
398 newfcn->eventtype = EventType;
399 UserHookTable = newfcn;
401 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
406 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
408 struct UserFunctionHook *cur, *p;
410 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
411 /* This will also remove duplicates if any */
412 while (cur != NULL &&
413 fcn_ptr == cur->h_function_pointer &&
414 EventType == cur->eventtype) {
415 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
418 if (cur == UserHookTable) {
427 void CtdlDestroyUserHooks(void)
429 struct UserFunctionHook *cur, *p;
434 lprintf(CTDL_INFO, "Destroyed user function \n");
439 UserHookTable = NULL;
443 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
447 struct MessageFunctionHook *newfcn;
449 newfcn = (struct MessageFunctionHook *)
450 malloc(sizeof(struct MessageFunctionHook));
451 newfcn->next = MessageHookTable;
452 newfcn->h_function_pointer = handler;
453 newfcn->eventtype = EventType;
454 MessageHookTable = newfcn;
456 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
461 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
464 struct MessageFunctionHook *cur, *p;
466 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
467 /* This will also remove duplicates if any */
468 while (cur != NULL &&
469 handler == cur->h_function_pointer &&
470 EventType == cur->eventtype) {
471 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
474 if (cur == MessageHookTable) {
475 MessageHookTable = p;
483 void CtdlDestroyMessageHook(void)
485 struct MessageFunctionHook *cur, *p;
487 cur = MessageHookTable;
490 lprintf(CTDL_INFO, "Destroyed message function \n");
495 MessageHookTable = NULL;
499 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
501 struct NetprocFunctionHook *newfcn;
503 newfcn = (struct NetprocFunctionHook *)
504 malloc(sizeof(struct NetprocFunctionHook));
505 newfcn->next = NetprocHookTable;
506 newfcn->h_function_pointer = handler;
507 NetprocHookTable = newfcn;
509 lprintf(CTDL_INFO, "Registered a new netproc function\n");
513 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
515 struct NetprocFunctionHook *cur, *p;
517 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
518 /* This will also remove duplicates if any */
519 while (cur != NULL &&
520 handler == cur->h_function_pointer ) {
521 lprintf(CTDL_INFO, "Unregistered netproc function\n");
523 if (cur == NetprocHookTable) {
524 NetprocHookTable = p;
532 void CtdlDestroyNetprocHooks(void)
534 struct NetprocFunctionHook *cur, *p;
536 cur = NetprocHookTable;
539 lprintf(CTDL_INFO, "Unregistered netproc function\n");
544 NetprocHookTable = NULL;
548 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
550 struct DeleteFunctionHook *newfcn;
552 newfcn = (struct DeleteFunctionHook *)
553 malloc(sizeof(struct DeleteFunctionHook));
554 newfcn->next = DeleteHookTable;
555 newfcn->h_function_pointer = handler;
556 DeleteHookTable = newfcn;
558 lprintf(CTDL_INFO, "Registered a new netproc function\n");
562 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
564 struct DeleteFunctionHook *cur, *p;
566 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
567 /* This will also remove duplicates if any */
568 while (cur != NULL &&
569 handler == cur->h_function_pointer ) {
570 lprintf(CTDL_INFO, "Unregistered netproc function\n");
572 if (cur == DeleteHookTable) {
580 void CtdlDestroyDeleteHooks(void)
582 struct DeleteFunctionHook *cur, *p;
584 cur = DeleteHookTable;
587 lprintf(CTDL_INFO, "Destroyed netproc function\n");
592 DeleteHookTable = NULL;
598 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
600 struct FixedOutputHook *newfcn;
602 newfcn = (struct FixedOutputHook *)
603 malloc(sizeof(struct FixedOutputHook));
604 newfcn->next = FixedOutputTable;
605 newfcn->h_function_pointer = handler;
606 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
607 FixedOutputTable = newfcn;
609 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
613 void CtdlUnregisterFixedOutputHook(char *content_type)
615 struct FixedOutputHook *cur, *p;
617 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
618 /* This will also remove duplicates if any */
619 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
620 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
622 if (cur == FixedOutputTable) {
623 FixedOutputTable = p;
631 void CtdlDestroyFixedOutputHooks(void)
633 struct FixedOutputHook *cur, *p;
635 cur = FixedOutputTable;
638 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
644 FixedOutputTable = NULL;
647 /* returns nonzero if we found a hook and used it */
648 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
650 struct FixedOutputHook *fcn;
652 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
653 if (!strcasecmp(content_type, fcn->content_type)) {
654 (*fcn->h_function_pointer) (content, content_length);
665 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
668 struct XmsgFunctionHook *newfcn;
670 newfcn = (struct XmsgFunctionHook *)
671 malloc(sizeof(struct XmsgFunctionHook));
672 newfcn->next = XmsgHookTable;
673 newfcn->order = order;
674 newfcn->h_function_pointer = fcn_ptr;
675 XmsgHookTable = newfcn;
676 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
680 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
682 struct XmsgFunctionHook *cur, *p;
684 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
685 /* This will also remove duplicates if any */
686 while (cur != NULL &&
687 fcn_ptr == cur->h_function_pointer &&
688 order == cur->order) {
689 lprintf(CTDL_INFO, "Unregistered x-msg function "
690 "(priority %d)\n", order);
692 if (cur == XmsgHookTable) {
701 void CtdlDestroyXmsgHooks(void)
703 struct XmsgFunctionHook *cur, *p;
708 lprintf(CTDL_INFO, "Destroyed x-msg function "
709 "(priority %d)\n", cur->order);
715 XmsgHookTable = NULL;
719 void CtdlRegisterServiceHook(int tcp_port,
721 void (*h_greeting_function) (void),
722 void (*h_command_function) (void),
723 void (*h_async_function) (void)
726 struct ServiceFunctionHook *newfcn;
731 newfcn = (struct ServiceFunctionHook *)
732 malloc(sizeof(struct ServiceFunctionHook));
733 message = (char*) malloc (SIZ);
735 newfcn->next = ServiceHookTable;
736 newfcn->tcp_port = tcp_port;
737 newfcn->sockpath = sockpath;
738 newfcn->h_greeting_function = h_greeting_function;
739 newfcn->h_command_function = h_command_function;
740 newfcn->h_async_function = h_async_function;
742 if (sockpath != NULL) {
743 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
744 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
746 else if (tcp_port <= 0) { /* port -1 to disable */
747 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
753 newfcn->msock = ig_tcp_server(config.c_ip_addr,
755 config.c_maxsessions,
757 snprintf(message, SIZ, "TCP port %s:%d: ",
758 config.c_ip_addr, tcp_port);
761 if (newfcn->msock > 0) {
762 ServiceHookTable = newfcn;
763 strcat(message, "registered.");
764 lprintf(CTDL_INFO, "%s\n", message);
767 AddPortError(message, error);
768 strcat(message, "FAILED.");
769 lprintf(CTDL_CRIT, "%s\n", message);
777 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
778 void (*h_greeting_function) (void),
779 void (*h_command_function) (void),
780 void (*h_async_function) (void)
783 struct ServiceFunctionHook *cur, *p;
785 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
786 /* This will also remove duplicates if any */
787 while (cur != NULL &&
788 !(sockpath && cur->sockpath &&
789 strcmp(sockpath, cur->sockpath)) &&
790 h_greeting_function == cur->h_greeting_function &&
791 h_command_function == cur->h_command_function &&
792 h_async_function == cur->h_async_function &&
793 tcp_port == cur->tcp_port) {
796 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
798 } else if (tcp_port) {
799 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
801 lprintf(CTDL_INFO, "Unregistered unknown service\n");
804 if (cur == ServiceHookTable) {
805 ServiceHookTable = p;
813 void CtdlDestroyServiceHook(void)
815 struct ServiceFunctionHook *cur, *p;
817 cur = ServiceHookTable;
822 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
824 } else if (cur->tcp_port) {
825 lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
827 lprintf(CTDL_INFO, "Unregistered unknown service\n");
833 ServiceHookTable = NULL;
837 void PerformSessionHooks(int EventType)
839 struct SessionFunctionHook *fcn = NULL;
841 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
842 if (fcn->eventtype == EventType) {
843 (*fcn->h_function_pointer) ();
848 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
850 struct UserFunctionHook *fcn = NULL;
852 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
853 if (fcn->eventtype == EventType) {
854 (*fcn->h_function_pointer) (usbuf);
859 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
861 struct MessageFunctionHook *fcn = NULL;
862 int total_retval = 0;
864 /* Other code may elect to protect this message from server-side
865 * handlers; if this is the case, don't do anything.
866 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
867 EventType, msg->cm_flags);
869 if (msg->cm_flags & CM_SKIP_HOOKS) {
870 lprintf(CTDL_DEBUG, "Skipping hooks\n");
874 /* Otherwise, run all the hooks appropriate to this event type.
876 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
877 if (fcn->eventtype == EventType) {
878 total_retval = total_retval +
879 (*fcn->h_function_pointer) (msg);
883 /* Return the sum of the return codes from the hook functions. If
884 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
885 * the save operation to abort.
892 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
894 struct NetprocFunctionHook *fcn;
895 int total_retval = 0;
897 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
898 total_retval = total_retval +
899 (*fcn->h_function_pointer) (msg, target_room);
902 /* Return the sum of the return codes from the hook functions.
903 * A nonzero return code will cause the message to *not* be imported.
909 void PerformDeleteHooks(char *room, long msgnum)
911 struct DeleteFunctionHook *fcn;
913 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
914 (*fcn->h_function_pointer) (room, msgnum);
922 int PerformXmsgHooks(char *sender, char *recp, char *msg)
924 struct XmsgFunctionHook *fcn;
928 for (p=0; p<MAX_XMSG_PRI; ++p) {
929 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
930 if (fcn->order == p) {
932 (*fcn->h_function_pointer)
936 /* Break out of the loop if a higher-priority function
937 * successfully delivered the message. This prevents duplicate
938 * deliveries to local users simultaneously signed onto
941 if (total_sent) break;