]> code.citadel.org Git - citadel.git/blob - citadel/server/serv_extensions.c
serv_extensions.c: style cleanup from 30,000 feet
[citadel.git] / citadel / server / serv_extensions.c
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.
4 //
5 // Copyright (c) 1987-2023 by the citadel.org team
6 //
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.
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
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"
19 #include "config.h"
20
21
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);
28 };
29 FixedOutputHook *FixedOutputTable = NULL;
30
31
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
35 // pertinent data.
36 typedef struct SessionFunctionHook SessionFunctionHook;
37 struct SessionFunctionHook {
38         SessionFunctionHook *next;
39         int Priority;
40         void (*h_function_pointer) (void);
41         int eventtype;
42 };
43 SessionFunctionHook *SessionHookTable = NULL;
44
45
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);
53         int eventtype;
54 };
55 UserFunctionHook *UserHookTable = NULL;
56
57
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);
64         int eventtype;
65 };
66 MessageFunctionHook *MessageHookTable = NULL;
67
68
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);
75 };
76 DeleteFunctionHook *DeleteHookTable = NULL;
77
78
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 *);
87         int order;
88 };
89 XmsgFunctionHook *XmsgHookTable = NULL;
90
91
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 *);
98 };
99 RoomFunctionHook *RoomHookTable = NULL;
100
101 ServiceFunctionHook *ServiceHookTable = NULL;
102
103 typedef struct ProtoFunctionHook ProtoFunctionHook;
104 struct ProtoFunctionHook {
105         void (*handler) (char *cmdbuf);
106         const char *cmd;
107         const char *desc;
108 };
109
110 HashList *ProtoHookList = NULL;
111
112
113 #define ERR_PORT (1 << 1)
114
115
116 static StrBuf *portlist = NULL;
117 static StrBuf *errormessages = NULL;
118
119
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")}
131 };
132
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")};
138
139
140 void LogPrintMessages(long err) {
141         StrBuf *Message;
142         StrBuf *List, *DetailList;
143         ConstStr *Short, *Where, *Hint; 
144
145         
146         Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
147         
148         DetailErrorFlags = DetailErrorFlags & ~err;
149
150         switch (err)
151         {
152         case ERR_PORT:
153                 Short = &ErrPortShort;
154                 Where = &ErrPortWhere;
155                 Hint  = &ErrPortHint;
156                 List  = portlist;
157                 DetailList = errormessages;
158                 break;
159         default:
160                 Short = &Empty;
161                 Where = &Empty;
162                 Hint  = &Empty;
163                 List  = NULL;
164                 DetailList = NULL;
165         }
166
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);
179
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);
183
184         FreeStrBuf(&Message);
185         FreeStrBuf(&List);
186         FreeStrBuf(&DetailList);
187 }
188
189
190 void AddPortError(char *Port, char *ErrorMessage) {
191         long len;
192
193         DetailErrorFlags |= ERR_PORT;
194
195         len = StrLength(errormessages);
196         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
197         else errormessages = NewStrBuf();
198         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
199
200         len = StrLength(portlist);
201         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
202         else portlist = NewStrBuf();
203         StrBufAppendBufPlain(portlist, Port, -1, 0);
204 }
205
206
207 int DLoader_Exec_Cmd(char *cmdbuf) {
208         void *vP;
209         ProtoFunctionHook *p;
210
211         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
212                 p = (ProtoFunctionHook*) vP;
213                 p->handler(&cmdbuf[5]);
214                 return 1;
215         }
216         return 0;
217 }
218
219
220 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc) {
221         ProtoFunctionHook *p;
222
223         if (ProtoHookList == NULL)
224                 ProtoHookList = NewHash (1, FourHash);
225
226
227         p = (ProtoFunctionHook *)
228                 malloc(sizeof(ProtoFunctionHook));
229
230         if (p == NULL) {
231                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
232                 exit(EXIT_FAILURE);
233         }
234         p->handler = handler;
235         p->cmd = cmd;
236         p->desc = desc;
237
238         Put(ProtoHookList, cmd, 4, p, NULL);
239         syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
240 }
241
242
243 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority) {
244         SessionFunctionHook *newfcn;
245
246         newfcn = (SessionFunctionHook *) malloc(sizeof(SessionFunctionHook));
247         newfcn->Priority = Priority;
248         newfcn->h_function_pointer = fcn_ptr;
249         newfcn->eventtype = EventType;
250
251         SessionFunctionHook **pfcn;
252         pfcn = &SessionHookTable;
253         while ((*pfcn != NULL) && 
254                ((*pfcn)->Priority < newfcn->Priority) &&
255                ((*pfcn)->next != NULL))
256                 pfcn = &(*pfcn)->next;
257                 
258         newfcn->next = *pfcn;
259         *pfcn = newfcn;
260         
261         syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
262 }
263
264
265 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType) {
266
267         UserFunctionHook *newfcn;
268
269         newfcn = (UserFunctionHook *) malloc(sizeof(UserFunctionHook));
270         newfcn->next = UserHookTable;
271         newfcn->h_function_pointer = fcn_ptr;
272         newfcn->eventtype = EventType;
273         UserHookTable = newfcn;
274
275         syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)", EventType);
276 }
277
278
279 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, struct recptypes *), int EventType) {
280
281         MessageFunctionHook *newfcn;
282
283         newfcn = (MessageFunctionHook *) malloc(sizeof(MessageFunctionHook));
284         newfcn->next = MessageHookTable;
285         newfcn->h_function_pointer = handler;
286         newfcn->eventtype = EventType;
287         MessageHookTable = newfcn;
288
289         syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
290 }
291
292
293 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *)) {
294         RoomFunctionHook *newfcn;
295
296         newfcn = (RoomFunctionHook *) malloc(sizeof(RoomFunctionHook));
297         newfcn->next = RoomHookTable;
298         newfcn->fcn_ptr = fcn_ptr;
299         RoomHookTable = newfcn;
300
301         syslog(LOG_DEBUG, "extensions: registered a new room function");
302 }
303
304
305 void CtdlRegisterDeleteHook(void (*handler)(char *, long) ) {
306         DeleteFunctionHook *newfcn;
307
308         newfcn = (DeleteFunctionHook *) malloc(sizeof(DeleteFunctionHook));
309         newfcn->next = DeleteHookTable;
310         newfcn->h_function_pointer = handler;
311         DeleteHookTable = newfcn;
312
313         syslog(LOG_DEBUG, "extensions: registered a new delete function");
314 }
315
316
317 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) ) {
318         FixedOutputHook *newfcn;
319
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;
325
326         syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
327 }
328
329
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;
333
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);
337                         return(1);
338                 }
339         }
340         return(0);
341 }
342
343
344 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order) {
345
346         XmsgFunctionHook *newfcn;
347
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);
354 }
355
356
357 void CtdlRegisterServiceHook(int tcp_port,
358                              char *sockpath,
359                              void (*h_greeting_function) (void),
360                              void (*h_command_function) (void),
361                              void (*h_async_function) (void),
362                              const char *ServiceName)
363 {
364         ServiceFunctionHook *newfcn;
365         char *message;
366
367         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
368         message = (char*) malloc (SIZ + SIZ);
369         
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;
377
378         if (sockpath != NULL) {
379                 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
380                 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
381         }
382         else if (tcp_port <= 0) {       // port -1 to disable
383                 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
384                 free (message);
385                 free(newfcn);
386                 return;
387         }
388         else {
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);
392         }
393
394         if (newfcn->msock > 0) {
395                 ServiceHookTable = newfcn;
396                 strcat(message, "registered.");
397                 syslog(LOG_INFO, "%s", message);
398         }
399         else {
400                 AddPortError(message, "failed");
401                 strcat(message, "FAILED.");
402                 syslog(LOG_ERR, "%s", message);
403                 free(newfcn);
404         }
405         free(message);
406 }
407
408
409 // During shutdown we can close all of the listening sockets.
410 void CtdlShutdownServiceHooks(void) {
411         ServiceFunctionHook *cur;
412
413         cur = ServiceHookTable;
414         while (cur != NULL) {
415                 if (cur->msock != -1) {
416                         close(cur->msock);
417                         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);
421                         }
422                         else {
423                                 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
424                         }
425                 }
426                 cur = cur->next;
427         }
428 }
429
430
431 void PerformSessionHooks(int EventType) {
432         SessionFunctionHook *fcn = NULL;
433
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
438                         }
439                         (*fcn->h_function_pointer) ();
440                 }
441         }
442 }
443
444
445 void PerformUserHooks(ctdluser *usbuf, int EventType) {
446         UserFunctionHook *fcn = NULL;
447
448         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
449                 if (fcn->eventtype == EventType) {
450                         (*fcn->h_function_pointer) (usbuf);
451                 }
452         }
453 }
454
455
456 int PerformMessageHooks(struct CtdlMessage *msg, struct recptypes *recps, int EventType) {
457         MessageFunctionHook *fcn = NULL;
458         int total_retval = 0;
459
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) {
463                 return(0);
464         }
465
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);
470                 }
471         }
472
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.
476         return total_retval;
477 }
478
479
480 int PerformRoomHooks(struct ctdlroom *target_room) {
481         RoomFunctionHook *fcn;
482         int total_retval = 0;
483
484         syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
485
486         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
487                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
488         }
489
490         // Return the sum of the return codes from the hook functions.
491         return total_retval;
492 }
493
494
495 void PerformDeleteHooks(char *room, long msgnum) {
496         DeleteFunctionHook *fcn;
497
498         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
499                 (*fcn->h_function_pointer) (room, msgnum);
500         }
501 }
502
503
504 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg) {
505         XmsgFunctionHook *fcn;
506         int total_sent = 0;
507         int p;
508
509         for (p=0; p<MAX_XMSG_PRI; ++p) {
510                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
511                         if (fcn->order == p) {
512                                 total_sent +=
513                                         (*fcn->h_function_pointer)
514                                                 (sender, sender_email, recp, msg);
515                         }
516                 }
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
520                 // remote services.
521                 if (total_sent) break;
522         }
523         return total_sent;
524 }
525
526
527 // "Start TLS" function that is (hopefully) adaptable for any protocol
528 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response) {
529 #ifdef HAVE_OPENSSL
530         CtdlStartTLS (ok_response, nosup_response, error_response);
531 #endif
532 }
533
534
535 CTDL_MODULE_INIT(modules) {
536         return "modules";
537 }