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