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