0c4c4dc0fb055c66d4dcff7bbd31f0da0ca3cc41
[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 <libcitadel.h>
19 #include "citadel.h"
20 #include "server.h"
21 #include "serv_extensions.h"
22 #include "sysdep_decls.h"
23 #include "msgbase.h"
24 #include "config.h"
25
26 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
27
28 #include "ctdl_module.h"
29
30 #ifndef HAVE_SNPRINTF
31 #include <stdarg.h>
32 #include "snprintf.h"
33 #endif
34
35 struct CleanupFunctionHook *CleanupHookTable = NULL;
36 struct SessionFunctionHook *SessionHookTable = NULL;
37 struct UserFunctionHook *UserHookTable = NULL;
38 struct XmsgFunctionHook *XmsgHookTable = NULL;
39 struct MessageFunctionHook *MessageHookTable = NULL;
40 struct NetprocFunctionHook *NetprocHookTable = NULL;
41 struct DeleteFunctionHook *DeleteHookTable = NULL;
42 struct ServiceFunctionHook *ServiceHookTable = NULL;
43 struct FixedOutputHook *FixedOutputTable = NULL;
44 struct RoomFunctionHook *RoomHookTable = NULL;
45 struct SearchFunctionHook *SearchFunctionHookTable = NULL;
46
47 struct ProtoFunctionHook {
48         void (*handler) (char *cmdbuf);
49         char *cmd;
50         char *desc;
51         struct ProtoFunctionHook *next;
52 } *ProtoHookList = NULL;
53
54
55 struct DirectoryServiceHook {
56         int (*handler) (char *cn, char *ou, void **object);
57         int cmd;
58         char *module;
59         struct DirectoryServiceHook *next;
60 } *DirectoryServiceHookList = NULL;
61
62 struct DirectoryObject {
63         char *module;
64         void *object;
65         struct DirectoryObject *next;
66 };
67
68 #define ERR_PORT (1 << 1)
69
70
71 static char *portlist = NULL;
72 static size_t nSizPort = 0;
73
74 static char *errormessages = NULL;
75 size_t nSizErrmsg = 0;
76
77
78 long   DetailErrorFlags;
79
80 char *ErrSubject = "Startup Problems";
81 char *ErrGeneral = "Citadel had trouble on starting up. %s This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
82 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
83 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
84 "The errors returned by the system were:\n%s\n"
85 "You can recheck the above if you follow this faq item:\n"
86 "http://www.citadel.org/doku.php/faq:mastering_your_os:net#netstat";
87
88
89 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
90 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
91 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
92 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
93 " and stop the programm, that binds these ports. You should eventually remove "
94 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
95 " After that goto Administration -> Shutdown Citadel to make Citadel retry to bind this port.\n";
96
97
98 void LogPrintMessages(long err)
99 {
100         char *List, *DetailList, *Short, *Where, *Hint, *Message; 
101         int n = nSizPort + nSizErrmsg + 5;
102
103         Message = (char*) malloc(n * SIZ);
104         
105         DetailErrorFlags = DetailErrorFlags & ~err;
106
107         switch (err)
108         {
109         case ERR_PORT:
110                 Short = ErrPortShort;
111                 Where = ErrPortWhere;
112                 Hint  = ErrPortHint;
113                 List  = portlist;
114                 DetailList = errormessages;
115                 break;
116         default:
117                 Short = "";
118                 Where = "";
119                 Hint  = "";
120                 List  = "";
121                 DetailList = "";
122         }
123
124
125         snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
126
127         CtdlLogPrintf(0,Message);
128         CtdlLogPrintf(0,ErrSubject);
129         quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
130         if (errormessages!=NULL) free (errormessages);
131         errormessages = NULL;
132         if (portlist!=NULL) free (portlist);
133         portlist = NULL;
134         free(Message);
135 }
136
137
138
139 void AppendString(char **target, char *append, size_t *len, size_t rate)
140 {
141         size_t oLen = 0;
142         long AddLen;
143         long RelPtr = 0;
144
145         AddLen = strlen(append);
146
147         if (*len == 0)
148         {
149                 *len = rate;
150
151                 *target = (char*)malloc (*len * SIZ);
152         }
153         else 
154         {
155                 oLen = strlen(*target);
156                 RelPtr = strlen(*target);
157                 if (oLen + AddLen + 2 > *len * SIZ)
158                 {
159                         char *Buff = *target;
160                         size_t NewSiz = *len + 10;
161                         *target = malloc (NewSiz * SIZ);
162                         memcpy (*target, Buff, NewSiz * SIZ);
163                         *len = NewSiz;
164                 }
165         }
166         memcpy (*target + oLen, append, AddLen);
167         (*target)[oLen + AddLen + 1] = '\n';
168         (*target)[oLen + AddLen + 2] = '\0';
169 }
170
171 void AddPortError(char *Port, char *ErrorMessage)
172 {
173         char *pos;
174         long len;
175
176         DetailErrorFlags |= ERR_PORT;
177
178         AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
179         AppendString(&portlist, Port, &nSizPort, 2);
180
181         pos = strchr (portlist, ':');
182         if (pos != NULL) *pos = ';';
183         
184         len = strlen (errormessages);
185         if (nSizErrmsg * SIZ > len + 3)
186         {
187                 errormessages[len] = ';';
188                 errormessages[len+1] = ' ';
189                 errormessages[len+2] = '\0';
190         }       
191 }
192
193
194 int DLoader_Exec_Cmd(char *cmdbuf)
195 {
196         struct ProtoFunctionHook *p;
197
198         for (p = ProtoHookList; p; p = p->next) {
199                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
200                         p->handler(&cmdbuf[5]);
201                         return 1;
202                 }
203         }
204         return 0;
205 }
206
207
208 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
209 {
210         struct ProtoFunctionHook *p;
211
212         p = (struct ProtoFunctionHook *)
213                 malloc(sizeof(struct ProtoFunctionHook));
214
215         if (p == NULL) {
216                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
217                 exit(EXIT_FAILURE);
218         }
219         p->handler = handler;
220         p->cmd = cmd;
221         p->desc = desc;
222         p->next = ProtoHookList;
223         ProtoHookList = p;
224         CtdlLogPrintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
225 }
226
227
228 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
229 {
230         struct ProtoFunctionHook *cur = NULL;
231         struct ProtoFunctionHook *p = NULL;
232         struct ProtoFunctionHook *lastcur = NULL;
233
234         for (cur = ProtoHookList; 
235              cur != NULL; 
236              cur = (cur != NULL)? cur->next: NULL) {
237                 /* This will also remove duplicates if any */
238                 while (cur != NULL &&
239                                 handler == cur->handler &&
240                                 !strcmp(cmd, cur->cmd)) {
241                         CtdlLogPrintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
242                                         cmd, cur->desc);
243                         p = cur->next;
244                         if (cur == ProtoHookList) {
245                                 ProtoHookList = p;
246                         }
247                         else if (lastcur != NULL)
248                         {
249                                 lastcur->next = p;
250                         }
251                         free(cur);
252                         cur = p;
253                 }
254                 lastcur = cur;
255         }
256 }
257
258 void CtdlDestroyProtoHooks(void)
259 {
260         struct ProtoFunctionHook *cur, *p;
261
262         cur = ProtoHookList; 
263         while (cur != NULL)
264         {
265                 CtdlLogPrintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
266                         cur->cmd, cur->desc);
267                 p = cur->next;
268                 free(cur);
269                 cur = p;
270         }
271         ProtoHookList = NULL;
272 }
273
274
275 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
276 {
277
278         struct CleanupFunctionHook *newfcn;
279
280         newfcn = (struct CleanupFunctionHook *)
281             malloc(sizeof(struct CleanupFunctionHook));
282         newfcn->next = CleanupHookTable;
283         newfcn->h_function_pointer = fcn_ptr;
284         CleanupHookTable = newfcn;
285
286         CtdlLogPrintf(CTDL_INFO, "Registered a new cleanup function\n");
287 }
288
289
290 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
291 {
292         struct CleanupFunctionHook *cur, *p;
293
294         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
295                 /* This will also remove duplicates if any */
296                 while (cur != NULL &&
297                                 fcn_ptr == cur->h_function_pointer) {
298                         CtdlLogPrintf(CTDL_INFO, "Unregistered cleanup function\n");
299                         p = cur->next;
300                         if (cur == CleanupHookTable) {
301                                 CleanupHookTable = p;
302                         }
303                         free(cur);
304                         cur = p;
305                 }
306         }
307 }
308
309 void CtdlDestroyCleanupHooks(void)
310 {
311         struct CleanupFunctionHook *cur, *p;
312
313         cur = CleanupHookTable;
314         while (cur != NULL)
315         {
316                 CtdlLogPrintf(CTDL_INFO, "Destroyed cleanup function\n");
317                 p = cur->next;
318                 free(cur);
319                 cur = p;
320         }
321         CleanupHookTable = NULL;
322 }
323
324
325 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
326 {
327
328         struct SessionFunctionHook *newfcn;
329
330         newfcn = (struct SessionFunctionHook *)
331             malloc(sizeof(struct SessionFunctionHook));
332         newfcn->next = SessionHookTable;
333         newfcn->h_function_pointer = fcn_ptr;
334         newfcn->eventtype = EventType;
335         SessionHookTable = newfcn;
336
337         CtdlLogPrintf(CTDL_INFO, "Registered a new session function (type %d)\n",
338                 EventType);
339 }
340
341
342 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
343 {
344         struct SessionFunctionHook *cur, *p;
345
346         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
347                 /* This will also remove duplicates if any */
348                 while (cur != NULL &&
349                                 fcn_ptr == cur->h_function_pointer &&
350                                 EventType == cur->eventtype) {
351                         CtdlLogPrintf(CTDL_INFO, "Unregistered session function (type %d)\n",
352                                         EventType);
353                         p = cur->next;
354                         if (cur == SessionHookTable) {
355                                 SessionHookTable = p;
356                         }
357                         free(cur);
358                         cur = p;
359                 }
360         }
361 }
362
363 void CtdlDestroySessionHooks(void)
364 {
365         struct SessionFunctionHook *cur, *p;
366
367         cur = SessionHookTable;
368         while (cur != NULL)
369         {
370                 CtdlLogPrintf(CTDL_INFO, "Destroyed session function\n");
371                 p = cur->next;
372                 free(cur);
373                 cur = p;
374         }
375         SessionHookTable = NULL;
376 }
377
378
379 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
380 {
381
382         struct UserFunctionHook *newfcn;
383
384         newfcn = (struct UserFunctionHook *)
385             malloc(sizeof(struct UserFunctionHook));
386         newfcn->next = UserHookTable;
387         newfcn->h_function_pointer = fcn_ptr;
388         newfcn->eventtype = EventType;
389         UserHookTable = newfcn;
390
391         CtdlLogPrintf(CTDL_INFO, "Registered a new user function (type %d)\n",
392                 EventType);
393 }
394
395
396 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
397 {
398         struct UserFunctionHook *cur, *p;
399
400         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
401                 /* This will also remove duplicates if any */
402                 while (cur != NULL &&
403                                 fcn_ptr == cur->h_function_pointer &&
404                                 EventType == cur->eventtype) {
405                         CtdlLogPrintf(CTDL_INFO, "Unregistered user function (type %d)\n",
406                                         EventType);
407                         p = cur->next;
408                         if (cur == UserHookTable) {
409                                 UserHookTable = p;
410                         }
411                         free(cur);
412                         cur = p;
413                 }
414         }
415 }
416
417 void CtdlDestroyUserHooks(void)
418 {
419         struct UserFunctionHook *cur, *p;
420
421         cur = UserHookTable;
422         while (cur != NULL)
423         {
424                 CtdlLogPrintf(CTDL_INFO, "Destroyed user function \n");
425                 p = cur->next;
426                 free(cur);
427                 cur = p;
428         }
429         UserHookTable = NULL;
430 }
431
432
433 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
434                                 int EventType)
435 {
436
437         struct MessageFunctionHook *newfcn;
438
439         newfcn = (struct MessageFunctionHook *)
440             malloc(sizeof(struct MessageFunctionHook));
441         newfcn->next = MessageHookTable;
442         newfcn->h_function_pointer = handler;
443         newfcn->eventtype = EventType;
444         MessageHookTable = newfcn;
445
446         CtdlLogPrintf(CTDL_INFO, "Registered a new message function (type %d)\n",
447                 EventType);
448 }
449
450
451 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
452                 int EventType)
453 {
454         struct MessageFunctionHook *cur, *p;
455
456         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
457                 /* This will also remove duplicates if any */
458                 while (cur != NULL &&
459                                 handler == cur->h_function_pointer &&
460                                 EventType == cur->eventtype) {
461                         CtdlLogPrintf(CTDL_INFO, "Unregistered message function (type %d)\n",
462                                         EventType);
463                         p = cur->next;
464                         if (cur == MessageHookTable) {
465                                 MessageHookTable = p;
466                         }
467                         free(cur);
468                         cur = p;
469                 }
470         }
471 }
472
473 void CtdlDestroyMessageHook(void)
474 {
475         struct MessageFunctionHook *cur, *p;
476
477         cur = MessageHookTable; 
478         while (cur != NULL)
479         {
480                 CtdlLogPrintf(CTDL_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
481                 p = cur->next;
482                 free(cur);
483                 cur = p;
484         }
485         MessageHookTable = NULL;
486 }
487
488
489 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
490 {
491         struct RoomFunctionHook *newfcn;
492
493         newfcn = (struct RoomFunctionHook *)
494             malloc(sizeof(struct RoomFunctionHook));
495         newfcn->next = RoomHookTable;
496         newfcn->fcn_ptr = fcn_ptr;
497         RoomHookTable = newfcn;
498
499         CtdlLogPrintf(CTDL_INFO, "Registered a new room function\n");
500 }
501
502
503 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
504 {
505         struct RoomFunctionHook *cur, *p;
506
507         for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
508                 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
509                         CtdlLogPrintf(CTDL_INFO, "Unregistered room function\n");
510                         p = cur->next;
511                         if (cur == RoomHookTable) {
512                                 RoomHookTable = p;
513                         }
514                         free(cur);
515                         cur = p;
516                 }
517         }
518 }
519
520
521 void CtdlDestroyRoomHooks(void)
522 {
523         struct RoomFunctionHook *cur, *p;
524
525         cur = RoomHookTable;
526         while (cur != NULL)
527         {
528                 CtdlLogPrintf(CTDL_INFO, "Destroyed room function\n");
529                 p = cur->next;
530                 free(cur);
531                 cur = p;
532         }
533         RoomHookTable = NULL;
534 }
535
536 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
537 {
538         struct NetprocFunctionHook *newfcn;
539
540         newfcn = (struct NetprocFunctionHook *)
541             malloc(sizeof(struct NetprocFunctionHook));
542         newfcn->next = NetprocHookTable;
543         newfcn->h_function_pointer = handler;
544         NetprocHookTable = newfcn;
545
546         CtdlLogPrintf(CTDL_INFO, "Registered a new netproc function\n");
547 }
548
549
550 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
551 {
552         struct NetprocFunctionHook *cur, *p;
553
554         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
555                 /* This will also remove duplicates if any */
556                 while (cur != NULL &&
557                                 handler == cur->h_function_pointer ) {
558                         CtdlLogPrintf(CTDL_INFO, "Unregistered netproc function\n");
559                         p = cur->next;
560                         if (cur == NetprocHookTable) {
561                                 NetprocHookTable = p;
562                         }
563                         free(cur);
564                         cur = p;
565                 }
566         }
567 }
568
569 void CtdlDestroyNetprocHooks(void)
570 {
571         struct NetprocFunctionHook *cur, *p;
572
573         cur = NetprocHookTable;
574         while (cur != NULL)
575         {
576                 CtdlLogPrintf(CTDL_INFO, "Destroyed netproc function\n");
577                 p = cur->next;
578                 free(cur);
579                 cur = p;
580         }
581         NetprocHookTable = NULL;
582 }
583
584
585 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
586 {
587         struct DeleteFunctionHook *newfcn;
588
589         newfcn = (struct DeleteFunctionHook *)
590             malloc(sizeof(struct DeleteFunctionHook));
591         newfcn->next = DeleteHookTable;
592         newfcn->h_function_pointer = handler;
593         DeleteHookTable = newfcn;
594
595         CtdlLogPrintf(CTDL_INFO, "Registered a new delete function\n");
596 }
597
598
599 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
600 {
601         struct DeleteFunctionHook *cur, *p;
602
603         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
604                 /* This will also remove duplicates if any */
605                 while (cur != NULL &&
606                                 handler == cur->h_function_pointer ) {
607                         CtdlLogPrintf(CTDL_INFO, "Unregistered delete function\n");
608                         p = cur->next;
609                         if (cur == DeleteHookTable) {
610                                 DeleteHookTable = p;
611                         }
612                         free(cur);
613                         cur = p;
614                 }
615         }
616 }
617 void CtdlDestroyDeleteHooks(void)
618 {
619         struct DeleteFunctionHook *cur, *p;
620
621         cur = DeleteHookTable;
622         while (cur != NULL)
623         {
624                 CtdlLogPrintf(CTDL_INFO, "Destroyed delete function\n");
625                 p = cur->next;
626                 free(cur);
627                 cur = p;                
628         }
629         DeleteHookTable = NULL;
630 }
631
632
633
634
635 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
636 {
637         struct FixedOutputHook *newfcn;
638
639         newfcn = (struct FixedOutputHook *)
640             malloc(sizeof(struct FixedOutputHook));
641         newfcn->next = FixedOutputTable;
642         newfcn->h_function_pointer = handler;
643         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
644         FixedOutputTable = newfcn;
645
646         CtdlLogPrintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
647 }
648
649
650 void CtdlUnregisterFixedOutputHook(char *content_type)
651 {
652         struct FixedOutputHook *cur, *p;
653
654         for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
655                 /* This will also remove duplicates if any */
656                 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
657                         CtdlLogPrintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
658                         p = cur->next;
659                         if (cur == FixedOutputTable) {
660                                 FixedOutputTable = p;
661                         }
662                         free(cur);
663                         cur = p;
664                 }
665         }
666 }
667
668 void CtdlDestroyFixedOutputHooks(void)
669 {
670         struct FixedOutputHook *cur, *p;
671
672         cur = FixedOutputTable; 
673         while (cur != NULL)
674         {
675                 CtdlLogPrintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
676                 p = cur->next;
677                 free(cur);
678                 cur = p;
679                 
680         }
681         FixedOutputTable = NULL;
682 }
683
684 /* returns nonzero if we found a hook and used it */
685 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
686 {
687         struct FixedOutputHook *fcn;
688
689         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
690                 if (!strcasecmp(content_type, fcn->content_type)) {
691                         (*fcn->h_function_pointer) (content, content_length);
692                         return(1);
693                 }
694         }
695         return(0);
696 }
697
698
699
700
701
702 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
703 {
704
705         struct XmsgFunctionHook *newfcn;
706
707         newfcn = (struct XmsgFunctionHook *) malloc(sizeof(struct XmsgFunctionHook));
708         newfcn->next = XmsgHookTable;
709         newfcn->order = order;
710         newfcn->h_function_pointer = fcn_ptr;
711         XmsgHookTable = newfcn;
712         CtdlLogPrintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
713 }
714
715
716 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
717 {
718         struct XmsgFunctionHook *cur, *p;
719
720         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
721                 /* This will also remove duplicates if any */
722                 while (cur != NULL &&
723                                 fcn_ptr == cur->h_function_pointer &&
724                                 order == cur->order) {
725                         CtdlLogPrintf(CTDL_INFO, "Unregistered x-msg function "
726                                         "(priority %d)\n", order);
727                         p = cur->next;
728                         if (cur == XmsgHookTable) {
729                                 XmsgHookTable = p;
730                         }
731                         free(cur);
732                         cur = p;
733                 }
734         }
735 }
736
737 void CtdlDestroyXmsgHooks(void)
738 {
739         struct XmsgFunctionHook *cur, *p;
740
741         cur = XmsgHookTable;
742         while (cur != NULL)
743         {
744                 CtdlLogPrintf(CTDL_INFO, "Destroyed x-msg function "
745                         "(priority %d)\n", cur->order);
746                 p = cur->next;
747                         
748                 free(cur);
749                 cur = p;
750         }
751         XmsgHookTable = NULL;
752 }
753
754
755 void CtdlRegisterServiceHook(int tcp_port,
756                              char *sockpath,
757                              void (*h_greeting_function) (void),
758                              void (*h_command_function) (void),
759                              void (*h_async_function) (void),
760                              const char *ServiceName)
761 {
762         struct ServiceFunctionHook *newfcn;
763         char *message;
764         char *error;
765
766         error = NULL;
767         newfcn = (struct ServiceFunctionHook *)
768             malloc(sizeof(struct ServiceFunctionHook));
769         message = (char*) malloc (SIZ);
770         
771         newfcn->next = ServiceHookTable;
772         newfcn->tcp_port = tcp_port;
773         newfcn->sockpath = sockpath;
774         newfcn->h_greeting_function = h_greeting_function;
775         newfcn->h_command_function = h_command_function;
776         newfcn->h_async_function = h_async_function;
777         newfcn->ServiceName = ServiceName;
778
779         if (sockpath != NULL) {
780                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
781                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
782         }
783         else if (tcp_port <= 0) {       /* port -1 to disable */
784                 CtdlLogPrintf(CTDL_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
785                 free (message);
786                 free(newfcn);
787                 return;
788         }
789         else {
790                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
791                                               tcp_port,
792                                               config.c_maxsessions, 
793                                               &error);
794                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
795                          config.c_ip_addr, tcp_port, ServiceName);
796         }
797
798         if (newfcn->msock > 0) {
799                 ServiceHookTable = newfcn;
800                 strcat(message, "registered.");
801                 CtdlLogPrintf(CTDL_INFO, "%s\n", message);
802         }
803         else {
804                 AddPortError(message, error);
805                 strcat(message, "FAILED.");
806                 CtdlLogPrintf(CTDL_CRIT, "%s\n", message);
807                 free(error);
808                 free(newfcn);
809         }
810         free(message);
811 }
812
813
814 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
815                         void (*h_greeting_function) (void),
816                         void (*h_command_function) (void),
817                         void (*h_async_function) (void)
818                         )
819 {
820         struct ServiceFunctionHook *cur, *p;
821
822         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
823                 /* This will also remove duplicates if any */
824                 while (cur != NULL &&
825                                 !(sockpath && cur->sockpath &&
826                                         strcmp(sockpath, cur->sockpath)) &&
827                                 h_greeting_function == cur->h_greeting_function &&
828                                 h_command_function == cur->h_command_function &&
829                                 h_async_function == cur->h_async_function &&
830                                 tcp_port == cur->tcp_port) {
831                         close(cur->msock);
832                         if (sockpath) {
833                                 CtdlLogPrintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
834                                                 sockpath);
835                         } else if (tcp_port) {
836                                 CtdlLogPrintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
837                         } else {
838                                 CtdlLogPrintf(CTDL_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
839                         }
840                         p = cur->next;
841                         if (cur == ServiceHookTable) {
842                                 ServiceHookTable = p;
843                         }
844                         free(cur);
845                         cur = p;
846                 }
847         }
848 }
849
850 void CtdlDestroyServiceHook(void)
851 {
852         struct ServiceFunctionHook *cur, *p;
853
854         cur = ServiceHookTable;
855         while (cur != NULL)
856         {
857                 close(cur->msock);
858                 if (cur->sockpath) {
859                         CtdlLogPrintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
860                                 cur->sockpath);
861                 } else if (cur->tcp_port) {
862                         CtdlLogPrintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
863                 } else {
864                         CtdlLogPrintf(CTDL_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
865                 }
866                 p = cur->next;
867                 free(cur);
868                 cur = p;
869         }
870         ServiceHookTable = NULL;
871 }
872
873 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, char *), char *name)
874 {
875         struct SearchFunctionHook *newfcn;
876
877         if (!name || !fcn_ptr) {
878                 return;
879         }
880         
881         newfcn = (struct SearchFunctionHook *)
882             malloc(sizeof(struct SearchFunctionHook));
883         newfcn->next = SearchFunctionHookTable;
884         newfcn->name = name;
885         newfcn->fcn_ptr = fcn_ptr;
886         SearchFunctionHookTable = newfcn;
887
888         CtdlLogPrintf(CTDL_INFO, "Registered a new search function (%s)\n", name);
889 }
890
891 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, char *), char *name)
892 {
893         struct SearchFunctionHook *cur, *p;
894         
895         for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
896                 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
897                         CtdlLogPrintf(CTDL_INFO, "Unregistered search function(%s)\n", name);
898                         p = cur->next;
899                         if (cur == SearchFunctionHookTable) {
900                                 SearchFunctionHookTable = p;
901                         }
902                         free (cur);
903                         cur = p;
904                 }
905         }
906 }
907
908 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, char *search_string, char *func_name)
909 {
910         struct SearchFunctionHook *fcn = NULL;
911
912         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
913                 if (!func_name || !strcmp(func_name, fcn->name)) {
914                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
915                         return;
916                 }
917         }
918         *num_msgs = 0;
919 }
920
921
922 void PerformSessionHooks(int EventType)
923 {
924         struct SessionFunctionHook *fcn = NULL;
925
926         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
927                 if (fcn->eventtype == EventType) {
928                         (*fcn->h_function_pointer) ();
929                 }
930         }
931 }
932
933 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
934 {
935         struct UserFunctionHook *fcn = NULL;
936
937         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
938                 if (fcn->eventtype == EventType) {
939                         (*fcn->h_function_pointer) (usbuf);
940                 }
941         }
942 }
943
944 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
945 {
946         struct MessageFunctionHook *fcn = NULL;
947         int total_retval = 0;
948
949         /* Other code may elect to protect this message from server-side
950          * handlers; if this is the case, don't do anything.
951         CtdlLogPrintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
952                 EventType, msg->cm_flags);
953          */
954         if (msg->cm_flags & CM_SKIP_HOOKS) {
955                 CtdlLogPrintf(CTDL_DEBUG, "Skipping hooks\n");
956                 return(0);
957         }
958
959         /* Otherwise, run all the hooks appropriate to this event type.
960          */
961         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
962                 if (fcn->eventtype == EventType) {
963                         total_retval = total_retval +
964                                 (*fcn->h_function_pointer) (msg);
965                 }
966         }
967
968         /* Return the sum of the return codes from the hook functions.  If
969          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
970          * the save operation to abort.
971          */
972         return total_retval;
973 }
974
975
976 int PerformRoomHooks(struct ctdlroom *target_room)
977 {
978         struct RoomFunctionHook *fcn;
979         int total_retval = 0;
980
981         CtdlLogPrintf(CTDL_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
982
983         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
984                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
985         }
986
987         /* Return the sum of the return codes from the hook functions.
988          */
989         return total_retval;
990 }
991
992
993 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
994 {
995         struct NetprocFunctionHook *fcn;
996         int total_retval = 0;
997
998         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
999                 total_retval = total_retval +
1000                         (*fcn->h_function_pointer) (msg, target_room);
1001         }
1002
1003         /* Return the sum of the return codes from the hook functions.
1004          * A nonzero return code will cause the message to *not* be imported.
1005          */
1006         return total_retval;
1007 }
1008
1009
1010 void PerformDeleteHooks(char *room, long msgnum)
1011 {
1012         struct DeleteFunctionHook *fcn;
1013
1014         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1015                 (*fcn->h_function_pointer) (room, msgnum);
1016         }
1017 }
1018
1019
1020
1021
1022
1023 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1024 {
1025         struct XmsgFunctionHook *fcn;
1026         int total_sent = 0;
1027         int p;
1028
1029         for (p=0; p<MAX_XMSG_PRI; ++p) {
1030                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1031                         if (fcn->order == p) {
1032                                 total_sent +=
1033                                         (*fcn->h_function_pointer)
1034                                                 (sender, sender_email, recp, msg);
1035                         }
1036                 }
1037                 /* Break out of the loop if a higher-priority function
1038                  * successfully delivered the message.  This prevents duplicate
1039                  * deliveries to local users simultaneously signed onto
1040                  * remote services.
1041                  */
1042                 if (total_sent) break;
1043         }
1044         return total_sent;
1045 }
1046
1047
1048
1049
1050 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module)
1051 {
1052         struct DirectoryServiceHook *newfcn;
1053         
1054         newfcn = DirectoryServiceHookList;
1055         while (newfcn)
1056         {
1057                 if (newfcn->cmd == cmd && !strcmp(newfcn->module, module))
1058                 {
1059                         CtdlLogPrintf(CTDL_ERR, "Directory service function already handled by module %s\n", module);
1060                         return -1;
1061                 }
1062                 newfcn = newfcn->next;
1063         }
1064         
1065         newfcn = (struct DirectoryServiceHook *) malloc (sizeof(struct DirectoryServiceHook));
1066         newfcn->handler = func;
1067         newfcn->cmd = cmd;
1068         newfcn->module = module;
1069         newfcn->next = DirectoryServiceHookList;
1070         DirectoryServiceHookList = newfcn;
1071         
1072         CtdlLogPrintf(CTDL_INFO, "Registered a new directory service function from module %s\n", module);
1073         return 0;
1074 }
1075
1076 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd)
1077 {
1078         struct DirectoryServiceHook *curfcn;
1079         struct DirectoryObject *our_object_list = NULL;
1080         struct DirectoryObject *newobject = NULL;
1081         struct DirectoryObject *oldobject = NULL;
1082         
1083         
1084         curfcn = DirectoryServiceHookList;
1085         if (object)
1086                 our_object_list = (struct DirectoryObject *) *object;
1087         
1088         while (curfcn)
1089         {
1090                 if (curfcn->cmd == cmd)
1091                 {
1092                         if (!module)
1093                         {
1094                                 if (cmd == DIRECTORY_CREATE_OBJECT)
1095                                 {
1096                                         newobject = (struct DirectoryObject*) malloc (sizeof(struct DirectoryObject));
1097                                         newobject->module = curfcn->module;
1098                                         newobject->object = NULL;
1099                                         newobject->next = our_object_list;
1100                                         our_object_list = newobject;
1101                                 }
1102                                 if (our_object_list)
1103                                 {
1104                                         for(newobject = our_object_list; newobject; newobject=newobject->next)
1105                                         {
1106                                                 if (!strcmp(newobject->module, curfcn->module))
1107                                                         (void) curfcn->handler(cn, ou, &newobject->object);
1108                                         }
1109                                 }
1110                                 else
1111                                         (void) curfcn->handler(cn, ou, NULL);
1112
1113                                 continue;
1114                         }
1115                         else 
1116                         {
1117                                 if(!strcmp(curfcn->module, module))
1118                                 {
1119                                         if (cmd == DIRECTORY_CREATE_OBJECT)
1120                                         {
1121                                                 newobject = (struct DirectoryObject*) malloc (sizeof(struct DirectoryObject));
1122                                                 newobject->module = module;
1123                                                 newobject->object = NULL;
1124                                                 newobject->next = our_object_list;
1125                                                 our_object_list = newobject;
1126                                         }
1127                                         if (our_object_list)
1128                                         {
1129                                                 for(newobject = our_object_list; newobject; newobject=newobject->next)
1130                                                 {
1131                                                         if (!strcmp(newobject->module, curfcn->module))
1132                                                                 (void) curfcn->handler(cn, ou, &newobject->object);
1133                                                 }
1134                                         }
1135                                         else
1136                                                 (void) (curfcn->handler(cn, ou, NULL));
1137
1138                                         break;
1139                                 }
1140                         }
1141                 }
1142                 curfcn=curfcn->next;
1143         }
1144         if (our_object_list)
1145         {
1146                 *object = our_object_list;
1147                 if (cmd == DIRECTORY_FREE_OBJECT)
1148                 {       // The objects pointed to by the list should have been freed by the module that created it
1149                         for(newobject = our_object_list; newobject; )
1150                         {
1151                                 oldobject=newobject;
1152                                 newobject=newobject->next;
1153                                 free(oldobject);
1154                         }
1155                         *object=NULL;
1156                 }
1157         }
1158         return 0;
1159 }
1160
1161 void CtdlDestroyDirectoryServiceFuncs(void)
1162 {
1163         struct DirectoryServiceHook *cur, *next;
1164         
1165         cur = DirectoryServiceHookList;
1166         DirectoryServiceHookList = NULL;
1167         while (cur)
1168         {
1169                 next = cur->next;
1170                 CtdlLogPrintf(CTDL_INFO, "Destroyed directory service function for module %s.\n", cur->module);
1171                 free(cur);
1172                 cur = next;
1173         }
1174 }
1175
1176 /*
1177  * Dirty hack until we impliment a hook mechanism for this
1178  */
1179 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1180 {
1181 #ifdef HAVE_OPENSSL
1182         CtdlStartTLS (ok_response, nosup_response, error_response);
1183 #endif
1184 }