]> code.citadel.org Git - citadel.git/blob - citadel/control.c
* Eliminated the EXPI command
[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 "serv_extensions.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("%d\n", config.c_rfc822_strict_from);
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("%d\n", config.c_purge_hour);
200                 cprintf("000\n");
201         }
202
203         else if (!strcasecmp(cmd, "SET")) {
204                 cprintf("%d Send configuration...\n", SEND_LISTING);
205                 a = 0;
206                 while (client_gets(buf), strcmp(buf, "000")) {
207                         switch (a) {
208                         case 0:
209                                 safestrncpy(config.c_nodename, buf,
210                                             sizeof config.c_nodename);
211                                 break;
212                         case 1:
213                                 safestrncpy(config.c_fqdn, buf,
214                                             sizeof config.c_fqdn);
215                                 break;
216                         case 2:
217                                 safestrncpy(config.c_humannode, buf,
218                                             sizeof config.c_humannode);
219                                 break;
220                         case 3:
221                                 safestrncpy(config.c_phonenum, buf,
222                                             sizeof config.c_phonenum);
223                                 break;
224                         case 4:
225                                 config.c_creataide = atoi(buf);
226                                 break;
227                         case 5:
228                                 config.c_sleeping = atoi(buf);
229                                 break;
230                         case 6:
231                                 config.c_initax = atoi(buf);
232                                 if (config.c_initax < 1)
233                                         config.c_initax = 1;
234                                 if (config.c_initax > 6)
235                                         config.c_initax = 6;
236                                 break;
237                         case 7:
238                                 config.c_regiscall = atoi(buf);
239                                 if (config.c_regiscall != 0)
240                                         config.c_regiscall = 1;
241                                 break;
242                         case 8:
243                                 config.c_twitdetect = atoi(buf);
244                                 if (config.c_twitdetect != 0)
245                                         config.c_twitdetect = 1;
246                                 break;
247                         case 9:
248                                 safestrncpy(config.c_twitroom, buf,
249                                             sizeof config.c_twitroom);
250                                 break;
251                         case 10:
252                                 safestrncpy(config.c_moreprompt, buf,
253                                             sizeof config.c_moreprompt);
254                                 break;
255                         case 11:
256                                 config.c_restrict = atoi(buf);
257                                 if (config.c_restrict != 0)
258                                         config.c_restrict = 1;
259                                 break;
260                         case 12:
261                                 safestrncpy(config.c_bbs_city, buf,
262                                             sizeof config.c_bbs_city);
263                                 break;
264                         case 13:
265                                 safestrncpy(config.c_sysadm, buf,
266                                             sizeof config.c_sysadm);
267                                 break;
268                         case 14:
269                                 config.c_maxsessions = atoi(buf);
270                                 if (config.c_maxsessions < 1)
271                                         config.c_maxsessions = 1;
272                                 break;
273                         case 15:
274                                 safestrncpy(config.c_net_password, buf,
275                                             sizeof config.c_net_password);
276                                 break;
277                         case 16:
278                                 config.c_userpurge = atoi(buf);
279                                 break;
280                         case 17:
281                                 config.c_roompurge = atoi(buf);
282                                 break;
283                         case 18:
284                                 safestrncpy(config.c_logpages, buf,
285                                             sizeof config.c_logpages);
286                                 break;
287                         case 19:
288                                 config.c_createax = atoi(buf);
289                                 if (config.c_createax < 1)
290                                         config.c_createax = 1;
291                                 if (config.c_createax > 6)
292                                         config.c_createax = 6;
293                                 break;
294                         case 20:
295                                 if (atoi(buf) >= 8192)
296                                         config.c_maxmsglen = atoi(buf);
297                                 break;
298                         case 21:
299                                 if (atoi(buf) >= 2)
300                                         config.c_min_workers = atoi(buf);
301                         case 22:
302                                 if (atoi(buf) >= config.c_min_workers)
303                                         config.c_max_workers = atoi(buf);
304                         case 23:
305                                 config.c_pop3_port = atoi(buf);
306                                 break;
307                         case 24:
308                                 config.c_smtp_port = atoi(buf);
309                                 break;
310                         case 25:
311                                 config.c_rfc822_strict_from = atoi(buf);
312                                 break;
313                         case 26:
314                                 config.c_aide_zap = atoi(buf);
315                                 if (config.c_aide_zap != 0)
316                                         config.c_aide_zap = 1;
317                                 break;
318                         case 27:
319                                 config.c_imap_port = atoi(buf);
320                                 break;
321                         case 28:
322                                 config.c_net_freq = atol(buf);
323                                 break;
324                         case 29:
325                                 config.c_disable_newu = atoi(buf);
326                                 if (config.c_disable_newu != 0)
327                                         config.c_disable_newu = 1;
328                                 break;
329                         case 30:
330                                 config.c_aide_mailboxes = atoi(buf);
331                                 if (config.c_aide_mailboxes != 0)
332                                         config.c_aide_mailboxes = 1;
333                                 break;
334                         case 31:
335                                 if ((config.c_purge_hour >= 0)
336                                    && (config.c_purge_hour <= 23)) {
337                                         config.c_purge_hour = atoi(buf);
338                                 }
339                                 break;
340                         }
341                         ++a;
342                 }
343                 put_config();
344                 snprintf(buf, sizeof buf,
345                          "Global system configuration edited by %s\n",
346                          CC->curr_user);
347                 aide_message(buf);
348
349                 if (strlen(config.c_logpages) > 0)
350                         create_room(config.c_logpages, 3, "", 0, 1, 1);
351         }
352
353         else if (!strcasecmp(cmd, "GETSYS")) {
354                 extract(confname, argbuf, 1);
355                 confptr = CtdlGetSysConfig(confname);
356                 if (confptr != NULL) {
357                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
358                         client_write(confptr, strlen(confptr));
359                         if (confptr[strlen(confptr) - 1] != 10)
360                                 client_write("\n", 1);
361                         cprintf("000\n");
362                         phree(confptr);
363                 } else {
364                         cprintf("%d No such configuration.\n",
365                                 ERROR + ILLEGAL_VALUE);
366                 }
367         }
368
369         else if (!strcasecmp(cmd, "PUTSYS")) {
370                 extract(confname, argbuf, 1);
371                 cprintf("%d %s\n", SEND_LISTING, confname);
372                 confptr = CtdlReadMessageBody("000",
373                                 config.c_maxmsglen, NULL, 0);
374                 CtdlPutSysConfig(confname, confptr);
375                 phree(confptr);
376         }
377
378         else {
379                 cprintf("%d Illegal option(s) specified.\n",
380                         ERROR + ILLEGAL_VALUE);
381         }
382 }