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