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