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