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