]> code.citadel.org Git - citadel.git/blobdiff - citadel/config.c
CtdlPutSysConfig() don't delete the old copy of the config until after the new one...
[citadel.git] / citadel / config.c
index bcbfd143be212d6203262dd9dc57ca2ad08247ad..bd915f4c4a6a9bad03b992cfa16342cc2dc1497a 100644 (file)
@@ -1,16 +1,11 @@
-/*
- * Read and write the citadel.config file
- *
- * Copyright (c) 1987-2017 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+//
+// Read and write the citadel.config file
+//
+// Copyright (c) 1987-2021 by the citadel.org team
+//
+// This program is open source software.  Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
+// The program is distributed without any warranty, expressed or implied.
 
 #include "sysdep.h"
 #include <stdlib.h>
@@ -27,15 +22,13 @@ long config_msgnum = 0;
 HashList *ctdlconfig = NULL;   // new configuration
 
 
-void config_warn_if_port_unset(char *key, int default_port)                    \
+void config_warn_if_port_unset(char *key, int default_port)
 {
        int p = CtdlGetConfigInt(key);
        if ((p < -1) || (p == 0) || (p > UINT16_MAX))
        {
-               syslog(LOG_ERR,
-                       "config: setting %s is not -1 (disabled) or a valid TCP-Port - check your config! Default setting is: %d",
-                       key, default_port
-               );
+               syslog(LOG_ERR, "config: setting %s is not -1 (disabled) or a valid TCP port - setting to default %d", key, default_port);
+               CtdlSetConfigInt(key, default_port);
        }
 }
 
@@ -59,28 +52,26 @@ void validate_config(void) {
        config_warn_if_empty("c_aideroom");
        config_warn_if_empty("c_twitroom");
        config_warn_if_empty("c_nodename");
-       config_warn_if_empty("c_default_cal_zone");
 
        /*
         * Sanity check for port bindings
         */
-       config_warn_if_port_unset("c_smtp_port", 25);
-       config_warn_if_port_unset("c_pop3_port", 110);
-       config_warn_if_port_unset("c_imap_port", 143);
-       config_warn_if_port_unset("c_msa_port", 587);
-       config_warn_if_port_unset("c_port_number", 504);
-       config_warn_if_port_unset("c_smtps_port", 465);
-       config_warn_if_port_unset("c_pop3s_port", 995);
-       config_warn_if_port_unset("c_imaps_port", 993);
-       config_warn_if_port_unset("c_pftcpdict_port", -1);
-       config_warn_if_port_unset("c_managesieve_port", 2020);
-       config_warn_if_port_unset("c_xmpp_c2s_port", 5222);
-       config_warn_if_port_unset("c_xmpp_s2s_port", 5269);
-       config_warn_if_port_unset("c_nntp_port", 119);
-       config_warn_if_port_unset("c_nntps_port", 563);
+       config_warn_if_port_unset("c_smtp_port",        25);
+       config_warn_if_port_unset("c_pop3_port",        110);
+       config_warn_if_port_unset("c_imap_port",        143);
+       config_warn_if_port_unset("c_msa_port",         587);
+       config_warn_if_port_unset("c_port_number",      504);
+       config_warn_if_port_unset("c_smtps_port",       465);
+       config_warn_if_port_unset("c_pop3s_port",       995);
+       config_warn_if_port_unset("c_imaps_port",       993);
+       config_warn_if_port_unset("c_pftcpdict_port",   -1);
+       config_warn_if_port_unset("c_xmpp_c2s_port",    5222);
+       config_warn_if_port_unset("c_xmpp_s2s_port",    5269);
+       config_warn_if_port_unset("c_nntp_port",        119);
+       config_warn_if_port_unset("c_nntps_port",       563);
 
        if (getpwuid(ctdluid) == NULL) {
-               syslog(LOG_ERR, "config: UID (%d) citadel is configured to use is not defined in your system (/etc/passwd?)!", ctdluid);
+               syslog(LOG_ERR, "config: uid (%d) does not exist ... citserver will run as root", ctdluid);
        }
 }
 
@@ -132,7 +123,6 @@ void brand_new_installation_set_defaults(void) {
        CtdlSetConfigInt("c_pop3s_port",        995);
        CtdlSetConfigInt("c_imaps_port",        993);
        CtdlSetConfigInt("c_pftcpdict_port",    -1);
-       CtdlSetConfigInt("c_managesieve_port",  2020);
        CtdlSetConfigInt("c_xmpp_c2s_port",     5222);
        CtdlSetConfigInt("c_xmpp_s2s_port",     5269);
        CtdlSetConfigInt("c_nntp_port",         119);
@@ -205,11 +195,8 @@ void migrate_legacy_config(struct legacy_config *lconfig)
        CtdlSetConfigStr(       "c_journal_dest"        ,       lconfig->c_journal_dest         );
        CtdlSetConfigStr(       "c_default_cal_zone"    ,       lconfig->c_default_cal_zone     );
        CtdlSetConfigInt(       "c_pftcpdict_port"      ,       lconfig->c_pftcpdict_port       );
-       CtdlSetConfigInt(       "c_managesieve_port"    ,       lconfig->c_managesieve_port     );
        CtdlSetConfigInt(       "c_auth_mode"           ,       lconfig->c_auth_mode            );
        CtdlSetConfigInt(       "c_rbl_at_greeting"     ,       lconfig->c_rbl_at_greeting      );
-       CtdlSetConfigStr(       "c_master_user"         ,       lconfig->c_master_user          );
-       CtdlSetConfigStr(       "c_master_pass"         ,       lconfig->c_master_pass          );
        CtdlSetConfigStr(       "c_pager_program"       ,       lconfig->c_pager_program        );
        CtdlSetConfigInt(       "c_imap_keep_from"      ,       lconfig->c_imap_keep_from       );
        CtdlSetConfigInt(       "c_xmpp_c2s_port"       ,       lconfig->c_xmpp_c2s_port        );
@@ -240,15 +227,6 @@ void initialize_config_system(void) {
                exit(CTDLEXIT_LIBCITADEL);
        }
 
-       if (chdir(ctdl_bbsbase_dir) != 0) {
-               fprintf(stderr,
-                       "This program could not be started.\nUnable to change directory to %s\nError: %s\n",
-                       ctdl_bbsbase_dir,
-                       strerror(errno)
-               );
-               exit(CTDLEXIT_HOME);
-       }
-
        memset(&lconfig, 0, sizeof(struct legacy_config));
        cfp = fopen(file_citadel_config, "rb");
        if (cfp != NULL) {
@@ -456,8 +434,7 @@ int CtdlGetConfigInt(char *key)
 /*
  * Fetch a system config value - long integer
  */
-long CtdlGetConfigLong(char *key)
-{
+long CtdlGetConfigLong(char *key) {
        char *s = CtdlGetConfigStr(key);
        if (s) return atol(s);
        return 0;
@@ -469,9 +446,12 @@ void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
 }
 
 
+/*
+ * This is for fetching longer configuration sets which are stored in the message base.
+ */
 char *CtdlGetSysConfig(char *sysconfname) {
        char hold_rm[ROOMNAMELEN];
-       long msgnum;
+       long msgnum = -1;
        char *conf;
        struct CtdlMessage *msg;
        char buf[SIZ];
@@ -482,18 +462,26 @@ char *CtdlGetSysConfig(char *sysconfname) {
                return NULL;
        }
 
-       /* We want the last (and probably only) config in this room */
-       begin_critical_section(S_CONFIG);
-       config_msgnum = (-1L);
-       CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL, CtdlGetSysConfigBackend, NULL);
-       msgnum = config_msgnum;
-       end_critical_section(S_CONFIG);
+       /* The new way: hunt for the message number in the config database */
+       msgnum = CtdlGetConfigLong(sysconfname);
+
+       /* Legacy format: hunt through the local system configuration room for a message with a matching MIME type */
+       if (msgnum <= 0) {
+               begin_critical_section(S_CONFIG);
+               config_msgnum = -1;
+               CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL, CtdlGetSysConfigBackend, NULL);
+               msgnum = config_msgnum;
+               end_critical_section(S_CONFIG);
+               if (msgnum > 0) {
+                       CtdlSetConfigLong(sysconfname, msgnum);         // store it the new way so we don't have to do this again
+               }
+       }
 
-       if (msgnum < 0L) {
+       if (msgnum <= 0) {
                conf = NULL;
        }
        else {
-               msg = CtdlFetchMessage(msgnum, 1, 1);
+               msg = CtdlFetchMessage(msgnum, 1);
                if (msg != NULL) {
                        conf = strdup(msg->cm_fields[eMesageText]);
                        CM_Free(msg);
@@ -505,7 +493,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
 
        CtdlGetRoom(&CC->room, hold_rm);
 
-       if (conf != NULL) {
+       if (conf != NULL) {             // Strip the MIME headers, leaving only the content
                do {
                        extract_token(buf, conf, 0, '\n', sizeof buf);
                        strcpy(conf, &conf[strlen(buf)+1]);
@@ -516,6 +504,24 @@ char *CtdlGetSysConfig(char *sysconfname) {
 }
 
 
+/*
+ * This is for storing longer configuration sets which are stored in the message base.
+ */
 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
-       CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
+       long old_msgnum = -1;
+       long new_msgnum = -1;
+
+       // Search for the previous copy of this config item, so we can delete it
+       old_msgnum = CtdlGetConfigLong(sysconfname);
+
+       // Go ahead and save it, and write the new msgnum to the config database so we can find it again
+       new_msgnum = CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 0);
+       if (new_msgnum > 0) {
+               CtdlSetConfigLong(sysconfname, new_msgnum);
+
+               // Now delete the old copy
+               if (old_msgnum > 0) {
+                       CtdlDeleteMessages(SYSCONFIGROOM, &old_msgnum, 1, "");
+               }
+       }
 }