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