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