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";
68 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
69 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
70 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
71 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
72 " and stop the programm, that binds these ports. You should eventually remove "
73 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
76 void LogPrintMessages(long err)
78 char *List, *DetailList, *Short, *Where, *Hint, *Message;
79 int n = nSizPort + nSizErrmsg + 5;
81 Message = (char*) malloc(n * SIZ);
90 DetailList = errormessages;
101 snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
103 quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
109 void AppendString(char **target, char *append, size_t *len, size_t rate)
115 AddLen = strlen(append);
121 *target = (char*)malloc (*len * SIZ);
125 oLen = strlen(*target);
126 RelPtr = strlen(*target);
127 if (oLen + AddLen + 2 > *len * SIZ)
129 char *Buff = *target;
130 size_t NewSiz = *len + 10;
131 *target = malloc (NewSiz * SIZ);
132 memcpy (*target, Buff, NewSiz * SIZ);
136 memcpy (*target + oLen, append, AddLen);
137 (*target)[oLen + AddLen + 1] = '\n';
138 (*target)[oLen + AddLen + 2] = '\0';
141 void AddPortError(char *Port, char *ErrorMessage)
146 DetailErrorFlags |= ERR_PORT;
148 AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
149 AppendString(&portlist, Port, &nSizPort, 2);
151 pos = strchr (portlist, ':');
154 len = strlen (errormessages);
155 if (nSizErrmsg * SIZ > len + 3)
157 errormessages[len] = ';';
158 errormessages[len+1] = ' ';
159 errormessages[len+2] = '\0';
164 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
166 struct ProtoFunctionHook *p;
168 p = (struct ProtoFunctionHook *)
169 malloc(sizeof(struct ProtoFunctionHook));
172 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
175 p->handler = handler;
178 p->next = ProtoHookList;
180 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
184 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
186 struct ProtoFunctionHook *cur = NULL;
187 struct ProtoFunctionHook *p = NULL;
188 struct ProtoFunctionHook *lastcur = NULL;
190 for (cur = ProtoHookList;
192 cur = (cur != NULL)? cur->next: NULL) {
193 /* This will also remove duplicates if any */
194 while (cur != NULL &&
195 handler == cur->handler &&
196 !strcmp(cmd, cur->cmd)) {
197 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
200 if (cur == ProtoHookList) {
203 else if (lastcur != NULL)
214 void CtdlDestroyProtoHooks(void)
216 struct ProtoFunctionHook *cur, *p;
221 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
222 cur->cmd, cur->desc);
227 ProtoHookList = NULL;
231 int DLoader_Exec_Cmd(char *cmdbuf)
233 struct ProtoFunctionHook *p;
235 for (p = ProtoHookList; p; p = p->next) {
236 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
237 p->handler(&cmdbuf[5]);
244 void initialize_server_extensions(void)
248 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
249 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
250 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
251 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
252 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
253 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
254 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
255 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
256 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
257 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
258 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
259 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
260 lprintf(CTDL_INFO, "%s\n", serv_network_init());
261 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
262 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
263 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
264 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
265 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
266 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
267 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
268 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
269 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
270 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
271 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
272 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
273 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
274 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
275 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
276 for (filter = 1; filter != 0; filter = filter << 1)
277 if ((filter & DetailErrorFlags) != 0)
278 LogPrintMessages(filter);
283 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
286 struct CleanupFunctionHook *newfcn;
288 newfcn = (struct CleanupFunctionHook *)
289 malloc(sizeof(struct CleanupFunctionHook));
290 newfcn->next = CleanupHookTable;
291 newfcn->h_function_pointer = fcn_ptr;
292 CleanupHookTable = newfcn;
294 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
298 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
300 struct CleanupFunctionHook *cur, *p;
302 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
303 /* This will also remove duplicates if any */
304 while (cur != NULL &&
305 fcn_ptr == cur->h_function_pointer) {
306 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
308 if (cur == CleanupHookTable) {
309 CleanupHookTable = p;
317 void CtdlDestroyCleanupHooks(void)
319 struct CleanupFunctionHook *cur, *p;
321 cur = CleanupHookTable;
324 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
329 CleanupHookTable = NULL;
333 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
336 struct SessionFunctionHook *newfcn;
338 newfcn = (struct SessionFunctionHook *)
339 malloc(sizeof(struct SessionFunctionHook));
340 newfcn->next = SessionHookTable;
341 newfcn->h_function_pointer = fcn_ptr;
342 newfcn->eventtype = EventType;
343 SessionHookTable = newfcn;
345 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
350 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
352 struct SessionFunctionHook *cur, *p;
354 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
355 /* This will also remove duplicates if any */
356 while (cur != NULL &&
357 fcn_ptr == cur->h_function_pointer &&
358 EventType == cur->eventtype) {
359 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
362 if (cur == SessionHookTable) {
363 SessionHookTable = p;
371 void CtdlDestroySessionHooks(void)
373 struct SessionFunctionHook *cur, *p;
375 cur = SessionHookTable;
378 lprintf(CTDL_INFO, "Destroyed session function\n");
383 SessionHookTable = NULL;
387 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
390 struct UserFunctionHook *newfcn;
392 newfcn = (struct UserFunctionHook *)
393 malloc(sizeof(struct UserFunctionHook));
394 newfcn->next = UserHookTable;
395 newfcn->h_function_pointer = fcn_ptr;
396 newfcn->eventtype = EventType;
397 UserHookTable = newfcn;
399 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
404 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
406 struct UserFunctionHook *cur, *p;
408 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
409 /* This will also remove duplicates if any */
410 while (cur != NULL &&
411 fcn_ptr == cur->h_function_pointer &&
412 EventType == cur->eventtype) {
413 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
416 if (cur == UserHookTable) {
425 void CtdlDestroyUserHooks(void)
427 struct UserFunctionHook *cur, *p;
432 lprintf(CTDL_INFO, "Destroyed user function \n");
437 UserHookTable = NULL;
441 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
445 struct MessageFunctionHook *newfcn;
447 newfcn = (struct MessageFunctionHook *)
448 malloc(sizeof(struct MessageFunctionHook));
449 newfcn->next = MessageHookTable;
450 newfcn->h_function_pointer = handler;
451 newfcn->eventtype = EventType;
452 MessageHookTable = newfcn;
454 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
459 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
462 struct MessageFunctionHook *cur, *p;
464 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
465 /* This will also remove duplicates if any */
466 while (cur != NULL &&
467 handler == cur->h_function_pointer &&
468 EventType == cur->eventtype) {
469 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
472 if (cur == MessageHookTable) {
473 MessageHookTable = p;
481 void CtdlDestroyMessageHook(void)
483 struct MessageFunctionHook *cur, *p;
485 cur = MessageHookTable;
488 lprintf(CTDL_INFO, "Destroyed message function \n");
493 MessageHookTable = NULL;
497 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
499 struct NetprocFunctionHook *newfcn;
501 newfcn = (struct NetprocFunctionHook *)
502 malloc(sizeof(struct NetprocFunctionHook));
503 newfcn->next = NetprocHookTable;
504 newfcn->h_function_pointer = handler;
505 NetprocHookTable = newfcn;
507 lprintf(CTDL_INFO, "Registered a new netproc function\n");
511 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
513 struct NetprocFunctionHook *cur, *p;
515 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
516 /* This will also remove duplicates if any */
517 while (cur != NULL &&
518 handler == cur->h_function_pointer ) {
519 lprintf(CTDL_INFO, "Unregistered netproc function\n");
521 if (cur == NetprocHookTable) {
522 NetprocHookTable = p;
530 void CtdlDestroyNetprocHooks(void)
532 struct NetprocFunctionHook *cur, *p;
534 cur = NetprocHookTable;
537 lprintf(CTDL_INFO, "Unregistered netproc function\n");
542 NetprocHookTable = NULL;
546 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
548 struct DeleteFunctionHook *newfcn;
550 newfcn = (struct DeleteFunctionHook *)
551 malloc(sizeof(struct DeleteFunctionHook));
552 newfcn->next = DeleteHookTable;
553 newfcn->h_function_pointer = handler;
554 DeleteHookTable = newfcn;
556 lprintf(CTDL_INFO, "Registered a new netproc function\n");
560 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
562 struct DeleteFunctionHook *cur, *p;
564 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
565 /* This will also remove duplicates if any */
566 while (cur != NULL &&
567 handler == cur->h_function_pointer ) {
568 lprintf(CTDL_INFO, "Unregistered netproc function\n");
570 if (cur == DeleteHookTable) {
578 void CtdlDestroyDeleteHooks(void)
580 struct DeleteFunctionHook *cur, *p;
582 cur = DeleteHookTable;
585 lprintf(CTDL_INFO, "Destroyed netproc function\n");
590 DeleteHookTable = NULL;
596 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
598 struct FixedOutputHook *newfcn;
600 newfcn = (struct FixedOutputHook *)
601 malloc(sizeof(struct FixedOutputHook));
602 newfcn->next = FixedOutputTable;
603 newfcn->h_function_pointer = handler;
604 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
605 FixedOutputTable = newfcn;
607 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
611 void CtdlUnregisterFixedOutputHook(char *content_type)
613 struct FixedOutputHook *cur, *p;
615 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
616 /* This will also remove duplicates if any */
617 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
618 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
620 if (cur == FixedOutputTable) {
621 FixedOutputTable = p;
629 void CtdlDestroyFixedOutputHooks(void)
631 struct FixedOutputHook *cur, *p;
633 cur = FixedOutputTable;
636 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
642 FixedOutputTable = NULL;
645 /* returns nonzero if we found a hook and used it */
646 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
648 struct FixedOutputHook *fcn;
650 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
651 if (!strcasecmp(content_type, fcn->content_type)) {
652 (*fcn->h_function_pointer) (content, content_length);
663 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
666 struct XmsgFunctionHook *newfcn;
668 newfcn = (struct XmsgFunctionHook *)
669 malloc(sizeof(struct XmsgFunctionHook));
670 newfcn->next = XmsgHookTable;
671 newfcn->order = order;
672 newfcn->h_function_pointer = fcn_ptr;
673 XmsgHookTable = newfcn;
674 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
678 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
680 struct XmsgFunctionHook *cur, *p;
682 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
683 /* This will also remove duplicates if any */
684 while (cur != NULL &&
685 fcn_ptr == cur->h_function_pointer &&
686 order == cur->order) {
687 lprintf(CTDL_INFO, "Unregistered x-msg function "
688 "(priority %d)\n", order);
690 if (cur == XmsgHookTable) {
699 void CtdlDestroyXmsgHooks(void)
701 struct XmsgFunctionHook *cur, *p;
706 lprintf(CTDL_INFO, "Destroyed x-msg function "
707 "(priority %d)\n", cur->order);
713 XmsgHookTable = NULL;
717 void CtdlRegisterServiceHook(int tcp_port,
719 void (*h_greeting_function) (void),
720 void (*h_command_function) (void),
721 void (*h_async_function) (void)
724 struct ServiceFunctionHook *newfcn;
729 newfcn = (struct ServiceFunctionHook *)
730 malloc(sizeof(struct ServiceFunctionHook));
731 message = (char*) malloc (SIZ);
733 newfcn->next = ServiceHookTable;
734 newfcn->tcp_port = tcp_port;
735 newfcn->sockpath = sockpath;
736 newfcn->h_greeting_function = h_greeting_function;
737 newfcn->h_command_function = h_command_function;
738 newfcn->h_async_function = h_async_function;
740 if (sockpath != NULL) {
741 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
742 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
744 else if (tcp_port <= 0) { /* port -1 to disable */
745 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
751 newfcn->msock = ig_tcp_server(config.c_ip_addr,
753 config.c_maxsessions,
755 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
758 if (newfcn->msock > 0) {
759 ServiceHookTable = newfcn;
760 strcat(message, "registered.");
761 lprintf(CTDL_INFO, "%s\n", message);
764 AddPortError(message, error);
765 strcat(message, "FAILED.");
766 lprintf(CTDL_CRIT, "%s\n", message);
774 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
775 void (*h_greeting_function) (void),
776 void (*h_command_function) (void),
777 void (*h_async_function) (void)
780 struct ServiceFunctionHook *cur, *p;
782 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
783 /* This will also remove duplicates if any */
784 while (cur != NULL &&
785 !(sockpath && cur->sockpath &&
786 strcmp(sockpath, cur->sockpath)) &&
787 h_greeting_function == cur->h_greeting_function &&
788 h_command_function == cur->h_command_function &&
789 h_async_function == cur->h_async_function &&
790 tcp_port == cur->tcp_port) {
793 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
795 } else if (tcp_port) {
796 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
798 lprintf(CTDL_INFO, "Unregistered unknown service\n");
801 if (cur == ServiceHookTable) {
802 ServiceHookTable = p;
810 void CtdlDestroyServiceHook(void)
812 struct ServiceFunctionHook *cur, *p;
814 cur = ServiceHookTable;
819 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
821 } else if (cur->tcp_port) {
822 lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
824 lprintf(CTDL_INFO, "Unregistered unknown service\n");
830 ServiceHookTable = NULL;
834 void PerformSessionHooks(int EventType)
836 struct SessionFunctionHook *fcn = NULL;
838 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
839 if (fcn->eventtype == EventType) {
840 (*fcn->h_function_pointer) ();
845 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
847 struct UserFunctionHook *fcn = NULL;
849 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
850 if (fcn->eventtype == EventType) {
851 (*fcn->h_function_pointer) (usbuf);
856 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
858 struct MessageFunctionHook *fcn = NULL;
859 int total_retval = 0;
861 /* Other code may elect to protect this message from server-side
862 * handlers; if this is the case, don't do anything.
863 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
864 EventType, msg->cm_flags);
866 if (msg->cm_flags & CM_SKIP_HOOKS) {
867 lprintf(CTDL_DEBUG, "Skipping hooks\n");
871 /* Otherwise, run all the hooks appropriate to this event type.
873 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
874 if (fcn->eventtype == EventType) {
875 total_retval = total_retval +
876 (*fcn->h_function_pointer) (msg);
880 /* Return the sum of the return codes from the hook functions. If
881 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
882 * the save operation to abort.
889 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
891 struct NetprocFunctionHook *fcn;
892 int total_retval = 0;
894 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
895 total_retval = total_retval +
896 (*fcn->h_function_pointer) (msg, target_room);
899 /* Return the sum of the return codes from the hook functions.
900 * A nonzero return code will cause the message to *not* be imported.
906 void PerformDeleteHooks(char *room, long msgnum)
908 struct DeleteFunctionHook *fcn;
910 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
911 (*fcn->h_function_pointer) (room, msgnum);
919 int PerformXmsgHooks(char *sender, char *recp, char *msg)
921 struct XmsgFunctionHook *fcn;
925 for (p=0; p<MAX_XMSG_PRI; ++p) {
926 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
927 if (fcn->order == p) {
929 (*fcn->h_function_pointer)
933 /* Break out of the loop if a higher-priority function
934 * successfully delivered the message. This prevents duplicate
935 * deliveries to local users simultaneously signed onto
938 if (total_sent) break;