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