207291faf45be9bf7a4f91c01c658b04d2a3ea71
[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 #ifdef HAVE_LDAP
200                 cprintf("%s\n", config.c_ldap_host);
201                 cprintf("%d\n", config.c_ldap_port);
202 #else
203                 cprintf("\n");
204                 cprintf("0\n");
205 #endif
206                 cprintf("000\n");
207         }
208
209         else if (!strcasecmp(cmd, "SET")) {
210                 cprintf("%d Send configuration...\n", SEND_LISTING);
211                 a = 0;
212                 while (client_gets(buf), strcmp(buf, "000")) {
213                         switch (a) {
214                         case 0:
215                                 safestrncpy(config.c_nodename, buf,
216                                             sizeof config.c_nodename);
217                                 break;
218                         case 1:
219                                 safestrncpy(config.c_fqdn, buf,
220                                             sizeof config.c_fqdn);
221                                 break;
222                         case 2:
223                                 safestrncpy(config.c_humannode, buf,
224                                             sizeof config.c_humannode);
225                                 break;
226                         case 3:
227                                 safestrncpy(config.c_phonenum, buf,
228                                             sizeof config.c_phonenum);
229                                 break;
230                         case 4:
231                                 config.c_creataide = atoi(buf);
232                                 break;
233                         case 5:
234                                 config.c_sleeping = atoi(buf);
235                                 break;
236                         case 6:
237                                 config.c_initax = atoi(buf);
238                                 if (config.c_initax < 1)
239                                         config.c_initax = 1;
240                                 if (config.c_initax > 6)
241                                         config.c_initax = 6;
242                                 break;
243                         case 7:
244                                 config.c_regiscall = atoi(buf);
245                                 if (config.c_regiscall != 0)
246                                         config.c_regiscall = 1;
247                                 break;
248                         case 8:
249                                 config.c_twitdetect = atoi(buf);
250                                 if (config.c_twitdetect != 0)
251                                         config.c_twitdetect = 1;
252                                 break;
253                         case 9:
254                                 safestrncpy(config.c_twitroom, buf,
255                                             sizeof config.c_twitroom);
256                                 break;
257                         case 10:
258                                 safestrncpy(config.c_moreprompt, buf,
259                                             sizeof config.c_moreprompt);
260                                 break;
261                         case 11:
262                                 config.c_restrict = atoi(buf);
263                                 if (config.c_restrict != 0)
264                                         config.c_restrict = 1;
265                                 break;
266                         case 12:
267                                 safestrncpy(config.c_bbs_city, buf,
268                                             sizeof config.c_bbs_city);
269                                 break;
270                         case 13:
271                                 safestrncpy(config.c_sysadm, buf,
272                                             sizeof config.c_sysadm);
273                                 break;
274                         case 14:
275                                 config.c_maxsessions = atoi(buf);
276                                 if (config.c_maxsessions < 1)
277                                         config.c_maxsessions = 1;
278                                 break;
279                         case 15:
280                                 /* placeholder -- field no longer in use */
281                                 break;
282                         case 16:
283                                 config.c_userpurge = atoi(buf);
284                                 break;
285                         case 17:
286                                 config.c_roompurge = atoi(buf);
287                                 break;
288                         case 18:
289                                 safestrncpy(config.c_logpages, buf,
290                                             sizeof config.c_logpages);
291                                 break;
292                         case 19:
293                                 config.c_createax = atoi(buf);
294                                 if (config.c_createax < 1)
295                                         config.c_createax = 1;
296                                 if (config.c_createax > 6)
297                                         config.c_createax = 6;
298                                 break;
299                         case 20:
300                                 if (atoi(buf) >= 8192)
301                                         config.c_maxmsglen = atoi(buf);
302                                 break;
303                         case 21:
304                                 if (atoi(buf) >= 2)
305                                         config.c_min_workers = atoi(buf);
306                         case 22:
307                                 if (atoi(buf) >= config.c_min_workers)
308                                         config.c_max_workers = atoi(buf);
309                         case 23:
310                                 config.c_pop3_port = atoi(buf);
311                                 break;
312                         case 24:
313                                 config.c_smtp_port = atoi(buf);
314                                 break;
315                         case 25:
316                                 config.c_rfc822_strict_from = atoi(buf);
317                                 break;
318                         case 26:
319                                 config.c_aide_zap = atoi(buf);
320                                 if (config.c_aide_zap != 0)
321                                         config.c_aide_zap = 1;
322                                 break;
323                         case 27:
324                                 config.c_imap_port = atoi(buf);
325                                 break;
326                         case 28:
327                                 config.c_net_freq = atol(buf);
328                                 break;
329                         case 29:
330                                 config.c_disable_newu = atoi(buf);
331                                 if (config.c_disable_newu != 0)
332                                         config.c_disable_newu = 1;
333                                 break;
334                         case 30:
335                                 config.c_aide_mailboxes = atoi(buf);
336                                 if (config.c_aide_mailboxes != 0)
337                                         config.c_aide_mailboxes = 1;
338                                 break;
339                         case 31:
340                                 if ((config.c_purge_hour >= 0)
341                                    && (config.c_purge_hour <= 23)) {
342                                         config.c_purge_hour = atoi(buf);
343                                 }
344                                 break;
345 #ifdef HAVE_LDAP
346                         case 32:
347                                 safestrncpy(config.c_ldap_host, buf,
348                                             sizeof config.c_ldap_host);
349                                 break;
350                         case 33:
351                                 config.c_ldap_port = atoi(buf);
352                                 break;
353 #endif
354                         }
355                         ++a;
356                 }
357                 put_config();
358                 snprintf(buf, sizeof buf,
359                          "Global system configuration edited by %s\n",
360                          CC->curr_user);
361                 aide_message(buf);
362
363                 if (strlen(config.c_logpages) > 0)
364                         create_room(config.c_logpages, 3, "", 0, 1, 1);
365         }
366
367         else if (!strcasecmp(cmd, "GETSYS")) {
368                 extract(confname, argbuf, 1);
369                 confptr = CtdlGetSysConfig(confname);
370                 if (confptr != NULL) {
371                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
372                         client_write(confptr, strlen(confptr));
373                         if (confptr[strlen(confptr) - 1] != 10)
374                                 client_write("\n", 1);
375                         cprintf("000\n");
376                         phree(confptr);
377                 } else {
378                         cprintf("%d No such configuration.\n",
379                                 ERROR + ILLEGAL_VALUE);
380                 }
381         }
382
383         else if (!strcasecmp(cmd, "PUTSYS")) {
384                 extract(confname, argbuf, 1);
385                 cprintf("%d %s\n", SEND_LISTING, confname);
386                 confptr = CtdlReadMessageBody("000",
387                                 config.c_maxmsglen, NULL, 0);
388                 CtdlPutSysConfig(confname, confptr);
389                 phree(confptr);
390         }
391
392         else {
393                 cprintf("%d Illegal option(s) specified.\n",
394                         ERROR + ILLEGAL_VALUE);
395         }
396 }