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