- port to Cygwin (DLL support, etc.)
[citadel.git] / citadel / control.c
1 /*
2  * $Id$
3  *
4  * This module handles states which are global to the entire server.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <ctype.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <syslog.h>
35 #include <sys/types.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "control.h"
39 #include "dynloader.h"
40 #include "sysdep_decls.h"
41 #include "support.h"
42 #include "config.h"
43 #include "msgbase.h"
44 #include "citserver.h"
45 #include "tools.h"
46 #include "room_ops.h"
47
48 struct CitControl CitControl;
49 struct config config;
50 FILE *control_fp = NULL;
51
52 /*
53  * get_control  -  read the control record into memory.
54  */
55 void get_control(void) {
56
57         /* Zero it out.  If the control record on disk is missing or short,
58          * the system functions with all control record fields initialized
59          * to zero.
60          */
61         memset(&CitControl, 0, sizeof(struct CitControl));
62         if (control_fp == NULL) {
63                 control_fp = fopen("citadel.control", "rb+");
64                 if (control_fp != NULL) {
65                         fchown(fileno(control_fp), config.c_bbsuid, -1);
66                 }
67         }
68         if (control_fp == NULL) {
69                 control_fp = fopen("citadel.control", "wb+");
70                 if (control_fp != NULL) {
71                         fchown(fileno(control_fp), config.c_bbsuid, -1);
72                         memset(&CitControl, 0, sizeof(struct CitControl));
73                         fwrite(&CitControl, sizeof(struct CitControl),
74                                 1, control_fp);
75                         rewind(control_fp);
76                 }
77         }
78         if (control_fp == NULL) {
79                 lprintf(1, "ERROR opening citadel.control: %s\n",
80                         strerror(errno));
81                 return;
82         }
83
84         rewind(control_fp);
85         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
86         }
87
88 /*
89  * put_control  -  write the control record to disk.
90  */
91 void put_control(void) {
92
93         if (control_fp != NULL) {
94                 rewind(control_fp);
95                 fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
96                 fflush(control_fp);
97                 }
98         }
99
100
101 /*
102  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
103  */
104 long get_new_message_number(void) {
105         begin_critical_section(S_CONTROL);
106         get_control();
107         ++CitControl.MMhighest;
108         put_control();
109         end_critical_section(S_CONTROL);
110         return(CitControl.MMhighest);
111         }
112
113
114 /*
115  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
116  */
117 long get_new_user_number(void) {
118         begin_critical_section(S_CONTROL);
119         get_control();
120         ++CitControl.MMnextuser;
121         put_control();
122         end_critical_section(S_CONTROL);
123         return(CitControl.MMnextuser);
124         }
125
126
127
128 /*
129  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
130  */
131 long get_new_room_number(void) {
132         begin_critical_section(S_CONTROL);
133         get_control();
134         ++CitControl.MMnextroom;
135         put_control();
136         end_critical_section(S_CONTROL);
137         return(CitControl.MMnextroom);
138         }
139
140
141
142 /* 
143  * Get or set global configuration options
144  */
145 void cmd_conf(char *argbuf) {
146         char cmd[SIZ];
147         char buf[SIZ];
148         int a;
149         char *confptr;
150         char confname[SIZ];
151
152         if (CtdlAccessCheck(ac_aide)) return;
153
154         extract(cmd, argbuf, 0);
155         if (!strcasecmp(cmd, "GET")) {
156                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
157                 cprintf("%s\n", config.c_nodename);
158                 cprintf("%s\n", config.c_fqdn);
159                 cprintf("%s\n", config.c_humannode);
160                 cprintf("%s\n", config.c_phonenum);
161                 cprintf("%d\n", config.c_creataide);
162                 cprintf("%d\n", config.c_sleeping);
163                 cprintf("%d\n", config.c_initax);
164                 cprintf("%d\n", config.c_regiscall);
165                 cprintf("%d\n", config.c_twitdetect);
166                 cprintf("%s\n", config.c_twitroom);
167                 cprintf("%s\n", config.c_moreprompt);
168                 cprintf("%d\n", config.c_restrict);
169                 cprintf("%s\n", config.c_bbs_city);
170                 cprintf("%s\n", config.c_sysadm);
171                 cprintf("%d\n", config.c_maxsessions);
172                 cprintf("%s\n", config.c_net_password);
173                 cprintf("%d\n", config.c_userpurge);
174                 cprintf("%d\n", config.c_roompurge);
175                 cprintf("%s\n", config.c_logpages);
176                 cprintf("%d\n", config.c_createax);
177                 cprintf("%d\n", config.c_maxmsglen);
178                 cprintf("%d\n", config.c_min_workers);
179                 cprintf("%d\n", config.c_max_workers);
180                 cprintf("%d\n", config.c_pop3_port);
181                 cprintf("%d\n", config.c_smtp_port);
182                 cprintf("%d\n", config.c_default_filter);
183                 cprintf("%d\n", config.c_aide_zap);
184                 cprintf("%d\n", config.c_imap_port);
185                 cprintf("000\n");
186                 }
187
188         else if (!strcasecmp(cmd, "SET")) {
189                 cprintf("%d Send configuration...\n", SEND_LISTING);
190                 a = 0;
191                 while (client_gets(buf), strcmp(buf, "000")) {
192                     switch(a) {
193                         case 0: safestrncpy(config.c_nodename, buf,
194                                         sizeof config.c_nodename);
195                                 break;
196                         case 1: safestrncpy(config.c_fqdn, buf,
197                                         sizeof config.c_fqdn);
198                                 break;
199                         case 2: safestrncpy(config.c_humannode, buf,
200                                         sizeof config.c_humannode);
201                                 break;
202                         case 3: safestrncpy(config.c_phonenum, buf,
203                                         sizeof config.c_phonenum);
204                                 break;
205                         case 4: config.c_creataide = atoi(buf);
206                                 break;
207                         case 5: config.c_sleeping = atoi(buf);
208                                 break;
209                         case 6: config.c_initax = atoi(buf);
210                                 if (config.c_initax < 1) config.c_initax = 1;
211                                 if (config.c_initax > 6) config.c_initax = 6;
212                                 break;
213                         case 7: config.c_regiscall = atoi(buf);
214                                 if (config.c_regiscall != 0)
215                                         config.c_regiscall = 1;
216                                 break;
217                         case 8: config.c_twitdetect = atoi(buf);
218                                 if (config.c_twitdetect != 0)
219                                         config.c_twitdetect = 1;
220                                 break;
221                         case 9: safestrncpy(config.c_twitroom, buf,
222                                         sizeof config.c_twitroom);
223                                 break;
224                         case 10: safestrncpy(config.c_moreprompt, buf,
225                                         sizeof config.c_moreprompt);
226                                 break;
227                         case 11: config.c_restrict = atoi(buf);
228                                 if (config.c_restrict != 0)
229                                         config.c_restrict = 1;
230                                 break;
231                         case 12: safestrncpy(config.c_bbs_city, buf,
232                                         sizeof config.c_bbs_city);
233                                 break;
234                         case 13: safestrncpy(config.c_sysadm, buf,
235                                         sizeof config.c_sysadm);
236                                 break;
237                         case 14: config.c_maxsessions = atoi(buf);
238                                 if (config.c_maxsessions < 1)
239                                         config.c_maxsessions = 1;
240                                 break;
241                         case 15: safestrncpy(config.c_net_password, buf,
242                                         sizeof config.c_net_password);
243                                 break;
244                         case 16: config.c_userpurge = atoi(buf);
245                                 break;
246                         case 17: config.c_roompurge = atoi(buf);
247                                 break;
248                         case 18: safestrncpy(config.c_logpages, buf,
249                                         sizeof config.c_logpages);
250                                 break;
251                         case 19: config.c_createax = atoi(buf);
252                                 if (config.c_createax < 1)
253                                         config.c_createax = 1;
254                                 if (config.c_createax > 6)
255                                         config.c_createax = 6;
256                                 break;
257                         case 20: if (atoi(buf) >= 8192)
258                                         config.c_maxmsglen = atoi(buf);
259                                 break;
260                         case 21: if (atoi(buf) >= 2)
261                                         config.c_min_workers = atoi(buf);
262                         case 22: if (atoi(buf) >= config.c_min_workers)
263                                         config.c_max_workers = atoi(buf);
264                         case 23: config.c_pop3_port = atoi(buf);
265                                 break;
266                         case 24: config.c_smtp_port = atoi(buf);
267                                 break;
268                         case 25: config.c_default_filter = atoi(buf);
269                                 break;
270                         case 26: config.c_aide_zap = atoi(buf);
271                                 if (config.c_aide_zap != 0)
272                                         config.c_aide_zap = 1;
273                                 break;
274                         case 27: config.c_imap_port = atoi(buf);
275                                 break;
276                         }
277                     ++a;
278                     }
279                 put_config();
280                 snprintf(buf,sizeof buf,
281                          "Global system configuration edited by %s\n",
282                          CC->curr_user);
283                 aide_message(buf);
284
285                 if (strlen(config.c_logpages) > 0)
286                         create_room(config.c_logpages, 3, "", 0, 1);
287                 }
288
289         else if (!strcasecmp(cmd, "GETSYS")) {
290                 extract(confname, argbuf, 1);
291                 confptr = CtdlGetSysConfig(confname);
292                 if (confptr != NULL) {
293                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
294                         client_write(confptr, strlen(confptr));
295                         if (confptr[strlen(confptr)-1] != 10)
296                                 client_write("\n", 1);
297                         cprintf("000\n");
298                         phree(confptr);
299                 }
300                 else {
301                         cprintf("%d No such configuration.\n",
302                                 ERROR+ILLEGAL_VALUE);
303                 }
304         }
305
306         else if (!strcasecmp(cmd, "PUTSYS")) {
307                 extract(confname, argbuf, 1);
308                 cprintf("%d %s\n", SEND_LISTING, confname);
309                 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL);
310                 CtdlPutSysConfig(confname, confptr);
311                 phree(confptr);
312         }
313
314         else {
315                 cprintf("%d Illegal option(s) specified.\n",
316                         ERROR+ILLEGAL_VALUE);
317                 }
318         }