CONF GET command ported to new config system
[citadel.git] / citadel / control.c
1 /*
2  * This module handles states which are global to the entire server.
3  *
4  * Copyright (c) 1987-2015 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <sys/file.h>
17 #include <libcitadel.h>
18
19 #include "ctdl_module.h"
20 #include "config.h"
21 #include "citserver.h"
22 #include "user_ops.h"
23
24 struct CitControl CitControl;
25 FILE *control_fp = NULL;
26 long control_highest_user = 0;
27
28
29 /*
30  * lock_control  -  acquire a lock on the control record file.
31  *                  This keeps multiple citservers from running concurrently.
32  */
33 void lock_control(void)
34 {
35 #if defined(LOCK_EX) && defined(LOCK_NB)
36         if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
37                 syslog(LOG_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
38                 syslog(LOG_EMERG, "Is another citserver already running?\n");
39                 exit(CTDLEXIT_CONTROL);
40         }
41 #endif
42 }
43
44 /*
45  * callback to get highest room number when rebuilding control file
46  */
47 void control_find_highest(struct ctdlroom *qrbuf, void *data)
48 {
49         struct ctdlroom room;
50         struct cdbdata *cdbfr;
51         long *msglist;
52         int num_msgs=0;
53         int c;
54         int room_fixed = 0;
55         int message_fixed = 0;
56         
57         if (qrbuf->QRnumber > CitControl.MMnextroom)
58         {
59                 CitControl.MMnextroom = qrbuf->QRnumber;
60                 room_fixed = 1;
61         }
62                 
63         CtdlGetRoom (&room, qrbuf->QRname);
64         
65         /* Load the message list */
66         cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
67         if (cdbfr != NULL) {
68                 msglist = (long *) cdbfr->ptr;
69                 num_msgs = cdbfr->len / sizeof(long);
70         } else {
71                 return; /* No messages at all?  No further action. */
72         }
73
74         if (num_msgs>0)
75         {
76                 for (c=0; c<num_msgs; c++)
77                 {
78                         if (msglist[c] > CitControl.MMhighest)
79                         {
80                                 CitControl.MMhighest = msglist[c];
81                                 message_fixed = 1;
82                         }
83                 }
84         }
85         cdb_free(cdbfr);
86         if (room_fixed)
87                 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
88         if (message_fixed)
89                 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
90         return;
91 }
92
93
94 /*
95  * Callback to get highest user number.
96  */
97  
98 void control_find_user (struct ctdluser *EachUser, void *out_data)
99 {
100         int user_fixed = 0;
101         
102         if (EachUser->usernum > CitControl.MMnextuser)
103         {
104                 CitControl.MMnextuser = EachUser->usernum;
105                 user_fixed = 1;
106         }
107         if(user_fixed)
108                 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
109 }
110
111
112 /*
113  * get_control  -  read the control record into memory.
114  */
115 void get_control(void)
116 {
117         static int already_have_control = 0;
118         int rv = 0;
119
120         /*
121          * If we already have the control record in memory, there's no point
122          * in reading it from disk again.
123          */
124         if (already_have_control) return;
125
126         /* Zero it out.  If the control record on disk is missing or short,
127          * the system functions with all control record fields initialized
128          * to zero.
129          */
130         memset(&CitControl, 0, sizeof(struct CitControl));
131         if (control_fp == NULL) {
132                 control_fp = fopen(file_citadel_control, "rb+");
133                 if (control_fp != NULL) {
134                         lock_control();
135                         rv = fchown(fileno(control_fp), ctdluid, -1);
136                         if (rv == -1)
137                                 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n", 
138                                        file_citadel_control, strerror(errno));
139                         rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
140                         if (rv == -1)
141                                 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n", 
142                                        file_citadel_control, strerror(errno));
143                 }
144         }
145         if (control_fp == NULL) {
146                 control_fp = fopen(file_citadel_control, "wb+");
147                 if (control_fp != NULL) {
148                         lock_control();
149                         memset(&CitControl, 0, sizeof(struct CitControl));
150
151                         rv = fchown(fileno(control_fp), ctdluid, -1);
152                         if (rv == -1)
153                                 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n", 
154                                        file_citadel_control, strerror(errno));
155
156                         rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
157                         if (rv == -1)
158                                 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n", 
159                                        file_citadel_control, strerror(errno));
160                         rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
161                         if (rv == -1)
162                                 syslog(LOG_EMERG, "Failed to write: %s [%s]\n", 
163                                        file_citadel_control, strerror(errno));
164                         rewind(control_fp);
165                 }
166         }
167         if (control_fp == NULL) {
168                 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
169                 return;
170         }
171
172         rewind(control_fp);
173         rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
174         if (rv == -1)
175                 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n", 
176                        file_citadel_control, strerror(errno));
177         already_have_control = 1;
178         rv = chown(file_citadel_control, ctdluid, (-1));
179         if (rv == -1)
180                 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n", 
181                        file_citadel_control, strerror(errno));  
182 }
183
184 /*
185  * put_control  -  write the control record to disk.
186  */
187 void put_control(void)
188 {
189         int rv = 0;
190
191         if (control_fp != NULL) {
192                 rewind(control_fp);
193                 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
194                 if (rv == -1)
195                         syslog(LOG_EMERG, "Failed to write: %s [%s]\n", 
196                                file_citadel_control, strerror(errno));
197                 fflush(control_fp);
198         }
199 }
200
201
202 /*
203  * check_control   -  check the control record has sensible values for message, user and room numbers
204  */
205 void check_control(void)
206 {
207         syslog(LOG_INFO, "Checking/re-building control record\n");
208         get_control();
209         // Find highest room number and message number.
210         CtdlForEachRoom(control_find_highest, NULL);
211         ForEachUser(control_find_user, NULL);
212         put_control();
213 }
214
215
216 /*
217  * release_control - close our fd on exit
218  */
219 void release_control(void)
220 {
221         if (control_fp != NULL) {
222                 fclose(control_fp);
223         }
224         control_fp = NULL;
225 }
226
227 /*
228  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
229  */
230 long get_new_message_number(void)
231 {
232         long retval = 0L;
233         begin_critical_section(S_CONTROL);
234         get_control();
235         retval = ++CitControl.MMhighest;
236         put_control();
237         end_critical_section(S_CONTROL);
238         return(retval);
239 }
240
241
242 /*
243  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
244  * This provides a quick way to initialise a variable that might be used to indicate
245  * messages that should not be processed. EG. a new Sieve script will use this
246  * to record determine that messages older than this should not be processed.
247  */
248 long CtdlGetCurrentMessageNumber(void)
249 {
250         long retval = 0L;
251         begin_critical_section(S_CONTROL);
252         get_control();
253         retval = CitControl.MMhighest;
254         end_critical_section(S_CONTROL);
255         return(retval);
256 }
257
258
259 /*
260  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
261  */
262 long get_new_user_number(void)
263 {
264         long retval = 0L;
265         begin_critical_section(S_CONTROL);
266         get_control();
267         retval = ++CitControl.MMnextuser;
268         put_control();
269         end_critical_section(S_CONTROL);
270         return(retval);
271 }
272
273
274
275 /*
276  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
277  */
278 long get_new_room_number(void)
279 {
280         long retval = 0L;
281         begin_critical_section(S_CONTROL);
282         get_control();
283         retval = ++CitControl.MMnextroom;
284         put_control();
285         end_critical_section(S_CONTROL);
286         return(retval);
287 }
288
289
290
291 /* 
292  * Get or set global configuration options
293  *
294  * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
295  * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
296  *
297  */
298 void cmd_conf(char *argbuf)
299 {
300         char cmd[16];
301         char buf[256];
302         int a;
303         char *confptr;
304         char confname[128];
305
306         if (CtdlAccessCheck(ac_aide)) return;
307
308         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
309         if (!strcasecmp(cmd, "GET")) {
310                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
311                 cprintf("%s\n",         CtdlGetConfigStr("c_nodename"));
312                 cprintf("%s\n",         CtdlGetConfigStr("c_fqdn"));
313                 cprintf("%s\n",         CtdlGetConfigStr("c_humannode"));
314                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
315                 cprintf("%d\n",         CtdlGetConfigInt("c_creataide"));
316                 cprintf("%d\n",         CtdlGetConfigInt("c_sleeping"));
317                 cprintf("%d\n",         CtdlGetConfigInt("c_initax"));
318                 cprintf("%d\n",         CtdlGetConfigInt("c_regiscall"));
319                 cprintf("%d\n",         CtdlGetConfigInt("c_twitdetect"));
320                 cprintf("%s\n",         CtdlGetConfigStr("c_twitroom"));
321                 cprintf("%s\n",         CtdlGetConfigStr("c_moreprompt"));
322                 cprintf("%d\n",         CtdlGetConfigInt("c_restrict"));
323                 cprintf("%s\n",         CtdlGetConfigStr("c_site_location"));
324                 cprintf("%s\n",         CtdlGetConfigStr("c_sysadm"));
325                 cprintf("%d\n",         CtdlGetConfigInt("c_maxsessions"));
326                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
327                 cprintf("%d\n",         CtdlGetConfigInt("c_userpurge"));
328                 cprintf("%d\n",         CtdlGetConfigInt("c_roompurge"));
329                 cprintf("%s\n",         CtdlGetConfigStr("c_logpages"));
330                 cprintf("%d\n",         CtdlGetConfigInt("c_createax"));
331                 cprintf("%ld\n",        CtdlGetConfigLong("c_maxmsglen"));
332                 cprintf("%d\n",         CtdlGetConfigInt("c_min_workers"));
333                 cprintf("%d\n",         CtdlGetConfigInt("c_max_workers"));
334                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3_port"));
335                 cprintf("%d\n",         CtdlGetConfigInt("c_smtp_port"));
336                 cprintf("%d\n",         CtdlGetConfigInt("c_rfc822_strict_from"));
337                 cprintf("%d\n",         CtdlGetConfigInt("c_aide_zap"));
338                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_port"));
339                 cprintf("%ld\n",        CtdlGetConfigLong("c_net_freq"));
340                 cprintf("%d\n",         CtdlGetConfigInt("c_disable_newu"));
341                 cprintf("1\n"); /* niu */
342                 cprintf("%d\n",         CtdlGetConfigInt("c_purge_hour"));
343 #ifdef HAVE_LDAP
344                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_host"));
345                 cprintf("%d\n",         CtdlGetConfigInt("c_ldap_port"));
346                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_base_dn"));
347                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_dn"));
348                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_pw"));
349 #else
350                 cprintf("\n");
351                 cprintf("0\n");
352                 cprintf("\n");
353                 cprintf("\n");
354                 cprintf("\n");
355 #endif
356                 cprintf("%s\n",         CtdlGetConfigStr("c_ip_addr"));
357                 cprintf("%d\n",         CtdlGetConfigInt("c_msa_port"));
358                 cprintf("%d\n",         CtdlGetConfigInt("c_imaps_port"));
359                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3s_port"));
360                 cprintf("%d\n",         CtdlGetConfigInt("c_smtps_port"));
361                 cprintf("%d\n",         CtdlGetConfigInt("c_enable_fulltext"));
362                 cprintf("%d\n",         CtdlGetConfigInt("c_auto_cull"));
363                 cprintf("1\n");
364                 cprintf("%d\n",         CtdlGetConfigInt("c_allow_spoofing"));
365                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_email"));
366                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_pubmsgs"));
367                 cprintf("%s\n",         CtdlGetConfigStr("c_journal_dest"));
368                 cprintf("%s\n",         CtdlGetConfigStr("c_default_cal_zone"));
369                 cprintf("%d\n",         CtdlGetConfigInt("c_pftcpdict_port"));
370                 cprintf("%d\n",         CtdlGetConfigInt("c_managesieve_port"));
371                 cprintf("%d\n",         CtdlGetConfigInt("c_auth_mode"));
372                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_host"));
373                 cprintf("%d\n",         CtdlGetConfigInt("c_funambol_port"));
374                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_source"));
375                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_auth"));
376                 cprintf("%d\n",         CtdlGetConfigInt("c_rbl_at_greeting"));
377                 cprintf("%s\n",         CtdlGetConfigStr("c_master_user"));
378                 cprintf("%s\n",         CtdlGetConfigStr("c_master_pass"));
379                 cprintf("%s\n",         CtdlGetConfigStr("c_pager_program"));
380                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_keep_from"));
381                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_c2s_port"));
382                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_s2s_port"));
383                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fetch"));
384                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fastest"));
385                 cprintf("%d\n",         CtdlGetConfigInt("c_spam_flag_only"));
386                 cprintf("%d\n",         CtdlGetConfigInt("c_guest_logins"));
387                 cprintf("%d\n",         CtdlGetConfigInt("c_port_number"));
388                 cprintf("%d\n",         ctdluid);
389                 cprintf("%d\n",         CtdlGetConfigInt("c_nntp_port"));
390                 cprintf("%d\n",         CtdlGetConfigInt("c_nntps_port"));
391                 cprintf("000\n");
392         }
393
394         else if (!strcasecmp(cmd, "SET")) {
395                 unbuffer_output();
396                 cprintf("%d Send configuration...\n", SEND_LISTING);
397                 a = 0;
398                 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
399                         switch (a) {
400                         case 0:
401                                 safestrncpy(config.c_nodename, buf, sizeof config.c_nodename);
402                                 break;
403                         case 1:
404                                 safestrncpy(config.c_fqdn, buf, sizeof config.c_fqdn);
405                                 break;
406                         case 2:
407                                 safestrncpy(config.c_humannode, buf, sizeof config.c_humannode);
408                                 break;
409                         case 3:
410                                 /* placeholder -- field no longer in use */
411                                 break;
412                         case 4:
413                                 config.c_creataide = atoi(buf);
414                                 break;
415                         case 5:
416                                 config.c_sleeping = atoi(buf);
417                                 break;
418                         case 6:
419                                 config.c_initax = atoi(buf);
420                                 if (config.c_initax < 1)
421                                         config.c_initax = 1;
422                                 if (config.c_initax > 6)
423                                         config.c_initax = 6;
424                                 break;
425                         case 7:
426                                 config.c_regiscall = atoi(buf);
427                                 if (config.c_regiscall != 0)
428                                         config.c_regiscall = 1;
429                                 break;
430                         case 8:
431                                 config.c_twitdetect = atoi(buf);
432                                 if (config.c_twitdetect != 0)
433                                         config.c_twitdetect = 1;
434                                 break;
435                         case 9:
436                                 safestrncpy(config.c_twitroom, buf, sizeof config.c_twitroom);
437                                 break;
438                         case 10:
439                                 safestrncpy(config.c_moreprompt, buf, sizeof config.c_moreprompt);
440                                 break;
441                         case 11:
442                                 config.c_restrict = atoi(buf);
443                                 if (config.c_restrict != 0)
444                                         config.c_restrict = 1;
445                                 break;
446                         case 12:
447                                 safestrncpy(config.c_site_location, buf, sizeof config.c_site_location);
448                                 break;
449                         case 13:
450                                 safestrncpy(config.c_sysadm, buf, sizeof config.c_sysadm);
451                                 break;
452                         case 14:
453                                 config.c_maxsessions = atoi(buf);
454                                 if (config.c_maxsessions < 0)
455                                         config.c_maxsessions = 0;
456                                 break;
457                         case 15:
458                                 /* placeholder -- field no longer in use */
459                                 break;
460                         case 16:
461                                 config.c_userpurge = atoi(buf);
462                                 break;
463                         case 17:
464                                 config.c_roompurge = atoi(buf);
465                                 break;
466                         case 18:
467                                 safestrncpy(config.c_logpages, buf, sizeof config.c_logpages);
468                                 break;
469                         case 19:
470                                 config.c_createax = atoi(buf);
471                                 if (config.c_createax < 1)
472                                         config.c_createax = 1;
473                                 if (config.c_createax > 6)
474                                         config.c_createax = 6;
475                                 break;
476                         case 20:
477                                 if (atoi(buf) >= 8192)
478                                         config.c_maxmsglen = atoi(buf);
479                                 break;
480                         case 21:
481                                 if (atoi(buf) >= 2)
482                                         config.c_min_workers = atoi(buf);
483                         case 22:
484                                 if (atoi(buf) >= config.c_min_workers)
485                                         config.c_max_workers = atoi(buf);
486                         case 23:
487                                 config.c_pop3_port = atoi(buf);
488                                 break;
489                         case 24:
490                                 config.c_smtp_port = atoi(buf);
491                                 break;
492                         case 25:
493                                 config.c_rfc822_strict_from = atoi(buf);
494                                 break;
495                         case 26:
496                                 config.c_aide_zap = atoi(buf);
497                                 if (config.c_aide_zap != 0)
498                                         config.c_aide_zap = 1;
499                                 break;
500                         case 27:
501                                 config.c_imap_port = atoi(buf);
502                                 break;
503                         case 28:
504                                 config.c_net_freq = atol(buf);
505                                 break;
506                         case 29:
507                                 config.c_disable_newu = atoi(buf);
508                                 if (config.c_disable_newu != 0)
509                                         config.c_disable_newu = 1;
510                                 break;
511                         case 30:
512                                 /* niu */
513                                 break;
514                         case 31:
515                                 if ((config.c_purge_hour >= 0)
516                                     && (config.c_purge_hour <= 23)) {
517                                         config.c_purge_hour = atoi(buf);
518                                 }
519                                 break;
520 #ifdef HAVE_LDAP
521                         case 32:
522                                 safestrncpy(config.c_ldap_host, buf, sizeof config.c_ldap_host);
523                                 break;
524                         case 33:
525                                 config.c_ldap_port = atoi(buf);
526                                 break;
527                         case 34:
528                                 safestrncpy(config.c_ldap_base_dn, buf, sizeof config.c_ldap_base_dn);
529                                 break;
530                         case 35:
531                                 safestrncpy(config.c_ldap_bind_dn, buf, sizeof config.c_ldap_bind_dn);
532                                 break;
533                         case 36:
534                                 safestrncpy(config.c_ldap_bind_pw, buf, sizeof config.c_ldap_bind_pw);
535                                 break;
536 #endif
537                         case 37:
538                                 safestrncpy(config.c_ip_addr, buf, sizeof config.c_ip_addr);
539                         case 38:
540                                 config.c_msa_port = atoi(buf);
541                                 break;
542                         case 39:
543                                 config.c_imaps_port = atoi(buf);
544                                 break;
545                         case 40:
546                                 config.c_pop3s_port = atoi(buf);
547                                 break;
548                         case 41:
549                                 config.c_smtps_port = atoi(buf);
550                                 break;
551                         case 42:
552                                 config.c_enable_fulltext = atoi(buf);
553                                 break;
554                         case 43:
555                                 config.c_auto_cull = atoi(buf);
556                                 break;
557                         case 44:
558                                 /* niu */
559                                 break;
560                         case 45:
561                                 config.c_allow_spoofing = atoi(buf);
562                                 break;
563                         case 46:
564                                 config.c_journal_email = atoi(buf);
565                                 break;
566                         case 47:
567                                 config.c_journal_pubmsgs = atoi(buf);
568                                 break;
569                         case 48:
570                                 safestrncpy(config.c_journal_dest, buf, sizeof config.c_journal_dest);
571                         case 49:
572                                 safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
573                                 break;
574                         case 50:
575                                 config.c_pftcpdict_port = atoi(buf);
576                                 break;
577                         case 51:
578                                 config.c_managesieve_port = atoi(buf);
579                                 break;
580                         case 52:
581                                 config.c_auth_mode = atoi(buf);
582                         case 53:
583                                 safestrncpy(config.c_funambol_host, buf, sizeof config.c_funambol_host);
584                                 break;
585                         case 54:
586                                 config.c_funambol_port = atoi(buf);
587                                 break;
588                         case 55:
589                                 safestrncpy(config.c_funambol_source, buf, sizeof config.c_funambol_source);
590                                 break;
591                         case 56:
592                                 safestrncpy(config.c_funambol_auth, buf, sizeof config.c_funambol_auth);
593                                 break;
594                         case 57:
595                                 config.c_rbl_at_greeting = atoi(buf);
596                                 break;
597                         case 58:
598                                 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
599                                 break;
600                         case 59:
601                                 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
602                                 break;
603                         case 60:
604                                 safestrncpy(config.c_pager_program, buf, sizeof config.c_pager_program);
605                                 break;
606                         case 61:
607                                 config.c_imap_keep_from = atoi(buf);
608                                 break;
609                         case 62:
610                                 config.c_xmpp_c2s_port = atoi(buf);
611                                 break;
612                         case 63:
613                                 config.c_xmpp_s2s_port = atoi(buf);
614                                 break;
615                         case 64:
616                                 config.c_pop3_fetch = atol(buf);
617                                 break;
618                         case 65:
619                                 config.c_pop3_fastest = atol(buf);
620                                 break;
621                         case 66:
622                                 config.c_spam_flag_only = atoi(buf);
623                                 break;
624                         case 67:
625                                 config.c_guest_logins = atoi(buf);
626                                 break;
627                         case 68:
628                                 config.c_port_number = atoi(buf);
629                                 break;
630                         case 69:
631                                 /* niu */
632                                 break;
633                         case 70:
634                                 config.c_nntp_port = atoi(buf);
635                                 break;
636                         case 71:
637                                 config.c_nntps_port = atoi(buf);
638                                 break;
639                         }
640                         ++a;
641                 }
642                 put_config();
643                 snprintf(buf, sizeof buf,
644                         "The global system configuration has been edited by %s.\n",
645                          (CC->logged_in ? CC->curr_user : "an administrator")
646                 );
647                 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
648
649                 if (!IsEmptyStr(config.c_logpages))
650                         CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
651
652                 /* If full text indexing has been disabled, invalidate the
653                  * index so it doesn't try to use it later.
654                  */
655                 if (config.c_enable_fulltext == 0) {
656                         CitControl.MM_fulltext_wordbreaker = 0;
657                         put_control();
658                 }
659         }
660
661         else if (!strcasecmp(cmd, "GETSYS")) {
662                 extract_token(confname, argbuf, 1, '|', sizeof confname);
663                 confptr = CtdlGetSysConfig(confname);
664                 if (confptr != NULL) {
665                         long len; 
666
667                         len = strlen(confptr);
668                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
669                         client_write(confptr, len);
670                         if ((len > 0) && (confptr[len - 1] != 10))
671                                 client_write("\n", 1);
672                         cprintf("000\n");
673                         free(confptr);
674                 } else {
675                         cprintf("%d No such configuration.\n",
676                                 ERROR + ILLEGAL_VALUE);
677                 }
678         }
679
680         else if (!strcasecmp(cmd, "PUTSYS")) {
681                 extract_token(confname, argbuf, 1, '|', sizeof confname);
682                 unbuffer_output();
683                 cprintf("%d %s\n", SEND_LISTING, confname);
684                 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
685                 CtdlPutSysConfig(confname, confptr);
686                 free(confptr);
687         }
688
689         else {
690                 cprintf("%d Illegal option(s) specified.\n",
691                         ERROR + ILLEGAL_VALUE);
692         }
693 }
694
695 typedef struct __ConfType {
696         ConstStr Name;
697         long Type;
698 }ConfType;
699
700 ConfType CfgNames[] = {
701         { {HKEY("localhost") },    0},
702         { {HKEY("directory") },    0},
703         { {HKEY("smarthost") },    2},
704         { {HKEY("fallbackhost") }, 2},
705         { {HKEY("rbl") },          3},
706         { {HKEY("spamassassin") }, 3},
707         { {HKEY("masqdomain") },   1},
708         { {HKEY("clamav") },       3},
709         { {HKEY("notify") },       3},
710         { {NULL, 0}, 0}
711 };
712
713 HashList *CfgNameHash = NULL;
714 void cmd_gvdn(char *argbuf)
715 {
716         const ConfType *pCfg;
717         char *confptr;
718         long min = atol(argbuf);
719         const char *Pos = NULL;
720         const char *PPos = NULL;
721         const char *HKey;
722         long HKLen;
723         StrBuf *Line;
724         StrBuf *Config;
725         StrBuf *Cfg;
726         StrBuf *CfgToken;
727         HashList *List;
728         HashPos *It;
729         void *vptr;
730         
731         List = NewHash(1, NULL);
732         Cfg = NewStrBufPlain(config.c_fqdn, -1);
733         Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
734         Cfg = NULL;
735
736         confptr = CtdlGetSysConfig(INTERNETCFG);
737         Config = NewStrBufPlain(confptr, -1);
738         free(confptr);
739
740         Line = NewStrBufPlain(NULL, StrLength(Config));
741         CfgToken = NewStrBufPlain(NULL, StrLength(Config));
742         while (StrBufSipLine(Line, Config, &Pos))
743         {
744                 if (Cfg == NULL)
745                         Cfg = NewStrBufPlain(NULL, StrLength(Line));
746                 PPos = NULL;
747                 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
748                 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
749                 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
750                     (vptr != NULL))
751                 {
752                         pCfg = (ConfType *) vptr;
753                         if (pCfg->Type <= min)
754                         {
755                                 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
756                                 Cfg = NULL;
757                         }
758                 }
759         }
760
761         cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
762         It = GetNewHashPos(List, 1);
763         while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
764         {
765                 cputbuf(vptr);
766                 cprintf("\n");
767         }
768         cprintf("000\n");
769
770         DeleteHashPos(&It);
771         DeleteHash(&List);
772         FreeStrBuf(&Cfg);
773         FreeStrBuf(&Line);
774         FreeStrBuf(&CfgToken);
775         FreeStrBuf(&Config);
776 }
777
778 /*****************************************************************************/
779 /*                      MODULE INITIALIZATION STUFF                          */
780 /*****************************************************************************/
781
782 void control_cleanup(void)
783 {
784         DeleteHash(&CfgNameHash);
785 }
786 CTDL_MODULE_INIT(control)
787 {
788         if (!threading) {
789                 int i;
790
791                 CfgNameHash = NewHash(1, NULL);
792                 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
793                         Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
794
795                 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
796                 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
797                 CtdlRegisterCleanupHook(control_cleanup);
798
799         }
800         /* return our id for the Log */
801         return "control";
802 }