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