0b882e85529ba1e8782dd83f4894e23e80b9fae9
[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_ctdluid, -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_ctdluid, -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[16];
157         char buf[256];
158         int a;
159         char *confptr;
160         char confname[128];
161
162         if (CtdlAccessCheck(ac_aide)) return;
163
164         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
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_site_location);
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"); /* niu */
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("%d\n", config.c_msa_port);
214                 cprintf("%d\n", config.c_imaps_port);
215                 cprintf("%d\n", config.c_pop3s_port);
216                 cprintf("%d\n", config.c_smtps_port);
217                 cprintf("%d\n", config.c_enable_fulltext);
218                 cprintf("%d\n", config.c_auto_cull);
219                 cprintf("000\n");
220         }
221
222         else if (!strcasecmp(cmd, "SET")) {
223                 unbuffer_output();
224                 cprintf("%d Send configuration...\n", SEND_LISTING);
225                 a = 0;
226                 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
227                         switch (a) {
228                         case 0:
229                                 safestrncpy(config.c_nodename, buf,
230                                             sizeof config.c_nodename);
231                                 break;
232                         case 1:
233                                 safestrncpy(config.c_fqdn, buf,
234                                             sizeof config.c_fqdn);
235                                 break;
236                         case 2:
237                                 safestrncpy(config.c_humannode, buf,
238                                             sizeof config.c_humannode);
239                                 break;
240                         case 3:
241                                 safestrncpy(config.c_phonenum, buf,
242                                             sizeof config.c_phonenum);
243                                 break;
244                         case 4:
245                                 config.c_creataide = atoi(buf);
246                                 break;
247                         case 5:
248                                 config.c_sleeping = atoi(buf);
249                                 break;
250                         case 6:
251                                 config.c_initax = atoi(buf);
252                                 if (config.c_initax < 1)
253                                         config.c_initax = 1;
254                                 if (config.c_initax > 6)
255                                         config.c_initax = 6;
256                                 break;
257                         case 7:
258                                 config.c_regiscall = atoi(buf);
259                                 if (config.c_regiscall != 0)
260                                         config.c_regiscall = 1;
261                                 break;
262                         case 8:
263                                 config.c_twitdetect = atoi(buf);
264                                 if (config.c_twitdetect != 0)
265                                         config.c_twitdetect = 1;
266                                 break;
267                         case 9:
268                                 safestrncpy(config.c_twitroom, buf,
269                                             sizeof config.c_twitroom);
270                                 break;
271                         case 10:
272                                 safestrncpy(config.c_moreprompt, buf,
273                                             sizeof config.c_moreprompt);
274                                 break;
275                         case 11:
276                                 config.c_restrict = atoi(buf);
277                                 if (config.c_restrict != 0)
278                                         config.c_restrict = 1;
279                                 break;
280                         case 12:
281                                 safestrncpy(config.c_site_location, buf,
282                                             sizeof config.c_site_location);
283                                 break;
284                         case 13:
285                                 safestrncpy(config.c_sysadm, buf,
286                                             sizeof config.c_sysadm);
287                                 break;
288                         case 14:
289                                 config.c_maxsessions = atoi(buf);
290                                 if (config.c_maxsessions < 0)
291                                         config.c_maxsessions = 0;
292                                 break;
293                         case 15:
294                                 /* placeholder -- field no longer in use */
295                                 break;
296                         case 16:
297                                 config.c_userpurge = atoi(buf);
298                                 break;
299                         case 17:
300                                 config.c_roompurge = atoi(buf);
301                                 break;
302                         case 18:
303                                 safestrncpy(config.c_logpages, buf,
304                                             sizeof config.c_logpages);
305                                 break;
306                         case 19:
307                                 config.c_createax = atoi(buf);
308                                 if (config.c_createax < 1)
309                                         config.c_createax = 1;
310                                 if (config.c_createax > 6)
311                                         config.c_createax = 6;
312                                 break;
313                         case 20:
314                                 if (atoi(buf) >= 8192)
315                                         config.c_maxmsglen = atoi(buf);
316                                 break;
317                         case 21:
318                                 if (atoi(buf) >= 2)
319                                         config.c_min_workers = atoi(buf);
320                         case 22:
321                                 if (atoi(buf) >= config.c_min_workers)
322                                         config.c_max_workers = atoi(buf);
323                         case 23:
324                                 config.c_pop3_port = atoi(buf);
325                                 break;
326                         case 24:
327                                 config.c_smtp_port = atoi(buf);
328                                 break;
329                         case 25:
330                                 config.c_rfc822_strict_from = atoi(buf);
331                                 break;
332                         case 26:
333                                 config.c_aide_zap = atoi(buf);
334                                 if (config.c_aide_zap != 0)
335                                         config.c_aide_zap = 1;
336                                 break;
337                         case 27:
338                                 config.c_imap_port = atoi(buf);
339                                 break;
340                         case 28:
341                                 config.c_net_freq = atol(buf);
342                                 break;
343                         case 29:
344                                 config.c_disable_newu = atoi(buf);
345                                 if (config.c_disable_newu != 0)
346                                         config.c_disable_newu = 1;
347                                 break;
348                         case 30:
349                                 /* niu */
350                                 break;
351                         case 31:
352                                 if ((config.c_purge_hour >= 0)
353                                    && (config.c_purge_hour <= 23)) {
354                                         config.c_purge_hour = atoi(buf);
355                                 }
356                                 break;
357 #ifdef HAVE_LDAP
358                         case 32:
359                                 safestrncpy(config.c_ldap_host, buf,
360                                             sizeof config.c_ldap_host);
361                                 break;
362                         case 33:
363                                 config.c_ldap_port = atoi(buf);
364                                 break;
365                         case 34:
366                                 safestrncpy(config.c_ldap_base_dn, buf,
367                                             sizeof config.c_ldap_base_dn);
368                                 break;
369                         case 35:
370                                 safestrncpy(config.c_ldap_bind_dn, buf,
371                                             sizeof config.c_ldap_bind_dn);
372                                 break;
373                         case 36:
374                                 safestrncpy(config.c_ldap_bind_pw, buf,
375                                             sizeof config.c_ldap_bind_pw);
376                                 break;
377 #endif
378                         case 37:
379                                 safestrncpy(config.c_ip_addr, buf,
380                                                 sizeof config.c_ip_addr);
381                         case 38:
382                                 config.c_msa_port = atoi(buf);
383                                 break;
384                         case 39:
385                                 config.c_imaps_port = atoi(buf);
386                                 break;
387                         case 40:
388                                 config.c_pop3s_port = atoi(buf);
389                                 break;
390                         case 41:
391                                 config.c_smtps_port = atoi(buf);
392                                 break;
393                         case 42:
394                                 config.c_enable_fulltext = atoi(buf);
395                                 break;
396                         case 43:
397                                 config.c_auto_cull = atoi(buf);
398                                 break;
399                         }
400                         ++a;
401                 }
402                 put_config();
403                 snprintf(buf, sizeof buf,
404                          "Global system configuration edited by %s\n",
405                          CC->curr_user);
406                 aide_message(buf);
407
408                 if (strlen(config.c_logpages) > 0)
409                         create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
410
411                 /* If full text indexing has been disabled, invalidate the
412                  * index so it doesn't try to use it later.
413                  */
414                 if (!config.c_enable_fulltext == 0) {
415                         CitControl.fulltext_wordbreaker = 0;
416                         put_control();
417                 }
418         }
419
420         else if (!strcasecmp(cmd, "GETSYS")) {
421                 extract_token(confname, argbuf, 1, '|', sizeof confname);
422                 confptr = CtdlGetSysConfig(confname);
423                 if (confptr != NULL) {
424                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
425                         client_write(confptr, strlen(confptr));
426                         if (confptr[strlen(confptr) - 1] != 10)
427                                 client_write("\n", 1);
428                         cprintf("000\n");
429                         free(confptr);
430                 } else {
431                         cprintf("%d No such configuration.\n",
432                                 ERROR + ILLEGAL_VALUE);
433                 }
434         }
435
436         else if (!strcasecmp(cmd, "PUTSYS")) {
437                 extract_token(confname, argbuf, 1, '|', sizeof confname);
438                 unbuffer_output();
439                 cprintf("%d %s\n", SEND_LISTING, confname);
440                 confptr = CtdlReadMessageBody("000",
441                                 config.c_maxmsglen, NULL, 0);
442                 CtdlPutSysConfig(confname, confptr);
443                 free(confptr);
444         }
445
446         else {
447                 cprintf("%d Illegal option(s) specified.\n",
448                         ERROR + ILLEGAL_VALUE);
449         }
450 }