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