* control.c: fixed a potential concurrency/race condition in
[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 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 #  include <sys/time.h>
21 # else
22 #  include <time.h>
23 # endif
24 #endif
25
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "control.h"
34 #include "serv_extensions.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "citserver.h"
40 #include "tools.h"
41 #include "room_ops.h"
42
43 #ifndef HAVE_SNPRINTF
44 #include "snprintf.h"
45 #endif
46
47 struct CitControl CitControl;
48 extern struct config config;
49 FILE *control_fp = NULL;
50
51 /*
52  * get_control  -  read the control record into memory.
53  */
54 void get_control(void)
55 {
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(
64 #ifndef HAVE_DATA_DIR
65                                                    "."
66 #else
67                                                    DATA_DIR
68 #endif
69                                                    "/citadel.control", "rb+");
70                 if (control_fp != NULL) {
71                         fchown(fileno(control_fp), config.c_ctdluid, -1);
72                 }
73         }
74         if (control_fp == NULL) {
75                 control_fp = fopen(
76 #ifndef HAVE_DATA_DIR
77                                                    "."
78 #else
79                                                    DATA_DIR
80 #endif
81                                                    "/citadel.control", "wb+");
82                 if (control_fp != NULL) {
83                         fchown(fileno(control_fp), config.c_ctdluid, -1);
84                         memset(&CitControl, 0, sizeof(struct CitControl));
85                         fwrite(&CitControl, sizeof(struct CitControl),
86                                1, control_fp);
87                         rewind(control_fp);
88                 }
89         }
90         if (control_fp == NULL) {
91                 lprintf(CTDL_ALERT, "ERROR opening citadel.control: %s\n",
92                         strerror(errno));
93                 return;
94         }
95
96         rewind(control_fp);
97         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
98 }
99
100 /*
101  * put_control  -  write the control record to disk.
102  */
103 void put_control(void)
104 {
105
106         if (control_fp != NULL) {
107                 rewind(control_fp);
108                 fwrite(&CitControl, sizeof(struct CitControl), 1,
109                        control_fp);
110                 fflush(control_fp);
111         }
112 }
113
114
115 /*
116  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
117  */
118 long get_new_message_number(void)
119 {
120         long retval = 0L;
121         begin_critical_section(S_CONTROL);
122         get_control();
123         retval = ++CitControl.MMhighest;
124         put_control();
125         end_critical_section(S_CONTROL);
126         return(retval);
127 }
128
129
130 /*
131  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
132  */
133 long get_new_user_number(void)
134 {
135         long retval = 0L;
136         begin_critical_section(S_CONTROL);
137         get_control();
138         retval = ++CitControl.MMnextuser;
139         put_control();
140         end_critical_section(S_CONTROL);
141         return(retval);
142 }
143
144
145
146 /*
147  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
148  */
149 long get_new_room_number(void)
150 {
151         long retval = 0L;
152         begin_critical_section(S_CONTROL);
153         get_control();
154         retval = ++CitControl.MMnextroom;
155         put_control();
156         end_critical_section(S_CONTROL);
157         return(retval);
158 }
159
160
161
162 /* 
163  * Get or set global configuration options
164  */
165 void cmd_conf(char *argbuf)
166 {
167         char cmd[16];
168         char buf[256];
169         int a;
170         char *confptr;
171         char confname[128];
172
173         if (CtdlAccessCheck(ac_aide)) return;
174
175         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
176         if (!strcasecmp(cmd, "GET")) {
177                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
178                 cprintf("%s\n", config.c_nodename);
179                 cprintf("%s\n", config.c_fqdn);
180                 cprintf("%s\n", config.c_humannode);
181                 cprintf("%s\n", config.c_phonenum);
182                 cprintf("%d\n", config.c_creataide);
183                 cprintf("%d\n", config.c_sleeping);
184                 cprintf("%d\n", config.c_initax);
185                 cprintf("%d\n", config.c_regiscall);
186                 cprintf("%d\n", config.c_twitdetect);
187                 cprintf("%s\n", config.c_twitroom);
188                 cprintf("%s\n", config.c_moreprompt);
189                 cprintf("%d\n", config.c_restrict);
190                 cprintf("%s\n", config.c_site_location);
191                 cprintf("%s\n", config.c_sysadm);
192                 cprintf("%d\n", config.c_maxsessions);
193                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
194                 cprintf("%d\n", config.c_userpurge);
195                 cprintf("%d\n", config.c_roompurge);
196                 cprintf("%s\n", config.c_logpages);
197                 cprintf("%d\n", config.c_createax);
198                 cprintf("%ld\n", config.c_maxmsglen);
199                 cprintf("%d\n", config.c_min_workers);
200                 cprintf("%d\n", config.c_max_workers);
201                 cprintf("%d\n", config.c_pop3_port);
202                 cprintf("%d\n", config.c_smtp_port);
203                 cprintf("%d\n", config.c_rfc822_strict_from);
204                 cprintf("%d\n", config.c_aide_zap);
205                 cprintf("%d\n", config.c_imap_port);
206                 cprintf("%ld\n", config.c_net_freq);
207                 cprintf("%d\n", config.c_disable_newu);
208                 cprintf("1\n"); /* niu */
209                 cprintf("%d\n", config.c_purge_hour);
210 #ifdef HAVE_LDAP
211                 cprintf("%s\n", config.c_ldap_host);
212                 cprintf("%d\n", config.c_ldap_port);
213                 cprintf("%s\n", config.c_ldap_base_dn);
214                 cprintf("%s\n", config.c_ldap_bind_dn);
215                 cprintf("%s\n", config.c_ldap_bind_pw);
216 #else
217                 cprintf("\n");
218                 cprintf("0\n");
219                 cprintf("\n");
220                 cprintf("\n");
221                 cprintf("\n");
222 #endif
223                 cprintf("%s\n", config.c_ip_addr);
224                 cprintf("%d\n", config.c_msa_port);
225                 cprintf("%d\n", config.c_imaps_port);
226                 cprintf("%d\n", config.c_pop3s_port);
227                 cprintf("%d\n", config.c_smtps_port);
228                 cprintf("%d\n", config.c_enable_fulltext);
229                 cprintf("%d\n", config.c_auto_cull);
230                 cprintf("%d\n", config.c_instant_expunge);
231                 cprintf("%d\n", config.c_allow_spoofing);
232                 cprintf("000\n");
233         }
234
235         else if (!strcasecmp(cmd, "SET")) {
236                 unbuffer_output();
237                 cprintf("%d Send configuration...\n", SEND_LISTING);
238                 a = 0;
239                 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
240                         switch (a) {
241                         case 0:
242                                 safestrncpy(config.c_nodename, buf,
243                                             sizeof config.c_nodename);
244                                 break;
245                         case 1:
246                                 safestrncpy(config.c_fqdn, buf,
247                                             sizeof config.c_fqdn);
248                                 break;
249                         case 2:
250                                 safestrncpy(config.c_humannode, buf,
251                                             sizeof config.c_humannode);
252                                 break;
253                         case 3:
254                                 safestrncpy(config.c_phonenum, buf,
255                                             sizeof config.c_phonenum);
256                                 break;
257                         case 4:
258                                 config.c_creataide = atoi(buf);
259                                 break;
260                         case 5:
261                                 config.c_sleeping = atoi(buf);
262                                 break;
263                         case 6:
264                                 config.c_initax = atoi(buf);
265                                 if (config.c_initax < 1)
266                                         config.c_initax = 1;
267                                 if (config.c_initax > 6)
268                                         config.c_initax = 6;
269                                 break;
270                         case 7:
271                                 config.c_regiscall = atoi(buf);
272                                 if (config.c_regiscall != 0)
273                                         config.c_regiscall = 1;
274                                 break;
275                         case 8:
276                                 config.c_twitdetect = atoi(buf);
277                                 if (config.c_twitdetect != 0)
278                                         config.c_twitdetect = 1;
279                                 break;
280                         case 9:
281                                 safestrncpy(config.c_twitroom, buf,
282                                             sizeof config.c_twitroom);
283                                 break;
284                         case 10:
285                                 safestrncpy(config.c_moreprompt, buf,
286                                             sizeof config.c_moreprompt);
287                                 break;
288                         case 11:
289                                 config.c_restrict = atoi(buf);
290                                 if (config.c_restrict != 0)
291                                         config.c_restrict = 1;
292                                 break;
293                         case 12:
294                                 safestrncpy(config.c_site_location, buf,
295                                             sizeof config.c_site_location);
296                                 break;
297                         case 13:
298                                 safestrncpy(config.c_sysadm, buf,
299                                             sizeof config.c_sysadm);
300                                 break;
301                         case 14:
302                                 config.c_maxsessions = atoi(buf);
303                                 if (config.c_maxsessions < 0)
304                                         config.c_maxsessions = 0;
305                                 break;
306                         case 15:
307                                 /* placeholder -- field no longer in use */
308                                 break;
309                         case 16:
310                                 config.c_userpurge = atoi(buf);
311                                 break;
312                         case 17:
313                                 config.c_roompurge = atoi(buf);
314                                 break;
315                         case 18:
316                                 safestrncpy(config.c_logpages, buf,
317                                             sizeof config.c_logpages);
318                                 break;
319                         case 19:
320                                 config.c_createax = atoi(buf);
321                                 if (config.c_createax < 1)
322                                         config.c_createax = 1;
323                                 if (config.c_createax > 6)
324                                         config.c_createax = 6;
325                                 break;
326                         case 20:
327                                 if (atoi(buf) >= 8192)
328                                         config.c_maxmsglen = atoi(buf);
329                                 break;
330                         case 21:
331                                 if (atoi(buf) >= 2)
332                                         config.c_min_workers = atoi(buf);
333                         case 22:
334                                 if (atoi(buf) >= config.c_min_workers)
335                                         config.c_max_workers = atoi(buf);
336                         case 23:
337                                 config.c_pop3_port = atoi(buf);
338                                 break;
339                         case 24:
340                                 config.c_smtp_port = atoi(buf);
341                                 break;
342                         case 25:
343                                 config.c_rfc822_strict_from = atoi(buf);
344                                 break;
345                         case 26:
346                                 config.c_aide_zap = atoi(buf);
347                                 if (config.c_aide_zap != 0)
348                                         config.c_aide_zap = 1;
349                                 break;
350                         case 27:
351                                 config.c_imap_port = atoi(buf);
352                                 break;
353                         case 28:
354                                 config.c_net_freq = atol(buf);
355                                 break;
356                         case 29:
357                                 config.c_disable_newu = atoi(buf);
358                                 if (config.c_disable_newu != 0)
359                                         config.c_disable_newu = 1;
360                                 break;
361                         case 30:
362                                 /* niu */
363                                 break;
364                         case 31:
365                                 if ((config.c_purge_hour >= 0)
366                                    && (config.c_purge_hour <= 23)) {
367                                         config.c_purge_hour = atoi(buf);
368                                 }
369                                 break;
370 #ifdef HAVE_LDAP
371                         case 32:
372                                 safestrncpy(config.c_ldap_host, buf,
373                                             sizeof config.c_ldap_host);
374                                 break;
375                         case 33:
376                                 config.c_ldap_port = atoi(buf);
377                                 break;
378                         case 34:
379                                 safestrncpy(config.c_ldap_base_dn, buf,
380                                             sizeof config.c_ldap_base_dn);
381                                 break;
382                         case 35:
383                                 safestrncpy(config.c_ldap_bind_dn, buf,
384                                             sizeof config.c_ldap_bind_dn);
385                                 break;
386                         case 36:
387                                 safestrncpy(config.c_ldap_bind_pw, buf,
388                                             sizeof config.c_ldap_bind_pw);
389                                 break;
390 #endif
391                         case 37:
392                                 safestrncpy(config.c_ip_addr, buf,
393                                                 sizeof config.c_ip_addr);
394                         case 38:
395                                 config.c_msa_port = atoi(buf);
396                                 break;
397                         case 39:
398                                 config.c_imaps_port = atoi(buf);
399                                 break;
400                         case 40:
401                                 config.c_pop3s_port = atoi(buf);
402                                 break;
403                         case 41:
404                                 config.c_smtps_port = atoi(buf);
405                                 break;
406                         case 42:
407                                 config.c_enable_fulltext = atoi(buf);
408                                 break;
409                         case 43:
410                                 config.c_auto_cull = atoi(buf);
411                                 break;
412                         case 44:
413                                 config.c_instant_expunge = atoi(buf);
414                                 break;
415                         case 45:
416                                 config.c_allow_spoofing = atoi(buf);
417                                 break;
418                         }
419                         ++a;
420                 }
421                 put_config();
422                 snprintf(buf, sizeof buf,
423                          "The global system configuration has been edited by %s.\n",
424                          CC->curr_user);
425                 aide_message(buf);
426
427                 if (strlen(config.c_logpages) > 0)
428                         create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
429
430                 /* If full text indexing has been disabled, invalidate the
431                  * index so it doesn't try to use it later.
432                  */
433                 if (!config.c_enable_fulltext == 0) {
434                         CitControl.fulltext_wordbreaker = 0;
435                         put_control();
436                 }
437         }
438
439         else if (!strcasecmp(cmd, "GETSYS")) {
440                 extract_token(confname, argbuf, 1, '|', sizeof confname);
441                 confptr = CtdlGetSysConfig(confname);
442                 if (confptr != NULL) {
443                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
444                         client_write(confptr, strlen(confptr));
445                         if (confptr[strlen(confptr) - 1] != 10)
446                                 client_write("\n", 1);
447                         cprintf("000\n");
448                         free(confptr);
449                 } else {
450                         cprintf("%d No such configuration.\n",
451                                 ERROR + ILLEGAL_VALUE);
452                 }
453         }
454
455         else if (!strcasecmp(cmd, "PUTSYS")) {
456                 extract_token(confname, argbuf, 1, '|', sizeof confname);
457                 unbuffer_output();
458                 cprintf("%d %s\n", SEND_LISTING, confname);
459                 confptr = CtdlReadMessageBody("000",
460                                 config.c_maxmsglen, NULL, 0);
461                 CtdlPutSysConfig(confname, confptr);
462                 free(confptr);
463         }
464
465         else {
466                 cprintf("%d Illegal option(s) specified.\n",
467                         ERROR + ILLEGAL_VALUE);
468         }
469 }