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