]> code.citadel.org Git - citadel.git/blobdiff - citadel/config.c
* New function CtdlDelConfig() to delete a config db record
[citadel.git] / citadel / config.c
index fffdadc6e0a8adfcba8f6b11c862103cf9a0f5e5..a993a63ee1cb52ee5a64aceba5ae57a334b87fcc 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-2016 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"
 
@@ -256,15 +257,15 @@ void initialize_config_system(void) {
                exit(CTDLEXIT_HOME);
        }
 
-       memset(&config, 0, sizeof(struct legacy_config));
+       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);
                }
-               rv = fread((char *) &config, sizeof(struct legacy_config), 1, cfp);
+               rv = fread((char *) &lconfig, sizeof(struct legacy_config), 1, cfp);
                if (rv != 1)
                {
                        fprintf(stderr, 
@@ -296,7 +297,7 @@ void initialize_config_system(void) {
 
        /* Only allow LDAP auth mode if we actually have LDAP support */
 #ifndef HAVE_LDAP
-       if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
+       if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
                fprintf(stderr, "Your system is configured for LDAP authentication,\n"
                                "but you are running a server built without OpenLDAP support.\n");
                exit(CTDL_EXIT_UNSUP_AUTH);
@@ -392,6 +393,32 @@ 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.
  */
@@ -401,6 +428,8 @@ char *CtdlGetConfigStr(char *key)
        struct cdbdata *cdb;
        int key_len = strlen(key);
 
+       if (IsEmptyStr(key)) return(NULL);
+
        /* First look in memory */
        if (GetHash(ctdlconfig, key, key_len, (void *)&value))
        {
@@ -423,6 +452,26 @@ char *CtdlGetConfigStr(char *key)
 }
 
 
+/*
+ * Fetch a system config value - integer
+ */
+int CtdlGetConfigInt(char *key)
+{
+       char *s = CtdlGetConfigStr(key);
+       if (s) return atoi(s);
+       return 0;
+}
+
+
+/*
+ * Fetch a system config value - long integer
+ */
+long CtdlGetConfigLong(char *key)
+{
+       char *s = CtdlGetConfigStr(key);
+       if (s) return atol(s);
+       return 0;
+}
 
 
 
@@ -468,7 +517,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);