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