2339f07f4b180aa1dcabd6a61c35f2d878f593df
[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  * check_control   -  check the control record has sensible values for message, user and room numbers
146  */
147 void check_control(void)
148 {
149         syslog(LOG_INFO, "Sanity checking the recorded highest message, user, and room numbers\n");
150         CtdlForEachRoom(control_find_highest, NULL);
151         ForEachUser(control_find_user, NULL);
152 }
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 /*
202  * get_new_room_number()  -  Obtain a new, unique ID to be used for a room.
203  */
204 long get_new_room_number(void)
205 {
206         long retval = 0L;
207         begin_critical_section(S_CONTROL);
208         retval = CtdlGetConfigLong("MMnextroom");
209         ++retval;
210         CtdlSetConfigLong("MMnextroom", retval);
211         end_critical_section(S_CONTROL);
212         return(retval);
213 }
214
215
216
217 /*
218  * Helper function for cmd_conf() to handle boolean values
219  */
220 int confbool(char *v)
221 {
222         if (IsEmptyStr(v)) return(0);
223         if (atoi(v) != 0) return(1);
224         return(0);
225 }
226
227
228 /* 
229  * Get or set global configuration options
230  *
231  * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
232  * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
233  *
234  */
235 void cmd_conf(char *argbuf)
236 {
237         char cmd[16];
238         char buf[256];
239         int a, i;
240         long ii;
241         char *confptr;
242         char confname[128];
243
244         if (CtdlAccessCheck(ac_aide)) return;
245
246         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
247         if (!strcasecmp(cmd, "GET")) {
248                 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
249                 cprintf("%s\n",         CtdlGetConfigStr("c_nodename"));
250                 cprintf("%s\n",         CtdlGetConfigStr("c_fqdn"));
251                 cprintf("%s\n",         CtdlGetConfigStr("c_humannode"));
252                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
253                 cprintf("%d\n",         CtdlGetConfigInt("c_creataide"));
254                 cprintf("%d\n",         CtdlGetConfigInt("c_sleeping"));
255                 cprintf("%d\n",         CtdlGetConfigInt("c_initax"));
256                 cprintf("%d\n",         CtdlGetConfigInt("c_regiscall"));
257                 cprintf("%d\n",         CtdlGetConfigInt("c_twitdetect"));
258                 cprintf("%s\n",         CtdlGetConfigStr("c_twitroom"));
259                 cprintf("%s\n",         CtdlGetConfigStr("c_moreprompt"));
260                 cprintf("%d\n",         CtdlGetConfigInt("c_restrict"));
261                 cprintf("%s\n",         CtdlGetConfigStr("c_site_location"));
262                 cprintf("%s\n",         CtdlGetConfigStr("c_sysadm"));
263                 cprintf("%d\n",         CtdlGetConfigInt("c_maxsessions"));
264                 cprintf("xxx\n"); /* placeholder -- field no longer in use */
265                 cprintf("%d\n",         CtdlGetConfigInt("c_userpurge"));
266                 cprintf("%d\n",         CtdlGetConfigInt("c_roompurge"));
267                 cprintf("%s\n",         CtdlGetConfigStr("c_logpages"));
268                 cprintf("%d\n",         CtdlGetConfigInt("c_createax"));
269                 cprintf("%ld\n",        CtdlGetConfigLong("c_maxmsglen"));
270                 cprintf("%d\n",         CtdlGetConfigInt("c_min_workers"));
271                 cprintf("%d\n",         CtdlGetConfigInt("c_max_workers"));
272                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3_port"));
273                 cprintf("%d\n",         CtdlGetConfigInt("c_smtp_port"));
274                 cprintf("%d\n",         CtdlGetConfigInt("c_rfc822_strict_from"));
275                 cprintf("%d\n",         CtdlGetConfigInt("c_aide_zap"));
276                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_port"));
277                 cprintf("%ld\n",        CtdlGetConfigLong("c_net_freq"));
278                 cprintf("%d\n",         CtdlGetConfigInt("c_disable_newu"));
279                 cprintf("1\n"); /* niu */
280                 cprintf("%d\n",         CtdlGetConfigInt("c_purge_hour"));
281 #ifdef HAVE_LDAP
282                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_host"));
283                 cprintf("%d\n",         CtdlGetConfigInt("c_ldap_port"));
284                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_base_dn"));
285                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_dn"));
286                 cprintf("%s\n",         CtdlGetConfigStr("c_ldap_bind_pw"));
287 #else
288                 cprintf("\n");
289                 cprintf("0\n");
290                 cprintf("\n");
291                 cprintf("\n");
292                 cprintf("\n");
293 #endif
294                 cprintf("%s\n",         CtdlGetConfigStr("c_ip_addr"));
295                 cprintf("%d\n",         CtdlGetConfigInt("c_msa_port"));
296                 cprintf("%d\n",         CtdlGetConfigInt("c_imaps_port"));
297                 cprintf("%d\n",         CtdlGetConfigInt("c_pop3s_port"));
298                 cprintf("%d\n",         CtdlGetConfigInt("c_smtps_port"));
299                 cprintf("%d\n",         CtdlGetConfigInt("c_enable_fulltext"));
300                 cprintf("%d\n",         CtdlGetConfigInt("c_auto_cull"));
301                 cprintf("1\n");
302                 cprintf("%d\n",         CtdlGetConfigInt("c_allow_spoofing"));
303                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_email"));
304                 cprintf("%d\n",         CtdlGetConfigInt("c_journal_pubmsgs"));
305                 cprintf("%s\n",         CtdlGetConfigStr("c_journal_dest"));
306                 cprintf("%s\n",         CtdlGetConfigStr("c_default_cal_zone"));
307                 cprintf("%d\n",         CtdlGetConfigInt("c_pftcpdict_port"));
308                 cprintf("%d\n",         CtdlGetConfigInt("c_managesieve_port"));
309                 cprintf("%d\n",         CtdlGetConfigInt("c_auth_mode"));
310                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_host"));
311                 cprintf("%d\n",         CtdlGetConfigInt("c_funambol_port"));
312                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_source"));
313                 cprintf("%s\n",         CtdlGetConfigStr("c_funambol_auth"));
314                 cprintf("%d\n",         CtdlGetConfigInt("c_rbl_at_greeting"));
315                 cprintf("%s\n",         CtdlGetConfigStr("c_master_user"));
316                 cprintf("%s\n",         CtdlGetConfigStr("c_master_pass"));
317                 cprintf("%s\n",         CtdlGetConfigStr("c_pager_program"));
318                 cprintf("%d\n",         CtdlGetConfigInt("c_imap_keep_from"));
319                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_c2s_port"));
320                 cprintf("%d\n",         CtdlGetConfigInt("c_xmpp_s2s_port"));
321                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fetch"));
322                 cprintf("%ld\n",        CtdlGetConfigLong("c_pop3_fastest"));
323                 cprintf("%d\n",         CtdlGetConfigInt("c_spam_flag_only"));
324                 cprintf("%d\n",         CtdlGetConfigInt("c_guest_logins"));
325                 cprintf("%d\n",         CtdlGetConfigInt("c_port_number"));
326                 cprintf("%d\n",         ctdluid);
327                 cprintf("%d\n",         CtdlGetConfigInt("c_nntp_port"));
328                 cprintf("%d\n",         CtdlGetConfigInt("c_nntps_port"));
329                 cprintf("000\n");
330         }
331
332         else if (!strcasecmp(cmd, "SET")) {
333                 unbuffer_output();
334                 cprintf("%d Send configuration...\n", SEND_LISTING);
335                 a = 0;
336                 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
337                         switch (a) {
338                         case 0:
339                                 CtdlSetConfigStr("c_nodename", buf);
340                                 break;
341                         case 1:
342                                 CtdlSetConfigStr("c_fqdn", buf);
343                                 break;
344                         case 2:
345                                 CtdlSetConfigStr("c_humannode", buf);
346                                 break;
347                         case 3:
348                                 /* placeholder -- field no longer in use */
349                                 break;
350                         case 4:
351                                 CtdlSetConfigInt("c_creataide", atoi(buf));
352                                 break;
353                         case 5:
354                                 CtdlSetConfigInt("c_sleeping", atoi(buf));
355                                 break;
356                         case 6:
357                                 i = atoi(buf);
358                                 if (i < 1) i = 1;
359                                 if (i > 6) i = 6;
360                                 CtdlSetConfigInt("c_initax", i);
361                                 break;
362                         case 7:
363                                 CtdlSetConfigInt("c_regiscall", confbool(buf));
364                                 break;
365                         case 8:
366                                 CtdlSetConfigInt("c_twitdetect", confbool(buf));
367                                 break;
368                         case 9:
369                                 CtdlSetConfigStr("c_twitroom", buf);
370                                 break;
371                         case 10:
372                                 CtdlSetConfigStr("c_moreprompt", buf);
373                                 break;
374                         case 11:
375                                 CtdlSetConfigInt("c_restrict", confbool(buf));
376                                 break;
377                         case 12:
378                                 CtdlSetConfigInt("c_site_location", confbool(buf));
379                                 break;
380                         case 13:
381                                 CtdlSetConfigInt("c_sysadm", confbool(buf));
382                                 break;
383                         case 14:
384                                 i = atoi(buf);
385                                 if (i < 0) i = 0;
386                                 CtdlSetConfigInt("c_maxsessions", i);
387                                 break;
388                         case 15:
389                                 /* placeholder -- field no longer in use */
390                                 break;
391                         case 16:
392                                 CtdlSetConfigInt("c_userpurge", atoi(buf));
393                                 break;
394                         case 17:
395                                 CtdlSetConfigInt("c_roompurge", atoi(buf));
396                                 break;
397                         case 18:
398                                 CtdlSetConfigStr("c_logpages", buf);
399                                 break;
400                         case 19:
401                                 i = atoi(buf);
402                                 if (i < 1) i = 1;
403                                 if (i > 6) i = 6;
404                                 CtdlSetConfigInt("c_createax", i);
405                                 break;
406                         case 20:
407                                 ii = atol(buf);
408                                 if (ii >= 8192) {
409                                         CtdlSetConfigLong("c_maxmsglen", ii);
410                                 }
411                                 break;
412                         case 21:
413                                 i = atoi(buf);
414                                 if (i >= 3) {                                   // minimum value
415                                         CtdlSetConfigInt("c_min_workers", i);
416                                 }
417                                 break;
418                         case 22:
419                                 i = atoi(buf);
420                                 if (i >= CtdlGetConfigInt("c_min_workers")) {   // max must be >= min
421                                         CtdlSetConfigInt("c_max_workers", i);
422                                 }
423                                 break;
424                         case 23:
425                                 CtdlSetConfigInt("c_pop3_port", atoi(buf));
426                                 break;
427                         case 24:
428                                 CtdlSetConfigInt("c_smtp_port", atoi(buf));
429                                 break;
430                         case 25:
431                                 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
432                                 break;
433                         case 26:
434                                 CtdlSetConfigInt("c_aide_zap", confbool(buf));
435                                 break;
436                         case 27:
437                                 CtdlSetConfigInt("c_imap_port", atoi(buf));
438                                 break;
439                         case 28:
440                                 CtdlSetConfigLong("c_net_freq", atol(buf));
441                                 break;
442                         case 29:
443                                 CtdlSetConfigInt("c_disable_newu", confbool(buf));
444                                 break;
445                         case 30:
446                                 /* niu */
447                                 break;
448                         case 31:
449                                 i = atoi(buf);
450                                 if ((i >= 0) && (i <= 23)) {
451                                         CtdlSetConfigInt("c_purge_hour", i);
452                                 }
453                                 break;
454                         case 32:
455                                 CtdlSetConfigStr("c_ldap_host", buf);
456                                 break;
457                         case 33:
458                                 CtdlSetConfigInt("c_ldap_port", atoi(buf));
459                                 break;
460                         case 34:
461                                 CtdlSetConfigStr("c_ldap_base_dn", buf);
462                                 break;
463                         case 35:
464                                 CtdlSetConfigStr("c_ldap_bind_dn", buf);
465                                 break;
466                         case 36:
467                                 CtdlSetConfigStr("c_ldap_bind_pw", buf);
468                                 break;
469                         case 37:
470                                 CtdlSetConfigStr("c_ip_addr", buf);
471                                 break;
472                         case 38:
473                                 CtdlSetConfigInt("c_msa_port", atoi(buf));
474                                 break;
475                         case 39:
476                                 CtdlSetConfigInt("c_imaps_port", atoi(buf));
477                                 break;
478                         case 40:
479                                 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
480                                 break;
481                         case 41:
482                                 CtdlSetConfigInt("c_smtps_port", atoi(buf));
483                                 break;
484                         case 42:
485                                 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
486                                 break;
487                         case 43:
488                                 CtdlSetConfigInt("c_auto_cull", confbool(buf));
489                                 break;
490                         case 44:
491                                 /* niu */
492                                 break;
493                         case 45:
494                                 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
495                                 break;
496                         case 46:
497                                 CtdlSetConfigInt("c_journal_email", confbool(buf));
498                                 break;
499                         case 47:
500                                 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
501                                 break;
502                         case 48:
503                                 CtdlSetConfigStr("c_journal_dest", buf);
504                                 break;
505                         case 49:
506                                 CtdlSetConfigStr("c_default_cal_zone", buf);
507                                 break;
508                         case 50:
509                                 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
510                                 break;
511                         case 51:
512                                 CtdlSetConfigInt("c_managesieve_port", atoi(buf));
513                                 break;
514                         case 52:
515                                 CtdlSetConfigInt("c_auth_mode", atoi(buf));
516                                 break;
517                         case 53:
518                                 CtdlSetConfigStr("c_funambol_host", buf);
519                                 break;
520                         case 54:
521                                 CtdlSetConfigInt("c_funambol_port", atoi(buf));
522                                 break;
523                         case 55:
524                                 CtdlSetConfigStr("c_funambol_source", buf);
525                                 break;
526                         case 56:
527                                 CtdlSetConfigStr("c_funambol_auth", buf);
528                                 break;
529                         case 57:
530                                 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
531                                 break;
532                         case 58:
533                                 CtdlSetConfigStr("c_master_user", buf);
534                                 break;
535                         case 59:
536                                 CtdlSetConfigStr("c_master_pass", buf);
537                                 break;
538                         case 60:
539                                 CtdlSetConfigStr("c_pager_program", buf);
540                                 break;
541                         case 61:
542                                 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
543                                 break;
544                         case 62:
545                                 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
546                                 break;
547                         case 63:
548                                 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
549                                 break;
550                         case 64:
551                                 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
552                                 break;
553                         case 65:
554                                 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
555                                 break;
556                         case 66:
557                                 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
558                                 break;
559                         case 67:
560                                 CtdlSetConfigInt("c_guest_logins", confbool(buf));
561                                 break;
562                         case 68:
563                                 CtdlSetConfigInt("c_port_number", atoi(buf));
564                                 break;
565                         case 69:
566                                 /* niu */
567                                 break;
568                         case 70:
569                                 CtdlSetConfigInt("c_nntp_port", atoi(buf));
570                                 break;
571                         case 71:
572                                 CtdlSetConfigInt("c_nntps_port", atoi(buf));
573                                 break;
574                         }
575                         ++a;
576                 }
577                 snprintf(buf, sizeof buf,
578                         "The global system configuration has been edited by %s.\n",
579                          (CC->logged_in ? CC->curr_user : "an administrator")
580                 );
581                 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
582
583                 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
584                         CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
585
586                 /* If full text indexing has been disabled, invalidate the
587                  * index so it doesn't try to use it later.
588                  */
589                 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
590                         CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
591                 }
592         }
593
594         else if (!strcasecmp(cmd, "GETSYS")) {
595                 extract_token(confname, argbuf, 1, '|', sizeof confname);
596                 confptr = CtdlGetSysConfig(confname);
597                 if (confptr != NULL) {
598                         long len; 
599
600                         len = strlen(confptr);
601                         cprintf("%d %s\n", LISTING_FOLLOWS, confname);
602                         client_write(confptr, len);
603                         if ((len > 0) && (confptr[len - 1] != 10))
604                                 client_write("\n", 1);
605                         cprintf("000\n");
606                         free(confptr);
607                 } else {
608                         cprintf("%d No such configuration.\n",
609                                 ERROR + ILLEGAL_VALUE);
610                 }
611         }
612
613         else if (!strcasecmp(cmd, "PUTSYS")) {
614                 extract_token(confname, argbuf, 1, '|', sizeof confname);
615                 unbuffer_output();
616                 cprintf("%d %s\n", SEND_LISTING, confname);
617                 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
618                 CtdlPutSysConfig(confname, confptr);
619                 free(confptr);
620         }
621
622         else {
623                 cprintf("%d Illegal option(s) specified.\n",
624                         ERROR + ILLEGAL_VALUE);
625         }
626 }
627
628 typedef struct __ConfType {
629         ConstStr Name;
630         long Type;
631 }ConfType;
632
633 ConfType CfgNames[] = {
634         { {HKEY("localhost") },    0},
635         { {HKEY("directory") },    0},
636         { {HKEY("smarthost") },    2},
637         { {HKEY("fallbackhost") }, 2},
638         { {HKEY("rbl") },          3},
639         { {HKEY("spamassassin") }, 3},
640         { {HKEY("masqdomain") },   1},
641         { {HKEY("clamav") },       3},
642         { {HKEY("notify") },       3},
643         { {NULL, 0}, 0}
644 };
645
646 HashList *CfgNameHash = NULL;
647 void cmd_gvdn(char *argbuf)
648 {
649         const ConfType *pCfg;
650         char *confptr;
651         long min = atol(argbuf);
652         const char *Pos = NULL;
653         const char *PPos = NULL;
654         const char *HKey;
655         long HKLen;
656         StrBuf *Line;
657         StrBuf *Config;
658         StrBuf *Cfg;
659         StrBuf *CfgToken;
660         HashList *List;
661         HashPos *It;
662         void *vptr;
663         
664         List = NewHash(1, NULL);
665         Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
666         Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
667         Cfg = NULL;
668
669         confptr = CtdlGetSysConfig(INTERNETCFG);
670         Config = NewStrBufPlain(confptr, -1);
671         free(confptr);
672
673         Line = NewStrBufPlain(NULL, StrLength(Config));
674         CfgToken = NewStrBufPlain(NULL, StrLength(Config));
675         while (StrBufSipLine(Line, Config, &Pos))
676         {
677                 if (Cfg == NULL)
678                         Cfg = NewStrBufPlain(NULL, StrLength(Line));
679                 PPos = NULL;
680                 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
681                 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
682                 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
683                     (vptr != NULL))
684                 {
685                         pCfg = (ConfType *) vptr;
686                         if (pCfg->Type <= min)
687                         {
688                                 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
689                                 Cfg = NULL;
690                         }
691                 }
692         }
693
694         cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
695         It = GetNewHashPos(List, 1);
696         while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
697         {
698                 cputbuf(vptr);
699                 cprintf("\n");
700         }
701         cprintf("000\n");
702
703         DeleteHashPos(&It);
704         DeleteHash(&List);
705         FreeStrBuf(&Cfg);
706         FreeStrBuf(&Line);
707         FreeStrBuf(&CfgToken);
708         FreeStrBuf(&Config);
709 }
710
711 /*****************************************************************************/
712 /*                      MODULE INITIALIZATION STUFF                          */
713 /*****************************************************************************/
714
715 void control_cleanup(void)
716 {
717         DeleteHash(&CfgNameHash);
718 }
719 CTDL_MODULE_INIT(control)
720 {
721         if (!threading) {
722                 int i;
723
724                 CfgNameHash = NewHash(1, NULL);
725                 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
726                         Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
727
728                 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
729                 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
730                 CtdlRegisterCleanupHook(control_cleanup);
731
732         }
733         /* return our id for the Log */
734         return "control";
735 }