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