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