* Network run frequency is now a site-definable setting
[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("%d\n", config.c_net_freq);
190                 cprintf("000\n");
191                 }
192
193         else if (!strcasecmp(cmd, "SET")) {
194                 cprintf("%d Send configuration...\n", SEND_LISTING);
195                 a = 0;
196                 while (client_gets(buf), strcmp(buf, "000")) {
197                     switch(a) {
198                         case 0: safestrncpy(config.c_nodename, buf,
199                                         sizeof config.c_nodename);
200                                 break;
201                         case 1: safestrncpy(config.c_fqdn, buf,
202                                         sizeof config.c_fqdn);
203                                 break;
204                         case 2: safestrncpy(config.c_humannode, buf,
205                                         sizeof config.c_humannode);
206                                 break;
207                         case 3: safestrncpy(config.c_phonenum, buf,
208                                         sizeof config.c_phonenum);
209                                 break;
210                         case 4: config.c_creataide = atoi(buf);
211                                 break;
212                         case 5: config.c_sleeping = atoi(buf);
213                                 break;
214                         case 6: config.c_initax = atoi(buf);
215                                 if (config.c_initax < 1) config.c_initax = 1;
216                                 if (config.c_initax > 6) config.c_initax = 6;
217                                 break;
218                         case 7: config.c_regiscall = atoi(buf);
219                                 if (config.c_regiscall != 0)
220                                         config.c_regiscall = 1;
221                                 break;
222                         case 8: config.c_twitdetect = atoi(buf);
223                                 if (config.c_twitdetect != 0)
224                                         config.c_twitdetect = 1;
225                                 break;
226                         case 9: safestrncpy(config.c_twitroom, buf,
227                                         sizeof config.c_twitroom);
228                                 break;
229                         case 10: safestrncpy(config.c_moreprompt, buf,
230                                         sizeof config.c_moreprompt);
231                                 break;
232                         case 11: config.c_restrict = atoi(buf);
233                                 if (config.c_restrict != 0)
234                                         config.c_restrict = 1;
235                                 break;
236                         case 12: safestrncpy(config.c_bbs_city, buf,
237                                         sizeof config.c_bbs_city);
238                                 break;
239                         case 13: safestrncpy(config.c_sysadm, buf,
240                                         sizeof config.c_sysadm);
241                                 break;
242                         case 14: config.c_maxsessions = atoi(buf);
243                                 if (config.c_maxsessions < 1)
244                                         config.c_maxsessions = 1;
245                                 break;
246                         case 15: safestrncpy(config.c_net_password, buf,
247                                         sizeof config.c_net_password);
248                                 break;
249                         case 16: config.c_userpurge = atoi(buf);
250                                 break;
251                         case 17: config.c_roompurge = atoi(buf);
252                                 break;
253                         case 18: safestrncpy(config.c_logpages, buf,
254                                         sizeof config.c_logpages);
255                                 break;
256                         case 19: config.c_createax = atoi(buf);
257                                 if (config.c_createax < 1)
258                                         config.c_createax = 1;
259                                 if (config.c_createax > 6)
260                                         config.c_createax = 6;
261                                 break;
262                         case 20: if (atoi(buf) >= 8192)
263                                         config.c_maxmsglen = atoi(buf);
264                                 break;
265                         case 21: if (atoi(buf) >= 2)
266                                         config.c_min_workers = atoi(buf);
267                         case 22: if (atoi(buf) >= config.c_min_workers)
268                                         config.c_max_workers = atoi(buf);
269                         case 23: config.c_pop3_port = atoi(buf);
270                                 break;
271                         case 24: config.c_smtp_port = atoi(buf);
272                                 break;
273                         case 25: config.c_default_filter = atoi(buf);
274                                 break;
275                         case 26: config.c_aide_zap = atoi(buf);
276                                 if (config.c_aide_zap != 0)
277                                         config.c_aide_zap = 1;
278                                 break;
279                         case 27: config.c_imap_port = atoi(buf);
280                                 break;
281                         case 28: config.c_net_freq = atol(buf);
282                                 break;
283                         }
284                     ++a;
285                     }
286                 put_config();
287                 snprintf(buf,sizeof buf,
288                          "Global system configuration edited by %s\n",
289                          CC->curr_user);
290                 aide_message(buf);
291
292                 if (strlen(config.c_logpages) > 0)
293                         create_room(config.c_logpages, 3, "", 0, 1);
294                 }
295
296         else if (!strcasecmp(cmd, "GETSYS")) {
297                 extract(confname, argbuf, 1);
298                 confptr = CtdlGetSysConfig(confname);
299                 if (confptr != NULL) {
300                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
301                         client_write(confptr, strlen(confptr));
302                         if (confptr[strlen(confptr)-1] != 10)
303                                 client_write("\n", 1);
304                         cprintf("000\n");
305                         phree(confptr);
306                 }
307                 else {
308                         cprintf("%d No such configuration.\n",
309                                 ERROR+ILLEGAL_VALUE);
310                 }
311         }
312
313         else if (!strcasecmp(cmd, "PUTSYS")) {
314                 extract(confname, argbuf, 1);
315                 cprintf("%d %s\n", SEND_LISTING, confname);
316                 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL);
317                 CtdlPutSysConfig(confname, confptr);
318                 phree(confptr);
319         }
320
321         else {
322                 cprintf("%d Illegal option(s) specified.\n",
323                         ERROR+ILLEGAL_VALUE);
324                 }
325         }