Finished migrating legacy control record to new config format
[citadel.git] / citadel / control.c
1 /*
2  * This module handles states which are global to the entire server.
3  *
4  * Copyright (c) 1987-2015 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 control record for the message base... 
28  */
29 struct legacy_ctrl_format {
30         long MMhighest;                 /* highest message number in file   */
31         unsigned MMflags;               /* Global system flags              */
32         long MMnextuser;                /* highest user number on system    */
33         long MMnextroom;                /* highest room number on system    */
34         int MM_hosted_upgrade_level;    /* Server-hosted upgrade level      */
35         int MM_fulltext_wordbreaker;    /* ID of wordbreaker in use         */
36         long MMfulltext;                /* highest message number indexed   */
37         int MMdbversion;                /* Version of Berkeley DB used on previous server run */
38 };
39
40
41
42 /*
43  * callback to get highest room number when rebuilding control file
44  */
45 void control_find_highest(struct ctdlroom *qrbuf, void *data)
46 {
47         struct ctdlroom room;
48         struct cdbdata *cdbfr;
49         long *msglist;
50         int num_msgs=0;
51         int c;
52         int room_fixed = 0;
53         int message_fixed = 0;
54         
55         if (qrbuf->QRnumber > CtdlGetConfigLong("MMnextroom"))
56         {
57                 CtdlSetConfigLong("MMnextroom", qrbuf->QRnumber);
58                 room_fixed = 1;
59         }
60                 
61         CtdlGetRoom (&room, qrbuf->QRname);
62         
63         /* Load the message list */
64         cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
65         if (cdbfr != NULL) {
66                 msglist = (long *) cdbfr->ptr;
67                 num_msgs = cdbfr->len / sizeof(long);
68         } else {
69                 return; /* No messages at all?  No further action. */
70         }
71
72         if (num_msgs>0)
73         {
74                 for (c=0; c<num_msgs; c++)
75                 {
76                         if (msglist[c] > CtdlGetConfigLong("MMhighest"))
77                         {
78                                 CtdlSetConfigLong("MMhighest", msglist[c]);
79                                 message_fixed = 1;
80                         }
81                 }
82         }
83         cdb_free(cdbfr);
84         if (room_fixed) {
85                 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
86         }
87         if (message_fixed) {
88                 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
89         }
90         return;
91 }
92
93
94 /*
95  * Callback to get highest user number.
96  */
97  
98 void control_find_user (struct ctdluser *EachUser, void *out_data)
99 {
100         int user_fixed = 0;
101         
102         if (EachUser->usernum > CtdlGetConfigLong("MMnextuser"))
103         {
104                 CtdlSetConfigLong("MMnextuser", EachUser->usernum);
105                 user_fixed = 1;
106         }
107         if(user_fixed)
108                 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
109 }
110
111
112 /*
113  * If we have a legacy format control record on disk, import it.
114  */
115 void migrate_legacy_control_record(void)
116 {
117         FILE *fp = NULL;
118         struct legacy_ctrl_format c;
119         memset(&c, 0, sizeof(c));
120
121         fp = fopen(file_citadel_control, "rb+");
122         if (fp != NULL) {
123                 syslog(LOG_INFO, "Legacy format control record found -- importing to db");
124                 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
125                 
126                 CtdlSetConfigLong(      "MMhighest",                    c.MMhighest);
127                 CtdlSetConfigInt(       "MMflags",                      c.MMflags);
128                 CtdlSetConfigLong(      "MMnextuser",                   c.MMnextuser);
129                 CtdlSetConfigLong(      "MMnextroom",                   c.MMnextroom);
130                 CtdlSetConfigInt(       "MM_hosted_upgrade_level",      c.MM_hosted_upgrade_level);
131                 CtdlSetConfigInt(       "MM_fulltext_wordbreaker",      c.MM_fulltext_wordbreaker);
132                 CtdlSetConfigLong(      "MMfulltext",                   c.MMfulltext);
133
134                 fclose(fp);
135                 if (unlink(file_citadel_control) != 0) {
136                         fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
137                         fprintf(stderr, "Exiting to prevent data corruption.\n");
138                         exit(CTDLEXIT_CONFIG);
139                 }
140         }
141 }
142
143
144
145
146
147 /*
148  * check_control   -  check the control record has sensible values for message, user and room numbers
149  */
150 void check_control(void)
151 {
152         syslog(LOG_INFO, "Sanity checking the recorded highest message, user, and room numbers\n");
153         CtdlForEachRoom(control_find_highest, NULL);
154         ForEachUser(control_find_user, NULL);
155 }
156
157
158
159 /*
160  * get_new_message_number()  -  Obtain a new, unique ID to be used for a message.
161  */
162 long get_new_message_number(void)
163 {
164         long retval = 0L;
165         begin_critical_section(S_CONTROL);
166         retval = CtdlGetConfigLong("MMhighest");
167         ++retval;
168         CtdlSetConfigLong("MMhighest", retval);
169         end_critical_section(S_CONTROL);
170         return(retval);
171 }
172
173
174 /*
175  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
176  * This provides a quick way to initialise a variable that might be used to indicate
177  * messages that should not be processed. EG. a new Sieve script will use this
178  * to record determine that messages older than this should not be processed.
179  *
180  * (Why is this function here?  Can't we just go straight to the config variable it fetches?)
181  */
182 long CtdlGetCurrentMessageNumber(void)
183 {
184         return CtdlGetConfigLong("MMhighest");
185 }
186
187
188 /*
189  * get_new_user_number()  -  Obtain a new, unique ID to be used for a user.
190  */
191 long get_new_user_number(void)
192 {
193         long retval = 0L;
194         begin_critical_section(S_CONTROL);
195         retval = CtdlGetConfigLong("MMnextuser");
196         ++retval;
197         CtdlSetConfigLong("MMnextuser", retval);
198         end_critical_section(S_CONTROL);
199         return(retval);
200 }
201
202
203
204 /*
205  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
206  */
207 long get_new_room_number(void)
208 {
209         long retval = 0L;
210         begin_critical_section(S_CONTROL);
211         retval = CtdlGetConfigLong("MMnextroom");
212         ++retval;
213         CtdlSetConfigLong("MMnextroom", retval);
214         end_critical_section(S_CONTROL);
215         return(retval);
216 }
217
218
219
220 /*
221  * Helper function for cmd_conf() to handle boolean values
222  */
223 int confbool(char *v)
224 {
225         if (IsEmptyStr(v)) return(0);
226         if (atoi(v) != 0) return(1);
227         return(0);
228 }
229
230
231 /* 
232  * Get or set global configuration options
233  *
234  * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
235  * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
236  *
237  */
238 void cmd_conf(char *argbuf)
239 {
240         char cmd[16];
241         char buf[256];
242         int a, i;
243         long ii;
244         char *confptr;
245         char confname[128];
246
247         if (CtdlAccessCheck(ac_aide)) return;
248
249         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
250         if (!strcasecmp(cmd, "GET")) {
251                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
252                 cprintf("%s\n",         CtdlGetConfigStr("c_nodename"));
253                 cprintf("%s\n",         CtdlGetConfigStr("c_fqdn"));
254                 cprintf("%s\n",         CtdlGetConfigStr("c_humannode"));
255                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
256                 cprintf("%d\n",         CtdlGetConfigInt("c_creataide"));
257                 cprintf("%d\n",         CtdlGetConfigInt("c_sleeping"));
258                 cprintf("%d\n",         CtdlGetConfigInt("c_initax"));
259                 cprintf("%d\n",         CtdlGetConfigInt("c_regiscall"));
260                 cprintf("%d\n",         CtdlGetConfigInt("c_twitdetect"));
261                 cprintf("%s\n",         CtdlGetConfigStr("c_twitroom"));
262                 cprintf("%s\n",         CtdlGetConfigStr("c_moreprompt"));
263                 cprintf("%d\n",         CtdlGetConfigInt("c_restrict"));
264                 cprintf("%s\n",         CtdlGetConfigStr("c_site_location"));
265                 cprintf("%s\n",         CtdlGetConfigStr("c_sysadm"));
266                 cprintf("%d\n",         CtdlGetConfigInt("c_maxsessions"));
267                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
268                 cprintf("%d\n",         CtdlGetConfigInt("c_userpurge"));
269                 cprintf("%d\n",         CtdlGetConfigInt("c_roompurge"));
270                 cprintf("%s\n",         CtdlGetConfigStr("c_logpages"));
271                 cprintf("%d\n",         CtdlGetConfigInt("c_createax"));
272                 cprintf("%ld\n",        CtdlGetConfigLong("c_maxmsglen"));
273                 cprintf("%d\n",         CtdlGetConfigInt("c_min_workers"));
274                 cprintf("%d\n",         CtdlGetConfigInt("c_max_workers"));
275                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3_port"));
276                 cprintf("%d\n",         CtdlGetConfigInt("c_smtp_port"));
277                 cprintf("%d\n",         CtdlGetConfigInt("c_rfc822_strict_from"));
278                 cprintf("%d\n",         CtdlGetConfigInt("c_aide_zap"));
279                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_port"));
280                 cprintf("%ld\n",        CtdlGetConfigLong("c_net_freq"));
281                 cprintf("%d\n",         CtdlGetConfigInt("c_disable_newu"));
282                 cprintf("1\n"); /* niu */
283                 cprintf("%d\n",         CtdlGetConfigInt("c_purge_hour"));
284 #ifdef HAVE_LDAP
285                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_host"));
286                 cprintf("%d\n",         CtdlGetConfigInt("c_ldap_port"));
287                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_base_dn"));
288                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_dn"));
289                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_pw"));
290 #else
291                 cprintf("\n");
292                 cprintf("0\n");
293                 cprintf("\n");
294                 cprintf("\n");
295                 cprintf("\n");
296 #endif
297                 cprintf("%s\n",         CtdlGetConfigStr("c_ip_addr"));
298                 cprintf("%d\n",         CtdlGetConfigInt("c_msa_port"));
299                 cprintf("%d\n",         CtdlGetConfigInt("c_imaps_port"));
300                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3s_port"));
301                 cprintf("%d\n",         CtdlGetConfigInt("c_smtps_port"));
302                 cprintf("%d\n",         CtdlGetConfigInt("c_enable_fulltext"));
303                 cprintf("%d\n",         CtdlGetConfigInt("c_auto_cull"));
304                 cprintf("1\n");
305                 cprintf("%d\n",         CtdlGetConfigInt("c_allow_spoofing"));
306                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_email"));
307                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_pubmsgs"));
308                 cprintf("%s\n",         CtdlGetConfigStr("c_journal_dest"));
309                 cprintf("%s\n",         CtdlGetConfigStr("c_default_cal_zone"));
310                 cprintf("%d\n",         CtdlGetConfigInt("c_pftcpdict_port"));
311                 cprintf("%d\n",         CtdlGetConfigInt("c_managesieve_port"));
312                 cprintf("%d\n",         CtdlGetConfigInt("c_auth_mode"));
313                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_host"));
314                 cprintf("%d\n",         CtdlGetConfigInt("c_funambol_port"));
315                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_source"));
316                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_auth"));
317                 cprintf("%d\n",         CtdlGetConfigInt("c_rbl_at_greeting"));
318                 cprintf("%s\n",         CtdlGetConfigStr("c_master_user"));
319                 cprintf("%s\n",         CtdlGetConfigStr("c_master_pass"));
320                 cprintf("%s\n",         CtdlGetConfigStr("c_pager_program"));
321                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_keep_from"));
322                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_c2s_port"));
323                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_s2s_port"));
324                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fetch"));
325                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fastest"));
326                 cprintf("%d\n",         CtdlGetConfigInt("c_spam_flag_only"));
327                 cprintf("%d\n",         CtdlGetConfigInt("c_guest_logins"));
328                 cprintf("%d\n",         CtdlGetConfigInt("c_port_number"));
329                 cprintf("%d\n",         ctdluid);
330                 cprintf("%d\n",         CtdlGetConfigInt("c_nntp_port"));
331                 cprintf("%d\n",         CtdlGetConfigInt("c_nntps_port"));
332                 cprintf("000\n");
333         }
334
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", atoi(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                                 CtdlSetConfigInt("c_site_location", confbool(buf));
382                                 break;
383                         case 13:
384                                 CtdlSetConfigInt("c_sysadm", confbool(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                                 CtdlSetConfigStr("c_funambol_host", buf);
522                                 break;
523                         case 54:
524                                 CtdlSetConfigInt("c_funambol_port", atoi(buf));
525                                 break;
526                         case 55:
527                                 CtdlSetConfigStr("c_funambol_source", buf);
528                                 break;
529                         case 56:
530                                 CtdlSetConfigStr("c_funambol_auth", buf);
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         else if (!strcasecmp(cmd, "GETSYS")) {
598                 extract_token(confname, argbuf, 1, '|', sizeof confname);
599                 confptr = CtdlGetSysConfig(confname);
600                 if (confptr != NULL) {
601                         long len; 
602
603                         len = strlen(confptr);
604                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
605                         client_write(confptr, len);
606                         if ((len > 0) && (confptr[len - 1] != 10))
607                                 client_write("\n", 1);
608                         cprintf("000\n");
609                         free(confptr);
610                 } else {
611                         cprintf("%d No such configuration.\n",
612                                 ERROR + ILLEGAL_VALUE);
613                 }
614         }
615
616         else if (!strcasecmp(cmd, "PUTSYS")) {
617                 extract_token(confname, argbuf, 1, '|', sizeof confname);
618                 unbuffer_output();
619                 cprintf("%d %s\n", SEND_LISTING, confname);
620                 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
621                 CtdlPutSysConfig(confname, confptr);
622                 free(confptr);
623         }
624
625         else {
626                 cprintf("%d Illegal option(s) specified.\n",
627                         ERROR + ILLEGAL_VALUE);
628         }
629 }
630
631 typedef struct __ConfType {
632         ConstStr Name;
633         long Type;
634 }ConfType;
635
636 ConfType CfgNames[] = {
637         { {HKEY("localhost") },    0},
638         { {HKEY("directory") },    0},
639         { {HKEY("smarthost") },    2},
640         { {HKEY("fallbackhost") }, 2},
641         { {HKEY("rbl") },          3},
642         { {HKEY("spamassassin") }, 3},
643         { {HKEY("masqdomain") },   1},
644         { {HKEY("clamav") },       3},
645         { {HKEY("notify") },       3},
646         { {NULL, 0}, 0}
647 };
648
649 HashList *CfgNameHash = NULL;
650 void cmd_gvdn(char *argbuf)
651 {
652         const ConfType *pCfg;
653         char *confptr;
654         long min = atol(argbuf);
655         const char *Pos = NULL;
656         const char *PPos = NULL;
657         const char *HKey;
658         long HKLen;
659         StrBuf *Line;
660         StrBuf *Config;
661         StrBuf *Cfg;
662         StrBuf *CfgToken;
663         HashList *List;
664         HashPos *It;
665         void *vptr;
666         
667         List = NewHash(1, NULL);
668         Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
669         Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
670         Cfg = NULL;
671
672         confptr = CtdlGetSysConfig(INTERNETCFG);
673         Config = NewStrBufPlain(confptr, -1);
674         free(confptr);
675
676         Line = NewStrBufPlain(NULL, StrLength(Config));
677         CfgToken = NewStrBufPlain(NULL, StrLength(Config));
678         while (StrBufSipLine(Line, Config, &Pos))
679         {
680                 if (Cfg == NULL)
681                         Cfg = NewStrBufPlain(NULL, StrLength(Line));
682                 PPos = NULL;
683                 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
684                 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
685                 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
686                     (vptr != NULL))
687                 {
688                         pCfg = (ConfType *) vptr;
689                         if (pCfg->Type <= min)
690                         {
691                                 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
692                                 Cfg = NULL;
693                         }
694                 }
695         }
696
697         cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
698         It = GetNewHashPos(List, 1);
699         while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
700         {
701                 cputbuf(vptr);
702                 cprintf("\n");
703         }
704         cprintf("000\n");
705
706         DeleteHashPos(&It);
707         DeleteHash(&List);
708         FreeStrBuf(&Cfg);
709         FreeStrBuf(&Line);
710         FreeStrBuf(&CfgToken);
711         FreeStrBuf(&Config);
712 }
713
714 /*****************************************************************************/
715 /*                      MODULE INITIALIZATION STUFF                          */
716 /*****************************************************************************/
717
718 void control_cleanup(void)
719 {
720         DeleteHash(&CfgNameHash);
721 }
722 CTDL_MODULE_INIT(control)
723 {
724         if (!threading) {
725                 int i;
726
727                 CfgNameHash = NewHash(1, NULL);
728                 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
729                         Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
730
731                 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
732                 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
733                 CtdlRegisterCleanupHook(control_cleanup);
734
735         }
736         /* return our id for the Log */
737         return "control";
738 }