* double free
[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 "citadel.h"
19 #include "server.h"
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
22 #include "msgbase.h"
23 #include "tools.h"
24 #include "config.h"
25
26 #ifndef HAVE_SNPRINTF
27 #include <stdarg.h>
28 #include "snprintf.h"
29 #endif
30
31 struct CleanupFunctionHook *CleanupHookTable = NULL;
32 struct SessionFunctionHook *SessionHookTable = NULL;
33 struct UserFunctionHook *UserHookTable = NULL;
34 struct XmsgFunctionHook *XmsgHookTable = NULL;
35 struct MessageFunctionHook *MessageHookTable = NULL;
36 struct NetprocFunctionHook *NetprocHookTable = NULL;
37 struct DeleteFunctionHook *DeleteHookTable = NULL;
38 struct ServiceFunctionHook *ServiceHookTable = NULL;
39 struct FixedOutputHook *FixedOutputTable = NULL;
40
41 struct ProtoFunctionHook {
42         void (*handler) (char *cmdbuf);
43         char *cmd;
44         char *desc;
45         struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
47
48
49 #define ERR_PORT (1 << 1)
50
51
52 static char *portlist = NULL;
53 static size_t nSizPort = 0;
54
55 static char *errormessages = NULL;
56 static size_t nSizErrmsg = 0;
57
58
59 static long   DetailErrorFlags;
60
61
62 char *ErrSubject = "Startup Problems";
63 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"
64 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
65 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
66 "The errors returned by the system were:\n%s\n";
67
68
69 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
70 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
71 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
72 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
73 " and stop the programm, that binds these ports. You should eventually remove "
74 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
75
76
77 void LogPrintMessages(long err)
78 {
79         char *List, *DetailList, *Short, *Where, *Hint, *Message; 
80         int n = nSizPort + nSizErrmsg + 5;
81
82         Message = (char*) malloc(n * SIZ);
83
84         switch (err)
85         {
86         case ERR_PORT:
87                 Short = ErrPortShort;
88                 Where = ErrPortWhere;
89                 Hint  = ErrPortHint;
90                 List  = portlist;
91                 DetailList = errormessages;
92                 break;
93         default:
94                 Short = "";
95                 Where = "";
96                 Hint  = "";
97                 List  = "";
98                 DetailList = "";
99         }
100
101
102         snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
103
104         quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
105         free(Message);
106 }
107
108
109
110 void AppendString(char **target, char *append, size_t *len, size_t rate)
111 {
112         size_t oLen = 0;
113         long AddLen;
114         long RelPtr = 0;
115
116         AddLen = strlen(append);
117
118         if (*len == 0)
119         {
120                 *len = rate;
121
122                 *target = (char*)malloc (*len * SIZ);
123         }
124         else 
125         {
126                 oLen = strlen(*target);
127                 RelPtr = strlen(*target);
128                 if (oLen + AddLen + 2 > *len * SIZ)
129                 {
130                         char *Buff = *target;
131                         size_t NewSiz = *len + 10;
132                         *target = malloc (NewSiz * SIZ);
133                         memcpy (*target, Buff, NewSiz * SIZ);
134                         *len = NewSiz;
135                 }
136         }
137         memcpy (*target + oLen, append, AddLen);
138         (*target)[AddLen + 1] = '\n';
139         (*target)[AddLen + 2] = '\0';
140 }
141
142 void AddPortError(char *Port, char *ErrorMessage)
143 {
144         DetailErrorFlags |= ERR_PORT;
145
146         AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
147         AppendString(&portlist, Port, &nSizPort, 2);
148 }
149
150
151 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
152 {
153         struct ProtoFunctionHook *p;
154
155         p = (struct ProtoFunctionHook *)
156                 malloc(sizeof(struct ProtoFunctionHook));
157
158         if (p == NULL) {
159                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
160                 exit(EXIT_FAILURE);
161         }
162         p->handler = handler;
163         p->cmd = cmd;
164         p->desc = desc;
165         p->next = ProtoHookList;
166         ProtoHookList = p;
167         lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
168 }
169
170
171 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
172 {
173         struct ProtoFunctionHook *cur = NULL;
174         struct ProtoFunctionHook *p = NULL;
175         struct ProtoFunctionHook *lastcur = NULL;
176
177         for (cur = ProtoHookList; 
178              cur != NULL; 
179              cur = (cur != NULL)? cur->next: NULL) {
180                 /* This will also remove duplicates if any */
181                 while (cur != NULL &&
182                                 handler == cur->handler &&
183                                 !strcmp(cmd, cur->cmd)) {
184                         lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
185                                         cmd, cur->desc);
186                         p = cur->next;
187                         if (cur == ProtoHookList) {
188                                 ProtoHookList = p;
189                         }
190                         else if (lastcur != NULL)
191                         {
192                                 lastcur->next = p;
193                         }
194                         free(cur);
195                         cur = p;
196                 }
197                 lastcur = cur;
198         }
199 }
200
201 void CtdlDestroyProtoHooks(void)
202 {
203         struct ProtoFunctionHook *cur, *p;
204
205         cur = ProtoHookList; 
206         while (cur != NULL)
207         {
208                 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
209                         cur->cmd, cur->desc);
210                 p = cur->next;
211                 free(cur);
212                 cur = p;
213         }
214         ProtoHookList = NULL;
215 }
216
217
218 int DLoader_Exec_Cmd(char *cmdbuf)
219 {
220         struct ProtoFunctionHook *p;
221
222         for (p = ProtoHookList; p; p = p->next) {
223                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
224                         p->handler(&cmdbuf[5]);
225                         return 1;
226                 }
227         }
228         return 0;
229 }
230
231 void initialize_server_extensions(void)
232 {
233         long filter;
234
235         lprintf(CTDL_INFO, "%s\n", serv_bio_init());
236         lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
237         lprintf(CTDL_INFO, "%s\n", serv_notes_init());
238         lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
239         lprintf(CTDL_INFO, "%s\n", serv_chat_init());
240         lprintf(CTDL_INFO, "%s\n", serv_expire_init());
241         lprintf(CTDL_INFO, "%s\n", serv_imap_init());
242         lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
243         lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
244         lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
245         lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
246         lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
247         lprintf(CTDL_INFO, "%s\n", serv_network_init());
248         lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
249         lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
250         lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
251         lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
252         lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
253         lprintf(CTDL_INFO, "%s\n", serv_spam_init());
254         /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
255         lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
256         lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
257         lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
258         lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
259         lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
260         lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
261         lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
262         lprintf(CTDL_INFO, "%s\n", serv_funambol_init());
263         for (filter = 1; filter != 0; filter = filter << 1)
264                 if ((filter & DetailErrorFlags) != 0)
265                         LogPrintMessages(filter);
266 }
267
268
269
270 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
271 {
272
273         struct CleanupFunctionHook *newfcn;
274
275         newfcn = (struct CleanupFunctionHook *)
276             malloc(sizeof(struct CleanupFunctionHook));
277         newfcn->next = CleanupHookTable;
278         newfcn->h_function_pointer = fcn_ptr;
279         CleanupHookTable = newfcn;
280
281         lprintf(CTDL_INFO, "Registered a new cleanup function\n");
282 }
283
284
285 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
286 {
287         struct CleanupFunctionHook *cur, *p;
288
289         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
290                 /* This will also remove duplicates if any */
291                 while (cur != NULL &&
292                                 fcn_ptr == cur->h_function_pointer) {
293                         lprintf(CTDL_INFO, "Unregistered cleanup function\n");
294                         p = cur->next;
295                         if (cur == CleanupHookTable) {
296                                 CleanupHookTable = p;
297                         }
298                         free(cur);
299                         cur = p;
300                 }
301         }
302 }
303
304 void CtdlDestroyCleanupHooks(void)
305 {
306         struct CleanupFunctionHook *cur, *p;
307
308         cur = CleanupHookTable;
309         while (cur != NULL)
310         {
311                 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
312                 p = cur->next;
313                 free(cur);
314                 cur = p;
315         }
316         CleanupHookTable = NULL;
317 }
318
319
320 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
321 {
322
323         struct SessionFunctionHook *newfcn;
324
325         newfcn = (struct SessionFunctionHook *)
326             malloc(sizeof(struct SessionFunctionHook));
327         newfcn->next = SessionHookTable;
328         newfcn->h_function_pointer = fcn_ptr;
329         newfcn->eventtype = EventType;
330         SessionHookTable = newfcn;
331
332         lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
333                 EventType);
334 }
335
336
337 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
338 {
339         struct SessionFunctionHook *cur, *p;
340
341         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
342                 /* This will also remove duplicates if any */
343                 while (cur != NULL &&
344                                 fcn_ptr == cur->h_function_pointer &&
345                                 EventType == cur->eventtype) {
346                         lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
347                                         EventType);
348                         p = cur->next;
349                         if (cur == SessionHookTable) {
350                                 SessionHookTable = p;
351                         }
352                         free(cur);
353                         cur = p;
354                 }
355         }
356 }
357
358 void CtdlDestroySessionHooks(void)
359 {
360         struct SessionFunctionHook *cur, *p;
361
362         cur = SessionHookTable;
363         while (cur != NULL)
364         {
365                 lprintf(CTDL_INFO, "Destroyed session function\n");
366                 p = cur->next;
367                 free(cur);
368                 cur = p;
369         }
370         SessionHookTable = NULL;
371 }
372
373
374 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
375 {
376
377         struct UserFunctionHook *newfcn;
378
379         newfcn = (struct UserFunctionHook *)
380             malloc(sizeof(struct UserFunctionHook));
381         newfcn->next = UserHookTable;
382         newfcn->h_function_pointer = fcn_ptr;
383         newfcn->eventtype = EventType;
384         UserHookTable = newfcn;
385
386         lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
387                 EventType);
388 }
389
390
391 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
392 {
393         struct UserFunctionHook *cur, *p;
394
395         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
396                 /* This will also remove duplicates if any */
397                 while (cur != NULL &&
398                                 fcn_ptr == cur->h_function_pointer &&
399                                 EventType == cur->eventtype) {
400                         lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
401                                         EventType);
402                         p = cur->next;
403                         if (cur == UserHookTable) {
404                                 UserHookTable = p;
405                         }
406                         free(cur);
407                         cur = p;
408                 }
409         }
410 }
411
412 void CtdlDestroyUserHooks(void)
413 {
414         struct UserFunctionHook *cur, *p;
415
416         cur = UserHookTable;
417         while (cur != NULL)
418         {
419                 lprintf(CTDL_INFO, "Destroyed user function \n");
420                 p = cur->next;
421                 free(cur);
422                 cur = p;
423         }
424         UserHookTable = NULL;
425 }
426
427
428 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
429                                 int EventType)
430 {
431
432         struct MessageFunctionHook *newfcn;
433
434         newfcn = (struct MessageFunctionHook *)
435             malloc(sizeof(struct MessageFunctionHook));
436         newfcn->next = MessageHookTable;
437         newfcn->h_function_pointer = handler;
438         newfcn->eventtype = EventType;
439         MessageHookTable = newfcn;
440
441         lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
442                 EventType);
443 }
444
445
446 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
447                 int EventType)
448 {
449         struct MessageFunctionHook *cur, *p;
450
451         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
452                 /* This will also remove duplicates if any */
453                 while (cur != NULL &&
454                                 handler == cur->h_function_pointer &&
455                                 EventType == cur->eventtype) {
456                         lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
457                                         EventType);
458                         p = cur->next;
459                         if (cur == MessageHookTable) {
460                                 MessageHookTable = p;
461                         }
462                         free(cur);
463                         cur = p;
464                 }
465         }
466 }
467
468 void CtdlDestroyMessageHook(void)
469 {
470         struct MessageFunctionHook *cur, *p;
471
472         cur = MessageHookTable; 
473         while (cur != NULL)
474         {
475                 lprintf(CTDL_INFO, "Destroyed message function \n");
476                 p = cur->next;
477                 free(cur);
478                 cur = p;
479         }
480         MessageHookTable = NULL;
481 }
482
483
484 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
485 {
486         struct NetprocFunctionHook *newfcn;
487
488         newfcn = (struct NetprocFunctionHook *)
489             malloc(sizeof(struct NetprocFunctionHook));
490         newfcn->next = NetprocHookTable;
491         newfcn->h_function_pointer = handler;
492         NetprocHookTable = newfcn;
493
494         lprintf(CTDL_INFO, "Registered a new netproc function\n");
495 }
496
497
498 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
499 {
500         struct NetprocFunctionHook *cur, *p;
501
502         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
503                 /* This will also remove duplicates if any */
504                 while (cur != NULL &&
505                                 handler == cur->h_function_pointer ) {
506                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
507                         p = cur->next;
508                         if (cur == NetprocHookTable) {
509                                 NetprocHookTable = p;
510                         }
511                         free(cur);
512                         cur = p;
513                 }
514         }
515 }
516
517 void CtdlDestroyNetprocHooks(void)
518 {
519         struct NetprocFunctionHook *cur, *p;
520
521         cur = NetprocHookTable;
522         while (cur != NULL)
523         {
524                 lprintf(CTDL_INFO, "Unregistered netproc function\n");
525                 p = cur->next;
526                 free(cur);
527                 cur = p;
528         }
529         NetprocHookTable = NULL;
530 }
531
532
533 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
534 {
535         struct DeleteFunctionHook *newfcn;
536
537         newfcn = (struct DeleteFunctionHook *)
538             malloc(sizeof(struct DeleteFunctionHook));
539         newfcn->next = DeleteHookTable;
540         newfcn->h_function_pointer = handler;
541         DeleteHookTable = newfcn;
542
543         lprintf(CTDL_INFO, "Registered a new netproc function\n");
544 }
545
546
547 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
548 {
549         struct DeleteFunctionHook *cur, *p;
550
551         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
552                 /* This will also remove duplicates if any */
553                 while (cur != NULL &&
554                                 handler == cur->h_function_pointer ) {
555                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
556                         p = cur->next;
557                         if (cur == DeleteHookTable) {
558                                 DeleteHookTable = p;
559                         }
560                         free(cur);
561                         cur = p;
562                 }
563         }
564 }
565 void CtdlDestroyDeleteHooks(void)
566 {
567         struct DeleteFunctionHook *cur, *p;
568
569         cur = DeleteHookTable;
570         while (cur != NULL)
571         {
572                 lprintf(CTDL_INFO, "Destroyed netproc function\n");
573                 p = cur->next;
574                 free(cur);
575                 cur = p;                
576         }
577         DeleteHookTable = NULL;
578 }
579
580
581
582
583 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
584 {
585         struct FixedOutputHook *newfcn;
586
587         newfcn = (struct FixedOutputHook *)
588             malloc(sizeof(struct FixedOutputHook));
589         newfcn->next = FixedOutputTable;
590         newfcn->h_function_pointer = handler;
591         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
592         FixedOutputTable = newfcn;
593
594         lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
595 }
596
597
598 void CtdlUnregisterFixedOutputHook(char *content_type)
599 {
600         struct FixedOutputHook *cur, *p;
601
602         for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
603                 /* This will also remove duplicates if any */
604                 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
605                         lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
606                         p = cur->next;
607                         if (cur == FixedOutputTable) {
608                                 FixedOutputTable = p;
609                         }
610                         free(cur);
611                         cur = p;
612                 }
613         }
614 }
615
616 void CtdlDestroyFixedOutputHooks(void)
617 {
618         struct FixedOutputHook *cur, *p;
619
620         cur = FixedOutputTable; 
621         while (cur != NULL)
622         {
623                 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
624                 p = cur->next;
625                 free(cur);
626                 cur = p;
627                 
628         }
629         FixedOutputTable = NULL;
630 }
631
632 /* returns nonzero if we found a hook and used it */
633 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
634 {
635         struct FixedOutputHook *fcn;
636
637         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
638                 if (!strcasecmp(content_type, fcn->content_type)) {
639                         (*fcn->h_function_pointer) (content, content_length);
640                         return(1);
641                 }
642         }
643         return(0);
644 }
645
646
647
648
649
650 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
651 {
652
653         struct XmsgFunctionHook *newfcn;
654
655         newfcn = (struct XmsgFunctionHook *)
656             malloc(sizeof(struct XmsgFunctionHook));
657         newfcn->next = XmsgHookTable;
658         newfcn->order = order;
659         newfcn->h_function_pointer = fcn_ptr;
660         XmsgHookTable = newfcn;
661         lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
662 }
663
664
665 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
666 {
667         struct XmsgFunctionHook *cur, *p;
668
669         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
670                 /* This will also remove duplicates if any */
671                 while (cur != NULL &&
672                                 fcn_ptr == cur->h_function_pointer &&
673                                 order == cur->order) {
674                         lprintf(CTDL_INFO, "Unregistered x-msg function "
675                                         "(priority %d)\n", order);
676                         p = cur->next;
677                         if (cur == XmsgHookTable) {
678                                 XmsgHookTable = p;
679                         }
680                         free(cur);
681                         cur = p;
682                 }
683         }
684 }
685
686 void CtdlDestroyXmsgHooks(void)
687 {
688         struct XmsgFunctionHook *cur, *p;
689
690         cur = XmsgHookTable;
691         while (cur != NULL)
692         {
693                 lprintf(CTDL_INFO, "Destroyed x-msg function "
694                         "(priority %d)\n", cur->order);
695                 p = cur->next;
696                         
697                 free(cur);
698                 cur = p;
699         }
700         XmsgHookTable = NULL;
701 }
702
703
704 void CtdlRegisterServiceHook(int tcp_port,
705                         char *sockpath,
706                         void (*h_greeting_function) (void),
707                         void (*h_command_function) (void),
708                         void (*h_async_function) (void)
709                         )
710 {
711         struct ServiceFunctionHook *newfcn;
712         char *message;
713         char *error;
714
715         error = NULL;
716         newfcn = (struct ServiceFunctionHook *)
717             malloc(sizeof(struct ServiceFunctionHook));
718         message = (char*) malloc (SIZ);
719         
720         newfcn->next = ServiceHookTable;
721         newfcn->tcp_port = tcp_port;
722         newfcn->sockpath = sockpath;
723         newfcn->h_greeting_function = h_greeting_function;
724         newfcn->h_command_function = h_command_function;
725         newfcn->h_async_function = h_async_function;
726
727         if (sockpath != NULL) {
728                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
729                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
730         }
731         else if (tcp_port <= 0) {       /* port -1 to disable */
732                 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
733                 free (message);
734                 free(newfcn);
735                 return;
736         }
737         else {
738                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
739                                               tcp_port,
740                                               config.c_maxsessions, 
741                                               &error);
742                 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
743         }
744
745         if (newfcn->msock > 0) {
746                 ServiceHookTable = newfcn;
747                 strcat(message, "registered.");
748                 lprintf(CTDL_INFO, "%s\n", message);
749         }
750         else {
751                 AddPortError(message, error);
752                 strcat(message, "FAILED.");
753                 lprintf(CTDL_CRIT, "%s\n", message);
754                 free(error);
755                 free(newfcn);
756         }
757         free(message);
758 }
759
760
761 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
762                         void (*h_greeting_function) (void),
763                         void (*h_command_function) (void),
764                         void (*h_async_function) (void)
765                         )
766 {
767         struct ServiceFunctionHook *cur, *p;
768
769         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
770                 /* This will also remove duplicates if any */
771                 while (cur != NULL &&
772                                 !(sockpath && cur->sockpath &&
773                                         strcmp(sockpath, cur->sockpath)) &&
774                                 h_greeting_function == cur->h_greeting_function &&
775                                 h_command_function == cur->h_command_function &&
776                                 h_async_function == cur->h_async_function &&
777                                 tcp_port == cur->tcp_port) {
778                         close(cur->msock);
779                         if (sockpath) {
780                                 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
781                                                 sockpath);
782                         } else if (tcp_port) {
783                                 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
784                         } else {
785                                 lprintf(CTDL_INFO, "Unregistered unknown service\n");
786                         }
787                         p = cur->next;
788                         if (cur == ServiceHookTable) {
789                                 ServiceHookTable = p;
790                         }
791                         free(cur);
792                         cur = p;
793                 }
794         }
795 }
796
797 void CtdlDestroyServiceHook(void)
798 {
799         struct ServiceFunctionHook *cur, *p;
800
801         cur = ServiceHookTable;
802         while (cur != NULL)
803         {
804                 close(cur->msock);
805                 if (cur->sockpath) {
806                         lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
807                                 cur->sockpath);
808                 } else if (cur->tcp_port) {
809                         lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
810                 } else {
811                         lprintf(CTDL_INFO, "Unregistered unknown service\n");
812                 }
813                 p = cur->next;
814                 free(cur);
815                 cur = p;
816         }
817         ServiceHookTable = NULL;
818 }
819
820
821 void PerformSessionHooks(int EventType)
822 {
823         struct SessionFunctionHook *fcn = NULL;
824
825         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
826                 if (fcn->eventtype == EventType) {
827                         (*fcn->h_function_pointer) ();
828                 }
829         }
830 }
831
832 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
833 {
834         struct UserFunctionHook *fcn = NULL;
835
836         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
837                 if (fcn->eventtype == EventType) {
838                         (*fcn->h_function_pointer) (usbuf);
839                 }
840         }
841 }
842
843 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
844 {
845         struct MessageFunctionHook *fcn = NULL;
846         int total_retval = 0;
847
848         /* Other code may elect to protect this message from server-side
849          * handlers; if this is the case, don't do anything.
850         lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
851                 EventType, msg->cm_flags);
852          */
853         if (msg->cm_flags & CM_SKIP_HOOKS) {
854                 lprintf(CTDL_DEBUG, "Skipping hooks\n");
855                 return(0);
856         }
857
858         /* Otherwise, run all the hooks appropriate to this event type.
859          */
860         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
861                 if (fcn->eventtype == EventType) {
862                         total_retval = total_retval +
863                                 (*fcn->h_function_pointer) (msg);
864                 }
865         }
866
867         /* Return the sum of the return codes from the hook functions.  If
868          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
869          * the save operation to abort.
870          */
871         return total_retval;
872 }
873
874
875
876 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
877 {
878         struct NetprocFunctionHook *fcn;
879         int total_retval = 0;
880
881         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
882                 total_retval = total_retval +
883                         (*fcn->h_function_pointer) (msg, target_room);
884         }
885
886         /* Return the sum of the return codes from the hook functions.
887          * A nonzero return code will cause the message to *not* be imported.
888          */
889         return total_retval;
890 }
891
892
893 void PerformDeleteHooks(char *room, long msgnum)
894 {
895         struct DeleteFunctionHook *fcn;
896
897         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
898                 (*fcn->h_function_pointer) (room, msgnum);
899         }
900 }
901
902
903
904
905
906 int PerformXmsgHooks(char *sender, char *recp, char *msg)
907 {
908         struct XmsgFunctionHook *fcn;
909         int total_sent = 0;
910         int p;
911
912         for (p=0; p<MAX_XMSG_PRI; ++p) {
913                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
914                         if (fcn->order == p) {
915                                 total_sent +=
916                                         (*fcn->h_function_pointer)
917                                                 (sender, recp, msg);
918                         }
919                 }
920                 /* Break out of the loop if a higher-priority function
921                  * successfully delivered the message.  This prevents duplicate
922                  * deliveries to local users simultaneously signed onto
923                  * remote services.
924                  */
925                 if (total_sent) break;
926         }
927         return total_sent;
928 }