* Implemented CONF server command for site-global configs
[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 <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <time.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <pthread.h>
20 #include <syslog.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
29 struct CitControl CitControl;
30 struct config config;
31
32 /*
33  * get_control  -  read the control record into memory.
34  */
35 void get_control(void) {
36         FILE *fp;
37
38         /* Zero it out.  If the control record on disk is missing or short,
39          * the system functions with all control record fields initialized
40          * to zero.
41          */
42         memset(&CitControl, 0, sizeof(struct CitControl));
43         fp = fopen("citadel.control", "rb");
44         if (fp == NULL) return;
45
46         fread(&CitControl, sizeof(struct CitControl), 1, fp);
47         fclose(fp);
48         }
49
50 /*
51  * put_control  -  write the control record to disk.
52  */
53 void put_control(void) {
54         FILE *fp;
55
56         fp = fopen("citadel.control", "wb");
57         if (fp != NULL) {
58                 fwrite(&CitControl, sizeof(struct CitControl), 1, fp);
59                 fclose(fp);
60                 }
61         }
62
63
64 /*
65  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
66  */
67 long get_new_message_number(void) {
68         begin_critical_section(S_CONTROL);
69         get_control();
70         ++CitControl.MMhighest;
71         put_control();
72         end_critical_section(S_CONTROL);
73         return(CitControl.MMhighest);
74         }
75
76
77 /*
78  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
79  */
80 long get_new_user_number(void) {
81         begin_critical_section(S_CONTROL);
82         get_control();
83         ++CitControl.MMnextuser;
84         put_control();
85         end_critical_section(S_CONTROL);
86         return(CitControl.MMnextuser);
87         }
88
89
90
91 /* 
92  * Get or set global configuration options
93  */
94 void cmd_conf(char *argbuf) {
95         char cmd[256];
96         char buf[256];
97         int a;
98
99         if (!(CC->logged_in)) {
100                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
101                 return;
102                 }
103
104         if (CC->usersupp.axlevel < 6) {
105                 cprintf("%d Higher access required.\n",
106                         ERROR+HIGHER_ACCESS_REQUIRED);
107                 return;
108                 }
109
110         extract(cmd, argbuf, 0);
111         if (!strcasecmp(cmd, "GET")) {
112                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
113                 cprintf("%s\n", config.c_nodename);
114                 cprintf("%s\n", config.c_fqdn);
115                 cprintf("%s\n", config.c_humannode);
116                 cprintf("%s\n", config.c_phonenum);
117                 cprintf("%d\n", config.c_creataide);
118                 cprintf("%d\n", config.c_sleeping);
119                 cprintf("%d\n", config.c_initax);
120                 cprintf("%d\n", config.c_regiscall);
121                 cprintf("%d\n", config.c_twitdetect);
122                 cprintf("%s\n", config.c_twitroom);
123                 cprintf("%s\n", config.c_moreprompt);
124                 cprintf("%d\n", config.c_restrict);
125                 cprintf("%s\n", config.c_bbs_city);
126                 cprintf("%s\n", config.c_sysadm);
127                 cprintf("%d\n", config.c_maxsessions);
128                 cprintf("%s\n", config.c_net_password);
129                 cprintf("%d\n", config.c_userpurge);
130                 cprintf("000\n");
131                 }
132
133         else if (!strcasecmp(cmd, "SET")) {
134                 cprintf("%d Send configuration...\n", SEND_LISTING);
135                 a = 0;
136                 while (client_gets(buf), strcmp(buf, "000")) {
137                     switch(a) {
138                         case 0: strncpy(config.c_nodename, buf, 16);
139                                 break;
140                         case 1: strncpy(config.c_fqdn, buf, 64);
141                                 break;
142                         case 2: strncpy(config.c_humannode, buf, 21);
143                                 break;
144                         case 3: strncpy(config.c_phonenum, buf, 16);
145                                 break;
146                         case 4: config.c_creataide = atoi(buf);
147                                 break;
148                         case 5: config.c_sleeping = atoi(buf);
149                                 break;
150                         case 6: config.c_initax = atoi(buf);
151                                 if (config.c_initax < 1) config.c_initax = 1;
152                                 if (config.c_initax > 6) config.c_initax = 6;
153                                 break;
154                         case 7: config.c_regiscall = atoi(buf);
155                                 if (config.c_regiscall != 0)
156                                         config.c_regiscall = 1;
157                                 break;
158                         case 8: config.c_twitdetect = atoi(buf);
159                                 if (config.c_twitdetect != 0)
160                                         config.c_twitdetect = 1;
161                                 break;
162                         case 9: strncpy(config.c_twitroom,
163                                         buf, ROOMNAMELEN);
164                                 break;
165                         case 10: strncpy(config.c_moreprompt, buf, 80);
166                                 break;
167                         case 11: config.c_restrict = atoi(buf);
168                                 if (config.c_restrict != 0)
169                                         config.c_restrict = 1;
170                                 break;
171                         case 12: strncpy(config.c_bbs_city, buf, 32);
172                                 break;
173                         case 13: strncpy(config.c_sysadm, buf, 26);
174                                 break;
175                         case 14: config.c_maxsessions = atoi(buf);
176                                 if (config.c_maxsessions < 1)
177                                         config.c_maxsessions = 1;
178                                 break;
179                         case 15: strncpy(config.c_net_password, buf, 20);
180                                 break;
181                         case 16: config.c_userpurge = atoi(buf);
182                                 break;
183                                 }
184                     ++a;
185                     }
186                 put_config();
187                 sprintf(buf,"Global system configuration edited by %s",
188                         CC->curr_user);
189                 aide_message(buf);
190                 }
191
192         else {
193                 cprintf("%d The only valid options are GET and SET.\n",
194                         ERROR+ILLEGAL_VALUE);
195                 }
196         }