More conversion to the new config system. WARNING BROKEN BUILD
[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  * Helper function for cmd_conf() to handle boolean values
293  */
294 int confbool(char *v)
295 {
296         if (IsEmptyStr(v)) return(0);
297         if (atoi(v) != 0) return(1);
298         return(0);
299 }
300
301
302 /* 
303  * Get or set global configuration options
304  *
305  * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
306  * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
307  *
308  */
309 void cmd_conf(char *argbuf)
310 {
311         char cmd[16];
312         char buf[256];
313         int a, i;
314         long ii;
315         char *confptr;
316         char confname[128];
317
318         if (CtdlAccessCheck(ac_aide)) return;
319
320         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
321         if (!strcasecmp(cmd, "GET")) {
322                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
323                 cprintf("%s\n",         CtdlGetConfigStr("c_nodename"));
324                 cprintf("%s\n",         CtdlGetConfigStr("c_fqdn"));
325                 cprintf("%s\n",         CtdlGetConfigStr("c_humannode"));
326                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
327                 cprintf("%d\n",         CtdlGetConfigInt("c_creataide"));
328                 cprintf("%d\n",         CtdlGetConfigInt("c_sleeping"));
329                 cprintf("%d\n",         CtdlGetConfigInt("c_initax"));
330                 cprintf("%d\n",         CtdlGetConfigInt("c_regiscall"));
331                 cprintf("%d\n",         CtdlGetConfigInt("c_twitdetect"));
332                 cprintf("%s\n",         CtdlGetConfigStr("c_twitroom"));
333                 cprintf("%s\n",         CtdlGetConfigStr("c_moreprompt"));
334                 cprintf("%d\n",         CtdlGetConfigInt("c_restrict"));
335                 cprintf("%s\n",         CtdlGetConfigStr("c_site_location"));
336                 cprintf("%s\n",         CtdlGetConfigStr("c_sysadm"));
337                 cprintf("%d\n",         CtdlGetConfigInt("c_maxsessions"));
338                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
339                 cprintf("%d\n",         CtdlGetConfigInt("c_userpurge"));
340                 cprintf("%d\n",         CtdlGetConfigInt("c_roompurge"));
341                 cprintf("%s\n",         CtdlGetConfigStr("c_logpages"));
342                 cprintf("%d\n",         CtdlGetConfigInt("c_createax"));
343                 cprintf("%ld\n",        CtdlGetConfigLong("c_maxmsglen"));
344                 cprintf("%d\n",         CtdlGetConfigInt("c_min_workers"));
345                 cprintf("%d\n",         CtdlGetConfigInt("c_max_workers"));
346                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3_port"));
347                 cprintf("%d\n",         CtdlGetConfigInt("c_smtp_port"));
348                 cprintf("%d\n",         CtdlGetConfigInt("c_rfc822_strict_from"));
349                 cprintf("%d\n",         CtdlGetConfigInt("c_aide_zap"));
350                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_port"));
351                 cprintf("%ld\n",        CtdlGetConfigLong("c_net_freq"));
352                 cprintf("%d\n",         CtdlGetConfigInt("c_disable_newu"));
353                 cprintf("1\n"); /* niu */
354                 cprintf("%d\n",         CtdlGetConfigInt("c_purge_hour"));
355 #ifdef HAVE_LDAP
356                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_host"));
357                 cprintf("%d\n",         CtdlGetConfigInt("c_ldap_port"));
358                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_base_dn"));
359                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_dn"));
360                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_pw"));
361 #else
362                 cprintf("\n");
363                 cprintf("0\n");
364                 cprintf("\n");
365                 cprintf("\n");
366                 cprintf("\n");
367 #endif
368                 cprintf("%s\n",         CtdlGetConfigStr("c_ip_addr"));
369                 cprintf("%d\n",         CtdlGetConfigInt("c_msa_port"));
370                 cprintf("%d\n",         CtdlGetConfigInt("c_imaps_port"));
371                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3s_port"));
372                 cprintf("%d\n",         CtdlGetConfigInt("c_smtps_port"));
373                 cprintf("%d\n",         CtdlGetConfigInt("c_enable_fulltext"));
374                 cprintf("%d\n",         CtdlGetConfigInt("c_auto_cull"));
375                 cprintf("1\n");
376                 cprintf("%d\n",         CtdlGetConfigInt("c_allow_spoofing"));
377                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_email"));
378                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_pubmsgs"));
379                 cprintf("%s\n",         CtdlGetConfigStr("c_journal_dest"));
380                 cprintf("%s\n",         CtdlGetConfigStr("c_default_cal_zone"));
381                 cprintf("%d\n",         CtdlGetConfigInt("c_pftcpdict_port"));
382                 cprintf("%d\n",         CtdlGetConfigInt("c_managesieve_port"));
383                 cprintf("%d\n",         CtdlGetConfigInt("c_auth_mode"));
384                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_host"));
385                 cprintf("%d\n",         CtdlGetConfigInt("c_funambol_port"));
386                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_source"));
387                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_auth"));
388                 cprintf("%d\n",         CtdlGetConfigInt("c_rbl_at_greeting"));
389                 cprintf("%s\n",         CtdlGetConfigStr("c_master_user"));
390                 cprintf("%s\n",         CtdlGetConfigStr("c_master_pass"));
391                 cprintf("%s\n",         CtdlGetConfigStr("c_pager_program"));
392                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_keep_from"));
393                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_c2s_port"));
394                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_s2s_port"));
395                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fetch"));
396                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fastest"));
397                 cprintf("%d\n",         CtdlGetConfigInt("c_spam_flag_only"));
398                 cprintf("%d\n",         CtdlGetConfigInt("c_guest_logins"));
399                 cprintf("%d\n",         CtdlGetConfigInt("c_port_number"));
400                 cprintf("%d\n",         ctdluid);
401                 cprintf("%d\n",         CtdlGetConfigInt("c_nntp_port"));
402                 cprintf("%d\n",         CtdlGetConfigInt("c_nntps_port"));
403                 cprintf("000\n");
404         }
405
406         else if (!strcasecmp(cmd, "SET")) {
407                 unbuffer_output();
408                 cprintf("%d Send configuration...\n", SEND_LISTING);
409                 a = 0;
410                 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
411                         switch (a) {
412                         case 0:
413                                 CtdlSetConfigStr("c_nodename", buf);
414                                 break;
415                         case 1:
416                                 CtdlSetConfigStr("c_fqdn", buf);
417                                 break;
418                         case 2:
419                                 CtdlSetConfigStr("c_humannode", buf);
420                                 break;
421                         case 3:
422                                 /* placeholder -- field no longer in use */
423                                 break;
424                         case 4:
425                                 CtdlSetConfigInt("c_creataide", atoi(buf));
426                                 break;
427                         case 5:
428                                 CtdlSetConfigInt("c_sleeping", atoi(buf));
429                                 break;
430                         case 6:
431                                 i = atoi(buf);
432                                 if (i < 1) i = 1;
433                                 if (i > 6) i = 6;
434                                 CtdlSetConfigInt("c_initax", i);
435                                 break;
436                         case 7:
437                                 CtdlSetConfigInt("c_regiscall", confbool(buf));
438                                 break;
439                         case 8:
440                                 CtdlSetConfigInt("c_twitdetect", confbool(buf));
441                                 break;
442                         case 9:
443                                 CtdlSetConfigStr("c_twitroom", buf);
444                                 break;
445                         case 10:
446                                 CtdlSetConfigStr("c_moreprompt", buf);
447                                 break;
448                         case 11:
449                                 CtdlSetConfigInt("c_restrict", confbool(buf));
450                                 break;
451                         case 12:
452                                 CtdlSetConfigInt("c_site_location", confbool(buf));
453                                 break;
454                         case 13:
455                                 CtdlSetConfigInt("c_sysadm", confbool(buf));
456                                 break;
457                         case 14:
458                                 i = atoi(buf);
459                                 if (i < 0) i = 0;
460                                 CtdlSetConfigInt("c_maxsessions", i);
461                                 break;
462                         case 15:
463                                 /* placeholder -- field no longer in use */
464                                 break;
465                         case 16:
466                                 CtdlSetConfigInt("c_userpurge", atoi(buf));
467                                 break;
468                         case 17:
469                                 CtdlSetConfigInt("c_roompurge", atoi(buf));
470                                 break;
471                         case 18:
472                                 CtdlSetConfigStr("c_logpages", buf);
473                                 break;
474                         case 19:
475                                 i = atoi(buf);
476                                 if (i < 1) i = 1;
477                                 if (i > 6) i = 6;
478                                 CtdlSetConfigInt("c_createax", i);
479                                 break;
480                         case 20:
481                                 ii = atol(buf);
482                                 if (ii >= 8192) {
483                                         CtdlSetConfigLong("c_maxmsglen", ii);
484                                 }
485                                 break;
486                         case 21:
487                                 i = atoi(buf);
488                                 if (i >= 3) {                                   // minimum value
489                                         CtdlSetConfigInt("c_min_workers", i);
490                                 }
491                                 break;
492                         case 22:
493                                 i = atoi(buf);
494                                 if (i >= CtdlGetConfigInt("c_min_workers")) {   // max must be >= min
495                                         CtdlSetConfigInt("c_max_workers", i);
496                                 }
497                                 break;
498                         case 23:
499                                 CtdlSetConfigInt("c_pop3_port", atoi(buf));
500                                 break;
501                         case 24:
502                                 CtdlSetConfigInt("c_smtp_port", atoi(buf));
503                                 break;
504                         case 25:
505                                 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
506                                 break;
507                         case 26:
508                                 CtdlSetConfigInt("c_aide_zap", confbool(buf));
509                                 break;
510                         case 27:
511                                 CtdlSetConfigInt("c_imap_port", atoi(buf));
512                                 break;
513                         case 28:
514                                 CtdlSetConfigLong("c_net_freq", atol(buf));
515                                 break;
516                         case 29:
517                                 CtdlSetConfigInt("c_disable_newu", confbool(buf));
518                                 break;
519                         case 30:
520                                 /* niu */
521                                 break;
522                         case 31:
523                                 if ((config.c_purge_hour >= 0)
524                                     && (config.c_purge_hour <= 23)) {
525                                         config.c_purge_hour = atoi(buf);
526                                 }
527                                 break;
528 #ifdef HAVE_LDAP
529                         case 32:
530                                 safestrncpy(config.c_ldap_host, buf, sizeof config.c_ldap_host);
531                                 break;
532                         case 33:
533                                 config.c_ldap_port = atoi(buf);
534                                 break;
535                         case 34:
536                                 safestrncpy(config.c_ldap_base_dn, buf, sizeof config.c_ldap_base_dn);
537                                 break;
538                         case 35:
539                                 safestrncpy(config.c_ldap_bind_dn, buf, sizeof config.c_ldap_bind_dn);
540                                 break;
541                         case 36:
542                                 safestrncpy(config.c_ldap_bind_pw, buf, sizeof config.c_ldap_bind_pw);
543                                 break;
544 #endif
545                         case 37:
546                                 safestrncpy(config.c_ip_addr, buf, sizeof config.c_ip_addr);
547                         case 38:
548                                 config.c_msa_port = atoi(buf);
549                                 break;
550                         case 39:
551                                 config.c_imaps_port = atoi(buf);
552                                 break;
553                         case 40:
554                                 config.c_pop3s_port = atoi(buf);
555                                 break;
556                         case 41:
557                                 config.c_smtps_port = atoi(buf);
558                                 break;
559                         case 42:
560                                 config.c_enable_fulltext = atoi(buf);
561                                 break;
562                         case 43:
563                                 config.c_auto_cull = atoi(buf);
564                                 break;
565                         case 44:
566                                 /* niu */
567                                 break;
568                         case 45:
569                                 config.c_allow_spoofing = atoi(buf);
570                                 break;
571                         case 46:
572                                 config.c_journal_email = atoi(buf);
573                                 break;
574                         case 47:
575                                 config.c_journal_pubmsgs = atoi(buf);
576                                 break;
577                         case 48:
578                                 safestrncpy(config.c_journal_dest, buf, sizeof config.c_journal_dest);
579                         case 49:
580                                 safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
581                                 break;
582                         case 50:
583                                 config.c_pftcpdict_port = atoi(buf);
584                                 break;
585                         case 51:
586                                 config.c_managesieve_port = atoi(buf);
587                                 break;
588                         case 52:
589                                 config.c_auth_mode = atoi(buf);
590                         case 53:
591                                 safestrncpy(config.c_funambol_host, buf, sizeof config.c_funambol_host);
592                                 break;
593                         case 54:
594                                 config.c_funambol_port = atoi(buf);
595                                 break;
596                         case 55:
597                                 safestrncpy(config.c_funambol_source, buf, sizeof config.c_funambol_source);
598                                 break;
599                         case 56:
600                                 safestrncpy(config.c_funambol_auth, buf, sizeof config.c_funambol_auth);
601                                 break;
602                         case 57:
603                                 config.c_rbl_at_greeting = atoi(buf);
604                                 break;
605                         case 58:
606                                 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
607                                 break;
608                         case 59:
609                                 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
610                                 break;
611                         case 60:
612                                 safestrncpy(config.c_pager_program, buf, sizeof config.c_pager_program);
613                                 break;
614                         case 61:
615                                 config.c_imap_keep_from = atoi(buf);
616                                 break;
617                         case 62:
618                                 config.c_xmpp_c2s_port = atoi(buf);
619                                 break;
620                         case 63:
621                                 config.c_xmpp_s2s_port = atoi(buf);
622                                 break;
623                         case 64:
624                                 config.c_pop3_fetch = atol(buf);
625                                 break;
626                         case 65:
627                                 config.c_pop3_fastest = atol(buf);
628                                 break;
629                         case 66:
630                                 config.c_spam_flag_only = atoi(buf);
631                                 break;
632                         case 67:
633                                 config.c_guest_logins = atoi(buf);
634                                 break;
635                         case 68:
636                                 config.c_port_number = atoi(buf);
637                                 break;
638                         case 69:
639                                 /* niu */
640                                 break;
641                         case 70:
642                                 config.c_nntp_port = atoi(buf);
643                                 break;
644                         case 71:
645                                 config.c_nntps_port = atoi(buf);
646                                 break;
647                         }
648                         ++a;
649                 }
650                 put_config();
651                 snprintf(buf, sizeof buf,
652                         "The global system configuration has been edited by %s.\n",
653                          (CC->logged_in ? CC->curr_user : "an administrator")
654                 );
655                 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
656
657                 if (!IsEmptyStr(config.c_logpages))
658                         CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
659
660                 /* If full text indexing has been disabled, invalidate the
661                  * index so it doesn't try to use it later.
662                  */
663                 if (config.c_enable_fulltext == 0) {
664                         CitControl.MM_fulltext_wordbreaker = 0;
665                         put_control();
666                 }
667         }
668
669         else if (!strcasecmp(cmd, "GETSYS")) {
670                 extract_token(confname, argbuf, 1, '|', sizeof confname);
671                 confptr = CtdlGetSysConfig(confname);
672                 if (confptr != NULL) {
673                         long len; 
674
675                         len = strlen(confptr);
676                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
677                         client_write(confptr, len);
678                         if ((len > 0) && (confptr[len - 1] != 10))
679                                 client_write("\n", 1);
680                         cprintf("000\n");
681                         free(confptr);
682                 } else {
683                         cprintf("%d No such configuration.\n",
684                                 ERROR + ILLEGAL_VALUE);
685                 }
686         }
687
688         else if (!strcasecmp(cmd, "PUTSYS")) {
689                 extract_token(confname, argbuf, 1, '|', sizeof confname);
690                 unbuffer_output();
691                 cprintf("%d %s\n", SEND_LISTING, confname);
692                 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
693                 CtdlPutSysConfig(confname, confptr);
694                 free(confptr);
695         }
696
697         else {
698                 cprintf("%d Illegal option(s) specified.\n",
699                         ERROR + ILLEGAL_VALUE);
700         }
701 }
702
703 typedef struct __ConfType {
704         ConstStr Name;
705         long Type;
706 }ConfType;
707
708 ConfType CfgNames[] = {
709         { {HKEY("localhost") },    0},
710         { {HKEY("directory") },    0},
711         { {HKEY("smarthost") },    2},
712         { {HKEY("fallbackhost") }, 2},
713         { {HKEY("rbl") },          3},
714         { {HKEY("spamassassin") }, 3},
715         { {HKEY("masqdomain") },   1},
716         { {HKEY("clamav") },       3},
717         { {HKEY("notify") },       3},
718         { {NULL, 0}, 0}
719 };
720
721 HashList *CfgNameHash = NULL;
722 void cmd_gvdn(char *argbuf)
723 {
724         const ConfType *pCfg;
725         char *confptr;
726         long min = atol(argbuf);
727         const char *Pos = NULL;
728         const char *PPos = NULL;
729         const char *HKey;
730         long HKLen;
731         StrBuf *Line;
732         StrBuf *Config;
733         StrBuf *Cfg;
734         StrBuf *CfgToken;
735         HashList *List;
736         HashPos *It;
737         void *vptr;
738         
739         List = NewHash(1, NULL);
740         Cfg = NewStrBufPlain(config.c_fqdn, -1);
741         Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
742         Cfg = NULL;
743
744         confptr = CtdlGetSysConfig(INTERNETCFG);
745         Config = NewStrBufPlain(confptr, -1);
746         free(confptr);
747
748         Line = NewStrBufPlain(NULL, StrLength(Config));
749         CfgToken = NewStrBufPlain(NULL, StrLength(Config));
750         while (StrBufSipLine(Line, Config, &Pos))
751         {
752                 if (Cfg == NULL)
753                         Cfg = NewStrBufPlain(NULL, StrLength(Line));
754                 PPos = NULL;
755                 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
756                 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
757                 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
758                     (vptr != NULL))
759                 {
760                         pCfg = (ConfType *) vptr;
761                         if (pCfg->Type <= min)
762                         {
763                                 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
764                                 Cfg = NULL;
765                         }
766                 }
767         }
768
769         cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
770         It = GetNewHashPos(List, 1);
771         while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
772         {
773                 cputbuf(vptr);
774                 cprintf("\n");
775         }
776         cprintf("000\n");
777
778         DeleteHashPos(&It);
779         DeleteHash(&List);
780         FreeStrBuf(&Cfg);
781         FreeStrBuf(&Line);
782         FreeStrBuf(&CfgToken);
783         FreeStrBuf(&Config);
784 }
785
786 /*****************************************************************************/
787 /*                      MODULE INITIALIZATION STUFF                          */
788 /*****************************************************************************/
789
790 void control_cleanup(void)
791 {
792         DeleteHash(&CfgNameHash);
793 }
794 CTDL_MODULE_INIT(control)
795 {
796         if (!threading) {
797                 int i;
798
799                 CfgNameHash = NewHash(1, NULL);
800                 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
801                         Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
802
803                 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
804                 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
805                 CtdlRegisterCleanupHook(control_cleanup);
806
807         }
808         /* return our id for the Log */
809         return "control";
810 }