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