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