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