1 // This began as a framework written by Brian Costello (btx) that loaded server extensions as dynamic modules.
2 // We don't do it that way anymore but the concept lives on as a high degree of modularity in the server.
3 // The functions in this file handle registration and execution of the server hooks used by static linked modules.
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.
14 #include <libcitadel.h>
15 #include "sysdep_decls.h"
16 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
17 #include "serv_extensions.h"
18 #include "ctdl_module.h"
23 * Structure defentitions for hook tables
26 typedef struct FixedOutputHook FixedOutputHook;
27 struct FixedOutputHook {
28 FixedOutputHook *next;
29 char content_type[64];
30 void (*h_function_pointer) (char *, int);
32 FixedOutputHook *FixedOutputTable = NULL;
36 * SessionFunctionHook extensions are used for any type of hook for which
37 * the context in which it's being called (which is determined by the event
38 * type) will make it obvious for the hook function to know where to look for
41 typedef struct SessionFunctionHook SessionFunctionHook;
42 struct SessionFunctionHook {
43 SessionFunctionHook *next;
45 void (*h_function_pointer) (void);
48 SessionFunctionHook *SessionHookTable = NULL;
52 * UserFunctionHook extensions are used for any type of hook which implements
53 * an operation on a user or username (potentially) other than the one
54 * operating the current session.
56 typedef struct UserFunctionHook UserFunctionHook;
57 struct UserFunctionHook {
58 UserFunctionHook *next;
59 void (*h_function_pointer) (struct ctdluser *usbuf);
62 UserFunctionHook *UserHookTable = NULL;
66 * MessageFunctionHook extensions are used for hooks which implement handlers
67 * for various types of message operations (save, read, etc.)
69 typedef struct MessageFunctionHook MessageFunctionHook;
70 struct MessageFunctionHook {
71 MessageFunctionHook *next;
72 int (*h_function_pointer) (struct CtdlMessage *msg, struct recptypes *recps);
75 MessageFunctionHook *MessageHookTable = NULL;
79 * DeleteFunctionHook extensions are used for hooks which get called when a
80 * message is about to be deleted.
82 typedef struct DeleteFunctionHook DeleteFunctionHook;
83 struct DeleteFunctionHook {
84 DeleteFunctionHook *next;
85 void (*h_function_pointer) (char *target_room, long msgnum);
87 DeleteFunctionHook *DeleteHookTable = NULL;
91 * ExpressMessageFunctionHook extensions are used for hooks which implement
92 * the sending of an instant message through various channels. Any function
93 * registered should return the number of recipients to whom the message was
94 * successfully transmitted.
96 typedef struct XmsgFunctionHook XmsgFunctionHook;
97 struct XmsgFunctionHook {
98 XmsgFunctionHook *next;
99 int (*h_function_pointer) (char *, char *, char *, char *);
102 XmsgFunctionHook *XmsgHookTable = NULL;
106 * RoomFunctionHook extensions are used for hooks which impliment room
107 * processing functions when new messages are added.
109 typedef struct RoomFunctionHook RoomFunctionHook;
110 struct RoomFunctionHook {
111 RoomFunctionHook *next;
112 int (*fcn_ptr) (struct ctdlroom *);
114 RoomFunctionHook *RoomHookTable = NULL;
117 typedef struct SearchFunctionHook SearchFunctionHook;
118 struct SearchFunctionHook {
119 SearchFunctionHook *next;
120 void (*fcn_ptr) (int *, long **, const char *);
123 SearchFunctionHook *SearchFunctionHookTable = NULL;
125 ServiceFunctionHook *ServiceHookTable = NULL;
127 typedef struct ProtoFunctionHook ProtoFunctionHook;
128 struct ProtoFunctionHook {
129 void (*handler) (char *cmdbuf);
134 HashList *ProtoHookList = NULL;
137 #define ERR_PORT (1 << 1)
140 static StrBuf *portlist = NULL;
141 static StrBuf *errormessages = NULL;
144 long DetailErrorFlags;
145 ConstStr Empty = {HKEY("")};
146 char *ErrSubject = "Startup Problems";
147 ConstStr ErrGeneral[] = {
148 {HKEY("Citadel had trouble on starting up. ")},
149 {HKEY(" This means, Citadel won't be the service provider for a specific service you configured it to.\n\n"
150 "If you don't want Citadel to provide these services, turn them off in WebCit via: ")},
151 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
152 "The errors returned by the system were:\n")},
153 {HKEY("You can recheck the above if you follow this faq item:\n"
154 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
157 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by Citadel Server.\n")};
158 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
159 ConstStr ErrPortHint = { HKEY("If you want Citadel to provide you with that functionality, "
160 "check the output of \"netstat -lnp\" on Linux, or \"netstat -na\" on BSD"
161 " and disable the program that binds these ports.\n")};
164 void LogPrintMessages(long err) {
166 StrBuf *List, *DetailList;
167 ConstStr *Short, *Where, *Hint;
170 Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
172 DetailErrorFlags = DetailErrorFlags & ~err;
177 Short = &ErrPortShort;
178 Where = &ErrPortWhere;
181 DetailList = errormessages;
191 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
192 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
193 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
194 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
195 StrBufAppendBuf(Message, List, 0);
196 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
197 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
198 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
199 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
200 StrBufAppendBuf(Message, DetailList, 0);
201 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
202 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
204 syslog(LOG_ERR, "extensions: %s", ChrPtr(Message));
205 syslog(LOG_ERR, "extensions: %s", ErrSubject);
206 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
208 FreeStrBuf(&Message);
210 FreeStrBuf(&DetailList);
214 void AddPortError(char *Port, char *ErrorMessage) {
217 DetailErrorFlags |= ERR_PORT;
219 len = StrLength(errormessages);
220 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
221 else errormessages = NewStrBuf();
222 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
225 len = StrLength(portlist);
226 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
227 else portlist = NewStrBuf();
228 StrBufAppendBufPlain(portlist, Port, -1, 0);
232 int DLoader_Exec_Cmd(char *cmdbuf) {
234 ProtoFunctionHook *p;
236 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
237 p = (ProtoFunctionHook*) vP;
238 p->handler(&cmdbuf[5]);
245 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc) {
246 ProtoFunctionHook *p;
248 if (ProtoHookList == NULL)
249 ProtoHookList = NewHash (1, FourHash);
252 p = (ProtoFunctionHook *)
253 malloc(sizeof(ProtoFunctionHook));
256 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
259 p->handler = handler;
263 Put(ProtoHookList, cmd, 4, p, NULL);
264 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
268 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
270 SessionFunctionHook *newfcn;
272 newfcn = (SessionFunctionHook *)
273 malloc(sizeof(SessionFunctionHook));
274 newfcn->Priority = Priority;
275 newfcn->h_function_pointer = fcn_ptr;
276 newfcn->eventtype = EventType;
278 SessionFunctionHook **pfcn;
279 pfcn = &SessionHookTable;
280 while ((*pfcn != NULL) &&
281 ((*pfcn)->Priority < newfcn->Priority) &&
282 ((*pfcn)->next != NULL))
283 pfcn = &(*pfcn)->next;
285 newfcn->next = *pfcn;
288 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
292 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
294 SessionFunctionHook *cur, *p, *last;
296 cur = SessionHookTable;
297 while (cur != NULL) {
298 if ((fcn_ptr == cur->h_function_pointer) &&
299 (EventType == cur->eventtype))
301 syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
310 SessionHookTable = p;
321 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
324 UserFunctionHook *newfcn;
326 newfcn = (UserFunctionHook *)
327 malloc(sizeof(UserFunctionHook));
328 newfcn->next = UserHookTable;
329 newfcn->h_function_pointer = fcn_ptr;
330 newfcn->eventtype = EventType;
331 UserHookTable = newfcn;
333 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
338 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
340 UserFunctionHook *cur, *p, *last;
343 while (cur != NULL) {
344 if ((fcn_ptr == cur->h_function_pointer) &&
345 (EventType == cur->eventtype))
347 syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
367 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
370 MessageFunctionHook *newfcn;
372 newfcn = (MessageFunctionHook *)
373 malloc(sizeof(MessageFunctionHook));
374 newfcn->next = MessageHookTable;
375 newfcn->h_function_pointer = handler;
376 newfcn->eventtype = EventType;
377 MessageHookTable = newfcn;
379 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
383 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
385 MessageFunctionHook *cur, *p, *last;
387 cur = MessageHookTable;
388 while (cur != NULL) {
389 if ((handler == cur->h_function_pointer) &&
390 (EventType == cur->eventtype))
392 syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
400 MessageHookTable = p;
411 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
413 RoomFunctionHook *newfcn;
415 newfcn = (RoomFunctionHook *)
416 malloc(sizeof(RoomFunctionHook));
417 newfcn->next = RoomHookTable;
418 newfcn->fcn_ptr = fcn_ptr;
419 RoomHookTable = newfcn;
421 syslog(LOG_DEBUG, "extensions: registered a new room function");
425 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
427 RoomFunctionHook *cur, *p, *last;
432 if (fcn_ptr == cur->fcn_ptr) {
433 syslog(LOG_DEBUG, "extensions: unregistered room function");
453 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
455 DeleteFunctionHook *newfcn;
457 newfcn = (DeleteFunctionHook *)
458 malloc(sizeof(DeleteFunctionHook));
459 newfcn->next = DeleteHookTable;
460 newfcn->h_function_pointer = handler;
461 DeleteHookTable = newfcn;
463 syslog(LOG_DEBUG, "extensions: registered a new delete function");
467 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
469 DeleteFunctionHook *cur, *p, *last;
472 cur = DeleteHookTable;
473 while (cur != NULL) {
474 if (handler == cur->h_function_pointer )
476 syslog(LOG_DEBUG, "extensions: unregistered delete function");
495 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
497 FixedOutputHook *newfcn;
499 newfcn = (FixedOutputHook *)
500 malloc(sizeof(FixedOutputHook));
501 newfcn->next = FixedOutputTable;
502 newfcn->h_function_pointer = handler;
503 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
504 FixedOutputTable = newfcn;
506 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
510 void CtdlUnregisterFixedOutputHook(char *content_type)
512 FixedOutputHook *cur, *p, *last;
515 cur = FixedOutputTable;
516 while (cur != NULL) {
517 /* This will also remove duplicates if any */
518 if (!strcasecmp(content_type, cur->content_type)) {
519 syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
526 FixedOutputTable = p;
539 /* returns nonzero if we found a hook and used it */
540 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
542 FixedOutputHook *fcn;
544 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
545 if (!strcasecmp(content_type, fcn->content_type)) {
546 (*fcn->h_function_pointer) (content, content_length);
554 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
557 XmsgFunctionHook *newfcn;
559 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
560 newfcn->next = XmsgHookTable;
561 newfcn->order = order;
562 newfcn->h_function_pointer = fcn_ptr;
563 XmsgHookTable = newfcn;
564 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
568 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
570 XmsgFunctionHook *cur, *p, *last;
574 while (cur != NULL) {
575 /* This will also remove duplicates if any */
576 if (fcn_ptr == cur->h_function_pointer &&
577 order == cur->order) {
578 syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
598 void CtdlRegisterServiceHook(int tcp_port,
600 void (*h_greeting_function) (void),
601 void (*h_command_function) (void),
602 void (*h_async_function) (void),
603 const char *ServiceName)
605 ServiceFunctionHook *newfcn;
608 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
609 message = (char*) malloc (SIZ + SIZ);
611 newfcn->next = ServiceHookTable;
612 newfcn->tcp_port = tcp_port;
613 newfcn->sockpath = sockpath;
614 newfcn->h_greeting_function = h_greeting_function;
615 newfcn->h_command_function = h_command_function;
616 newfcn->h_async_function = h_async_function;
617 newfcn->ServiceName = ServiceName;
619 if (sockpath != NULL) {
620 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
621 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
623 else if (tcp_port <= 0) { /* port -1 to disable */
624 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
630 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"), tcp_port, CtdlGetConfigInt("c_maxsessions"));
631 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
632 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
635 if (newfcn->msock > 0) {
636 ServiceHookTable = newfcn;
637 strcat(message, "registered.");
638 syslog(LOG_INFO, "%s", message);
641 AddPortError(message, "failed");
642 strcat(message, "FAILED.");
643 syslog(LOG_ERR, "%s", message);
650 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
651 void (*h_greeting_function) (void),
652 void (*h_command_function) (void),
653 void (*h_async_function) (void)
656 ServiceFunctionHook *cur, *p, *last;
659 cur = ServiceHookTable;
660 while (cur != NULL) {
661 /* This will also remove duplicates if any */
662 if (h_greeting_function == cur->h_greeting_function &&
663 h_command_function == cur->h_command_function &&
664 h_async_function == cur->h_async_function &&
665 tcp_port == cur->tcp_port &&
666 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
671 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
673 } else if (tcp_port) {
674 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
676 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
683 ServiceHookTable = p;
694 // During shutdown we can close all of the listening sockets.
695 void CtdlShutdownServiceHooks(void) {
696 ServiceFunctionHook *cur;
698 cur = ServiceHookTable;
699 while (cur != NULL) {
700 if (cur->msock != -1) {
703 if (cur->sockpath != NULL){
704 syslog(LOG_INFO, "extensions: [%s] closed unix domain socket %s", cur->ServiceName, cur->sockpath);
705 unlink(cur->sockpath);
707 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
715 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name) {
716 SearchFunctionHook *newfcn;
718 if (!name || !fcn_ptr) {
722 newfcn = (SearchFunctionHook *)
723 malloc(sizeof(SearchFunctionHook));
724 newfcn->next = SearchFunctionHookTable;
726 newfcn->fcn_ptr = fcn_ptr;
727 SearchFunctionHookTable = newfcn;
729 syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
733 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name) {
734 SearchFunctionHook *cur, *p, *last;
737 cur = SearchFunctionHookTable;
738 while (cur != NULL) {
740 (cur->fcn_ptr == fcn_ptr) &&
741 name && !strcmp(name, cur->name))
743 syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
749 SearchFunctionHookTable = p;
760 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name) {
761 SearchFunctionHook *fcn = NULL;
763 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
764 if (!func_name || !strcmp(func_name, fcn->name)) {
765 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
773 void PerformSessionHooks(int EventType) {
774 SessionFunctionHook *fcn = NULL;
776 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
777 if (fcn->eventtype == EventType) {
778 if (EventType == EVT_TIMER) {
779 pthread_setspecific(MyConKey, NULL); /* for every hook */
781 (*fcn->h_function_pointer) ();
787 void PerformUserHooks(ctdluser *usbuf, int EventType) {
788 UserFunctionHook *fcn = NULL;
790 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
791 if (fcn->eventtype == EventType) {
792 (*fcn->h_function_pointer) (usbuf);
798 int PerformMessageHooks(struct CtdlMessage *msg, struct recptypes *recps, int EventType) {
799 MessageFunctionHook *fcn = NULL;
800 int total_retval = 0;
802 /* Other code may elect to protect this message from server-side
803 * handlers; if this is the case, don't do anything.
805 if (msg->cm_flags & CM_SKIP_HOOKS) {
809 /* Otherwise, run all the hooks appropriate to this event type.
811 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
812 if (fcn->eventtype == EventType) {
813 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
817 /* Return the sum of the return codes from the hook functions. If
818 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
819 * the save operation to abort.
825 int PerformRoomHooks(struct ctdlroom *target_room) {
826 RoomFunctionHook *fcn;
827 int total_retval = 0;
829 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
831 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
832 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
835 /* Return the sum of the return codes from the hook functions.
841 void PerformDeleteHooks(char *room, long msgnum) {
842 DeleteFunctionHook *fcn;
844 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
845 (*fcn->h_function_pointer) (room, msgnum);
850 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg) {
851 XmsgFunctionHook *fcn;
855 for (p=0; p<MAX_XMSG_PRI; ++p) {
856 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
857 if (fcn->order == p) {
859 (*fcn->h_function_pointer)
860 (sender, sender_email, recp, msg);
863 /* Break out of the loop if a higher-priority function
864 * successfully delivered the message. This prevents duplicate
865 * deliveries to local users simultaneously signed onto
868 if (total_sent) break;
875 * "Start TLS" function that is (hopefully) adaptable for any protocol
877 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response) {
879 CtdlStartTLS (ok_response, nosup_response, error_response);
884 CTDL_MODULE_INIT(modules) {