Updated more modules to the new logging standard
[citadel.git] / citadel / config.c
index f41533c5b13b858f27bf70d4f929c38211a76319..46105383bccbdb45333c4beb69fa5d1defdb2b0e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Read and write the citadel.config file
  *
- * Copyright (c) 1987-2015 by the citadel.org team
+ * 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.
@@ -16,6 +16,7 @@
 #include <stdio.h>
 #include <sys/utsname.h>
 #include <libcitadel.h>
+#include <assert.h>
 #include "config.h"
 #include "ctdl_module.h"
 
@@ -28,8 +29,8 @@ 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_EMERG,
-                       "configuration setting %s is not -1 (disabled) or a valid TCP-Port - check your config! Default setting is: %d",
+               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
                );
        }
@@ -40,12 +41,11 @@ void config_warn_if_empty(char *key)
 {
        if (IsEmptyStr(CtdlGetConfigStr(key)))
        {
-               syslog(LOG_EMERG, "configuration setting %s is empty, but must not - check your config!", key);
+               syslog(LOG_ERR, "config: setting %s is empty, but must not - check your config!", key);
        }
 }
 
 
-
 void validate_config(void) {
 
        /*
@@ -77,11 +77,11 @@ void validate_config(void) {
        config_warn_if_port_unset("c_nntps_port", 563);
 
        if (getpwuid(ctdluid) == NULL) {
-               syslog(LOG_EMERG, "The UID (%d) citadel is configured to use is not defined in your system (/etc/passwd?)!", ctdluid);
+               syslog(LOG_ERR, "config: UID (%d) citadel is configured to use is not defined in your system (/etc/passwd?)!", ctdluid);
        }
-       
 }
 
+
 /*
  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
  */
@@ -142,7 +142,6 @@ void brand_new_installation_set_defaults(void) {
 }
 
 
-
 /*
  * Migrate a supplied legacy configuration to the new in-db format.
  * No individual site should ever have to do this more than once.
@@ -225,7 +224,6 @@ void migrate_legacy_config(struct legacy_config *lconfig)
 }
 
 
-
 /*
  * Called during the initialization of Citadel server.
  * It verifies the system's integrity and reads citadel.config into memory.
@@ -238,12 +236,8 @@ void initialize_config_system(void) {
 
        /* Ensure that we are linked to the correct version of libcitadel */
        if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
-               fprintf(stderr, "You are running libcitadel version %d.%02d\n",
-                       (libcitadel_version_number() / 100), (libcitadel_version_number() % 100)
-               );
-               fprintf(stderr, "citserver was compiled against version %d.%02d\n",
-                       (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100)
-               );
+               fprintf(stderr, "You are running libcitadel version %d\n", libcitadel_version_number());
+               fprintf(stderr, "citserver was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER);
                exit(CTDLEXIT_LIBCITADEL);
        }
 
@@ -259,7 +253,7 @@ void initialize_config_system(void) {
        memset(&lconfig, 0, sizeof(struct legacy_config));
        cfp = fopen(file_citadel_config, "rb");
        if (cfp != NULL) {
-               if (CtdlGetConfigLong("c_config_created_or_migrated") <= 0) {
+               if (CtdlGetConfigLong("c_config_created_or_migrated") > 0) {
                        fprintf(stderr, "Citadel Server found BOTH legacy and new configurations present.\n");
                        fprintf(stderr, "Exiting to prevent data corruption.\n");
                        exit(CTDLEXIT_CONFIG);
@@ -336,7 +330,6 @@ void initialize_config_system(void) {
 }
 
 
-
 /*
  * Called when Citadel server is shutting down.
  * Clears out the config hash table.
@@ -347,7 +340,6 @@ void shutdown_config_system(void)
 }
 
 
-
 /*
  * Set a system config value.  Simple key/value here.
  */
@@ -356,9 +348,6 @@ void CtdlSetConfigStr(char *key, char *value)
        int key_len = strlen(key);
        int value_len = strlen(value);
 
-       /* FIXME we are noisy logging for now */
-       syslog(LOG_DEBUG, "\033[31mSET CONFIG: '%s' = '%s'\033[0m", key, value);
-
        /* Save it in memory */
        Put(ctdlconfig, key, key_len, strdup(value), NULL);
 
@@ -395,6 +384,29 @@ void CtdlSetConfigInt(char *key, int value)
 }
 
 
+/*
+ * Delete a system config value.
+ */
+void CtdlDelConfig(char *key)
+{
+       int key_len = strlen(key);
+
+       if (IsEmptyStr(key)) return;
+
+       /* Delete from the database. */
+       cdb_delete(CDB_CONFIG, key, key_len);
+
+       /* Delete from the in-memory cache */
+       HashPos *Pos = GetNewHashPos(ctdlconfig, 1);
+       if (GetHashPosFromKey(ctdlconfig, key, key_len, Pos)) {
+               DeleteEntryFromHash(ctdlconfig, Pos);
+       }
+       DeleteHashPos(&Pos);
+
+       assert(Pos == NULL);    // no memory leaks allowed
+}
+
+
 /*
  * Fetch a system config value.  Caller does *not* own the returned value and may not alter it.
  */
@@ -406,16 +418,9 @@ char *CtdlGetConfigStr(char *key)
 
        if (IsEmptyStr(key)) return(NULL);
 
-       /* Temporary hack to make sure we didn't mess up any porting - FIXME remove this after testing thoroughly */
-       if (!strncmp(key, "config", 6)) {
-               syslog(LOG_EMERG, "You requested a key starting with 'config' which probably means a porting error: %s", key);
-               abort();
-       }
-
        /* First look in memory */
        if (GetHash(ctdlconfig, key, key_len, (void *)&value))
        {
-               syslog(LOG_DEBUG, "\033[32mGET CONFIG: '%s' = '%s'\033[0m", key, value);
                return value;
        }
 
@@ -424,7 +429,6 @@ char *CtdlGetConfigStr(char *key)
        cdb = cdb_fetch(CDB_CONFIG, key, key_len);
 
        if (cdb == NULL) {      /* nope, not there either. */
-               syslog(LOG_DEBUG, "\033[32mGET CONFIG: '%s' = NULL\033[0m", key);
                return(NULL);
        }
 
@@ -432,7 +436,6 @@ char *CtdlGetConfigStr(char *key)
        value = strdup(cdb->ptr + key_len + 1);         /* The key was stored there too; skip past it */
        cdb_free(cdb);
        Put(ctdlconfig, key, key_len, value, NULL);
-       syslog(LOG_DEBUG, "\033[32mGET CONFIG: '%s' = '%s'\033[0m", key, value);
        return value;
 }
 
@@ -459,18 +462,6 @@ long CtdlGetConfigLong(char *key)
 }
 
 
-
-/**********************************************************************/
-
-
-
-
-
-
-
-
-
-
 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
        config_msgnum = msgnum;
 }
@@ -489,7 +480,6 @@ 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);
@@ -502,7 +492,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
                conf = NULL;
        }
        else {
-               msg = CtdlFetchMessage(msgnum, 1);
+               msg = CtdlFetchMessage(msgnum, 1, 1);
                if (msg != NULL) {
                        conf = strdup(msg->cm_fields[eMesageText]);
                        CM_Free(msg);
@@ -526,4 +516,3 @@ char *CtdlGetSysConfig(char *sysconfname) {
 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
        CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
 }
-