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