* Bumped internal version number to 6.23
[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                 malloc(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                         free(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             malloc(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                         free(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             malloc(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                         free(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             malloc(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                         free(cur);
235                         cur = p;
236                 }
237         }
238 }
239
240
241 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
242 {
243
244         struct UserFunctionHook *newfcn;
245
246         newfcn = (struct UserFunctionHook *)
247             malloc(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) (struct ctdluser *), 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                         free(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             malloc(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                         free(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             malloc(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                         free(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             malloc(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                         free(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             malloc(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                         free(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                         void (*h_async_function) (void)
431                         )
432 {
433         struct ServiceFunctionHook *newfcn;
434         char message[SIZ];
435
436         newfcn = (struct ServiceFunctionHook *)
437             malloc(sizeof(struct ServiceFunctionHook));
438         newfcn->next = ServiceHookTable;
439         newfcn->tcp_port = tcp_port;
440         newfcn->sockpath = sockpath;
441         newfcn->h_greeting_function = h_greeting_function;
442         newfcn->h_command_function = h_command_function;
443         newfcn->h_async_function = h_async_function;
444
445         if (sockpath != NULL) {
446                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
447                 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
448         }
449         else if (tcp_port <= 0) {       /* port -1 to disable */
450                 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
451                 free(newfcn);
452                 return;
453         }
454         else {
455                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
456                                         tcp_port,
457                                         config.c_maxsessions);
458                 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
459         }
460
461         if (newfcn->msock > 0) {
462                 ServiceHookTable = newfcn;
463                 strcat(message, "registered.");
464                 lprintf(CTDL_INFO, "%s\n", message);
465         }
466         else {
467                 strcat(message, "FAILED.");
468                 lprintf(CTDL_CRIT, "%s\n", message);
469                 free(newfcn);
470         }
471 }
472
473
474 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
475                         void (*h_greeting_function) (void),
476                         void (*h_command_function) (void),
477                         void (*h_async_function) (void)
478                         )
479 {
480         struct ServiceFunctionHook *cur, *p;
481
482         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
483                 /* This will also remove duplicates if any */
484                 while (cur != NULL &&
485                                 !(sockpath && cur->sockpath &&
486                                         strcmp(sockpath, cur->sockpath)) &&
487                                 h_greeting_function == cur->h_greeting_function &&
488                                 h_command_function == cur->h_command_function &&
489                                 h_async_function == cur->h_async_function &&
490                                 tcp_port == cur->tcp_port) {
491                         close(cur->msock);
492                         if (sockpath) {
493                                 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
494                                                 sockpath);
495                         } else if (tcp_port) {
496                                 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
497                         } else {
498                                 lprintf(CTDL_INFO, "Unregistered unknown service\n");
499                         }
500                         p = cur->next;
501                         if (cur == ServiceHookTable) {
502                                 ServiceHookTable = p;
503                         }
504                         free(cur);
505                         cur = p;
506                 }
507         }
508 }
509
510
511 void PerformSessionHooks(int EventType)
512 {
513         struct SessionFunctionHook *fcn;
514
515         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
516                 if (fcn->eventtype == EventType) {
517                         (*fcn->h_function_pointer) ();
518                 }
519         }
520 }
521
522 void PerformLogHooks(int loglevel, char *logmsg)
523 {
524         struct LogFunctionHook *fcn;
525
526         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
527                 if (fcn->loglevel >= loglevel) {
528                         (*fcn->h_function_pointer) (logmsg);
529                 }
530         }
531 }
532
533 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
534 {
535         struct UserFunctionHook *fcn;
536
537         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
538                 if (fcn->eventtype == EventType) {
539                         (*fcn->h_function_pointer) (usbuf);
540                 }
541         }
542 }
543
544 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
545 {
546         struct MessageFunctionHook *fcn;
547         int total_retval = 0;
548
549         /* Other code may elect to protect this message from server-side
550          * handlers; if this is the case, don't do anything.
551         lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
552                 EventType, msg->cm_flags);
553          */
554         if (msg->cm_flags & CM_SKIP_HOOKS) {
555                 lprintf(CTDL_DEBUG, "Skipping hooks\n");
556                 return(0);
557         }
558
559         /* Otherwise, run all the hooks appropriate to this event type.
560          */
561         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
562                 if (fcn->eventtype == EventType) {
563                         total_retval = total_retval +
564                                 (*fcn->h_function_pointer) (msg);
565                 }
566         }
567
568         /* Return the sum of the return codes from the hook functions.  If
569          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
570          * the save operation to abort.
571          */
572         return total_retval;
573 }
574
575
576
577 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
578 {
579         struct NetprocFunctionHook *fcn;
580         int total_retval = 0;
581
582         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
583                 total_retval = total_retval +
584                         (*fcn->h_function_pointer) (msg, target_room);
585         }
586
587         /* Return the sum of the return codes from the hook functions.
588          * A nonzero return code will cause the message to *not* be imported.
589          */
590         return total_retval;
591 }
592
593
594 void PerformDeleteHooks(char *room, long msgnum)
595 {
596         struct DeleteFunctionHook *fcn;
597
598         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
599                 (*fcn->h_function_pointer) (room, msgnum);
600         }
601 }
602
603
604
605 int PerformXmsgHooks(char *sender, char *recp, char *msg)
606 {
607         struct XmsgFunctionHook *fcn;
608         int total_sent = 0;
609         int p;
610
611         for (p=0; p<MAX_XMSG_PRI; ++p) {
612                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
613                         if (fcn->order == p) {
614                                 total_sent +=
615                                         (*fcn->h_function_pointer)
616                                                 (sender, recp, msg);
617                         }
618                 }
619                 /* Break out of the loop if a higher-priority function
620                  * successfully delivered the message.  This prevents duplicate
621                  * deliveries to local users simultaneously signed onto
622                  * remote services.
623                  */
624                 if (total_sent) break;
625         }
626         return total_sent;
627 }