2 * Citadel Extension Loader
3 * Originally written by Brian Costello <btx@calyx.net>
5 * Copyright (c) 1987-2021 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 #include <libcitadel.h>
21 #include "sysdep_decls.h"
22 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
23 #include "serv_extensions.h"
24 #include "ctdl_module.h"
29 * Structure defentitions for hook tables
32 typedef struct FixedOutputHook FixedOutputHook;
33 struct FixedOutputHook {
34 FixedOutputHook *next;
35 char content_type[64];
36 void (*h_function_pointer) (char *, int);
38 FixedOutputHook *FixedOutputTable = NULL;
42 * SessionFunctionHook extensions are used for any type of hook for which
43 * the context in which it's being called (which is determined by the event
44 * type) will make it obvious for the hook function to know where to look for
47 typedef struct SessionFunctionHook SessionFunctionHook;
48 struct SessionFunctionHook {
49 SessionFunctionHook *next;
51 void (*h_function_pointer) (void);
54 SessionFunctionHook *SessionHookTable = NULL;
58 * UserFunctionHook extensions are used for any type of hook which implements
59 * an operation on a user or username (potentially) other than the one
60 * operating the current session.
62 typedef struct UserFunctionHook UserFunctionHook;
63 struct UserFunctionHook {
64 UserFunctionHook *next;
65 void (*h_function_pointer) (struct ctdluser *usbuf);
68 UserFunctionHook *UserHookTable = NULL;
72 * MessageFunctionHook extensions are used for hooks which implement handlers
73 * for various types of message operations (save, read, etc.)
75 typedef struct MessageFunctionHook MessageFunctionHook;
76 struct MessageFunctionHook {
77 MessageFunctionHook *next;
78 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
81 MessageFunctionHook *MessageHookTable = NULL;
85 * DeleteFunctionHook extensions are used for hooks which get called when a
86 * message is about to be deleted.
88 typedef struct DeleteFunctionHook DeleteFunctionHook;
89 struct DeleteFunctionHook {
90 DeleteFunctionHook *next;
91 void (*h_function_pointer) (char *target_room, long msgnum);
93 DeleteFunctionHook *DeleteHookTable = NULL;
97 * ExpressMessageFunctionHook extensions are used for hooks which implement
98 * the sending of an instant message through various channels. Any function
99 * registered should return the number of recipients to whom the message was
100 * successfully transmitted.
102 typedef struct XmsgFunctionHook XmsgFunctionHook;
103 struct XmsgFunctionHook {
104 XmsgFunctionHook *next;
105 int (*h_function_pointer) (char *, char *, char *, char *);
108 XmsgFunctionHook *XmsgHookTable = NULL;
112 * RoomFunctionHook extensions are used for hooks which impliment room
113 * processing functions when new messages are added.
115 typedef struct RoomFunctionHook RoomFunctionHook;
116 struct RoomFunctionHook {
117 RoomFunctionHook *next;
118 int (*fcn_ptr) (struct ctdlroom *);
120 RoomFunctionHook *RoomHookTable = NULL;
123 typedef struct SearchFunctionHook SearchFunctionHook;
124 struct SearchFunctionHook {
125 SearchFunctionHook *next;
126 void (*fcn_ptr) (int *, long **, const char *);
129 SearchFunctionHook *SearchFunctionHookTable = NULL;
131 ServiceFunctionHook *ServiceHookTable = NULL;
133 typedef struct ProtoFunctionHook ProtoFunctionHook;
134 struct ProtoFunctionHook {
135 void (*handler) (char *cmdbuf);
140 HashList *ProtoHookList = NULL;
143 #define ERR_PORT (1 << 1)
146 static StrBuf *portlist = NULL;
147 static StrBuf *errormessages = NULL;
150 long DetailErrorFlags;
151 ConstStr Empty = {HKEY("")};
152 char *ErrSubject = "Startup Problems";
153 ConstStr ErrGeneral[] = {
154 {HKEY("Citadel had trouble on starting up. ")},
155 {HKEY(" This means, Citadel won't be the service provider for a specific service you configured it to.\n\n"
156 "If you don't want Citadel to provide these services, turn them off in WebCit via: ")},
157 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
158 "The errors returned by the system were:\n")},
159 {HKEY("You can recheck the above if you follow this faq item:\n"
160 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
163 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by Citadel Server.\n")};
164 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
165 ConstStr ErrPortHint = { HKEY("If you want Citadel to provide you with that functionality, "
166 "check the output of \"netstat -lnp\" on Linux, or \"netstat -na\" on BSD"
167 " and disable the program that binds these ports.\n")};
170 void LogPrintMessages(long err)
173 StrBuf *List, *DetailList;
174 ConstStr *Short, *Where, *Hint;
177 Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
179 DetailErrorFlags = DetailErrorFlags & ~err;
184 Short = &ErrPortShort;
185 Where = &ErrPortWhere;
188 DetailList = errormessages;
198 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
199 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
200 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
201 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
202 StrBufAppendBuf(Message, List, 0);
203 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
204 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
205 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
206 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
207 StrBufAppendBuf(Message, DetailList, 0);
208 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
209 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
211 syslog(LOG_EMERG, "extensions: %s", ChrPtr(Message));
212 syslog(LOG_EMERG, "extensions: %s", ErrSubject);
213 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
215 FreeStrBuf(&Message);
217 FreeStrBuf(&DetailList);
221 void AddPortError(char *Port, char *ErrorMessage)
225 DetailErrorFlags |= ERR_PORT;
227 len = StrLength(errormessages);
228 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
229 else errormessages = NewStrBuf();
230 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
233 len = StrLength(portlist);
234 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
235 else portlist = NewStrBuf();
236 StrBufAppendBufPlain(portlist, Port, -1, 0);
240 int DLoader_Exec_Cmd(char *cmdbuf)
243 ProtoFunctionHook *p;
245 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
246 p = (ProtoFunctionHook*) vP;
247 p->handler(&cmdbuf[5]);
254 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
256 ProtoFunctionHook *p;
258 if (ProtoHookList == NULL)
259 ProtoHookList = NewHash (1, FourHash);
262 p = (ProtoFunctionHook *)
263 malloc(sizeof(ProtoFunctionHook));
266 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
269 p->handler = handler;
273 Put(ProtoHookList, cmd, 4, p, NULL);
274 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
278 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
280 SessionFunctionHook *newfcn;
282 newfcn = (SessionFunctionHook *)
283 malloc(sizeof(SessionFunctionHook));
284 newfcn->Priority = Priority;
285 newfcn->h_function_pointer = fcn_ptr;
286 newfcn->eventtype = EventType;
288 SessionFunctionHook **pfcn;
289 pfcn = &SessionHookTable;
290 while ((*pfcn != NULL) &&
291 ((*pfcn)->Priority < newfcn->Priority) &&
292 ((*pfcn)->next != NULL))
293 pfcn = &(*pfcn)->next;
295 newfcn->next = *pfcn;
298 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
302 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
304 SessionFunctionHook *cur, *p, *last;
306 cur = SessionHookTable;
307 while (cur != NULL) {
308 if ((fcn_ptr == cur->h_function_pointer) &&
309 (EventType == cur->eventtype))
311 syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
320 SessionHookTable = p;
331 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
334 UserFunctionHook *newfcn;
336 newfcn = (UserFunctionHook *)
337 malloc(sizeof(UserFunctionHook));
338 newfcn->next = UserHookTable;
339 newfcn->h_function_pointer = fcn_ptr;
340 newfcn->eventtype = EventType;
341 UserHookTable = newfcn;
343 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
348 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
350 UserFunctionHook *cur, *p, *last;
353 while (cur != NULL) {
354 if ((fcn_ptr == cur->h_function_pointer) &&
355 (EventType == cur->eventtype))
357 syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
377 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *), int EventType)
380 MessageFunctionHook *newfcn;
382 newfcn = (MessageFunctionHook *)
383 malloc(sizeof(MessageFunctionHook));
384 newfcn->next = MessageHookTable;
385 newfcn->h_function_pointer = handler;
386 newfcn->eventtype = EventType;
387 MessageHookTable = newfcn;
389 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
393 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *), int EventType)
395 MessageFunctionHook *cur, *p, *last;
397 cur = MessageHookTable;
398 while (cur != NULL) {
399 if ((handler == cur->h_function_pointer) &&
400 (EventType == cur->eventtype))
402 syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
410 MessageHookTable = p;
421 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
423 RoomFunctionHook *newfcn;
425 newfcn = (RoomFunctionHook *)
426 malloc(sizeof(RoomFunctionHook));
427 newfcn->next = RoomHookTable;
428 newfcn->fcn_ptr = fcn_ptr;
429 RoomHookTable = newfcn;
431 syslog(LOG_DEBUG, "extensions: registered a new room function");
435 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
437 RoomFunctionHook *cur, *p, *last;
442 if (fcn_ptr == cur->fcn_ptr) {
443 syslog(LOG_DEBUG, "extensions: unregistered room function");
463 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
465 DeleteFunctionHook *newfcn;
467 newfcn = (DeleteFunctionHook *)
468 malloc(sizeof(DeleteFunctionHook));
469 newfcn->next = DeleteHookTable;
470 newfcn->h_function_pointer = handler;
471 DeleteHookTable = newfcn;
473 syslog(LOG_DEBUG, "extensions: registered a new delete function");
477 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
479 DeleteFunctionHook *cur, *p, *last;
482 cur = DeleteHookTable;
483 while (cur != NULL) {
484 if (handler == cur->h_function_pointer )
486 syslog(LOG_DEBUG, "extensions: unregistered delete function");
505 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
507 FixedOutputHook *newfcn;
509 newfcn = (FixedOutputHook *)
510 malloc(sizeof(FixedOutputHook));
511 newfcn->next = FixedOutputTable;
512 newfcn->h_function_pointer = handler;
513 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
514 FixedOutputTable = newfcn;
516 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
520 void CtdlUnregisterFixedOutputHook(char *content_type)
522 FixedOutputHook *cur, *p, *last;
525 cur = FixedOutputTable;
526 while (cur != NULL) {
527 /* This will also remove duplicates if any */
528 if (!strcasecmp(content_type, cur->content_type)) {
529 syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
536 FixedOutputTable = p;
549 /* returns nonzero if we found a hook and used it */
550 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
552 FixedOutputHook *fcn;
554 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
555 if (!strcasecmp(content_type, fcn->content_type)) {
556 (*fcn->h_function_pointer) (content, content_length);
564 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
567 XmsgFunctionHook *newfcn;
569 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
570 newfcn->next = XmsgHookTable;
571 newfcn->order = order;
572 newfcn->h_function_pointer = fcn_ptr;
573 XmsgHookTable = newfcn;
574 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
578 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
580 XmsgFunctionHook *cur, *p, *last;
584 while (cur != NULL) {
585 /* This will also remove duplicates if any */
586 if (fcn_ptr == cur->h_function_pointer &&
587 order == cur->order) {
588 syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
608 void CtdlRegisterServiceHook(int tcp_port,
610 void (*h_greeting_function) (void),
611 void (*h_command_function) (void),
612 void (*h_async_function) (void),
613 const char *ServiceName)
615 ServiceFunctionHook *newfcn;
618 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
619 message = (char*) malloc (SIZ + SIZ);
621 newfcn->next = ServiceHookTable;
622 newfcn->tcp_port = tcp_port;
623 newfcn->sockpath = sockpath;
624 newfcn->h_greeting_function = h_greeting_function;
625 newfcn->h_command_function = h_command_function;
626 newfcn->h_async_function = h_async_function;
627 newfcn->ServiceName = ServiceName;
629 if (sockpath != NULL) {
630 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
631 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
633 else if (tcp_port <= 0) { /* port -1 to disable */
634 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
640 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"), tcp_port, CtdlGetConfigInt("c_maxsessions"));
641 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
642 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
645 if (newfcn->msock > 0) {
646 ServiceHookTable = newfcn;
647 strcat(message, "registered.");
648 syslog(LOG_INFO, "%s", message);
651 AddPortError(message, "failed");
652 strcat(message, "FAILED.");
653 syslog(LOG_ERR, "%s", message);
660 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
661 void (*h_greeting_function) (void),
662 void (*h_command_function) (void),
663 void (*h_async_function) (void)
666 ServiceFunctionHook *cur, *p, *last;
669 cur = ServiceHookTable;
670 while (cur != NULL) {
671 /* This will also remove duplicates if any */
672 if (h_greeting_function == cur->h_greeting_function &&
673 h_command_function == cur->h_command_function &&
674 h_async_function == cur->h_async_function &&
675 tcp_port == cur->tcp_port &&
676 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
681 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
683 } else if (tcp_port) {
684 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
686 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
693 ServiceHookTable = p;
704 void CtdlShutdownServiceHooks(void)
706 /* sort of a duplicate of close_masters() but called earlier */
707 ServiceFunctionHook *cur;
709 cur = ServiceHookTable;
712 if (cur->msock != -1)
716 if (cur->sockpath != NULL){
717 syslog(LOG_INFO, "extensions: [%s] Closed UNIX domain socket %s", cur->ServiceName, cur->sockpath);
718 unlink(cur->sockpath);
720 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
728 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
730 SearchFunctionHook *newfcn;
732 if (!name || !fcn_ptr) {
736 newfcn = (SearchFunctionHook *)
737 malloc(sizeof(SearchFunctionHook));
738 newfcn->next = SearchFunctionHookTable;
740 newfcn->fcn_ptr = fcn_ptr;
741 SearchFunctionHookTable = newfcn;
743 syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
746 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
748 SearchFunctionHook *cur, *p, *last;
751 cur = SearchFunctionHookTable;
752 while (cur != NULL) {
754 (cur->fcn_ptr == fcn_ptr) &&
755 name && !strcmp(name, cur->name))
757 syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
763 SearchFunctionHookTable = p;
774 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
776 SearchFunctionHook *fcn = NULL;
778 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
779 if (!func_name || !strcmp(func_name, fcn->name)) {
780 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
788 void PerformSessionHooks(int EventType)
790 SessionFunctionHook *fcn = NULL;
792 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
793 if (fcn->eventtype == EventType) {
794 if (EventType == EVT_TIMER) {
795 pthread_setspecific(MyConKey, NULL); /* for every hook */
797 (*fcn->h_function_pointer) ();
802 void PerformUserHooks(ctdluser *usbuf, int EventType)
804 UserFunctionHook *fcn = NULL;
806 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
807 if (fcn->eventtype == EventType) {
808 (*fcn->h_function_pointer) (usbuf);
813 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
815 MessageFunctionHook *fcn = NULL;
816 int total_retval = 0;
818 /* Other code may elect to protect this message from server-side
819 * handlers; if this is the case, don't do anything.
821 if (msg->cm_flags & CM_SKIP_HOOKS) {
825 /* Otherwise, run all the hooks appropriate to this event type.
827 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
828 if (fcn->eventtype == EventType) {
829 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
833 /* Return the sum of the return codes from the hook functions. If
834 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
835 * the save operation to abort.
841 int PerformRoomHooks(struct ctdlroom *target_room)
843 RoomFunctionHook *fcn;
844 int total_retval = 0;
846 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
848 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
849 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
852 /* Return the sum of the return codes from the hook functions.
858 void PerformDeleteHooks(char *room, long msgnum)
860 DeleteFunctionHook *fcn;
862 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
863 (*fcn->h_function_pointer) (room, msgnum);
868 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
870 XmsgFunctionHook *fcn;
874 for (p=0; p<MAX_XMSG_PRI; ++p) {
875 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
876 if (fcn->order == p) {
878 (*fcn->h_function_pointer)
879 (sender, sender_email, recp, msg);
882 /* Break out of the loop if a higher-priority function
883 * successfully delivered the message. This prevents duplicate
884 * deliveries to local users simultaneously signed onto
887 if (total_sent) break;
894 * Dirty hack until we impliment a hook mechanism for this
896 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
899 CtdlStartTLS (ok_response, nosup_response, error_response);
904 CTDL_MODULE_INIT(modules)