* New server command-line option "-l" to send log output to the host
[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 <sys/types.h>
35 #include "citadel.h"
36 #include "server.h"
37 #include "control.h"
38 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
40 #include "support.h"
41 #include "config.h"
42 #include "msgbase.h"
43 #include "citserver.h"
44 #include "tools.h"
45 #include "room_ops.h"
46
47 #ifndef HAVE_SNPRINTF
48 #include "snprintf.h"
49 #endif
50
51 struct CitControl CitControl;
52 extern struct config config;
53 FILE *control_fp = NULL;
54
55 /*
56  * get_control  -  read the control record into memory.
57  */
58 void get_control(void)
59 {
60
61         /* Zero it out.  If the control record on disk is missing or short,
62          * the system functions with all control record fields initialized
63          * to zero.
64          */
65         memset(&CitControl, 0, sizeof(struct CitControl));
66         if (control_fp == NULL) {
67                 control_fp = fopen("citadel.control", "rb+");
68                 if (control_fp != NULL) {
69                         fchown(fileno(control_fp), config.c_bbsuid, -1);
70                 }
71         }
72         if (control_fp == NULL) {
73                 control_fp = fopen("citadel.control", "wb+");
74                 if (control_fp != NULL) {
75                         fchown(fileno(control_fp), config.c_bbsuid, -1);
76                         memset(&CitControl, 0, sizeof(struct CitControl));
77                         fwrite(&CitControl, sizeof(struct CitControl),
78                                1, control_fp);
79                         rewind(control_fp);
80                 }
81         }
82         if (control_fp == NULL) {
83                 lprintf(1, "ERROR opening citadel.control: %s\n",
84                         strerror(errno));
85                 return;
86         }
87
88         rewind(control_fp);
89         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
90 }
91
92 /*
93  * put_control  -  write the control record to disk.
94  */
95 void put_control(void)
96 {
97
98         if (control_fp != NULL) {
99                 rewind(control_fp);
100                 fwrite(&CitControl, sizeof(struct CitControl), 1,
101                        control_fp);
102                 fflush(control_fp);
103         }
104 }
105
106
107 /*
108  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
109  */
110 long get_new_message_number(void)
111 {
112         begin_critical_section(S_CONTROL);
113         get_control();
114         ++CitControl.MMhighest;
115         put_control();
116         end_critical_section(S_CONTROL);
117         return (CitControl.MMhighest);
118 }
119
120
121 /*
122  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
123  */
124 long get_new_user_number(void)
125 {
126         begin_critical_section(S_CONTROL);
127         get_control();
128         ++CitControl.MMnextuser;
129         put_control();
130         end_critical_section(S_CONTROL);
131         return (CitControl.MMnextuser);
132 }
133
134
135
136 /*
137  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
138  */
139 long get_new_room_number(void)
140 {
141         begin_critical_section(S_CONTROL);
142         get_control();
143         ++CitControl.MMnextroom;
144         put_control();
145         end_critical_section(S_CONTROL);
146         return (CitControl.MMnextroom);
147 }
148
149
150
151 /* 
152  * Get or set global configuration options
153  */
154 void cmd_conf(char *argbuf)
155 {
156         char cmd[SIZ];
157         char buf[SIZ];
158         int a;
159         char *confptr;
160         char confname[SIZ];
161
162         if (CtdlAccessCheck(ac_aide)) return;
163
164         extract(cmd, argbuf, 0);
165         if (!strcasecmp(cmd, "GET")) {
166                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
167                 cprintf("%s\n", config.c_nodename);
168                 cprintf("%s\n", config.c_fqdn);
169                 cprintf("%s\n", config.c_humannode);
170                 cprintf("%s\n", config.c_phonenum);
171                 cprintf("%d\n", config.c_creataide);
172                 cprintf("%d\n", config.c_sleeping);
173                 cprintf("%d\n", config.c_initax);
174                 cprintf("%d\n", config.c_regiscall);
175                 cprintf("%d\n", config.c_twitdetect);
176                 cprintf("%s\n", config.c_twitroom);
177                 cprintf("%s\n", config.c_moreprompt);
178                 cprintf("%d\n", config.c_restrict);
179                 cprintf("%s\n", config.c_bbs_city);
180                 cprintf("%s\n", config.c_sysadm);
181                 cprintf("%d\n", config.c_maxsessions);
182                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
183                 cprintf("%d\n", config.c_userpurge);
184                 cprintf("%d\n", config.c_roompurge);
185                 cprintf("%s\n", config.c_logpages);
186                 cprintf("%d\n", config.c_createax);
187                 cprintf("%ld\n", config.c_maxmsglen);
188                 cprintf("%d\n", config.c_min_workers);
189                 cprintf("%d\n", config.c_max_workers);
190                 cprintf("%d\n", config.c_pop3_port);
191                 cprintf("%d\n", config.c_smtp_port);
192                 cprintf("%d\n", config.c_rfc822_strict_from);
193                 cprintf("%d\n", config.c_aide_zap);
194                 cprintf("%d\n", config.c_imap_port);
195                 cprintf("%ld\n", config.c_net_freq);
196                 cprintf("%d\n", config.c_disable_newu);
197                 cprintf("%d\n", config.c_aide_mailboxes);
198                 cprintf("%d\n", config.c_purge_hour);
199                 cprintf("000\n");
200         }
201
202         else if (!strcasecmp(cmd, "SET")) {
203                 cprintf("%d Send configuration...\n", SEND_LISTING);
204                 a = 0;
205                 while (client_gets(buf), strcmp(buf, "000")) {
206                         switch (a) {
207                         case 0:
208                                 safestrncpy(config.c_nodename, buf,
209                                             sizeof config.c_nodename);
210                                 break;
211                         case 1:
212                                 safestrncpy(config.c_fqdn, buf,
213                                             sizeof config.c_fqdn);
214                                 break;
215                         case 2:
216                                 safestrncpy(config.c_humannode, buf,
217                                             sizeof config.c_humannode);
218                                 break;
219                         case 3:
220                                 safestrncpy(config.c_phonenum, buf,
221                                             sizeof config.c_phonenum);
222                                 break;
223                         case 4:
224                                 config.c_creataide = atoi(buf);
225                                 break;
226                         case 5:
227                                 config.c_sleeping = atoi(buf);
228                                 break;
229                         case 6:
230                                 config.c_initax = atoi(buf);
231                                 if (config.c_initax < 1)
232                                         config.c_initax = 1;
233                                 if (config.c_initax > 6)
234                                         config.c_initax = 6;
235                                 break;
236                         case 7:
237                                 config.c_regiscall = atoi(buf);
238                                 if (config.c_regiscall != 0)
239                                         config.c_regiscall = 1;
240                                 break;
241                         case 8:
242                                 config.c_twitdetect = atoi(buf);
243                                 if (config.c_twitdetect != 0)
244                                         config.c_twitdetect = 1;
245                                 break;
246                         case 9:
247                                 safestrncpy(config.c_twitroom, buf,
248                                             sizeof config.c_twitroom);
249                                 break;
250                         case 10:
251                                 safestrncpy(config.c_moreprompt, buf,
252                                             sizeof config.c_moreprompt);
253                                 break;
254                         case 11:
255                                 config.c_restrict = atoi(buf);
256                                 if (config.c_restrict != 0)
257                                         config.c_restrict = 1;
258                                 break;
259                         case 12:
260                                 safestrncpy(config.c_bbs_city, buf,
261                                             sizeof config.c_bbs_city);
262                                 break;
263                         case 13:
264                                 safestrncpy(config.c_sysadm, buf,
265                                             sizeof config.c_sysadm);
266                                 break;
267                         case 14:
268                                 config.c_maxsessions = atoi(buf);
269                                 if (config.c_maxsessions < 1)
270                                         config.c_maxsessions = 1;
271                                 break;
272                         case 15:
273                                 /* placeholder -- field no longer in use */
274                                 break;
275                         case 16:
276                                 config.c_userpurge = atoi(buf);
277                                 break;
278                         case 17:
279                                 config.c_roompurge = atoi(buf);
280                                 break;
281                         case 18:
282                                 safestrncpy(config.c_logpages, buf,
283                                             sizeof config.c_logpages);
284                                 break;
285                         case 19:
286                                 config.c_createax = atoi(buf);
287                                 if (config.c_createax < 1)
288                                         config.c_createax = 1;
289                                 if (config.c_createax > 6)
290                                         config.c_createax = 6;
291                                 break;
292                         case 20:
293                                 if (atoi(buf) >= 8192)
294                                         config.c_maxmsglen = atoi(buf);
295                                 break;
296                         case 21:
297                                 if (atoi(buf) >= 2)
298                                         config.c_min_workers = atoi(buf);
299                         case 22:
300                                 if (atoi(buf) >= config.c_min_workers)
301                                         config.c_max_workers = atoi(buf);
302                         case 23:
303                                 config.c_pop3_port = atoi(buf);
304                                 break;
305                         case 24:
306                                 config.c_smtp_port = atoi(buf);
307                                 break;
308                         case 25:
309                                 config.c_rfc822_strict_from = atoi(buf);
310                                 break;
311                         case 26:
312                                 config.c_aide_zap = atoi(buf);
313                                 if (config.c_aide_zap != 0)
314                                         config.c_aide_zap = 1;
315                                 break;
316                         case 27:
317                                 config.c_imap_port = atoi(buf);
318                                 break;
319                         case 28:
320                                 config.c_net_freq = atol(buf);
321                                 break;
322                         case 29:
323                                 config.c_disable_newu = atoi(buf);
324                                 if (config.c_disable_newu != 0)
325                                         config.c_disable_newu = 1;
326                                 break;
327                         case 30:
328                                 config.c_aide_mailboxes = atoi(buf);
329                                 if (config.c_aide_mailboxes != 0)
330                                         config.c_aide_mailboxes = 1;
331                                 break;
332                         case 31:
333                                 if ((config.c_purge_hour >= 0)
334                                    && (config.c_purge_hour <= 23)) {
335                                         config.c_purge_hour = atoi(buf);
336                                 }
337                                 break;
338                         }
339                         ++a;
340                 }
341                 put_config();
342                 snprintf(buf, sizeof buf,
343                          "Global system configuration edited by %s\n",
344                          CC->curr_user);
345                 aide_message(buf);
346
347                 if (strlen(config.c_logpages) > 0)
348                         create_room(config.c_logpages, 3, "", 0, 1, 1);
349         }
350
351         else if (!strcasecmp(cmd, "GETSYS")) {
352                 extract(confname, argbuf, 1);
353                 confptr = CtdlGetSysConfig(confname);
354                 if (confptr != NULL) {
355                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
356                         client_write(confptr, strlen(confptr));
357                         if (confptr[strlen(confptr) - 1] != 10)
358                                 client_write("\n", 1);
359                         cprintf("000\n");
360                         phree(confptr);
361                 } else {
362                         cprintf("%d No such configuration.\n",
363                                 ERROR + ILLEGAL_VALUE);
364                 }
365         }
366
367         else if (!strcasecmp(cmd, "PUTSYS")) {
368                 extract(confname, argbuf, 1);
369                 cprintf("%d %s\n", SEND_LISTING, confname);
370                 confptr = CtdlReadMessageBody("000",
371                                 config.c_maxmsglen, NULL, 0);
372                 CtdlPutSysConfig(confname, confptr);
373                 phree(confptr);
374         }
375
376         else {
377                 cprintf("%d Illegal option(s) specified.\n",
378                         ERROR + ILLEGAL_VALUE);
379         }
380 }