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