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