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