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