* New server command-line option "-l" to send log output to the host
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * $Id$
3  *
4  * Citadel Dynamic Loading Module
5  * Written by Brian Costello <btx@calyx.net>
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <dirent.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <ctype.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
22 #include "msgbase.h"
23 #include "tools.h"
24 #include "config.h"
25
26 #ifndef HAVE_SNPRINTF
27 #include <stdarg.h>
28 #include "snprintf.h"
29 #endif
30
31 struct LogFunctionHook *LogHookTable = NULL;
32 struct CleanupFunctionHook *CleanupHookTable = NULL;
33 struct SessionFunctionHook *SessionHookTable = NULL;
34 struct UserFunctionHook *UserHookTable = NULL;
35 struct XmsgFunctionHook *XmsgHookTable = NULL;
36 struct MessageFunctionHook *MessageHookTable = NULL;
37 struct NetprocFunctionHook *NetprocHookTable = NULL;
38 struct DeleteFunctionHook *DeleteHookTable = NULL;
39 struct ServiceFunctionHook *ServiceHookTable = NULL;
40
41 struct ProtoFunctionHook {
42         void (*handler) (char *cmdbuf);
43         char *cmd;
44         char *desc;
45         struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
47
48 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
49 {
50         struct ProtoFunctionHook *p;
51
52         p = (struct ProtoFunctionHook *)
53                 mallok(sizeof(struct ProtoFunctionHook));
54
55         if (p == NULL) {
56                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
57                 exit(EXIT_FAILURE);
58         }
59         p->handler = handler;
60         p->cmd = cmd;
61         p->desc = desc;
62         p->next = ProtoHookList;
63         ProtoHookList = p;
64         lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
65 }
66
67
68 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
69 {
70         struct ProtoFunctionHook *cur, *p;
71
72         for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
73                 /* This will also remove duplicates if any */
74                 while (cur != NULL &&
75                                 handler == cur->handler &&
76                                 !strcmp(cmd, cur->cmd)) {
77                         lprintf(5, "Unregistered server command %s (%s)\n",
78                                         cmd, cur->desc);
79                         p = cur->next;
80                         if (cur == ProtoHookList) {
81                                 ProtoHookList = p;
82                         }
83                         phree(cur);
84                         cur = p;
85                 }
86         }
87 }
88
89
90 int DLoader_Exec_Cmd(char *cmdbuf)
91 {
92         struct ProtoFunctionHook *p;
93
94         for (p = ProtoHookList; p; p = p->next) {
95                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
96                         p->handler(&cmdbuf[5]);
97                         return 1;
98                 }
99         }
100         return 0;
101 }
102
103 void initialize_server_extensions(void)
104 {
105         serv_bio_init();
106         serv_calendar_init();
107         serv_inithat_init();
108         serv_expire_init();
109         serv_imap_init();
110         serv_inetcfg_init();
111         serv_listsub_init();
112         serv_mrtg_init();
113         serv_netfilter_init();
114         serv_network_init();
115         serv_newuser_init();
116         serv_pas2_init();
117         serv_pop3_init();
118         serv_rwho_init();
119         serv_smtp_init();
120         serv_spam_init();
121         /* serv_test_init(); */
122         serv_upgrade_init();
123         serv_vandelay_init();
124         serv_vcard_init();
125 }
126
127
128
129 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
130 {
131
132         struct LogFunctionHook *newfcn;
133
134         newfcn = (struct LogFunctionHook *)
135             mallok(sizeof(struct LogFunctionHook));
136         newfcn->next = LogHookTable;
137         newfcn->h_function_pointer = fcn_ptr;
138         newfcn->loglevel = loglevel;
139         LogHookTable = newfcn;
140
141         lprintf(5, "Registered a new logging function\n");
142 }
143
144
145 void CtdlUnregisterLogHook(void (*fcn_ptr) (char *), int loglevel)
146 {
147         struct LogFunctionHook *cur, *p;
148
149         for (cur = LogHookTable; cur != NULL; cur = cur->next) {
150                 /* This will also remove duplicates if any */
151                 while (cur != NULL &&
152                                 fcn_ptr == cur->h_function_pointer &&
153                                 loglevel == cur->loglevel) {
154                         lprintf(5, "Unregistered logging function\n");
155                         p = cur->next;
156                         if (cur == LogHookTable) {
157                                 LogHookTable = p;
158                         }
159                         phree(cur);
160                         cur = p;
161                 }
162         }
163 }
164
165
166 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
167 {
168
169         struct CleanupFunctionHook *newfcn;
170
171         newfcn = (struct CleanupFunctionHook *)
172             mallok(sizeof(struct CleanupFunctionHook));
173         newfcn->next = CleanupHookTable;
174         newfcn->h_function_pointer = fcn_ptr;
175         CleanupHookTable = newfcn;
176
177         lprintf(5, "Registered a new cleanup function\n");
178 }
179
180
181 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
182 {
183         struct CleanupFunctionHook *cur, *p;
184
185         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
186                 /* This will also remove duplicates if any */
187                 while (cur != NULL &&
188                                 fcn_ptr == cur->h_function_pointer) {
189                         lprintf(5, "Unregistered cleanup function\n");
190                         p = cur->next;
191                         if (cur == CleanupHookTable) {
192                                 CleanupHookTable = p;
193                         }
194                         phree(cur);
195                         cur = p;
196                 }
197         }
198 }
199
200
201 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
202 {
203
204         struct SessionFunctionHook *newfcn;
205
206         newfcn = (struct SessionFunctionHook *)
207             mallok(sizeof(struct SessionFunctionHook));
208         newfcn->next = SessionHookTable;
209         newfcn->h_function_pointer = fcn_ptr;
210         newfcn->eventtype = EventType;
211         SessionHookTable = newfcn;
212
213         lprintf(5, "Registered a new session function (type %d)\n",
214                 EventType);
215 }
216
217
218 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
219 {
220         struct SessionFunctionHook *cur, *p;
221
222         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
223                 /* This will also remove duplicates if any */
224                 while (cur != NULL &&
225                                 fcn_ptr == cur->h_function_pointer &&
226                                 EventType == cur->eventtype) {
227                         lprintf(5, "Unregistered session function (type %d)\n",
228                                         EventType);
229                         p = cur->next;
230                         if (cur == SessionHookTable) {
231                                 SessionHookTable = p;
232                         }
233                         phree(cur);
234                         cur = p;
235                 }
236         }
237 }
238
239
240 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
241 {
242
243         struct UserFunctionHook *newfcn;
244
245         newfcn = (struct UserFunctionHook *)
246             mallok(sizeof(struct UserFunctionHook));
247         newfcn->next = UserHookTable;
248         newfcn->h_function_pointer = fcn_ptr;
249         newfcn->eventtype = EventType;
250         UserHookTable = newfcn;
251
252         lprintf(5, "Registered a new user function (type %d)\n",
253                 EventType);
254 }
255
256
257 void CtdlUnregisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
258 {
259         struct UserFunctionHook *cur, *p;
260
261         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
262                 /* This will also remove duplicates if any */
263                 while (cur != NULL &&
264                                 fcn_ptr == cur->h_function_pointer &&
265                                 EventType == cur->eventtype) {
266                         lprintf(5, "Unregistered user function (type %d)\n",
267                                         EventType);
268                         p = cur->next;
269                         if (cur == UserHookTable) {
270                                 UserHookTable = p;
271                         }
272                         phree(cur);
273                         cur = p;
274                 }
275         }
276 }
277
278
279 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
280                                 int EventType)
281 {
282
283         struct MessageFunctionHook *newfcn;
284
285         newfcn = (struct MessageFunctionHook *)
286             mallok(sizeof(struct MessageFunctionHook));
287         newfcn->next = MessageHookTable;
288         newfcn->h_function_pointer = handler;
289         newfcn->eventtype = EventType;
290         MessageHookTable = newfcn;
291
292         lprintf(5, "Registered a new message function (type %d)\n",
293                 EventType);
294 }
295
296
297 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
298                 int EventType)
299 {
300         struct MessageFunctionHook *cur, *p;
301
302         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
303                 /* This will also remove duplicates if any */
304                 while (cur != NULL &&
305                                 handler == cur->h_function_pointer &&
306                                 EventType == cur->eventtype) {
307                         lprintf(5, "Unregistered message function (type %d)\n",
308                                         EventType);
309                         p = cur->next;
310                         if (cur == MessageHookTable) {
311                                 MessageHookTable = p;
312                         }
313                         phree(cur);
314                         cur = p;
315                 }
316         }
317 }
318
319
320 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
321 {
322         struct NetprocFunctionHook *newfcn;
323
324         newfcn = (struct NetprocFunctionHook *)
325             mallok(sizeof(struct NetprocFunctionHook));
326         newfcn->next = NetprocHookTable;
327         newfcn->h_function_pointer = handler;
328         NetprocHookTable = newfcn;
329
330         lprintf(5, "Registered a new netproc function\n");
331 }
332
333
334 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
335 {
336         struct NetprocFunctionHook *cur, *p;
337
338         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
339                 /* This will also remove duplicates if any */
340                 while (cur != NULL &&
341                                 handler == cur->h_function_pointer ) {
342                         lprintf(5, "Unregistered netproc function\n");
343                         p = cur->next;
344                         if (cur == NetprocHookTable) {
345                                 NetprocHookTable = p;
346                         }
347                         phree(cur);
348                         cur = p;
349                 }
350         }
351 }
352
353
354 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
355 {
356         struct DeleteFunctionHook *newfcn;
357
358         newfcn = (struct DeleteFunctionHook *)
359             mallok(sizeof(struct DeleteFunctionHook));
360         newfcn->next = DeleteHookTable;
361         newfcn->h_function_pointer = handler;
362         DeleteHookTable = newfcn;
363
364         lprintf(5, "Registered a new netproc function\n");
365 }
366
367
368 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
369 {
370         struct DeleteFunctionHook *cur, *p;
371
372         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
373                 /* This will also remove duplicates if any */
374                 while (cur != NULL &&
375                                 handler == cur->h_function_pointer ) {
376                         lprintf(5, "Unregistered netproc function\n");
377                         p = cur->next;
378                         if (cur == DeleteHookTable) {
379                                 DeleteHookTable = p;
380                         }
381                         phree(cur);
382                         cur = p;
383                 }
384         }
385 }
386
387
388 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
389 {
390
391         struct XmsgFunctionHook *newfcn;
392
393         newfcn = (struct XmsgFunctionHook *)
394             mallok(sizeof(struct XmsgFunctionHook));
395         newfcn->next = XmsgHookTable;
396         newfcn->order = order;
397         newfcn->h_function_pointer = fcn_ptr;
398         XmsgHookTable = newfcn;
399         lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
400 }
401
402
403 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
404 {
405         struct XmsgFunctionHook *cur, *p;
406
407         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
408                 /* This will also remove duplicates if any */
409                 while (cur != NULL &&
410                                 fcn_ptr == cur->h_function_pointer &&
411                                 order == cur->order) {
412                         lprintf(5, "Unregistered x-msg function "
413                                         "(priority %d)\n", order);
414                         p = cur->next;
415                         if (cur == XmsgHookTable) {
416                                 XmsgHookTable = p;
417                         }
418                         phree(cur);
419                         cur = p;
420                 }
421         }
422 }
423
424
425 void CtdlRegisterServiceHook(int tcp_port,
426                         char *sockpath,
427                         void (*h_greeting_function) (void),
428                         void (*h_command_function) (void) )
429 {
430         struct ServiceFunctionHook *newfcn;
431         char message[SIZ];
432
433         newfcn = (struct ServiceFunctionHook *)
434             mallok(sizeof(struct ServiceFunctionHook));
435         newfcn->next = ServiceHookTable;
436         newfcn->tcp_port = tcp_port;
437         newfcn->sockpath = sockpath;
438         newfcn->h_greeting_function = h_greeting_function;
439         newfcn->h_command_function = h_command_function;
440
441         if (sockpath != NULL) {
442                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
443                 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
444         }
445         else if (tcp_port <= 0) {       /* port -1 to disable */
446                 lprintf(7, "Service has been manually disabled, skipping\n");
447                 phree(newfcn);
448                 return;
449         }
450         else {
451                 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
452                 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
453         }
454
455         if (newfcn->msock > 0) {
456                 ServiceHookTable = newfcn;
457                 strcat(message, "registered.");
458                 lprintf(5, "%s\n", message);
459         }
460         else {
461                 strcat(message, "FAILED.");
462                 lprintf(2, "%s\n", message);
463                 phree(newfcn);
464         }
465 }
466
467
468 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
469                         void (*h_greeting_function) (void),
470                         void (*h_command_function) (void) )
471 {
472         struct ServiceFunctionHook *cur, *p;
473
474         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
475                 /* This will also remove duplicates if any */
476                 while (cur != NULL &&
477                                 !(sockpath && cur->sockpath &&
478                                         strcmp(sockpath, cur->sockpath)) &&
479                                 h_greeting_function == cur->h_greeting_function &&
480                                 h_command_function == cur->h_command_function &&
481                                 tcp_port == cur->tcp_port) {
482                         close(cur->msock);
483                         if (sockpath) {
484                                 lprintf(5, "Closed UNIX domain socket %s\n",
485                                                 sockpath);
486                         } else if (tcp_port) {
487                                 lprintf(5, "Closed TCP port %d\n", tcp_port);
488                         } else {
489                                 lprintf(5, "Unregistered unknown service\n");
490                         }
491                         p = cur->next;
492                         if (cur == ServiceHookTable) {
493                                 ServiceHookTable = p;
494                         }
495                         phree(cur);
496                         cur = p;
497                 }
498         }
499 }
500
501
502 void PerformSessionHooks(int EventType)
503 {
504         struct SessionFunctionHook *fcn;
505
506         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
507                 if (fcn->eventtype == EventType) {
508                         (*fcn->h_function_pointer) ();
509                 }
510         }
511 }
512
513 void PerformLogHooks(int loglevel, char *logmsg)
514 {
515         struct LogFunctionHook *fcn;
516
517         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
518                 if (fcn->loglevel >= loglevel) {
519                         (*fcn->h_function_pointer) (logmsg);
520                 }
521         }
522 }
523
524 void PerformUserHooks(char *username, long usernum, int EventType)
525 {
526         struct UserFunctionHook *fcn;
527
528         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
529                 if (fcn->eventtype == EventType) {
530                         (*fcn->h_function_pointer) (username, usernum);
531                 }
532         }
533 }
534
535 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
536 {
537         struct MessageFunctionHook *fcn;
538         int total_retval = 0;
539
540         /* Other code may elect to protect this message from server-side
541          * handlers; if this is the case, don't do anything.
542         lprintf(9, "** Event type is %d, flags are %d\n",
543                 EventType, msg->cm_flags);
544          */
545         if (msg->cm_flags & CM_SKIP_HOOKS) {
546                 lprintf(9, "Skipping hooks\n");
547                 return(0);
548         }
549
550         /* Otherwise, run all the hooks appropriate to this event type.
551          */
552         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
553                 if (fcn->eventtype == EventType) {
554                         total_retval = total_retval +
555                                 (*fcn->h_function_pointer) (msg);
556                 }
557         }
558
559         /* Return the sum of the return codes from the hook functions.  If
560          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
561          * the save operation to abort.
562          */
563         return total_retval;
564 }
565
566
567
568 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
569 {
570         struct NetprocFunctionHook *fcn;
571         int total_retval = 0;
572
573         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
574                 total_retval = total_retval +
575                         (*fcn->h_function_pointer) (msg, target_room);
576         }
577
578         /* Return the sum of the return codes from the hook functions.
579          * A nonzero return code will cause the message to *not* be imported.
580          */
581         return total_retval;
582 }
583
584
585 void PerformDeleteHooks(char *room, long msgnum)
586 {
587         struct DeleteFunctionHook *fcn;
588
589         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
590                 (*fcn->h_function_pointer) (room, msgnum);
591         }
592 }
593
594
595
596 int PerformXmsgHooks(char *sender, char *recp, char *msg)
597 {
598         struct XmsgFunctionHook *fcn;
599         int total_sent = 0;
600         int p;
601
602         for (p=0; p<MAX_XMSG_PRI; ++p) {
603                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
604                         if (fcn->order == p) {
605                                 total_sent +=
606                                         (*fcn->h_function_pointer)
607                                                 (sender, recp, msg);
608                         }
609                 }
610                 /* Break out of the loop if a higher-priority function
611                  * successfully delivered the message.  This prevents duplicate
612                  * deliveries to local users simultaneously signed onto
613                  * remote services.
614                  */
615                 if (total_sent) break;
616         }
617         return total_sent;
618 }