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