* Allow Aides to zap rooms (site configurable 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 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <time.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <syslog.h>
20 #include <sys/types.h>
21 #include "citadel.h"
22 #include "server.h"
23 #include "control.h"
24 #include "sysdep_decls.h"
25 #include "support.h"
26 #include "config.h"
27 #include "msgbase.h"
28 #include "tools.h"
29 #include "room_ops.h"
30
31 struct CitControl CitControl;
32 struct config config;
33 FILE *control_fp = NULL;
34
35 /*
36  * get_control  -  read the control record into memory.
37  */
38 void get_control(void) {
39
40         /* Zero it out.  If the control record on disk is missing or short,
41          * the system functions with all control record fields initialized
42          * to zero.
43          */
44         memset(&CitControl, 0, sizeof(struct CitControl));
45         if (control_fp == NULL) {
46                 control_fp = fopen("citadel.control", "rb+");
47                 if (control_fp != NULL) {
48                         fchown(fileno(control_fp), config.c_bbsuid, -1);
49                 }
50         }
51         if (control_fp == NULL) {
52                 control_fp = fopen("citadel.control", "wb+");
53                 if (control_fp != NULL) {
54                         fchown(fileno(control_fp), config.c_bbsuid, -1);
55                         memset(&CitControl, 0, sizeof(struct CitControl));
56                         fwrite(&CitControl, sizeof(struct CitControl),
57                                 1, control_fp);
58                         rewind(control_fp);
59                 }
60         }
61         if (control_fp == NULL) {
62                 lprintf(1, "ERROR opening citadel.control: %s\n",
63                         strerror(errno));
64                 return;
65         }
66
67         rewind(control_fp);
68         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
69         }
70
71 /*
72  * put_control  -  write the control record to disk.
73  */
74 void put_control(void) {
75
76         if (control_fp != NULL) {
77                 rewind(control_fp);
78                 fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
79                 fflush(control_fp);
80                 }
81         }
82
83
84 /*
85  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
86  */
87 long get_new_message_number(void) {
88         begin_critical_section(S_CONTROL);
89         get_control();
90         ++CitControl.MMhighest;
91         put_control();
92         end_critical_section(S_CONTROL);
93         return(CitControl.MMhighest);
94         }
95
96
97 /*
98  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
99  */
100 long get_new_user_number(void) {
101         begin_critical_section(S_CONTROL);
102         get_control();
103         ++CitControl.MMnextuser;
104         put_control();
105         end_critical_section(S_CONTROL);
106         return(CitControl.MMnextuser);
107         }
108
109
110
111 /*
112  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
113  */
114 long get_new_room_number(void) {
115         begin_critical_section(S_CONTROL);
116         get_control();
117         ++CitControl.MMnextroom;
118         put_control();
119         end_critical_section(S_CONTROL);
120         return(CitControl.MMnextroom);
121         }
122
123
124
125 /* 
126  * Get or set global configuration options
127  */
128 void cmd_conf(char *argbuf) {
129         char cmd[256];
130         char buf[256];
131         int a;
132         char *confptr;
133         char confname[256];
134
135         if (!(CC->logged_in)) {
136                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
137                 return;
138                 }
139
140         if (CC->usersupp.axlevel < 6) {
141                 cprintf("%d Higher access required.\n",
142                         ERROR+HIGHER_ACCESS_REQUIRED);
143                 return;
144                 }
145
146         extract(cmd, argbuf, 0);
147         if (!strcasecmp(cmd, "GET")) {
148                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
149                 cprintf("%s\n", config.c_nodename);
150                 cprintf("%s\n", config.c_fqdn);
151                 cprintf("%s\n", config.c_humannode);
152                 cprintf("%s\n", config.c_phonenum);
153                 cprintf("%d\n", config.c_creataide);
154                 cprintf("%d\n", config.c_sleeping);
155                 cprintf("%d\n", config.c_initax);
156                 cprintf("%d\n", config.c_regiscall);
157                 cprintf("%d\n", config.c_twitdetect);
158                 cprintf("%s\n", config.c_twitroom);
159                 cprintf("%s\n", config.c_moreprompt);
160                 cprintf("%d\n", config.c_restrict);
161                 cprintf("%s\n", config.c_bbs_city);
162                 cprintf("%s\n", config.c_sysadm);
163                 cprintf("%d\n", config.c_maxsessions);
164                 cprintf("%s\n", config.c_net_password);
165                 cprintf("%d\n", config.c_userpurge);
166                 cprintf("%d\n", config.c_roompurge);
167                 cprintf("%s\n", config.c_logpages);
168                 cprintf("%d\n", config.c_createax);
169                 cprintf("%d\n", config.c_maxmsglen);
170                 cprintf("%d\n", config.c_min_workers);
171                 cprintf("%d\n", config.c_max_workers);
172                 cprintf("%d\n", config.c_pop3_port);
173                 cprintf("%d\n", config.c_smtp_port);
174                 cprintf("%d\n", config.c_default_filter);
175                 cprintf("%d\n", config.c_aide_zap);
176                 cprintf("000\n");
177                 }
178
179         else if (!strcasecmp(cmd, "SET")) {
180                 cprintf("%d Send configuration...\n", SEND_LISTING);
181                 a = 0;
182                 while (client_gets(buf), strcmp(buf, "000")) {
183                     switch(a) {
184                         case 0: safestrncpy(config.c_nodename, buf,
185                                         sizeof config.c_nodename);
186                                 break;
187                         case 1: safestrncpy(config.c_fqdn, buf,
188                                         sizeof config.c_fqdn);
189                                 break;
190                         case 2: safestrncpy(config.c_humannode, buf,
191                                         sizeof config.c_humannode);
192                                 break;
193                         case 3: safestrncpy(config.c_phonenum, buf,
194                                         sizeof config.c_phonenum);
195                                 break;
196                         case 4: config.c_creataide = atoi(buf);
197                                 break;
198                         case 5: config.c_sleeping = atoi(buf);
199                                 break;
200                         case 6: config.c_initax = atoi(buf);
201                                 if (config.c_initax < 1) config.c_initax = 1;
202                                 if (config.c_initax > 6) config.c_initax = 6;
203                                 break;
204                         case 7: config.c_regiscall = atoi(buf);
205                                 if (config.c_regiscall != 0)
206                                         config.c_regiscall = 1;
207                                 break;
208                         case 8: config.c_twitdetect = atoi(buf);
209                                 if (config.c_twitdetect != 0)
210                                         config.c_twitdetect = 1;
211                                 break;
212                         case 9: safestrncpy(config.c_twitroom, buf,
213                                         sizeof config.c_twitroom);
214                                 break;
215                         case 10: safestrncpy(config.c_moreprompt, buf,
216                                         sizeof config.c_moreprompt);
217                                 break;
218                         case 11: config.c_restrict = atoi(buf);
219                                 if (config.c_restrict != 0)
220                                         config.c_restrict = 1;
221                                 break;
222                         case 12: safestrncpy(config.c_bbs_city, buf,
223                                         sizeof config.c_bbs_city);
224                                 break;
225                         case 13: safestrncpy(config.c_sysadm, buf,
226                                         sizeof config.c_sysadm);
227                                 break;
228                         case 14: config.c_maxsessions = atoi(buf);
229                                 if (config.c_maxsessions < 1)
230                                         config.c_maxsessions = 1;
231                                 break;
232                         case 15: safestrncpy(config.c_net_password, buf,
233                                         sizeof config.c_net_password);
234                                 break;
235                         case 16: config.c_userpurge = atoi(buf);
236                                 break;
237                         case 17: config.c_roompurge = atoi(buf);
238                                 break;
239                         case 18: safestrncpy(config.c_logpages, buf,
240                                         sizeof config.c_logpages);
241                                 break;
242                         case 19: config.c_createax = atoi(buf);
243                                 if (config.c_createax < 1)
244                                         config.c_createax = 1;
245                                 if (config.c_createax > 6)
246                                         config.c_createax = 6;
247                                 break;
248                         case 20: if (atoi(buf) >= 8192)
249                                         config.c_maxmsglen = atoi(buf);
250                                 break;
251                         case 21: if (atoi(buf) >= 2)
252                                         config.c_min_workers = atoi(buf);
253                         case 22: if (atoi(buf) >= config.c_min_workers)
254                                         config.c_max_workers = atoi(buf);
255                         case 23: config.c_pop3_port = atoi(buf);
256                                 break;
257                         case 24: config.c_smtp_port = atoi(buf);
258                                 break;
259                         case 25: config.c_default_filter = atoi(buf);
260                                 break;
261                         case 26: config.c_aide_zap = atoi(buf);
262                                 if (config.c_aide_zap != 0)
263                                         config.c_aide_zap = 1;
264                                 break;
265                         }
266                     ++a;
267                     }
268                 put_config();
269                 snprintf(buf,sizeof buf,
270                          "Global system configuration edited by %s\n",
271                          CC->curr_user);
272                 aide_message(buf);
273
274                 if (strlen(config.c_logpages) > 0)
275                         create_room(config.c_logpages, 3, "", 0);
276                 }
277
278         else if (!strcasecmp(cmd, "GETSYS")) {
279                 extract(confname, argbuf, 1);
280                 confptr = CtdlGetSysConfig(confname);
281                 if (confptr != NULL) {
282                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
283                         client_write(confptr, strlen(confptr));
284                         if (confptr[strlen(confptr)-1] != 10)
285                                 client_write("\n", 1);
286                         cprintf("000\n");
287                         phree(confptr);
288                 }
289                 else {
290                         cprintf("%d No such configuration.\n",
291                                 ERROR+ILLEGAL_VALUE);
292                 }
293         }
294
295         else if (!strcasecmp(cmd, "PUTSYS")) {
296                 extract(confname, argbuf, 1);
297                 cprintf("%d %s\n", SEND_LISTING, confname);
298                 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL);
299                 CtdlPutSysConfig(confname, confptr);
300                 phree(confptr);
301         }
302
303         else {
304                 cprintf("%d Illegal option(s) specified.\n",
305                         ERROR+ILLEGAL_VALUE);
306                 }
307         }