* Mac OS X build fixes (everything but serv_imap now builds)
[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 extern 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
62         /* Zero it out.  If the control record on disk is missing or short,
63          * the system functions with all control record fields initialized
64          * to zero.
65          */
66         memset(&CitControl, 0, sizeof(struct CitControl));
67         if (control_fp == NULL) {
68                 control_fp = fopen("citadel.control", "rb+");
69                 if (control_fp != NULL) {
70                         fchown(fileno(control_fp), config.c_bbsuid, -1);
71                 }
72         }
73         if (control_fp == NULL) {
74                 control_fp = fopen("citadel.control", "wb+");
75                 if (control_fp != NULL) {
76                         fchown(fileno(control_fp), config.c_bbsuid, -1);
77                         memset(&CitControl, 0, sizeof(struct CitControl));
78                         fwrite(&CitControl, sizeof(struct CitControl),
79                                1, control_fp);
80                         rewind(control_fp);
81                 }
82         }
83         if (control_fp == NULL) {
84                 lprintf(1, "ERROR opening citadel.control: %s\n",
85                         strerror(errno));
86                 return;
87         }
88
89         rewind(control_fp);
90         fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
91 }
92
93 /*
94  * put_control  -  write the control record to disk.
95  */
96 void put_control(void)
97 {
98
99         if (control_fp != NULL) {
100                 rewind(control_fp);
101                 fwrite(&CitControl, sizeof(struct CitControl), 1,
102                        control_fp);
103                 fflush(control_fp);
104         }
105 }
106
107
108 /*
109  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
110  */
111 long get_new_message_number(void)
112 {
113         begin_critical_section(S_CONTROL);
114         get_control();
115         ++CitControl.MMhighest;
116         put_control();
117         end_critical_section(S_CONTROL);
118         return (CitControl.MMhighest);
119 }
120
121
122 /*
123  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
124  */
125 long get_new_user_number(void)
126 {
127         begin_critical_section(S_CONTROL);
128         get_control();
129         ++CitControl.MMnextuser;
130         put_control();
131         end_critical_section(S_CONTROL);
132         return (CitControl.MMnextuser);
133 }
134
135
136
137 /*
138  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
139  */
140 long get_new_room_number(void)
141 {
142         begin_critical_section(S_CONTROL);
143         get_control();
144         ++CitControl.MMnextroom;
145         put_control();
146         end_critical_section(S_CONTROL);
147         return (CitControl.MMnextroom);
148 }
149
150
151
152 /* 
153  * Get or set global configuration options
154  */
155 void cmd_conf(char *argbuf)
156 {
157         char cmd[SIZ];
158         char buf[SIZ];
159         int a;
160         char *confptr;
161         char confname[SIZ];
162
163         if (CtdlAccessCheck(ac_aide)) return;
164
165         extract(cmd, argbuf, 0);
166         if (!strcasecmp(cmd, "GET")) {
167                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
168                 cprintf("%s\n", config.c_nodename);
169                 cprintf("%s\n", config.c_fqdn);
170                 cprintf("%s\n", config.c_humannode);
171                 cprintf("%s\n", config.c_phonenum);
172                 cprintf("%d\n", config.c_creataide);
173                 cprintf("%d\n", config.c_sleeping);
174                 cprintf("%d\n", config.c_initax);
175                 cprintf("%d\n", config.c_regiscall);
176                 cprintf("%d\n", config.c_twitdetect);
177                 cprintf("%s\n", config.c_twitroom);
178                 cprintf("%s\n", config.c_moreprompt);
179                 cprintf("%d\n", config.c_restrict);
180                 cprintf("%s\n", config.c_bbs_city);
181                 cprintf("%s\n", config.c_sysadm);
182                 cprintf("%d\n", config.c_maxsessions);
183                 cprintf("%s\n", config.c_net_password);
184                 cprintf("%d\n", config.c_userpurge);
185                 cprintf("%d\n", config.c_roompurge);
186                 cprintf("%s\n", config.c_logpages);
187                 cprintf("%d\n", config.c_createax);
188                 cprintf("%ld\n", config.c_maxmsglen);
189                 cprintf("%d\n", config.c_min_workers);
190                 cprintf("%d\n", config.c_max_workers);
191                 cprintf("%d\n", config.c_pop3_port);
192                 cprintf("%d\n", config.c_smtp_port);
193                 cprintf("0\n"); /* position 25 no longer exists */
194                 cprintf("%d\n", config.c_aide_zap);
195                 cprintf("%d\n", config.c_imap_port);
196                 cprintf("%ld\n", config.c_net_freq);
197                 cprintf("%d\n", config.c_disable_newu);
198                 cprintf("%d\n", config.c_aide_mailboxes);
199                 cprintf("000\n");
200         }
201
202         else if (!strcasecmp(cmd, "SET")) {
203                 cprintf("%d Send configuration...\n", SEND_LISTING);
204                 a = 0;
205                 while (client_gets(buf), strcmp(buf, "000")) {
206                         switch (a) {
207                         case 0:
208                                 safestrncpy(config.c_nodename, buf,
209                                             sizeof config.c_nodename);
210                                 break;
211                         case 1:
212                                 safestrncpy(config.c_fqdn, buf,
213                                             sizeof config.c_fqdn);
214                                 break;
215                         case 2:
216                                 safestrncpy(config.c_humannode, buf,
217                                             sizeof config.c_humannode);
218                                 break;
219                         case 3:
220                                 safestrncpy(config.c_phonenum, buf,
221                                             sizeof config.c_phonenum);
222                                 break;
223                         case 4:
224                                 config.c_creataide = atoi(buf);
225                                 break;
226                         case 5:
227                                 config.c_sleeping = atoi(buf);
228                                 break;
229                         case 6:
230                                 config.c_initax = atoi(buf);
231                                 if (config.c_initax < 1)
232                                         config.c_initax = 1;
233                                 if (config.c_initax > 6)
234                                         config.c_initax = 6;
235                                 break;
236                         case 7:
237                                 config.c_regiscall = atoi(buf);
238                                 if (config.c_regiscall != 0)
239                                         config.c_regiscall = 1;
240                                 break;
241                         case 8:
242                                 config.c_twitdetect = atoi(buf);
243                                 if (config.c_twitdetect != 0)
244                                         config.c_twitdetect = 1;
245                                 break;
246                         case 9:
247                                 safestrncpy(config.c_twitroom, buf,
248                                             sizeof config.c_twitroom);
249                                 break;
250                         case 10:
251                                 safestrncpy(config.c_moreprompt, buf,
252                                             sizeof config.c_moreprompt);
253                                 break;
254                         case 11:
255                                 config.c_restrict = atoi(buf);
256                                 if (config.c_restrict != 0)
257                                         config.c_restrict = 1;
258                                 break;
259                         case 12:
260                                 safestrncpy(config.c_bbs_city, buf,
261                                             sizeof config.c_bbs_city);
262                                 break;
263                         case 13:
264                                 safestrncpy(config.c_sysadm, buf,
265                                             sizeof config.c_sysadm);
266                                 break;
267                         case 14:
268                                 config.c_maxsessions = atoi(buf);
269                                 if (config.c_maxsessions < 1)
270                                         config.c_maxsessions = 1;
271                                 break;
272                         case 15:
273                                 safestrncpy(config.c_net_password, buf,
274                                             sizeof config.c_net_password);
275                                 break;
276                         case 16:
277                                 config.c_userpurge = atoi(buf);
278                                 break;
279                         case 17:
280                                 config.c_roompurge = atoi(buf);
281                                 break;
282                         case 18:
283                                 safestrncpy(config.c_logpages, buf,
284                                             sizeof config.c_logpages);
285                                 break;
286                         case 19:
287                                 config.c_createax = atoi(buf);
288                                 if (config.c_createax < 1)
289                                         config.c_createax = 1;
290                                 if (config.c_createax > 6)
291                                         config.c_createax = 6;
292                                 break;
293                         case 20:
294                                 if (atoi(buf) >= 8192)
295                                         config.c_maxmsglen = atoi(buf);
296                                 break;
297                         case 21:
298                                 if (atoi(buf) >= 2)
299                                         config.c_min_workers = atoi(buf);
300                         case 22:
301                                 if (atoi(buf) >= config.c_min_workers)
302                                         config.c_max_workers = atoi(buf);
303                         case 23:
304                                 config.c_pop3_port = atoi(buf);
305                                 break;
306                         case 24:
307                                 config.c_smtp_port = atoi(buf);
308                                 break;
309                                 /* case 25 no longer exists */
310                         case 26:
311                                 config.c_aide_zap = atoi(buf);
312                                 if (config.c_aide_zap != 0)
313                                         config.c_aide_zap = 1;
314                                 break;
315                         case 27:
316                                 config.c_imap_port = atoi(buf);
317                                 break;
318                         case 28:
319                                 config.c_net_freq = atol(buf);
320                                 break;
321                         case 29:
322                                 config.c_disable_newu = atoi(buf);
323                                 if (config.c_disable_newu != 0)
324                                         config.c_disable_newu = 1;
325                                 break;
326                         case 30:
327                                 config.c_aide_mailboxes = atoi(buf);
328                                 if (config.c_aide_mailboxes != 0)
329                                         config.c_aide_mailboxes = 1;
330                                 break;
331                         }
332                         ++a;
333                 }
334                 put_config();
335                 snprintf(buf, sizeof buf,
336                          "Global system configuration edited by %s\n",
337                          CC->curr_user);
338                 aide_message(buf);
339
340                 if (strlen(config.c_logpages) > 0)
341                         create_room(config.c_logpages, 3, "", 0, 1, 1);
342         }
343
344         else if (!strcasecmp(cmd, "GETSYS")) {
345                 extract(confname, argbuf, 1);
346                 confptr = CtdlGetSysConfig(confname);
347                 if (confptr != NULL) {
348                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
349                         client_write(confptr, strlen(confptr));
350                         if (confptr[strlen(confptr) - 1] != 10)
351                                 client_write("\n", 1);
352                         cprintf("000\n");
353                         phree(confptr);
354                 } else {
355                         cprintf("%d No such configuration.\n",
356                                 ERROR + ILLEGAL_VALUE);
357                 }
358         }
359
360         else if (!strcasecmp(cmd, "PUTSYS")) {
361                 extract(confname, argbuf, 1);
362                 cprintf("%d %s\n", SEND_LISTING, confname);
363                 confptr =
364                     CtdlReadMessageBody("000", config.c_maxmsglen, NULL);
365                 CtdlPutSysConfig(confname, confptr);
366                 phree(confptr);
367         }
368
369         else {
370                 cprintf("%d Illegal option(s) specified.\n",
371                         ERROR + ILLEGAL_VALUE);
372         }
373 }