615a48b42b76c03988f18a01d119c0debba87eac
[citadel.git] / citadel / modules / inetcfg / serv_inetcfg.c
1 /*
2  * This module handles the loading/saving and maintenance of the
3  * system's Internet configuration.  It's not an optional component; I
4  * wrote it as a module merely to keep things as clean and loosely coupled
5  * as possible.
6  *
7  * Copyright (c) 1987-2012 by the citadel.org team
8  *
9  *  This program is open source software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 3.
11  *  
12  *  
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  
20  *  
21  *  
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "user_ops.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58 #include "genstamp.h"
59 #include "domain.h"
60
61
62 #include "ctdl_module.h"
63
64
65 void inetcfg_setTo(struct CtdlMessage *msg) {
66         char *conf;
67         char buf[SIZ];
68         
69         if (CM_IsEmpty(msg, eMesageText)) return;
70         conf = strdup(msg->cm_fields[eMesageText]);
71
72         if (conf != NULL) {
73                 do {
74                         extract_token(buf, conf, 0, '\n', sizeof buf);
75                         strcpy(conf, &conf[strlen(buf)+1]);
76                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
77
78                 if (inetcfg != NULL) free(inetcfg);
79                 inetcfg = conf;
80         }
81 }
82
83
84 /*
85  * This handler detects changes being made to the system's Internet
86  * configuration.
87  */
88 int inetcfg_aftersave(struct CtdlMessage *msg, recptypes *recp) {
89         char *ptr;
90         int linelen;
91
92         /* If this isn't the configuration room, or if this isn't a MIME
93          * message, don't bother.
94          */
95         if (strcasecmp(msg->cm_fields[eOriginalRoom], SYSCONFIGROOM)) return(0);
96         if (msg->cm_format_type != 4) return(0);
97
98         ptr = msg->cm_fields[eMesageText];
99         while (ptr != NULL) {
100         
101                 linelen = strcspn(ptr, "\n");
102                 if (linelen == 0) return(0);    /* end of headers */    
103                 
104                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
105                         if (!strncasecmp(&ptr[14], INTERNETCFG,
106                            strlen(INTERNETCFG))) {
107                                 inetcfg_setTo(msg);     /* changing configs */
108                         }
109                 }
110
111                 ptr = strchr((char *)ptr, '\n');
112                 if (ptr != NULL) ++ptr;
113         }
114
115         return(0);
116 }
117
118
119 void inetcfg_init_backend(long msgnum, void *userdata) {
120         struct CtdlMessage *msg;
121
122         msg = CtdlFetchMessage(msgnum, 1, 1);
123         if (msg != NULL) {
124                 inetcfg_setTo(msg);
125                 CM_Free(msg);
126         }
127 }
128
129
130 void inetcfg_init(void) {
131         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) return;
132         CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
133                 inetcfg_init_backend, NULL);
134 }
135
136
137
138
139 /*****************************************************************************/
140 /*                      MODULE INITIALIZATION STUFF                          */
141 /*****************************************************************************/
142 void clenaup_inetcfg(void)
143 {
144         char *buf;
145
146         buf = inetcfg;
147         inetcfg = NULL;
148         if (buf != NULL)
149                 free(buf);
150 }
151
152 CTDL_MODULE_INIT(inetcfg)
153 {
154         if (!threading)
155         {
156                 CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
157                 inetcfg_init();
158                 CtdlRegisterCleanupHook(clenaup_inetcfg);
159         }
160         
161         /* return our module name for the log */
162         return "inetcfg";
163 }
164