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