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