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);
106 if (errormessages!=NULL) free (errormessages);
107 errormessages = NULL;
108 if (portlist!=NULL) free (portlist);
115 void AppendString(char **target, char *append, size_t *len, size_t rate)
121 AddLen = strlen(append);
127 *target = (char*)malloc (*len * SIZ);
131 oLen = strlen(*target);
132 RelPtr = strlen(*target);
133 if (oLen + AddLen + 2 > *len * SIZ)
135 char *Buff = *target;
136 size_t NewSiz = *len + 10;
137 *target = malloc (NewSiz * SIZ);
138 memcpy (*target, Buff, NewSiz * SIZ);
142 memcpy (*target + oLen, append, AddLen);
143 (*target)[oLen + AddLen + 1] = '\n';
144 (*target)[oLen + AddLen + 2] = '\0';
147 void AddPortError(char *Port, char *ErrorMessage)
152 DetailErrorFlags |= ERR_PORT;
154 AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
155 AppendString(&portlist, Port, &nSizPort, 2);
157 pos = strchr (portlist, ':');
158 if (pos != NULL) *pos = ';';
160 len = strlen (errormessages);
161 if (nSizErrmsg * SIZ > len + 3)
163 errormessages[len] = ';';
164 errormessages[len+1] = ' ';
165 errormessages[len+2] = '\0';
170 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
172 struct ProtoFunctionHook *p;
174 p = (struct ProtoFunctionHook *)
175 malloc(sizeof(struct ProtoFunctionHook));
178 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
181 p->handler = handler;
184 p->next = ProtoHookList;
186 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
190 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
192 struct ProtoFunctionHook *cur = NULL;
193 struct ProtoFunctionHook *p = NULL;
194 struct ProtoFunctionHook *lastcur = NULL;
196 for (cur = ProtoHookList;
198 cur = (cur != NULL)? cur->next: NULL) {
199 /* This will also remove duplicates if any */
200 while (cur != NULL &&
201 handler == cur->handler &&
202 !strcmp(cmd, cur->cmd)) {
203 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
206 if (cur == ProtoHookList) {
209 else if (lastcur != NULL)
220 void CtdlDestroyProtoHooks(void)
222 struct ProtoFunctionHook *cur, *p;
227 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
228 cur->cmd, cur->desc);
233 ProtoHookList = NULL;
237 int DLoader_Exec_Cmd(char *cmdbuf)
239 struct ProtoFunctionHook *p;
241 for (p = ProtoHookList; p; p = p->next) {
242 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
243 p->handler(&cmdbuf[5]);
250 void initialize_server_extensions(void)
256 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
257 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
258 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
259 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
260 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
261 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
262 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
263 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
264 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
265 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
266 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
267 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
268 lprintf(CTDL_INFO, "%s\n", serv_network_init());
269 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
270 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
271 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
272 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
273 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
274 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
275 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
276 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
277 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
278 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
279 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
280 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
281 lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
282 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
283 lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
284 for (filter = 1; filter != 0; filter = filter << 1)
285 if ((filter & DetailErrorFlags) != 0)
286 LogPrintMessages(filter);
291 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
294 struct CleanupFunctionHook *newfcn;
296 newfcn = (struct CleanupFunctionHook *)
297 malloc(sizeof(struct CleanupFunctionHook));
298 newfcn->next = CleanupHookTable;
299 newfcn->h_function_pointer = fcn_ptr;
300 CleanupHookTable = newfcn;
302 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
306 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
308 struct CleanupFunctionHook *cur, *p;
310 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
311 /* This will also remove duplicates if any */
312 while (cur != NULL &&
313 fcn_ptr == cur->h_function_pointer) {
314 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
316 if (cur == CleanupHookTable) {
317 CleanupHookTable = p;
325 void CtdlDestroyCleanupHooks(void)
327 struct CleanupFunctionHook *cur, *p;
329 cur = CleanupHookTable;
332 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
337 CleanupHookTable = NULL;
341 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
344 struct SessionFunctionHook *newfcn;
346 newfcn = (struct SessionFunctionHook *)
347 malloc(sizeof(struct SessionFunctionHook));
348 newfcn->next = SessionHookTable;
349 newfcn->h_function_pointer = fcn_ptr;
350 newfcn->eventtype = EventType;
351 SessionHookTable = newfcn;
353 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
358 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
360 struct SessionFunctionHook *cur, *p;
362 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
363 /* This will also remove duplicates if any */
364 while (cur != NULL &&
365 fcn_ptr == cur->h_function_pointer &&
366 EventType == cur->eventtype) {
367 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
370 if (cur == SessionHookTable) {
371 SessionHookTable = p;
379 void CtdlDestroySessionHooks(void)
381 struct SessionFunctionHook *cur, *p;
383 cur = SessionHookTable;
386 lprintf(CTDL_INFO, "Destroyed session function\n");
391 SessionHookTable = NULL;
395 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
398 struct UserFunctionHook *newfcn;
400 newfcn = (struct UserFunctionHook *)
401 malloc(sizeof(struct UserFunctionHook));
402 newfcn->next = UserHookTable;
403 newfcn->h_function_pointer = fcn_ptr;
404 newfcn->eventtype = EventType;
405 UserHookTable = newfcn;
407 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
412 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
414 struct UserFunctionHook *cur, *p;
416 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
417 /* This will also remove duplicates if any */
418 while (cur != NULL &&
419 fcn_ptr == cur->h_function_pointer &&
420 EventType == cur->eventtype) {
421 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
424 if (cur == UserHookTable) {
433 void CtdlDestroyUserHooks(void)
435 struct UserFunctionHook *cur, *p;
440 lprintf(CTDL_INFO, "Destroyed user function \n");
445 UserHookTable = NULL;
449 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
453 struct MessageFunctionHook *newfcn;
455 newfcn = (struct MessageFunctionHook *)
456 malloc(sizeof(struct MessageFunctionHook));
457 newfcn->next = MessageHookTable;
458 newfcn->h_function_pointer = handler;
459 newfcn->eventtype = EventType;
460 MessageHookTable = newfcn;
462 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
467 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
470 struct MessageFunctionHook *cur, *p;
472 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
473 /* This will also remove duplicates if any */
474 while (cur != NULL &&
475 handler == cur->h_function_pointer &&
476 EventType == cur->eventtype) {
477 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
480 if (cur == MessageHookTable) {
481 MessageHookTable = p;
489 void CtdlDestroyMessageHook(void)
491 struct MessageFunctionHook *cur, *p;
493 cur = MessageHookTable;
496 lprintf(CTDL_INFO, "Destroyed message function \n");
501 MessageHookTable = NULL;
505 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
507 struct NetprocFunctionHook *newfcn;
509 newfcn = (struct NetprocFunctionHook *)
510 malloc(sizeof(struct NetprocFunctionHook));
511 newfcn->next = NetprocHookTable;
512 newfcn->h_function_pointer = handler;
513 NetprocHookTable = newfcn;
515 lprintf(CTDL_INFO, "Registered a new netproc function\n");
519 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
521 struct NetprocFunctionHook *cur, *p;
523 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
524 /* This will also remove duplicates if any */
525 while (cur != NULL &&
526 handler == cur->h_function_pointer ) {
527 lprintf(CTDL_INFO, "Unregistered netproc function\n");
529 if (cur == NetprocHookTable) {
530 NetprocHookTable = p;
538 void CtdlDestroyNetprocHooks(void)
540 struct NetprocFunctionHook *cur, *p;
542 cur = NetprocHookTable;
545 lprintf(CTDL_INFO, "Unregistered netproc function\n");
550 NetprocHookTable = NULL;
554 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
556 struct DeleteFunctionHook *newfcn;
558 newfcn = (struct DeleteFunctionHook *)
559 malloc(sizeof(struct DeleteFunctionHook));
560 newfcn->next = DeleteHookTable;
561 newfcn->h_function_pointer = handler;
562 DeleteHookTable = newfcn;
564 lprintf(CTDL_INFO, "Registered a new netproc function\n");
568 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
570 struct DeleteFunctionHook *cur, *p;
572 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
573 /* This will also remove duplicates if any */
574 while (cur != NULL &&
575 handler == cur->h_function_pointer ) {
576 lprintf(CTDL_INFO, "Unregistered netproc function\n");
578 if (cur == DeleteHookTable) {
586 void CtdlDestroyDeleteHooks(void)
588 struct DeleteFunctionHook *cur, *p;
590 cur = DeleteHookTable;
593 lprintf(CTDL_INFO, "Destroyed netproc function\n");
598 DeleteHookTable = NULL;
604 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
606 struct FixedOutputHook *newfcn;
608 newfcn = (struct FixedOutputHook *)
609 malloc(sizeof(struct FixedOutputHook));
610 newfcn->next = FixedOutputTable;
611 newfcn->h_function_pointer = handler;
612 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
613 FixedOutputTable = newfcn;
615 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
619 void CtdlUnregisterFixedOutputHook(char *content_type)
621 struct FixedOutputHook *cur, *p;
623 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
624 /* This will also remove duplicates if any */
625 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
626 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
628 if (cur == FixedOutputTable) {
629 FixedOutputTable = p;
637 void CtdlDestroyFixedOutputHooks(void)
639 struct FixedOutputHook *cur, *p;
641 cur = FixedOutputTable;
644 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
650 FixedOutputTable = NULL;
653 /* returns nonzero if we found a hook and used it */
654 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
656 struct FixedOutputHook *fcn;
658 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
659 if (!strcasecmp(content_type, fcn->content_type)) {
660 (*fcn->h_function_pointer) (content, content_length);
671 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
674 struct XmsgFunctionHook *newfcn;
676 newfcn = (struct XmsgFunctionHook *)
677 malloc(sizeof(struct XmsgFunctionHook));
678 newfcn->next = XmsgHookTable;
679 newfcn->order = order;
680 newfcn->h_function_pointer = fcn_ptr;
681 XmsgHookTable = newfcn;
682 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
686 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
688 struct XmsgFunctionHook *cur, *p;
690 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
691 /* This will also remove duplicates if any */
692 while (cur != NULL &&
693 fcn_ptr == cur->h_function_pointer &&
694 order == cur->order) {
695 lprintf(CTDL_INFO, "Unregistered x-msg function "
696 "(priority %d)\n", order);
698 if (cur == XmsgHookTable) {
707 void CtdlDestroyXmsgHooks(void)
709 struct XmsgFunctionHook *cur, *p;
714 lprintf(CTDL_INFO, "Destroyed x-msg function "
715 "(priority %d)\n", cur->order);
721 XmsgHookTable = NULL;
725 void CtdlRegisterServiceHook(int tcp_port,
727 void (*h_greeting_function) (void),
728 void (*h_command_function) (void),
729 void (*h_async_function) (void)
732 struct ServiceFunctionHook *newfcn;
737 newfcn = (struct ServiceFunctionHook *)
738 malloc(sizeof(struct ServiceFunctionHook));
739 message = (char*) malloc (SIZ);
741 newfcn->next = ServiceHookTable;
742 newfcn->tcp_port = tcp_port;
743 newfcn->sockpath = sockpath;
744 newfcn->h_greeting_function = h_greeting_function;
745 newfcn->h_command_function = h_command_function;
746 newfcn->h_async_function = h_async_function;
748 if (sockpath != NULL) {
749 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
750 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
752 else if (tcp_port <= 0) { /* port -1 to disable */
753 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
759 newfcn->msock = ig_tcp_server(config.c_ip_addr,
761 config.c_maxsessions,
763 snprintf(message, SIZ, "TCP port %s:%d: ",
764 config.c_ip_addr, tcp_port);
767 if (newfcn->msock > 0) {
768 ServiceHookTable = newfcn;
769 strcat(message, "registered.");
770 lprintf(CTDL_INFO, "%s\n", message);
773 AddPortError(message, error);
774 strcat(message, "FAILED.");
775 lprintf(CTDL_CRIT, "%s\n", message);
783 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
784 void (*h_greeting_function) (void),
785 void (*h_command_function) (void),
786 void (*h_async_function) (void)
789 struct ServiceFunctionHook *cur, *p;
791 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
792 /* This will also remove duplicates if any */
793 while (cur != NULL &&
794 !(sockpath && cur->sockpath &&
795 strcmp(sockpath, cur->sockpath)) &&
796 h_greeting_function == cur->h_greeting_function &&
797 h_command_function == cur->h_command_function &&
798 h_async_function == cur->h_async_function &&
799 tcp_port == cur->tcp_port) {
802 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
804 } else if (tcp_port) {
805 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
807 lprintf(CTDL_INFO, "Unregistered unknown service\n");
810 if (cur == ServiceHookTable) {
811 ServiceHookTable = p;
819 void CtdlDestroyServiceHook(void)
821 struct ServiceFunctionHook *cur, *p;
823 cur = ServiceHookTable;
828 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
830 } else if (cur->tcp_port) {
831 lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
833 lprintf(CTDL_INFO, "Unregistered unknown service\n");
839 ServiceHookTable = NULL;
843 void PerformSessionHooks(int EventType)
845 struct SessionFunctionHook *fcn = NULL;
847 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
848 if (fcn->eventtype == EventType) {
849 (*fcn->h_function_pointer) ();
854 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
856 struct UserFunctionHook *fcn = NULL;
858 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
859 if (fcn->eventtype == EventType) {
860 (*fcn->h_function_pointer) (usbuf);
865 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
867 struct MessageFunctionHook *fcn = NULL;
868 int total_retval = 0;
870 /* Other code may elect to protect this message from server-side
871 * handlers; if this is the case, don't do anything.
872 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
873 EventType, msg->cm_flags);
875 if (msg->cm_flags & CM_SKIP_HOOKS) {
876 lprintf(CTDL_DEBUG, "Skipping hooks\n");
880 /* Otherwise, run all the hooks appropriate to this event type.
882 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
883 if (fcn->eventtype == EventType) {
884 total_retval = total_retval +
885 (*fcn->h_function_pointer) (msg);
889 /* Return the sum of the return codes from the hook functions. If
890 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
891 * the save operation to abort.
898 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
900 struct NetprocFunctionHook *fcn;
901 int total_retval = 0;
903 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
904 total_retval = total_retval +
905 (*fcn->h_function_pointer) (msg, target_room);
908 /* Return the sum of the return codes from the hook functions.
909 * A nonzero return code will cause the message to *not* be imported.
915 void PerformDeleteHooks(char *room, long msgnum)
917 struct DeleteFunctionHook *fcn;
919 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
920 (*fcn->h_function_pointer) (room, msgnum);
928 int PerformXmsgHooks(char *sender, char *recp, char *msg)
930 struct XmsgFunctionHook *fcn;
934 for (p=0; p<MAX_XMSG_PRI; ++p) {
935 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
936 if (fcn->order == p) {
938 (*fcn->h_function_pointer)
942 /* Break out of the loop if a higher-priority function
943 * successfully delivered the message. This prevents duplicate
944 * deliveries to local users simultaneously signed onto
947 if (total_sent) break;