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