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