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-2023 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"
22 // Structure defentitions for hook tables
23 typedef struct FixedOutputHook FixedOutputHook;
24 struct FixedOutputHook {
25 FixedOutputHook *next;
26 char content_type[64];
27 void (*h_function_pointer) (char *, int);
29 FixedOutputHook *FixedOutputTable = NULL;
32 // SessionFunctionHook extensions are used for any type of hook for which
33 // the context in which it's being called (which is determined by the event
34 // type) will make it obvious for the hook function to know where to look for
36 typedef struct SessionFunctionHook SessionFunctionHook;
37 struct SessionFunctionHook {
38 SessionFunctionHook *next;
40 void (*h_function_pointer) (void);
43 SessionFunctionHook *SessionHookTable = NULL;
46 // UserFunctionHook extensions are used for any type of hook which implements
47 // an operation on a user or username (potentially) other than the one
48 // operating the current session.
49 typedef struct UserFunctionHook UserFunctionHook;
50 struct UserFunctionHook {
51 UserFunctionHook *next;
52 void (*h_function_pointer) (struct ctdluser *usbuf);
55 UserFunctionHook *UserHookTable = NULL;
58 // MessageFunctionHook extensions are used for hooks which implement handlers
59 // for various types of message operations (save, read, etc.)
60 typedef struct MessageFunctionHook MessageFunctionHook;
61 struct MessageFunctionHook {
62 MessageFunctionHook *next;
63 int (*h_function_pointer) (struct CtdlMessage *msg, struct recptypes *recps);
66 MessageFunctionHook *MessageHookTable = NULL;
69 // DeleteFunctionHook extensions are used for hooks which get called when a
70 // message is about to be deleted.
71 typedef struct DeleteFunctionHook DeleteFunctionHook;
72 struct DeleteFunctionHook {
73 DeleteFunctionHook *next;
74 void (*h_function_pointer) (char *target_room, long msgnum);
76 DeleteFunctionHook *DeleteHookTable = NULL;
79 // ExpressMessageFunctionHook extensions are used for hooks which implement
80 // the sending of an instant message through various channels. Any function
81 // registered should return the number of recipients to whom the message was
82 // successfully transmitted.
83 typedef struct XmsgFunctionHook XmsgFunctionHook;
84 struct XmsgFunctionHook {
85 XmsgFunctionHook *next;
86 int (*h_function_pointer) (char *, char *, char *, char *);
89 XmsgFunctionHook *XmsgHookTable = NULL;
92 // RoomFunctionHook extensions are used for hooks which implement room
93 // processing functions when new messages are added.
94 typedef struct RoomFunctionHook RoomFunctionHook;
95 struct RoomFunctionHook {
96 RoomFunctionHook *next;
97 int (*fcn_ptr) (struct ctdlroom *);
99 RoomFunctionHook *RoomHookTable = NULL;
101 ServiceFunctionHook *ServiceHookTable = NULL;
103 typedef struct ProtoFunctionHook ProtoFunctionHook;
104 struct ProtoFunctionHook {
105 void (*handler) (char *cmdbuf);
110 HashList *ProtoHookList = NULL;
113 #define ERR_PORT (1 << 1)
116 static StrBuf *portlist = NULL;
117 static StrBuf *errormessages = NULL;
120 long DetailErrorFlags;
121 ConstStr Empty = {HKEY("")};
122 char *ErrSubject = "Startup Problems";
123 ConstStr ErrGeneral[] = {
124 {HKEY("Citadel had trouble on starting up. ")},
125 {HKEY(" This means, Citadel won't be the service provider for a specific service you configured it to.\n\n"
126 "If you don't want Citadel to provide these services, turn them off in WebCit via: ")},
127 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
128 "The errors returned by the system were:\n")},
129 {HKEY("You can recheck the above if you follow this faq item:\n"
130 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
133 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by Citadel Server.\n")};
134 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
135 ConstStr ErrPortHint = { HKEY("If you want Citadel to provide you with that functionality, "
136 "check the output of \"netstat -lnp\" on Linux, or \"netstat -na\" on BSD"
137 " and disable the program that binds these ports.\n")};
140 void LogPrintMessages(long err) {
142 StrBuf *List, *DetailList;
143 ConstStr *Short, *Where, *Hint;
146 Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
148 DetailErrorFlags = DetailErrorFlags & ~err;
153 Short = &ErrPortShort;
154 Where = &ErrPortWhere;
157 DetailList = errormessages;
167 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
168 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
169 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
170 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
171 StrBufAppendBuf(Message, List, 0);
172 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
173 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
174 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
175 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
176 StrBufAppendBuf(Message, DetailList, 0);
177 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
178 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
180 syslog(LOG_ERR, "extensions: %s", ChrPtr(Message));
181 syslog(LOG_ERR, "extensions: %s", ErrSubject);
182 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
184 FreeStrBuf(&Message);
186 FreeStrBuf(&DetailList);
190 void AddPortError(char *Port, char *ErrorMessage) {
193 DetailErrorFlags |= ERR_PORT;
195 len = StrLength(errormessages);
196 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
197 else errormessages = NewStrBuf();
198 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
200 len = StrLength(portlist);
201 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
202 else portlist = NewStrBuf();
203 StrBufAppendBufPlain(portlist, Port, -1, 0);
207 int DLoader_Exec_Cmd(char *cmdbuf) {
209 ProtoFunctionHook *p;
211 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
212 p = (ProtoFunctionHook*) vP;
213 p->handler(&cmdbuf[5]);
220 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc) {
221 ProtoFunctionHook *p;
223 if (ProtoHookList == NULL)
224 ProtoHookList = NewHash (1, FourHash);
227 p = (ProtoFunctionHook *)
228 malloc(sizeof(ProtoFunctionHook));
231 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
234 p->handler = handler;
238 Put(ProtoHookList, cmd, 4, p, NULL);
239 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
243 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority) {
244 SessionFunctionHook *newfcn;
246 newfcn = (SessionFunctionHook *) malloc(sizeof(SessionFunctionHook));
247 newfcn->Priority = Priority;
248 newfcn->h_function_pointer = fcn_ptr;
249 newfcn->eventtype = EventType;
251 SessionFunctionHook **pfcn;
252 pfcn = &SessionHookTable;
253 while ((*pfcn != NULL) &&
254 ((*pfcn)->Priority < newfcn->Priority) &&
255 ((*pfcn)->next != NULL))
256 pfcn = &(*pfcn)->next;
258 newfcn->next = *pfcn;
261 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
265 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType) {
267 UserFunctionHook *newfcn;
269 newfcn = (UserFunctionHook *) malloc(sizeof(UserFunctionHook));
270 newfcn->next = UserHookTable;
271 newfcn->h_function_pointer = fcn_ptr;
272 newfcn->eventtype = EventType;
273 UserHookTable = newfcn;
275 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)", EventType);
279 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType) {
281 MessageFunctionHook *newfcn;
283 newfcn = (MessageFunctionHook *) malloc(sizeof(MessageFunctionHook));
284 newfcn->next = MessageHookTable;
285 newfcn->h_function_pointer = handler;
286 newfcn->eventtype = EventType;
287 MessageHookTable = newfcn;
289 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
293 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *)) {
294 RoomFunctionHook *newfcn;
296 newfcn = (RoomFunctionHook *) malloc(sizeof(RoomFunctionHook));
297 newfcn->next = RoomHookTable;
298 newfcn->fcn_ptr = fcn_ptr;
299 RoomHookTable = newfcn;
301 syslog(LOG_DEBUG, "extensions: registered a new room function");
305 void CtdlRegisterDeleteHook(void (*handler)(char *, long) ) {
306 DeleteFunctionHook *newfcn;
308 newfcn = (DeleteFunctionHook *) malloc(sizeof(DeleteFunctionHook));
309 newfcn->next = DeleteHookTable;
310 newfcn->h_function_pointer = handler;
311 DeleteHookTable = newfcn;
313 syslog(LOG_DEBUG, "extensions: registered a new delete function");
317 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) ) {
318 FixedOutputHook *newfcn;
320 newfcn = (FixedOutputHook *) malloc(sizeof(FixedOutputHook));
321 newfcn->next = FixedOutputTable;
322 newfcn->h_function_pointer = handler;
323 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
324 FixedOutputTable = newfcn;
326 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
330 // returns nonzero if we found a hook and used it
331 int PerformFixedOutputHooks(char *content_type, char *content, int content_length) {
332 FixedOutputHook *fcn;
334 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
335 if (!strcasecmp(content_type, fcn->content_type)) {
336 (*fcn->h_function_pointer) (content, content_length);
344 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order) {
346 XmsgFunctionHook *newfcn;
348 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
349 newfcn->next = XmsgHookTable;
350 newfcn->order = order;
351 newfcn->h_function_pointer = fcn_ptr;
352 XmsgHookTable = newfcn;
353 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
357 void CtdlRegisterServiceHook(int tcp_port,
359 void (*h_greeting_function) (void),
360 void (*h_command_function) (void),
361 void (*h_async_function) (void),
362 const char *ServiceName)
364 ServiceFunctionHook *newfcn;
367 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
368 message = (char*) malloc (SIZ + SIZ);
370 newfcn->next = ServiceHookTable;
371 newfcn->tcp_port = tcp_port;
372 newfcn->sockpath = sockpath;
373 newfcn->h_greeting_function = h_greeting_function;
374 newfcn->h_command_function = h_command_function;
375 newfcn->h_async_function = h_async_function;
376 newfcn->ServiceName = ServiceName;
378 if (sockpath != NULL) {
379 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
380 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
382 else if (tcp_port <= 0) { // port -1 to disable
383 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
389 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"), tcp_port, CtdlGetConfigInt("c_maxsessions"));
390 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
391 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
394 if (newfcn->msock > 0) {
395 ServiceHookTable = newfcn;
396 strcat(message, "registered.");
397 syslog(LOG_INFO, "%s", message);
400 AddPortError(message, "failed");
401 strcat(message, "FAILED.");
402 syslog(LOG_ERR, "%s", message);
409 // During shutdown we can close all of the listening sockets.
410 void CtdlShutdownServiceHooks(void) {
411 ServiceFunctionHook *cur;
413 cur = ServiceHookTable;
414 while (cur != NULL) {
415 if (cur->msock != -1) {
418 if (cur->sockpath != NULL){
419 syslog(LOG_INFO, "extensions: [%s] closed unix domain socket %s", cur->ServiceName, cur->sockpath);
420 unlink(cur->sockpath);
423 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
431 void PerformSessionHooks(int EventType) {
432 SessionFunctionHook *fcn = NULL;
434 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
435 if (fcn->eventtype == EventType) {
436 if (EventType == EVT_TIMER) {
437 pthread_setspecific(MyConKey, NULL); // for every hook
439 (*fcn->h_function_pointer) ();
445 void PerformUserHooks(ctdluser *usbuf, int EventType) {
446 UserFunctionHook *fcn = NULL;
448 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
449 if (fcn->eventtype == EventType) {
450 (*fcn->h_function_pointer) (usbuf);
456 int PerformMessageHooks(struct CtdlMessage *msg, struct recptypes *recps, int EventType) {
457 MessageFunctionHook *fcn = NULL;
458 int total_retval = 0;
460 // Other code may elect to protect this message from server-side
461 // handlers; if this is the case, don't do anything.
462 if (msg->cm_flags & CM_SKIP_HOOKS) {
466 // Otherwise, run all the hooks appropriate to this event type.
467 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
468 if (fcn->eventtype == EventType) {
469 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
473 // Return the sum of the return codes from the hook functions. If
474 // this is an EVT_BEFORESAVE event, a nonzero return code will cause
475 // the save operation to abort.
480 int PerformRoomHooks(struct ctdlroom *target_room) {
481 RoomFunctionHook *fcn;
482 int total_retval = 0;
484 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
486 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
487 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
490 // Return the sum of the return codes from the hook functions.
495 void PerformDeleteHooks(char *room, long msgnum) {
496 DeleteFunctionHook *fcn;
498 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
499 (*fcn->h_function_pointer) (room, msgnum);
504 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg) {
505 XmsgFunctionHook *fcn;
509 for (p=0; p<MAX_XMSG_PRI; ++p) {
510 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
511 if (fcn->order == p) {
513 (*fcn->h_function_pointer)
514 (sender, sender_email, recp, msg);
517 // Break out of the loop if a higher-priority function
518 // successfully delivered the message. This prevents duplicate
519 // deliveries to local users simultaneously signed onto
521 if (total_sent) break;
527 // "Start TLS" function that is (hopefully) adaptable for any protocol
528 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response) {
530 CtdlStartTLS (ok_response, nosup_response, error_response);
535 CTDL_MODULE_INIT(modules) {