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