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 = NULL;
174 struct ProtoFunctionHook *p = NULL;
175 struct ProtoFunctionHook *lastcur = NULL;
177 for (cur = ProtoHookList;
179 cur = (cur != NULL)? cur->next: NULL) {
180 /* This will also remove duplicates if any */
181 while (cur != NULL &&
182 handler == cur->handler &&
183 !strcmp(cmd, cur->cmd)) {
184 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
187 if (cur == ProtoHookList) {
190 else if (lastcur != NULL)
201 void CtdlDestroyProtoHooks(void)
203 struct ProtoFunctionHook *cur, *p;
208 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
209 cur->cmd, cur->desc);
214 ProtoHookList = NULL;
218 int DLoader_Exec_Cmd(char *cmdbuf)
220 struct ProtoFunctionHook *p;
222 for (p = ProtoHookList; p; p = p->next) {
223 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
224 p->handler(&cmdbuf[5]);
231 void initialize_server_extensions(void)
235 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
236 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
237 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
238 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
239 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
240 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
241 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
242 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
243 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
244 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
245 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
246 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
247 lprintf(CTDL_INFO, "%s\n", serv_network_init());
248 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
249 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
250 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
251 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
252 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
253 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
254 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
255 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
256 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
257 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
258 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
259 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
260 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
261 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
262 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
263 for (filter = 1; filter != 0; filter = filter << 1)
264 if ((filter & DetailErrorFlags) != 0)
265 LogPrintMessages(filter);
270 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
273 struct CleanupFunctionHook *newfcn;
275 newfcn = (struct CleanupFunctionHook *)
276 malloc(sizeof(struct CleanupFunctionHook));
277 newfcn->next = CleanupHookTable;
278 newfcn->h_function_pointer = fcn_ptr;
279 CleanupHookTable = newfcn;
281 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
285 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
287 struct CleanupFunctionHook *cur, *p;
289 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
290 /* This will also remove duplicates if any */
291 while (cur != NULL &&
292 fcn_ptr == cur->h_function_pointer) {
293 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
295 if (cur == CleanupHookTable) {
296 CleanupHookTable = p;
304 void CtdlDestroyCleanupHooks(void)
306 struct CleanupFunctionHook *cur, *p;
308 cur = CleanupHookTable;
311 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
316 CleanupHookTable = NULL;
320 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
323 struct SessionFunctionHook *newfcn;
325 newfcn = (struct SessionFunctionHook *)
326 malloc(sizeof(struct SessionFunctionHook));
327 newfcn->next = SessionHookTable;
328 newfcn->h_function_pointer = fcn_ptr;
329 newfcn->eventtype = EventType;
330 SessionHookTable = newfcn;
332 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
337 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
339 struct SessionFunctionHook *cur, *p;
341 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
342 /* This will also remove duplicates if any */
343 while (cur != NULL &&
344 fcn_ptr == cur->h_function_pointer &&
345 EventType == cur->eventtype) {
346 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
349 if (cur == SessionHookTable) {
350 SessionHookTable = p;
358 void CtdlDestroySessionHooks(void)
360 struct SessionFunctionHook *cur, *p;
362 cur = SessionHookTable;
365 lprintf(CTDL_INFO, "Destroyed session function\n");
370 SessionHookTable = NULL;
374 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
377 struct UserFunctionHook *newfcn;
379 newfcn = (struct UserFunctionHook *)
380 malloc(sizeof(struct UserFunctionHook));
381 newfcn->next = UserHookTable;
382 newfcn->h_function_pointer = fcn_ptr;
383 newfcn->eventtype = EventType;
384 UserHookTable = newfcn;
386 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
391 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
393 struct UserFunctionHook *cur, *p;
395 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
396 /* This will also remove duplicates if any */
397 while (cur != NULL &&
398 fcn_ptr == cur->h_function_pointer &&
399 EventType == cur->eventtype) {
400 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
403 if (cur == UserHookTable) {
412 void CtdlDestroyUserHooks(void)
414 struct UserFunctionHook *cur, *p;
419 lprintf(CTDL_INFO, "Destroyed user function \n");
424 UserHookTable = NULL;
428 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
432 struct MessageFunctionHook *newfcn;
434 newfcn = (struct MessageFunctionHook *)
435 malloc(sizeof(struct MessageFunctionHook));
436 newfcn->next = MessageHookTable;
437 newfcn->h_function_pointer = handler;
438 newfcn->eventtype = EventType;
439 MessageHookTable = newfcn;
441 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
446 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
449 struct MessageFunctionHook *cur, *p;
451 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
452 /* This will also remove duplicates if any */
453 while (cur != NULL &&
454 handler == cur->h_function_pointer &&
455 EventType == cur->eventtype) {
456 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
459 if (cur == MessageHookTable) {
460 MessageHookTable = p;
468 void CtdlDestroyMessageHook(void)
470 struct MessageFunctionHook *cur, *p;
472 cur = MessageHookTable;
475 lprintf(CTDL_INFO, "Destroyed message function \n");
480 MessageHookTable = NULL;
484 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
486 struct NetprocFunctionHook *newfcn;
488 newfcn = (struct NetprocFunctionHook *)
489 malloc(sizeof(struct NetprocFunctionHook));
490 newfcn->next = NetprocHookTable;
491 newfcn->h_function_pointer = handler;
492 NetprocHookTable = newfcn;
494 lprintf(CTDL_INFO, "Registered a new netproc function\n");
498 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
500 struct NetprocFunctionHook *cur, *p;
502 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
503 /* This will also remove duplicates if any */
504 while (cur != NULL &&
505 handler == cur->h_function_pointer ) {
506 lprintf(CTDL_INFO, "Unregistered netproc function\n");
508 if (cur == NetprocHookTable) {
509 NetprocHookTable = p;
517 void CtdlDestroyNetprocHooks(void)
519 struct NetprocFunctionHook *cur, *p;
521 cur = NetprocHookTable;
524 lprintf(CTDL_INFO, "Unregistered netproc function\n");
529 NetprocHookTable = NULL;
533 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
535 struct DeleteFunctionHook *newfcn;
537 newfcn = (struct DeleteFunctionHook *)
538 malloc(sizeof(struct DeleteFunctionHook));
539 newfcn->next = DeleteHookTable;
540 newfcn->h_function_pointer = handler;
541 DeleteHookTable = newfcn;
543 lprintf(CTDL_INFO, "Registered a new netproc function\n");
547 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
549 struct DeleteFunctionHook *cur, *p;
551 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
552 /* This will also remove duplicates if any */
553 while (cur != NULL &&
554 handler == cur->h_function_pointer ) {
555 lprintf(CTDL_INFO, "Unregistered netproc function\n");
557 if (cur == DeleteHookTable) {
565 void CtdlDestroyDeleteHooks(void)
567 struct DeleteFunctionHook *cur, *p;
569 cur = DeleteHookTable;
572 lprintf(CTDL_INFO, "Destroyed netproc function\n");
577 DeleteHookTable = NULL;
583 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
585 struct FixedOutputHook *newfcn;
587 newfcn = (struct FixedOutputHook *)
588 malloc(sizeof(struct FixedOutputHook));
589 newfcn->next = FixedOutputTable;
590 newfcn->h_function_pointer = handler;
591 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
592 FixedOutputTable = newfcn;
594 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
598 void CtdlUnregisterFixedOutputHook(char *content_type)
600 struct FixedOutputHook *cur, *p;
602 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
603 /* This will also remove duplicates if any */
604 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
605 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
607 if (cur == FixedOutputTable) {
608 FixedOutputTable = p;
616 void CtdlDestroyFixedOutputHooks(void)
618 struct FixedOutputHook *cur, *p;
620 cur = FixedOutputTable;
623 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
629 FixedOutputTable = NULL;
632 /* returns nonzero if we found a hook and used it */
633 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
635 struct FixedOutputHook *fcn;
637 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
638 if (!strcasecmp(content_type, fcn->content_type)) {
639 (*fcn->h_function_pointer) (content, content_length);
650 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
653 struct XmsgFunctionHook *newfcn;
655 newfcn = (struct XmsgFunctionHook *)
656 malloc(sizeof(struct XmsgFunctionHook));
657 newfcn->next = XmsgHookTable;
658 newfcn->order = order;
659 newfcn->h_function_pointer = fcn_ptr;
660 XmsgHookTable = newfcn;
661 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
665 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
667 struct XmsgFunctionHook *cur, *p;
669 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
670 /* This will also remove duplicates if any */
671 while (cur != NULL &&
672 fcn_ptr == cur->h_function_pointer &&
673 order == cur->order) {
674 lprintf(CTDL_INFO, "Unregistered x-msg function "
675 "(priority %d)\n", order);
677 if (cur == XmsgHookTable) {
686 void CtdlDestroyXmsgHooks(void)
688 struct XmsgFunctionHook *cur, *p;
693 lprintf(CTDL_INFO, "Destroyed x-msg function "
694 "(priority %d)\n", cur->order);
700 XmsgHookTable = NULL;
704 void CtdlRegisterServiceHook(int tcp_port,
706 void (*h_greeting_function) (void),
707 void (*h_command_function) (void),
708 void (*h_async_function) (void)
711 struct ServiceFunctionHook *newfcn;
716 newfcn = (struct ServiceFunctionHook *)
717 malloc(sizeof(struct ServiceFunctionHook));
718 message = (char*) malloc (SIZ);
720 newfcn->next = ServiceHookTable;
721 newfcn->tcp_port = tcp_port;
722 newfcn->sockpath = sockpath;
723 newfcn->h_greeting_function = h_greeting_function;
724 newfcn->h_command_function = h_command_function;
725 newfcn->h_async_function = h_async_function;
727 if (sockpath != NULL) {
728 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
729 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
731 else if (tcp_port <= 0) { /* port -1 to disable */
732 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
738 newfcn->msock = ig_tcp_server(config.c_ip_addr,
740 config.c_maxsessions,
742 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
745 if (newfcn->msock > 0) {
746 ServiceHookTable = newfcn;
747 strcat(message, "registered.");
748 lprintf(CTDL_INFO, "%s\n", message);
751 AddPortError(message, error);
752 strcat(message, "FAILED.");
753 lprintf(CTDL_CRIT, "%s\n", message);
762 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
763 void (*h_greeting_function) (void),
764 void (*h_command_function) (void),
765 void (*h_async_function) (void)
768 struct ServiceFunctionHook *cur, *p;
770 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
771 /* This will also remove duplicates if any */
772 while (cur != NULL &&
773 !(sockpath && cur->sockpath &&
774 strcmp(sockpath, cur->sockpath)) &&
775 h_greeting_function == cur->h_greeting_function &&
776 h_command_function == cur->h_command_function &&
777 h_async_function == cur->h_async_function &&
778 tcp_port == cur->tcp_port) {
781 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
783 } else if (tcp_port) {
784 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
786 lprintf(CTDL_INFO, "Unregistered unknown service\n");
789 if (cur == ServiceHookTable) {
790 ServiceHookTable = p;
798 void CtdlDestroyServiceHook(void)
800 struct ServiceFunctionHook *cur, *p;
802 cur = ServiceHookTable;
807 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
809 } else if (cur->tcp_port) {
810 lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
812 lprintf(CTDL_INFO, "Unregistered unknown service\n");
818 ServiceHookTable = NULL;
822 void PerformSessionHooks(int EventType)
824 struct SessionFunctionHook *fcn = NULL;
826 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
827 if (fcn->eventtype == EventType) {
828 (*fcn->h_function_pointer) ();
833 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
835 struct UserFunctionHook *fcn = NULL;
837 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
838 if (fcn->eventtype == EventType) {
839 (*fcn->h_function_pointer) (usbuf);
844 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
846 struct MessageFunctionHook *fcn = NULL;
847 int total_retval = 0;
849 /* Other code may elect to protect this message from server-side
850 * handlers; if this is the case, don't do anything.
851 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
852 EventType, msg->cm_flags);
854 if (msg->cm_flags & CM_SKIP_HOOKS) {
855 lprintf(CTDL_DEBUG, "Skipping hooks\n");
859 /* Otherwise, run all the hooks appropriate to this event type.
861 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
862 if (fcn->eventtype == EventType) {
863 total_retval = total_retval +
864 (*fcn->h_function_pointer) (msg);
868 /* Return the sum of the return codes from the hook functions. If
869 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
870 * the save operation to abort.
877 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
879 struct NetprocFunctionHook *fcn;
880 int total_retval = 0;
882 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
883 total_retval = total_retval +
884 (*fcn->h_function_pointer) (msg, target_room);
887 /* Return the sum of the return codes from the hook functions.
888 * A nonzero return code will cause the message to *not* be imported.
894 void PerformDeleteHooks(char *room, long msgnum)
896 struct DeleteFunctionHook *fcn;
898 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
899 (*fcn->h_function_pointer) (room, msgnum);
907 int PerformXmsgHooks(char *sender, char *recp, char *msg)
909 struct XmsgFunctionHook *fcn;
913 for (p=0; p<MAX_XMSG_PRI; ++p) {
914 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
915 if (fcn->order == p) {
917 (*fcn->h_function_pointer)
921 /* Break out of the loop if a higher-priority function
922 * successfully delivered the message. This prevents duplicate
923 * deliveries to local users simultaneously signed onto
926 if (total_sent) break;