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