04c34704b7de0bc15a0dc69d32fcbf89114463a6
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * Citadel Dynamic Loading Module
3  * Written by Brian Costello <btx@calyx.net>
4  *
5  * Copyright (c) 1987-2011 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include "sysdep.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <ctype.h>
31 #include <syslog.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "serv_extensions.h"
36 #include "sysdep_decls.h"
37 #include "msgbase.h"
38 #include "config.h"
39
40 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
41
42 #include "ctdl_module.h"
43
44 #ifndef HAVE_SNPRINTF
45 #include <stdarg.h>
46 #include "snprintf.h"
47 #endif
48
49  
50 /*
51  * Structure defentitions for hook tables
52  */
53
54 typedef struct __LogDebugEntry {
55         CtdlDbgFunction F;
56         const char *Name;
57         long Len;
58         const int *LogP;
59 } LogDebugEntry;
60 HashList *LogDebugEntryTable = NULL;
61
62 typedef struct LogFunctionHook LogFunctionHook;
63 struct LogFunctionHook {
64         LogFunctionHook *next;
65         int loglevel;
66         void (*h_function_pointer) (char *);
67 };
68 extern LogFunctionHook *LogHookTable;
69
70 typedef struct FixedOutputHook FixedOutputHook;
71 struct FixedOutputHook {
72         FixedOutputHook *next;
73         char content_type[64];
74         void (*h_function_pointer) (char *, int);
75 };
76 extern FixedOutputHook *FixedOutputTable;
77
78
79
80 /*
81  * SessionFunctionHook extensions are used for any type of hook for which
82  * the context in which it's being called (which is determined by the event
83  * type) will make it obvious for the hook function to know where to look for
84  * pertinent data.
85  */
86 typedef struct SessionFunctionHook SessionFunctionHook;
87 struct SessionFunctionHook {
88         SessionFunctionHook *next;
89         void (*h_function_pointer) (void);
90         int eventtype;
91 };
92 extern SessionFunctionHook *SessionHookTable;
93
94
95 /*
96  * UserFunctionHook extensions are used for any type of hook which implements
97  * an operation on a user or username (potentially) other than the one
98  * operating the current session.
99  */
100 typedef struct UserFunctionHook UserFunctionHook;
101 struct UserFunctionHook {
102         UserFunctionHook *next;
103         void (*h_function_pointer) (struct ctdluser *usbuf);
104         int eventtype;
105 };
106 extern UserFunctionHook *UserHookTable;
107
108 /*
109  * MessageFunctionHook extensions are used for hooks which implement handlers
110  * for various types of message operations (save, read, etc.)
111  */
112 typedef struct MessageFunctionHook MessageFunctionHook;
113 struct MessageFunctionHook {
114         MessageFunctionHook *next;
115         int (*h_function_pointer) (struct CtdlMessage *msg);
116         int eventtype;
117 };
118 extern MessageFunctionHook *MessageHookTable;
119
120
121 /*
122  * NetprocFunctionHook extensions are used for hooks which implement handlers
123  * for incoming network messages.
124  */
125 typedef struct NetprocFunctionHook NetprocFunctionHook;
126 struct NetprocFunctionHook {
127         NetprocFunctionHook *next;
128         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
129 };
130 extern NetprocFunctionHook *NetprocHookTable;
131
132
133 /*
134  * DeleteFunctionHook extensions are used for hooks which get called when a
135  * message is about to be deleted.
136  */
137 typedef struct DeleteFunctionHook DeleteFunctionHook;
138 struct DeleteFunctionHook {
139         DeleteFunctionHook *next;
140         void (*h_function_pointer) (char *target_room, long msgnum);
141 };
142 extern DeleteFunctionHook *DeleteHookTable;
143
144
145 /*
146  * ExpressMessageFunctionHook extensions are used for hooks which implement
147  * the sending of an instant message through various channels.  Any function
148  * registered should return the number of recipients to whom the message was
149  * successfully transmitted.
150  */
151 typedef struct XmsgFunctionHook XmsgFunctionHook;
152 struct XmsgFunctionHook {
153         XmsgFunctionHook *next;
154         int (*h_function_pointer) (char *, char *, char *, char *);
155         int order;
156 };
157 extern XmsgFunctionHook *XmsgHookTable;
158
159
160
161
162 /*
163  * RoomFunctionHook extensions are used for hooks which impliment room
164  * processing functions when new messages are added EG. SIEVE.
165  */
166 typedef struct RoomFunctionHook RoomFunctionHook;
167 struct RoomFunctionHook {
168         RoomFunctionHook *next;
169         int (*fcn_ptr) (struct ctdlroom *);
170 };
171 extern RoomFunctionHook *RoomHookTable;
172
173
174
175 typedef struct SearchFunctionHook SearchFunctionHook;
176 struct SearchFunctionHook {
177         SearchFunctionHook *next;
178         void (*fcn_ptr) (int *, long **, const char *);
179         char *name;
180 };
181 extern SearchFunctionHook *SearchFunctionHookTable;
182
183
184 CleanupFunctionHook *CleanupHookTable = NULL;
185 CleanupFunctionHook *EVCleanupHookTable = NULL;
186 SessionFunctionHook *SessionHookTable = NULL;
187 UserFunctionHook *UserHookTable = NULL;
188 XmsgFunctionHook *XmsgHookTable = NULL;
189 MessageFunctionHook *MessageHookTable = NULL;
190 NetprocFunctionHook *NetprocHookTable = NULL;
191 DeleteFunctionHook *DeleteHookTable = NULL;
192 ServiceFunctionHook *ServiceHookTable = NULL;
193 FixedOutputHook *FixedOutputTable = NULL;
194 RoomFunctionHook *RoomHookTable = NULL;
195 SearchFunctionHook *SearchFunctionHookTable = NULL;
196
197 typedef struct ProtoFunctionHook ProtoFunctionHook;
198 struct ProtoFunctionHook {
199         void (*handler) (char *cmdbuf);
200         const char *cmd;
201         const char *desc;
202 };
203
204 HashList *ProtoHookList = NULL;
205
206
207 #define ERR_PORT (1 << 1)
208
209
210 static StrBuf *portlist = NULL;
211
212 static StrBuf *errormessages = NULL;
213
214
215 long   DetailErrorFlags;
216 ConstStr Empty = {HKEY("")};
217 char *ErrSubject = "Startup Problems";
218 ConstStr ErrGeneral[] = {
219         {HKEY("Citadel had trouble on starting up. ")},
220         {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
221               "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
222         {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
223               "The errors returned by the system were:\n")},
224         {HKEY("You can recheck the above if you follow this faq item:\n"
225               "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
226 };
227
228 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
229 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
230 ConstStr ErrPortHint  = { HKEY("If you want citadel to provide you with that functionality, "
231                                "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
232                                " and stop the program that binds these ports.\n You should eventually remove "
233                                " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
234                                " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
235
236
237 void LogPrintMessages(long err)
238 {
239         StrBuf *Message;
240         StrBuf *List, *DetailList;
241         ConstStr *Short, *Where, *Hint; 
242
243         
244         Message = NewStrBufPlain(NULL, 
245                                  StrLength(portlist) + StrLength(errormessages));
246         
247         DetailErrorFlags = DetailErrorFlags & ~err;
248
249         switch (err)
250         {
251         case ERR_PORT:
252                 Short = &ErrPortShort;
253                 Where = &ErrPortWhere;
254                 Hint  = &ErrPortHint;
255                 List  = portlist;
256                 DetailList = errormessages;
257                 break;
258         default:
259                 Short = &Empty;
260                 Where = &Empty;
261                 Hint  = &Empty;
262                 List  = NULL;
263                 DetailList = NULL;
264         }
265
266         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
267         StrBufAppendBufPlain(Message, CKEY(*Short), 0); 
268         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
269         StrBufAppendBufPlain(Message, CKEY(*Where), 0);
270         StrBufAppendBuf(Message, List, 0);
271         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
272         StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
273         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
274         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
275         StrBufAppendBuf(Message, DetailList, 0);
276         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
277         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
278
279         syslog(LOG_EMERG, "%s", ChrPtr(Message));
280         syslog(LOG_EMERG, "%s", ErrSubject);
281         quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
282
283         FreeStrBuf(&Message);
284         FreeStrBuf(&List);
285         FreeStrBuf(&DetailList);
286 }
287
288
289 void AddPortError(char *Port, char *ErrorMessage)
290 {
291         long len;
292
293         DetailErrorFlags |= ERR_PORT;
294
295         len = StrLength(errormessages);
296         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
297         else errormessages = NewStrBuf();
298         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
299
300
301         len = StrLength(portlist);
302         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
303         else portlist = NewStrBuf();
304         StrBufAppendBufPlain(portlist, Port, -1, 0);
305 }
306
307
308 int DLoader_Exec_Cmd(char *cmdbuf)
309 {
310         void *vP;
311         ProtoFunctionHook *p;
312
313         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
314                 p = (ProtoFunctionHook*) vP;
315                 p->handler(&cmdbuf[5]);
316                 return 1;
317         }
318         return 0;
319 }
320
321 long FourHash(const char *key, long length) 
322 {
323         int i;
324         int ret = 0;
325         const unsigned char *ptr = (const unsigned char*)key;
326
327         for (i = 0; i < 4; i++, ptr ++) 
328                 ret = (ret << 8) | 
329                         ( ((*ptr >= 'a') &&
330                            (*ptr <= 'z'))? 
331                           *ptr - 'a' + 'A': 
332                           *ptr);
333
334         return ret;
335 }
336
337 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
338 {
339         LogDebugEntry *E;
340         if (LogDebugEntryTable == NULL)
341                 LogDebugEntryTable = NewHash(1, NULL);
342         E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
343         E->F = F;
344         E->Name = Name;
345         E->Len = Len;
346         E->LogP = LogP;
347         Put(LogDebugEntryTable, Name, Len, E, NULL);
348         
349 }
350 void CtdlSetDebugLogFacilities(const char **Str, long n)
351 {
352         StrBuf *Token = NULL;
353         StrBuf *Buf = NULL;
354         const char *ch;
355         int i;
356         int DoAll = 0;
357         void *vptr;
358
359         for (i=0; i < n; i++){
360                 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
361                         if (strcmp(Str[i], "all") == 0) {
362                                 DoAll = 1;
363                                 continue;
364                         }
365                         Buf = NewStrBufPlain(Str[i], -1);
366                         ch = NULL;
367                         if (Token == NULL)
368                                 Token = NewStrBufPlain(NULL, StrLength(Buf));
369                         while ((ch != StrBufNOTNULL) &&
370                                StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
371                                 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) && 
372                                     (vptr != NULL))
373                                 {
374                                         LogDebugEntry *E = (LogDebugEntry*)vptr;
375                                         E->F(1);
376                                 }
377                         }
378                 }
379                 FreeStrBuf(&Buf);
380         }
381         FreeStrBuf(&Token);
382         if (DoAll) {
383                 long HKLen;
384                 const char *ch;
385                 HashPos *Pos;
386
387                 Pos = GetNewHashPos(LogDebugEntryTable, 0);
388                 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
389                         LogDebugEntry *E = (LogDebugEntry*)vptr;
390                         E->F(1);
391                 }
392
393                 DeleteHashPos(&Pos);
394         }
395 }
396 void cmd_log_get(char *argbuf)
397 {
398         long HKLen;
399         const char *ch;
400         HashPos *Pos;
401         void *vptr;
402
403         if (CtdlAccessCheck(ac_aide)) return;
404
405         cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
406
407         Pos = GetNewHashPos(LogDebugEntryTable, 0);
408
409         while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
410                 LogDebugEntry *E = (LogDebugEntry*)vptr;
411                 cprintf("%s: %d\n", ch, *E->LogP);
412         }
413         
414         DeleteHashPos(&Pos);
415         cprintf("000\n");
416 }
417 void cmd_log_set(char *argbuf)
418 {
419         void *vptr;
420         int lset;
421         int wlen;
422         char which[SIZ] = "";
423
424         if (CtdlAccessCheck(ac_aide)) return;
425
426         wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
427         if (wlen < 0) wlen = 0;
428         lset = extract_int(argbuf, 1);
429         if (lset != 0) lset = 1;
430         if (GetHash(LogDebugEntryTable, which, wlen, &vptr) && 
431             (vptr != NULL))
432         {
433                 LogDebugEntry *E = (LogDebugEntry*)vptr;
434                 E->F(lset);
435                 cprintf("%d %s|%d\n", CIT_OK, which, lset);
436         }
437         else {
438                 cprintf("%d Log setting %s not known\n", 
439                         ERROR, which);
440         }
441 }
442 void CtdlDestroyDebugTable(void)
443 {
444
445         DeleteHash(&LogDebugEntryTable);
446 }
447
448 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
449 {
450         ProtoFunctionHook *p;
451
452         if (ProtoHookList == NULL)
453                 ProtoHookList = NewHash (1, FourHash);
454
455
456         p = (ProtoFunctionHook *)
457                 malloc(sizeof(ProtoFunctionHook));
458
459         if (p == NULL) {
460                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
461                 exit(EXIT_FAILURE);
462         }
463         p->handler = handler;
464         p->cmd = cmd;
465         p->desc = desc;
466
467         Put(ProtoHookList, cmd, 4, p, NULL);
468         syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
469 }
470
471 void CtdlDestroyProtoHooks(void)
472 {
473
474         DeleteHash(&ProtoHookList);
475 }
476
477
478 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
479 {
480
481         CleanupFunctionHook *newfcn;
482
483         newfcn = (CleanupFunctionHook *)
484             malloc(sizeof(CleanupFunctionHook));
485         newfcn->next = CleanupHookTable;
486         newfcn->h_function_pointer = fcn_ptr;
487         CleanupHookTable = newfcn;
488
489         syslog(LOG_INFO, "Registered a new cleanup function\n");
490 }
491
492
493 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
494 {
495         CleanupFunctionHook *cur, *p;
496
497         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
498                 /* This will also remove duplicates if any */
499                 while (cur != NULL &&
500                                 fcn_ptr == cur->h_function_pointer) {
501                         syslog(LOG_INFO, "Unregistered cleanup function\n");
502                         p = cur->next;
503                         if (cur == CleanupHookTable) {
504                                 CleanupHookTable = p;
505                         }
506                         free(cur);
507                         cur = p;
508                 }
509         }
510 }
511
512
513 void CtdlDestroyCleanupHooks(void)
514 {
515         CleanupFunctionHook *cur, *p;
516
517         cur = CleanupHookTable;
518         while (cur != NULL)
519         {
520                 syslog(LOG_INFO, "Destroyed cleanup function\n");
521                 p = cur->next;
522                 free(cur);
523                 cur = p;
524         }
525         CleanupHookTable = NULL;
526 }
527
528 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
529 {
530
531         CleanupFunctionHook *newfcn;
532
533         newfcn = (CleanupFunctionHook *)
534             malloc(sizeof(CleanupFunctionHook));
535         newfcn->next = EVCleanupHookTable;
536         newfcn->h_function_pointer = fcn_ptr;
537         EVCleanupHookTable = newfcn;
538
539         syslog(LOG_INFO, "Registered a new cleanup function\n");
540 }
541
542
543 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
544 {
545         CleanupFunctionHook *cur, *p;
546
547         for (cur = EVCleanupHookTable; cur != NULL; cur = cur->next) {
548                 /* This will also remove duplicates if any */
549                 while (cur != NULL &&
550                                 fcn_ptr == cur->h_function_pointer) {
551                         syslog(LOG_INFO, "Unregistered cleanup function\n");
552                         p = cur->next;
553                         if (cur == EVCleanupHookTable) {
554                                 EVCleanupHookTable = p;
555                         }
556                         free(cur);
557                         cur = p;
558                 }
559         }
560 }
561
562
563 void CtdlDestroyEVCleanupHooks(void)
564 {
565         CleanupFunctionHook *cur, *p;
566
567         cur = EVCleanupHookTable;
568         while (cur != NULL)
569         {
570                 syslog(LOG_INFO, "Destroyed cleanup function\n");
571                 p = cur->next;
572                 free(cur);
573                 cur = p;
574         }
575         EVCleanupHookTable = NULL;
576 }
577
578
579 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
580 {
581
582         SessionFunctionHook *newfcn;
583
584         newfcn = (SessionFunctionHook *)
585             malloc(sizeof(SessionFunctionHook));
586         newfcn->next = SessionHookTable;
587         newfcn->h_function_pointer = fcn_ptr;
588         newfcn->eventtype = EventType;
589         SessionHookTable = newfcn;
590
591         syslog(LOG_INFO, "Registered a new session function (type %d)\n",
592                 EventType);
593 }
594
595
596 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
597 {
598         SessionFunctionHook *cur, *p;
599
600         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
601                 /* This will also remove duplicates if any */
602                 while (cur != NULL &&
603                                 fcn_ptr == cur->h_function_pointer &&
604                                 EventType == cur->eventtype) {
605                         syslog(LOG_INFO, "Unregistered session function (type %d)\n",
606                                         EventType);
607                         p = cur->next;
608                         if (cur == SessionHookTable) {
609                                 SessionHookTable = p;
610                         }
611                         free(cur);
612                         cur = p;
613                 }
614         }
615 }
616
617 void CtdlDestroySessionHooks(void)
618 {
619         SessionFunctionHook *cur, *p;
620
621         cur = SessionHookTable;
622         while (cur != NULL)
623         {
624                 syslog(LOG_INFO, "Destroyed session function\n");
625                 p = cur->next;
626                 free(cur);
627                 cur = p;
628         }
629         SessionHookTable = NULL;
630 }
631
632
633 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
634 {
635
636         UserFunctionHook *newfcn;
637
638         newfcn = (UserFunctionHook *)
639             malloc(sizeof(UserFunctionHook));
640         newfcn->next = UserHookTable;
641         newfcn->h_function_pointer = fcn_ptr;
642         newfcn->eventtype = EventType;
643         UserHookTable = newfcn;
644
645         syslog(LOG_INFO, "Registered a new user function (type %d)\n",
646                 EventType);
647 }
648
649
650 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
651 {
652         UserFunctionHook *cur, *p;
653
654         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
655                 /* This will also remove duplicates if any */
656                 while (cur != NULL &&
657                                 fcn_ptr == cur->h_function_pointer &&
658                                 EventType == cur->eventtype) {
659                         syslog(LOG_INFO, "Unregistered user function (type %d)\n",
660                                         EventType);
661                         p = cur->next;
662                         if (cur == UserHookTable) {
663                                 UserHookTable = p;
664                         }
665                         free(cur);
666                         cur = p;
667                 }
668         }
669 }
670
671 void CtdlDestroyUserHooks(void)
672 {
673         UserFunctionHook *cur, *p;
674
675         cur = UserHookTable;
676         while (cur != NULL)
677         {
678                 syslog(LOG_INFO, "Destroyed user function \n");
679                 p = cur->next;
680                 free(cur);
681                 cur = p;
682         }
683         UserHookTable = NULL;
684 }
685
686
687 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
688                                 int EventType)
689 {
690
691         MessageFunctionHook *newfcn;
692
693         newfcn = (MessageFunctionHook *)
694             malloc(sizeof(MessageFunctionHook));
695         newfcn->next = MessageHookTable;
696         newfcn->h_function_pointer = handler;
697         newfcn->eventtype = EventType;
698         MessageHookTable = newfcn;
699
700         syslog(LOG_INFO, "Registered a new message function (type %d)\n",
701                 EventType);
702 }
703
704
705 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
706                 int EventType)
707 {
708         MessageFunctionHook *cur, *p;
709
710         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
711                 /* This will also remove duplicates if any */
712                 while (cur != NULL &&
713                                 handler == cur->h_function_pointer &&
714                                 EventType == cur->eventtype) {
715                         syslog(LOG_INFO, "Unregistered message function (type %d)\n",
716                                         EventType);
717                         p = cur->next;
718                         if (cur == MessageHookTable) {
719                                 MessageHookTable = p;
720                         }
721                         free(cur);
722                         cur = p;
723                 }
724         }
725 }
726
727 void CtdlDestroyMessageHook(void)
728 {
729         MessageFunctionHook *cur, *p;
730
731         cur = MessageHookTable; 
732         while (cur != NULL)
733         {
734                 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
735                 p = cur->next;
736                 free(cur);
737                 cur = p;
738         }
739         MessageHookTable = NULL;
740 }
741
742
743 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
744 {
745         RoomFunctionHook *newfcn;
746
747         newfcn = (RoomFunctionHook *)
748             malloc(sizeof(RoomFunctionHook));
749         newfcn->next = RoomHookTable;
750         newfcn->fcn_ptr = fcn_ptr;
751         RoomHookTable = newfcn;
752
753         syslog(LOG_INFO, "Registered a new room function\n");
754 }
755
756
757 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
758 {
759         RoomFunctionHook *cur, *p;
760
761         for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
762                 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
763                         syslog(LOG_INFO, "Unregistered room function\n");
764                         p = cur->next;
765                         if (cur == RoomHookTable) {
766                                 RoomHookTable = p;
767                         }
768                         free(cur);
769                         cur = p;
770                 }
771         }
772 }
773
774
775 void CtdlDestroyRoomHooks(void)
776 {
777         RoomFunctionHook *cur, *p;
778
779         cur = RoomHookTable;
780         while (cur != NULL)
781         {
782                 syslog(LOG_INFO, "Destroyed room function\n");
783                 p = cur->next;
784                 free(cur);
785                 cur = p;
786         }
787         RoomHookTable = NULL;
788 }
789
790 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
791 {
792         NetprocFunctionHook *newfcn;
793
794         newfcn = (NetprocFunctionHook *)
795             malloc(sizeof(NetprocFunctionHook));
796         newfcn->next = NetprocHookTable;
797         newfcn->h_function_pointer = handler;
798         NetprocHookTable = newfcn;
799
800         syslog(LOG_INFO, "Registered a new netproc function\n");
801 }
802
803
804 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
805 {
806         NetprocFunctionHook *cur, *p;
807
808         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
809                 /* This will also remove duplicates if any */
810                 while (cur != NULL &&
811                                 handler == cur->h_function_pointer ) {
812                         syslog(LOG_INFO, "Unregistered netproc function\n");
813                         p = cur->next;
814                         if (cur == NetprocHookTable) {
815                                 NetprocHookTable = p;
816                         }
817                         free(cur);
818                         cur = p;
819                 }
820         }
821 }
822
823 void CtdlDestroyNetprocHooks(void)
824 {
825         NetprocFunctionHook *cur, *p;
826
827         cur = NetprocHookTable;
828         while (cur != NULL)
829         {
830                 syslog(LOG_INFO, "Destroyed netproc function\n");
831                 p = cur->next;
832                 free(cur);
833                 cur = p;
834         }
835         NetprocHookTable = NULL;
836 }
837
838
839 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
840 {
841         DeleteFunctionHook *newfcn;
842
843         newfcn = (DeleteFunctionHook *)
844             malloc(sizeof(DeleteFunctionHook));
845         newfcn->next = DeleteHookTable;
846         newfcn->h_function_pointer = handler;
847         DeleteHookTable = newfcn;
848
849         syslog(LOG_INFO, "Registered a new delete function\n");
850 }
851
852
853 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
854 {
855         DeleteFunctionHook *cur, *p;
856
857         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
858                 /* This will also remove duplicates if any */
859                 while (cur != NULL &&
860                                 handler == cur->h_function_pointer ) {
861                         syslog(LOG_INFO, "Unregistered delete function\n");
862                         p = cur->next;
863                         if (cur == DeleteHookTable) {
864                                 DeleteHookTable = p;
865                         }
866                         free(cur);
867                         cur = p;
868                 }
869         }
870 }
871 void CtdlDestroyDeleteHooks(void)
872 {
873         DeleteFunctionHook *cur, *p;
874
875         cur = DeleteHookTable;
876         while (cur != NULL)
877         {
878                 syslog(LOG_INFO, "Destroyed delete function\n");
879                 p = cur->next;
880                 free(cur);
881                 cur = p;                
882         }
883         DeleteHookTable = NULL;
884 }
885
886
887
888
889 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
890 {
891         FixedOutputHook *newfcn;
892
893         newfcn = (FixedOutputHook *)
894             malloc(sizeof(FixedOutputHook));
895         newfcn->next = FixedOutputTable;
896         newfcn->h_function_pointer = handler;
897         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
898         FixedOutputTable = newfcn;
899
900         syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
901 }
902
903
904 void CtdlUnregisterFixedOutputHook(char *content_type)
905 {
906         FixedOutputHook *cur, *p;
907
908         for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
909                 /* This will also remove duplicates if any */
910                 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
911                         syslog(LOG_INFO, "Unregistered fixed output function for %s\n", content_type);
912                         p = cur->next;
913                         if (cur == FixedOutputTable) {
914                                 FixedOutputTable = p;
915                         }
916                         free(cur);
917                         cur = p;
918                 }
919         }
920 }
921
922 void CtdlDestroyFixedOutputHooks(void)
923 {
924         FixedOutputHook *cur, *p;
925
926         cur = FixedOutputTable; 
927         while (cur != NULL)
928         {
929                 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
930                 p = cur->next;
931                 free(cur);
932                 cur = p;
933                 
934         }
935         FixedOutputTable = NULL;
936 }
937
938 /* returns nonzero if we found a hook and used it */
939 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
940 {
941         FixedOutputHook *fcn;
942
943         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
944                 if (!strcasecmp(content_type, fcn->content_type)) {
945                         (*fcn->h_function_pointer) (content, content_length);
946                         return(1);
947                 }
948         }
949         return(0);
950 }
951
952
953
954
955
956 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
957 {
958
959         XmsgFunctionHook *newfcn;
960
961         newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
962         newfcn->next = XmsgHookTable;
963         newfcn->order = order;
964         newfcn->h_function_pointer = fcn_ptr;
965         XmsgHookTable = newfcn;
966         syslog(LOG_INFO, "Registered a new x-msg function (priority %d)\n", order);
967 }
968
969
970 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
971 {
972         XmsgFunctionHook *cur, *p;
973
974         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
975                 /* This will also remove duplicates if any */
976                 while (cur != NULL &&
977                                 fcn_ptr == cur->h_function_pointer &&
978                                 order == cur->order) {
979                         syslog(LOG_INFO, "Unregistered x-msg function "
980                                         "(priority %d)\n", order);
981                         p = cur->next;
982                         if (cur == XmsgHookTable) {
983                                 XmsgHookTable = p;
984                         }
985                         free(cur);
986                         cur = p;
987                 }
988         }
989 }
990
991 void CtdlDestroyXmsgHooks(void)
992 {
993         XmsgFunctionHook *cur, *p;
994
995         cur = XmsgHookTable;
996         while (cur != NULL)
997         {
998                 syslog(LOG_INFO, "Destroyed x-msg function "
999                         "(priority %d)\n", cur->order);
1000                 p = cur->next;
1001                         
1002                 free(cur);
1003                 cur = p;
1004         }
1005         XmsgHookTable = NULL;
1006 }
1007
1008
1009 void CtdlRegisterServiceHook(int tcp_port,
1010                              char *sockpath,
1011                              void (*h_greeting_function) (void),
1012                              void (*h_command_function) (void),
1013                              void (*h_async_function) (void),
1014                              const char *ServiceName)
1015 {
1016         ServiceFunctionHook *newfcn;
1017         char *message;
1018         char error[SIZ];
1019
1020         strcpy(error, "");
1021         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1022         message = (char*) malloc (SIZ + SIZ);
1023         
1024         newfcn->next = ServiceHookTable;
1025         newfcn->tcp_port = tcp_port;
1026         newfcn->sockpath = sockpath;
1027         newfcn->h_greeting_function = h_greeting_function;
1028         newfcn->h_command_function = h_command_function;
1029         newfcn->h_async_function = h_async_function;
1030         newfcn->ServiceName = ServiceName;
1031
1032         if (sockpath != NULL) {
1033                 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1034                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1035         }
1036         else if (tcp_port <= 0) {       /* port -1 to disable */
1037                 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1038                 free (message);
1039                 free(newfcn);
1040                 return;
1041         }
1042         else {
1043                 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1044                                               tcp_port,
1045                                               config.c_maxsessions, 
1046                                               error);
1047                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
1048                          config.c_ip_addr, tcp_port, ServiceName);
1049         }
1050
1051         if (newfcn->msock > 0) {
1052                 ServiceHookTable = newfcn;
1053                 strcat(message, "registered.");
1054                 syslog(LOG_INFO, "%s\n", message);
1055         }
1056         else {
1057                 AddPortError(message, error);
1058                 strcat(message, "FAILED.");
1059                 syslog(LOG_CRIT, "%s\n", message);
1060                 free(newfcn);
1061         }
1062         free(message);
1063 }
1064
1065
1066 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1067                         void (*h_greeting_function) (void),
1068                         void (*h_command_function) (void),
1069                         void (*h_async_function) (void)
1070                         )
1071 {
1072         ServiceFunctionHook *cur, *p;
1073
1074         cur = ServiceHookTable;
1075         while (cur != NULL) {
1076                 /* This will also remove duplicates if any */
1077                 while (cur != NULL &&
1078                                 !(sockpath && cur->sockpath &&
1079                                         strcmp(sockpath, cur->sockpath)) &&
1080                                 h_greeting_function == cur->h_greeting_function &&
1081                                 h_command_function == cur->h_command_function &&
1082                                 h_async_function == cur->h_async_function &&
1083                                 tcp_port == cur->tcp_port) {
1084                         close(cur->msock);
1085                         if (sockpath) {
1086                                 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1087                                                 sockpath);
1088                                 unlink(sockpath);
1089                         } else if (tcp_port) {
1090                                 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1091                         } else {
1092                                 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1093                         }
1094                         p = cur->next;
1095                         if (cur == ServiceHookTable) {
1096                                 ServiceHookTable = p;
1097                         }
1098                         free(cur);
1099                         cur = p;
1100                 }
1101         }
1102 }
1103
1104
1105 void CtdlShutdownServiceHooks(void)
1106 {
1107         /* sort of a duplicate of close_masters() but called earlier */
1108         ServiceFunctionHook *cur;
1109
1110         cur = ServiceHookTable;
1111         while (cur != NULL) 
1112         {
1113                 if (cur->msock != -1)
1114                 {
1115                         close(cur->msock);
1116                         cur->msock = -1;
1117                         if (cur->sockpath != NULL){
1118                                 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1119                                               cur->ServiceName,
1120                                               cur->sockpath);
1121                                 unlink(cur->sockpath);
1122                         } else {
1123                                 syslog(LOG_INFO, "[%s] closing service\n", 
1124                                               cur->ServiceName);
1125                         }
1126                 }
1127                 cur = cur->next;
1128         }
1129 }
1130
1131 void CtdlDestroyServiceHook(void)
1132 {
1133         ServiceFunctionHook *cur, *p;
1134
1135         cur = ServiceHookTable;
1136         while (cur != NULL)
1137         {
1138                 close(cur->msock);
1139                 if (cur->sockpath) {
1140                         syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1141                                 cur->sockpath);
1142                         unlink(cur->sockpath);
1143                 } else if (cur->tcp_port) {
1144                         syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1145                 } else {
1146                         syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1147                 }
1148                 p = cur->next;
1149                 free(cur);
1150                 cur = p;
1151         }
1152         ServiceHookTable = NULL;
1153 }
1154
1155 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1156 {
1157         SearchFunctionHook *newfcn;
1158
1159         if (!name || !fcn_ptr) {
1160                 return;
1161         }
1162         
1163         newfcn = (SearchFunctionHook *)
1164             malloc(sizeof(SearchFunctionHook));
1165         newfcn->next = SearchFunctionHookTable;
1166         newfcn->name = name;
1167         newfcn->fcn_ptr = fcn_ptr;
1168         SearchFunctionHookTable = newfcn;
1169
1170         syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1171 }
1172
1173 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1174 {
1175         SearchFunctionHook *cur, *p;
1176         
1177         for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
1178                 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
1179                         syslog(LOG_INFO, "Unregistered search function(%s)\n", name);
1180                         p = cur->next;
1181                         if (cur == SearchFunctionHookTable) {
1182                                 SearchFunctionHookTable = p;
1183                         }
1184                         free (cur);
1185                         cur = p;
1186                 }
1187         }
1188 }
1189
1190 void CtdlDestroySearchHooks(void)
1191 {
1192         SearchFunctionHook *cur, *p;
1193
1194         cur = SearchFunctionHookTable;
1195         SearchFunctionHookTable = NULL;
1196         while (cur != NULL) {
1197                 p = cur->next;
1198                 free(cur);
1199                 cur = p;
1200         }
1201 }
1202
1203 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1204 {
1205         SearchFunctionHook *fcn = NULL;
1206
1207         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1208                 if (!func_name || !strcmp(func_name, fcn->name)) {
1209                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1210                         return;
1211                 }
1212         }
1213         *num_msgs = 0;
1214 }
1215
1216
1217 void PerformSessionHooks(int EventType)
1218 {
1219         SessionFunctionHook *fcn = NULL;
1220
1221         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1222                 if (fcn->eventtype == EventType) {
1223                         if (EventType == EVT_TIMER) {
1224                                 pthread_setspecific(MyConKey, NULL);    /* for every hook */
1225                         }
1226                         (*fcn->h_function_pointer) ();
1227                 }
1228         }
1229 }
1230
1231 void PerformUserHooks(ctdluser *usbuf, int EventType)
1232 {
1233         UserFunctionHook *fcn = NULL;
1234
1235         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1236                 if (fcn->eventtype == EventType) {
1237                         (*fcn->h_function_pointer) (usbuf);
1238                 }
1239         }
1240 }
1241
1242 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1243 {
1244         MessageFunctionHook *fcn = NULL;
1245         int total_retval = 0;
1246
1247         /* Other code may elect to protect this message from server-side
1248          * handlers; if this is the case, don't do anything.
1249         syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1250          */
1251         if (msg->cm_flags & CM_SKIP_HOOKS) {
1252                 syslog(LOG_DEBUG, "Skipping hooks\n");
1253                 return(0);
1254         }
1255
1256         /* Otherwise, run all the hooks appropriate to this event type.
1257          */
1258         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1259                 if (fcn->eventtype == EventType) {
1260                         total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1261                 }
1262         }
1263
1264         /* Return the sum of the return codes from the hook functions.  If
1265          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1266          * the save operation to abort.
1267          */
1268         return total_retval;
1269 }
1270
1271
1272 int PerformRoomHooks(struct ctdlroom *target_room)
1273 {
1274         RoomFunctionHook *fcn;
1275         int total_retval = 0;
1276
1277         syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1278
1279         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1280                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1281         }
1282
1283         /* Return the sum of the return codes from the hook functions.
1284          */
1285         return total_retval;
1286 }
1287
1288
1289 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1290 {
1291         NetprocFunctionHook *fcn;
1292         int total_retval = 0;
1293
1294         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1295                 total_retval = total_retval +
1296                         (*fcn->h_function_pointer) (msg, target_room);
1297         }
1298
1299         /* Return the sum of the return codes from the hook functions.
1300          * A nonzero return code will cause the message to *not* be imported.
1301          */
1302         return total_retval;
1303 }
1304
1305
1306 void PerformDeleteHooks(char *room, long msgnum)
1307 {
1308         DeleteFunctionHook *fcn;
1309
1310         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1311                 (*fcn->h_function_pointer) (room, msgnum);
1312         }
1313 }
1314
1315
1316
1317
1318
1319 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1320 {
1321         XmsgFunctionHook *fcn;
1322         int total_sent = 0;
1323         int p;
1324
1325         for (p=0; p<MAX_XMSG_PRI; ++p) {
1326                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1327                         if (fcn->order == p) {
1328                                 total_sent +=
1329                                         (*fcn->h_function_pointer)
1330                                                 (sender, sender_email, recp, msg);
1331                         }
1332                 }
1333                 /* Break out of the loop if a higher-priority function
1334                  * successfully delivered the message.  This prevents duplicate
1335                  * deliveries to local users simultaneously signed onto
1336                  * remote services.
1337                  */
1338                 if (total_sent) break;
1339         }
1340         return total_sent;
1341 }
1342
1343
1344 /*
1345  * Dirty hack until we impliment a hook mechanism for this
1346  */
1347 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1348 {
1349 #ifdef HAVE_OPENSSL
1350         CtdlStartTLS (ok_response, nosup_response, error_response);
1351 #endif
1352 }
1353
1354
1355 CTDL_MODULE_INIT(modules)
1356 {
1357         if (!threading) {
1358                 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1359                 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");
1360         }
1361         return "modules";
1362 }