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