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