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