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