* Client and server options to disable self-service user account creation
[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 <syslog.h>
35 #include <sys/types.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "control.h"
39 #include "dynloader.h"
40 #include "sysdep_decls.h"
41 #include "support.h"
42 #include "config.h"
43 #include "msgbase.h"
44 #include "citserver.h"
45 #include "tools.h"
46 #include "room_ops.h"
47
48 #ifndef HAVE_SNPRINTF
49 #include "snprintf.h"
50 #endif
51
52 struct CitControl CitControl;
53 struct config config;
54 FILE *control_fp = NULL;
55
56 /*
57  * get_control  -  read the control record into memory.
58  */
59 void get_control(void) {
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_bbsuid, -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_bbsuid, -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(1, "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         if (control_fp != NULL) {
98                 rewind(control_fp);
99                 fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
100                 fflush(control_fp);
101                 }
102         }
103
104
105 /*
106  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
107  */
108 long get_new_message_number(void) {
109         begin_critical_section(S_CONTROL);
110         get_control();
111         ++CitControl.MMhighest;
112         put_control();
113         end_critical_section(S_CONTROL);
114         return(CitControl.MMhighest);
115         }
116
117
118 /*
119  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
120  */
121 long get_new_user_number(void) {
122         begin_critical_section(S_CONTROL);
123         get_control();
124         ++CitControl.MMnextuser;
125         put_control();
126         end_critical_section(S_CONTROL);
127         return(CitControl.MMnextuser);
128         }
129
130
131
132 /*
133  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
134  */
135 long get_new_room_number(void) {
136         begin_critical_section(S_CONTROL);
137         get_control();
138         ++CitControl.MMnextroom;
139         put_control();
140         end_critical_section(S_CONTROL);
141         return(CitControl.MMnextroom);
142         }
143
144
145
146 /* 
147  * Get or set global configuration options
148  */
149 void cmd_conf(char *argbuf) {
150         char cmd[SIZ];
151         char buf[SIZ];
152         int a;
153         char *confptr;
154         char confname[SIZ];
155
156         if (CtdlAccessCheck(ac_aide)) return;
157
158         extract(cmd, argbuf, 0);
159         if (!strcasecmp(cmd, "GET")) {
160                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
161                 cprintf("%s\n", config.c_nodename);
162                 cprintf("%s\n", config.c_fqdn);
163                 cprintf("%s\n", config.c_humannode);
164                 cprintf("%s\n", config.c_phonenum);
165                 cprintf("%d\n", config.c_creataide);
166                 cprintf("%d\n", config.c_sleeping);
167                 cprintf("%d\n", config.c_initax);
168                 cprintf("%d\n", config.c_regiscall);
169                 cprintf("%d\n", config.c_twitdetect);
170                 cprintf("%s\n", config.c_twitroom);
171                 cprintf("%s\n", config.c_moreprompt);
172                 cprintf("%d\n", config.c_restrict);
173                 cprintf("%s\n", config.c_bbs_city);
174                 cprintf("%s\n", config.c_sysadm);
175                 cprintf("%d\n", config.c_maxsessions);
176                 cprintf("%s\n", config.c_net_password);
177                 cprintf("%d\n", config.c_userpurge);
178                 cprintf("%d\n", config.c_roompurge);
179                 cprintf("%s\n", config.c_logpages);
180                 cprintf("%d\n", config.c_createax);
181                 cprintf("%ld\n", config.c_maxmsglen);
182                 cprintf("%d\n", config.c_min_workers);
183                 cprintf("%d\n", config.c_max_workers);
184                 cprintf("%d\n", config.c_pop3_port);
185                 cprintf("%d\n", config.c_smtp_port);
186                 cprintf("%d\n", config.c_default_filter);
187                 cprintf("%d\n", config.c_aide_zap);
188                 cprintf("%d\n", config.c_imap_port);
189                 cprintf("%ld\n", config.c_net_freq);
190                 cprintf("%d\n", config.c_disable_newu);
191                 cprintf("000\n");
192                 }
193
194         else if (!strcasecmp(cmd, "SET")) {
195                 cprintf("%d Send configuration...\n", SEND_LISTING);
196                 a = 0;
197                 while (client_gets(buf), strcmp(buf, "000")) {
198                     switch(a) {
199                         case 0: safestrncpy(config.c_nodename, buf,
200                                         sizeof config.c_nodename);
201                                 break;
202                         case 1: safestrncpy(config.c_fqdn, buf,
203                                         sizeof config.c_fqdn);
204                                 break;
205                         case 2: safestrncpy(config.c_humannode, buf,
206                                         sizeof config.c_humannode);
207                                 break;
208                         case 3: safestrncpy(config.c_phonenum, buf,
209                                         sizeof config.c_phonenum);
210                                 break;
211                         case 4: config.c_creataide = atoi(buf);
212                                 break;
213                         case 5: config.c_sleeping = atoi(buf);
214                                 break;
215                         case 6: config.c_initax = atoi(buf);
216                                 if (config.c_initax < 1) config.c_initax = 1;
217                                 if (config.c_initax > 6) config.c_initax = 6;
218                                 break;
219                         case 7: config.c_regiscall = atoi(buf);
220                                 if (config.c_regiscall != 0)
221                                         config.c_regiscall = 1;
222                                 break;
223                         case 8: config.c_twitdetect = atoi(buf);
224                                 if (config.c_twitdetect != 0)
225                                         config.c_twitdetect = 1;
226                                 break;
227                         case 9: safestrncpy(config.c_twitroom, buf,
228                                         sizeof config.c_twitroom);
229                                 break;
230                         case 10: safestrncpy(config.c_moreprompt, buf,
231                                         sizeof config.c_moreprompt);
232                                 break;
233                         case 11: config.c_restrict = atoi(buf);
234                                 if (config.c_restrict != 0)
235                                         config.c_restrict = 1;
236                                 break;
237                         case 12: safestrncpy(config.c_bbs_city, buf,
238                                         sizeof config.c_bbs_city);
239                                 break;
240                         case 13: safestrncpy(config.c_sysadm, buf,
241                                         sizeof config.c_sysadm);
242                                 break;
243                         case 14: config.c_maxsessions = atoi(buf);
244                                 if (config.c_maxsessions < 1)
245                                         config.c_maxsessions = 1;
246                                 break;
247                         case 15: safestrncpy(config.c_net_password, buf,
248                                         sizeof config.c_net_password);
249                                 break;
250                         case 16: config.c_userpurge = atoi(buf);
251                                 break;
252                         case 17: config.c_roompurge = atoi(buf);
253                                 break;
254                         case 18: safestrncpy(config.c_logpages, buf,
255                                         sizeof config.c_logpages);
256                                 break;
257                         case 19: config.c_createax = atoi(buf);
258                                 if (config.c_createax < 1)
259                                         config.c_createax = 1;
260                                 if (config.c_createax > 6)
261                                         config.c_createax = 6;
262                                 break;
263                         case 20: if (atoi(buf) >= 8192)
264                                         config.c_maxmsglen = atoi(buf);
265                                 break;
266                         case 21: if (atoi(buf) >= 2)
267                                         config.c_min_workers = atoi(buf);
268                         case 22: if (atoi(buf) >= config.c_min_workers)
269                                         config.c_max_workers = atoi(buf);
270                         case 23: config.c_pop3_port = atoi(buf);
271                                 break;
272                         case 24: config.c_smtp_port = atoi(buf);
273                                 break;
274                         case 25: config.c_default_filter = atoi(buf);
275                                 break;
276                         case 26: config.c_aide_zap = atoi(buf);
277                                 if (config.c_aide_zap != 0)
278                                         config.c_aide_zap = 1;
279                                 break;
280                         case 27: config.c_imap_port = atoi(buf);
281                                 break;
282                         case 28: config.c_net_freq = atol(buf);
283                                 break;
284                         case 29: config.c_disable_newu = atoi(buf);
285                                 if (config.c_disable_newu != 0)
286                                         config.c_disable_newu = 1;
287                                 break;
288                         }
289                     ++a;
290                     }
291                 put_config();
292                 snprintf(buf,sizeof buf,
293                          "Global system configuration edited by %s\n",
294                          CC->curr_user);
295                 aide_message(buf);
296
297                 if (strlen(config.c_logpages) > 0)
298                         create_room(config.c_logpages, 3, "", 0, 1);
299                 }
300
301         else if (!strcasecmp(cmd, "GETSYS")) {
302                 extract(confname, argbuf, 1);
303                 confptr = CtdlGetSysConfig(confname);
304                 if (confptr != NULL) {
305                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
306                         client_write(confptr, strlen(confptr));
307                         if (confptr[strlen(confptr)-1] != 10)
308                                 client_write("\n", 1);
309                         cprintf("000\n");
310                         phree(confptr);
311                 }
312                 else {
313                         cprintf("%d No such configuration.\n",
314                                 ERROR+ILLEGAL_VALUE);
315                 }
316         }
317
318         else if (!strcasecmp(cmd, "PUTSYS")) {
319                 extract(confname, argbuf, 1);
320                 cprintf("%d %s\n", SEND_LISTING, confname);
321                 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL);
322                 CtdlPutSysConfig(confname, confptr);
323                 phree(confptr);
324         }
325
326         else {
327                 cprintf("%d Illegal option(s) specified.\n",
328                         ERROR+ILLEGAL_VALUE);
329                 }
330         }