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