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, struct 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) {
172 StrBuf *List, *DetailList;
173 ConstStr *Short, *Where, *Hint;
176 Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
178 DetailErrorFlags = DetailErrorFlags & ~err;
183 Short = &ErrPortShort;
184 Where = &ErrPortWhere;
187 DetailList = errormessages;
197 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
198 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
199 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
200 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
201 StrBufAppendBuf(Message, List, 0);
202 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
203 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
204 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
205 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
206 StrBufAppendBuf(Message, DetailList, 0);
207 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
208 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
210 syslog(LOG_ERR, "extensions: %s", ChrPtr(Message));
211 syslog(LOG_ERR, "extensions: %s", ErrSubject);
212 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
214 FreeStrBuf(&Message);
216 FreeStrBuf(&DetailList);
220 void AddPortError(char *Port, char *ErrorMessage) {
223 DetailErrorFlags |= ERR_PORT;
225 len = StrLength(errormessages);
226 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
227 else errormessages = NewStrBuf();
228 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
231 len = StrLength(portlist);
232 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
233 else portlist = NewStrBuf();
234 StrBufAppendBufPlain(portlist, Port, -1, 0);
238 int DLoader_Exec_Cmd(char *cmdbuf) {
240 ProtoFunctionHook *p;
242 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
243 p = (ProtoFunctionHook*) vP;
244 p->handler(&cmdbuf[5]);
251 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc) {
252 ProtoFunctionHook *p;
254 if (ProtoHookList == NULL)
255 ProtoHookList = NewHash (1, FourHash);
258 p = (ProtoFunctionHook *)
259 malloc(sizeof(ProtoFunctionHook));
262 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
265 p->handler = handler;
269 Put(ProtoHookList, cmd, 4, p, NULL);
270 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
274 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
276 SessionFunctionHook *newfcn;
278 newfcn = (SessionFunctionHook *)
279 malloc(sizeof(SessionFunctionHook));
280 newfcn->Priority = Priority;
281 newfcn->h_function_pointer = fcn_ptr;
282 newfcn->eventtype = EventType;
284 SessionFunctionHook **pfcn;
285 pfcn = &SessionHookTable;
286 while ((*pfcn != NULL) &&
287 ((*pfcn)->Priority < newfcn->Priority) &&
288 ((*pfcn)->next != NULL))
289 pfcn = &(*pfcn)->next;
291 newfcn->next = *pfcn;
294 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
298 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
300 SessionFunctionHook *cur, *p, *last;
302 cur = SessionHookTable;
303 while (cur != NULL) {
304 if ((fcn_ptr == cur->h_function_pointer) &&
305 (EventType == cur->eventtype))
307 syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
316 SessionHookTable = p;
327 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
330 UserFunctionHook *newfcn;
332 newfcn = (UserFunctionHook *)
333 malloc(sizeof(UserFunctionHook));
334 newfcn->next = UserHookTable;
335 newfcn->h_function_pointer = fcn_ptr;
336 newfcn->eventtype = EventType;
337 UserHookTable = newfcn;
339 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
344 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
346 UserFunctionHook *cur, *p, *last;
349 while (cur != NULL) {
350 if ((fcn_ptr == cur->h_function_pointer) &&
351 (EventType == cur->eventtype))
353 syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
373 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
376 MessageFunctionHook *newfcn;
378 newfcn = (MessageFunctionHook *)
379 malloc(sizeof(MessageFunctionHook));
380 newfcn->next = MessageHookTable;
381 newfcn->h_function_pointer = handler;
382 newfcn->eventtype = EventType;
383 MessageHookTable = newfcn;
385 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
389 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
391 MessageFunctionHook *cur, *p, *last;
393 cur = MessageHookTable;
394 while (cur != NULL) {
395 if ((handler == cur->h_function_pointer) &&
396 (EventType == cur->eventtype))
398 syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
406 MessageHookTable = p;
417 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
419 RoomFunctionHook *newfcn;
421 newfcn = (RoomFunctionHook *)
422 malloc(sizeof(RoomFunctionHook));
423 newfcn->next = RoomHookTable;
424 newfcn->fcn_ptr = fcn_ptr;
425 RoomHookTable = newfcn;
427 syslog(LOG_DEBUG, "extensions: registered a new room function");
431 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
433 RoomFunctionHook *cur, *p, *last;
438 if (fcn_ptr == cur->fcn_ptr) {
439 syslog(LOG_DEBUG, "extensions: unregistered room function");
459 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
461 DeleteFunctionHook *newfcn;
463 newfcn = (DeleteFunctionHook *)
464 malloc(sizeof(DeleteFunctionHook));
465 newfcn->next = DeleteHookTable;
466 newfcn->h_function_pointer = handler;
467 DeleteHookTable = newfcn;
469 syslog(LOG_DEBUG, "extensions: registered a new delete function");
473 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
475 DeleteFunctionHook *cur, *p, *last;
478 cur = DeleteHookTable;
479 while (cur != NULL) {
480 if (handler == cur->h_function_pointer )
482 syslog(LOG_DEBUG, "extensions: unregistered delete function");
501 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
503 FixedOutputHook *newfcn;
505 newfcn = (FixedOutputHook *)
506 malloc(sizeof(FixedOutputHook));
507 newfcn->next = FixedOutputTable;
508 newfcn->h_function_pointer = handler;
509 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
510 FixedOutputTable = newfcn;
512 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
516 void CtdlUnregisterFixedOutputHook(char *content_type)
518 FixedOutputHook *cur, *p, *last;
521 cur = FixedOutputTable;
522 while (cur != NULL) {
523 /* This will also remove duplicates if any */
524 if (!strcasecmp(content_type, cur->content_type)) {
525 syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
532 FixedOutputTable = p;
545 /* returns nonzero if we found a hook and used it */
546 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
548 FixedOutputHook *fcn;
550 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
551 if (!strcasecmp(content_type, fcn->content_type)) {
552 (*fcn->h_function_pointer) (content, content_length);
560 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
563 XmsgFunctionHook *newfcn;
565 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
566 newfcn->next = XmsgHookTable;
567 newfcn->order = order;
568 newfcn->h_function_pointer = fcn_ptr;
569 XmsgHookTable = newfcn;
570 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
574 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
576 XmsgFunctionHook *cur, *p, *last;
580 while (cur != NULL) {
581 /* This will also remove duplicates if any */
582 if (fcn_ptr == cur->h_function_pointer &&
583 order == cur->order) {
584 syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
604 void CtdlRegisterServiceHook(int tcp_port,
606 void (*h_greeting_function) (void),
607 void (*h_command_function) (void),
608 void (*h_async_function) (void),
609 const char *ServiceName)
611 ServiceFunctionHook *newfcn;
614 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
615 message = (char*) malloc (SIZ + SIZ);
617 newfcn->next = ServiceHookTable;
618 newfcn->tcp_port = tcp_port;
619 newfcn->sockpath = sockpath;
620 newfcn->h_greeting_function = h_greeting_function;
621 newfcn->h_command_function = h_command_function;
622 newfcn->h_async_function = h_async_function;
623 newfcn->ServiceName = ServiceName;
625 if (sockpath != NULL) {
626 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
627 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
629 else if (tcp_port <= 0) { /* port -1 to disable */
630 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
636 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"), tcp_port, CtdlGetConfigInt("c_maxsessions"));
637 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
638 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
641 if (newfcn->msock > 0) {
642 ServiceHookTable = newfcn;
643 strcat(message, "registered.");
644 syslog(LOG_INFO, "%s", message);
647 AddPortError(message, "failed");
648 strcat(message, "FAILED.");
649 syslog(LOG_ERR, "%s", message);
656 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
657 void (*h_greeting_function) (void),
658 void (*h_command_function) (void),
659 void (*h_async_function) (void)
662 ServiceFunctionHook *cur, *p, *last;
665 cur = ServiceHookTable;
666 while (cur != NULL) {
667 /* This will also remove duplicates if any */
668 if (h_greeting_function == cur->h_greeting_function &&
669 h_command_function == cur->h_command_function &&
670 h_async_function == cur->h_async_function &&
671 tcp_port == cur->tcp_port &&
672 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
677 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
679 } else if (tcp_port) {
680 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
682 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
689 ServiceHookTable = p;
700 // During shutdown we can close all of the listening sockets.
701 void CtdlShutdownServiceHooks(void) {
702 ServiceFunctionHook *cur;
704 cur = ServiceHookTable;
705 while (cur != NULL) {
706 if (cur->msock != -1) {
709 if (cur->sockpath != NULL){
710 syslog(LOG_INFO, "extensions: [%s] closed unix domain socket %s", cur->ServiceName, cur->sockpath);
711 unlink(cur->sockpath);
713 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
721 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name) {
722 SearchFunctionHook *newfcn;
724 if (!name || !fcn_ptr) {
728 newfcn = (SearchFunctionHook *)
729 malloc(sizeof(SearchFunctionHook));
730 newfcn->next = SearchFunctionHookTable;
732 newfcn->fcn_ptr = fcn_ptr;
733 SearchFunctionHookTable = newfcn;
735 syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
739 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name) {
740 SearchFunctionHook *cur, *p, *last;
743 cur = SearchFunctionHookTable;
744 while (cur != NULL) {
746 (cur->fcn_ptr == fcn_ptr) &&
747 name && !strcmp(name, cur->name))
749 syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
755 SearchFunctionHookTable = p;
766 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name) {
767 SearchFunctionHook *fcn = NULL;
769 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
770 if (!func_name || !strcmp(func_name, fcn->name)) {
771 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
779 void PerformSessionHooks(int EventType) {
780 SessionFunctionHook *fcn = NULL;
782 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
783 if (fcn->eventtype == EventType) {
784 if (EventType == EVT_TIMER) {
785 pthread_setspecific(MyConKey, NULL); /* for every hook */
787 (*fcn->h_function_pointer) ();
793 void PerformUserHooks(ctdluser *usbuf, int EventType) {
794 UserFunctionHook *fcn = NULL;
796 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
797 if (fcn->eventtype == EventType) {
798 (*fcn->h_function_pointer) (usbuf);
804 int PerformMessageHooks(struct CtdlMessage *msg, struct recptypes *recps, int EventType) {
805 MessageFunctionHook *fcn = NULL;
806 int total_retval = 0;
808 /* Other code may elect to protect this message from server-side
809 * handlers; if this is the case, don't do anything.
811 if (msg->cm_flags & CM_SKIP_HOOKS) {
815 /* Otherwise, run all the hooks appropriate to this event type.
817 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
818 if (fcn->eventtype == EventType) {
819 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
823 /* Return the sum of the return codes from the hook functions. If
824 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
825 * the save operation to abort.
831 int PerformRoomHooks(struct ctdlroom *target_room) {
832 RoomFunctionHook *fcn;
833 int total_retval = 0;
835 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
837 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
838 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
841 /* Return the sum of the return codes from the hook functions.
847 void PerformDeleteHooks(char *room, long msgnum) {
848 DeleteFunctionHook *fcn;
850 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
851 (*fcn->h_function_pointer) (room, msgnum);
856 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg) {
857 XmsgFunctionHook *fcn;
861 for (p=0; p<MAX_XMSG_PRI; ++p) {
862 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
863 if (fcn->order == p) {
865 (*fcn->h_function_pointer)
866 (sender, sender_email, recp, msg);
869 /* Break out of the loop if a higher-priority function
870 * successfully delivered the message. This prevents duplicate
871 * deliveries to local users simultaneously signed onto
874 if (total_sent) break;
881 * "Start TLS" function that is (hopefully) adaptable for any protocol
883 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response) {
885 CtdlStartTLS (ok_response, nosup_response, error_response);
890 CTDL_MODULE_INIT(modules) {