updated copyright years and pekka's email on some files
[silc.git] / apps / silcd / serverconfig.c
index 11f28a0f2f7c9a11ab387ae1b4b2756e5a7f026f..568e19f8e752a9377a34c80eaadd12373ca5d830 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Johnny Mnemonic <johnny@themnemonic.org>
 
-  Copyright (C) 1997 - 2002 Johnny Mnemonic
+  Copyright (C) 1997 - 2002 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -430,6 +430,10 @@ SILC_CONFIG_CALLBACK(fetch_serverinfo)
 
   if (type == SILC_CONFIG_ARG_BLOCK) {
     /* check for mandatory inputs */
+    if (!server_info->public_key || !server_info->private_key) {
+      got_errno = SILC_CONFIG_EMISSFIELDS;
+      goto got_err;
+    }
     return SILC_CONFIG_OK;
   }
   if (!strcmp(name, "hostname")) {
@@ -880,10 +884,6 @@ SILC_CONFIG_CALLBACK(fetch_server)
       goto got_err;
     }
   }
-  else if (!strcmp(name, "versionid")) {
-    CONFIG_IS_DOUBLE(tmp->version);
-    tmp->version = strdup((char *) val);
-  }
   else if (!strcmp(name, "params")) {
     CONFIG_IS_DOUBLE(tmp->param);
     tmp->param = my_find_param(config, (char *) val, line);
@@ -902,7 +902,6 @@ SILC_CONFIG_CALLBACK(fetch_server)
 
  got_err:
   silc_free(tmp->host);
-  silc_free(tmp->version);
   CONFIG_FREE_AUTH(tmp);
   silc_free(tmp);
   config->tmp = NULL;
@@ -962,10 +961,6 @@ SILC_CONFIG_CALLBACK(fetch_router)
       goto got_err;
     }
   }
-  else if (!strcmp(name, "versionid")) {
-    CONFIG_IS_DOUBLE(tmp->version);
-    tmp->version = strdup((char *) val);
-  }
   else if (!strcmp(name, "params")) {
     CONFIG_IS_DOUBLE(tmp->param);
     tmp->param = my_find_param(config, (char *) val, line);
@@ -1000,7 +995,6 @@ SILC_CONFIG_CALLBACK(fetch_router)
 
  got_err:
   silc_free(tmp->host);
-  silc_free(tmp->version);
   silc_free(tmp->backup_replace_ip);
   CONFIG_FREE_AUTH(tmp);
   silc_free(tmp);
@@ -1140,7 +1134,6 @@ static const SilcConfigTable table_serverconn[] = {
   { "host",            SILC_CONFIG_ARG_STRE,   fetch_server,   NULL },
   { "passphrase",      SILC_CONFIG_ARG_STR,    fetch_server,   NULL },
   { "publickey",       SILC_CONFIG_ARG_STR,    fetch_server,   NULL },
-  { "versionid",       SILC_CONFIG_ARG_STR,    fetch_server,   NULL },
   { "params",          SILC_CONFIG_ARG_STR,    fetch_server,   NULL },
   { "backup",          SILC_CONFIG_ARG_TOGGLE, fetch_server,   NULL },
   { 0, 0, 0, 0 }
@@ -1151,7 +1144,6 @@ static const SilcConfigTable table_routerconn[] = {
   { "port",            SILC_CONFIG_ARG_INT,    fetch_router,   NULL },
   { "passphrase",      SILC_CONFIG_ARG_STR,    fetch_router,   NULL },
   { "publickey",       SILC_CONFIG_ARG_STR,    fetch_router,   NULL },
-  { "versionid",       SILC_CONFIG_ARG_STR,    fetch_router,   NULL },
   { "params",          SILC_CONFIG_ARG_STR,    fetch_router,   NULL },
   { "initiator",       SILC_CONFIG_ARG_TOGGLE, fetch_router,   NULL },
   { "backuphost",      SILC_CONFIG_ARG_STRE,   fetch_router,   NULL },
@@ -1195,10 +1187,11 @@ static void silc_server_config_set_defaults(SilcServerConfig config)
 }
 
 /* Allocates a new configuration object, opens configuration file and
- * parses it. The parsed data is returned to the newly allocated
- * configuration object. */
+   parses it. The parsed data is returned to the newly allocated
+   configuration object. The SilcServerConfig must be freed by calling
+   the silc_server_config_destroy function. */
 
-SilcServerConfig silc_server_config_alloc(char *filename)
+SilcServerConfig silc_server_config_alloc(const char *filename)
 {
   SilcServerConfig config_new;
   SilcConfigEntity ent;
@@ -1207,13 +1200,18 @@ SilcServerConfig silc_server_config_alloc(char *filename)
   SILC_LOG_DEBUG(("Loading config data from `%s'", filename));
 
   /* alloc a config object */
-  config_new = (SilcServerConfig) silc_calloc(1, sizeof(*config_new));
+  config_new = silc_calloc(1, sizeof(*config_new));
+  if (!config_new)
+    return NULL;
+
   /* obtain a config file object */
   file = silc_config_open(filename);
   if (!file) {
-    SILC_SERVER_LOG_ERROR(("\nError: can't open config file `%s'\n", filename));
+    SILC_SERVER_LOG_ERROR(("\nError: can't open config file `%s'\n",
+                          filename));
     return NULL;
   }
+
   /* obtain a SilcConfig entity, we can use it to start the parsing */
   ent = silc_config_init(file);
 
@@ -1235,18 +1233,21 @@ SilcServerConfig silc_server_config_alloc(char *filename)
       SILC_SERVER_LOG_ERROR(("Error while parsing config file: %s.\n",
                             silc_config_strerror(ret)));
       linebuf = silc_config_read_line(file, line);
-      SILC_SERVER_LOG_ERROR(("  file %s line %lu:  %s\n", filename, line, linebuf));
+      SILC_SERVER_LOG_ERROR(("  file %s line %lu:  %s\n", filename,
+                            line, linebuf));
       silc_free(linebuf);
     }
     silc_server_config_destroy(config_new);
     return NULL;
   }
+
   /* close (destroy) the file object */
   silc_config_close(file);
 
   /* If config_new is incomplete, abort the object and return NULL */
   if (!config_new->server_info) {
-    SILC_SERVER_LOG_ERROR(("\nError: Missing mandatory block `server_info'\n"));
+    SILC_SERVER_LOG_ERROR(("\nError: Missing mandatory block "
+                          "`server_info'\n"));
     silc_server_config_destroy(config_new);
     return NULL;
   }
@@ -1256,6 +1257,7 @@ SilcServerConfig silc_server_config_alloc(char *filename)
   /* Set default to configuration parameters */
   silc_server_config_set_defaults(config_new);
 
+  config_new->refcount = 1;
   return config_new;
 }
 
@@ -1268,8 +1270,8 @@ void silc_server_config_ref(SilcServerConfigRef *ref, SilcServerConfig config,
     config->refcount++;
     ref->config = config;
     ref->ref_ptr = ref_ptr;
-    SILC_LOG_DEBUG(("Referencing config [%p] New Ref=%hu", config,
-                   config->refcount));
+    SILC_LOG_DEBUG(("Referencing config [%p] refcnt %hu->%hu", config,
+                   config->refcount - 1, config->refcount));
   }
 }
 
@@ -1278,16 +1280,8 @@ void silc_server_config_ref(SilcServerConfigRef *ref, SilcServerConfig config,
 
 void silc_server_config_unref(SilcServerConfigRef *ref)
 {
-  SilcServerConfig config = ref->config;
-
-  if (config) {
-    config->refcount--;
-    SILC_LOG_DEBUG(("Unreferencing config [%p] New Ref=%hu", config,
-                   config->refcount));
-    if (!config->refcount)
-      silc_server_config_destroy(config);
-    memset(ref, 0, sizeof(*ref));
-  }
+  if (ref->ref_ptr)
+    silc_server_config_destroy(ref->config);
 }
 
 /* Destroy a config object with all his children lists */
@@ -1295,7 +1289,20 @@ void silc_server_config_unref(SilcServerConfigRef *ref)
 void silc_server_config_destroy(SilcServerConfig config)
 {
   void *tmp;
+
+  config->refcount--;
+  SILC_LOG_DEBUG(("Unreferencing config [%p] refcnt %hu->%hu", config,
+                 config->refcount + 1, config->refcount));
+  if (config->refcount > 0)
+    return;
+
+  SILC_LOG_DEBUG(("Freeing config context"));
+
+  /* Destroy general config stuff */
   silc_free(config->module_path);
+  silc_free(config->param.version_protocol);
+  silc_free(config->param.version_software);
+  silc_free(config->param.version_software_vendor);
 
   /* Destroy Logging channels */
   if (config->logging_info)
@@ -1320,6 +1327,8 @@ void silc_server_config_destroy(SilcServerConfig config)
     silc_free(si->group);
     silc_free(si->motd_file);
     silc_free(si->pid_file);
+    silc_pkcs_public_key_free(si->public_key);
+    silc_pkcs_private_key_free(si->private_key);
   }
 
   /* Now let's destroy the lists */
@@ -1344,8 +1353,15 @@ void silc_server_config_destroy(SilcServerConfig config)
     silc_free(di->name);
     silc_free(di);
   }
-  SILC_SERVER_CONFIG_LIST_DESTROY(SilcServerConfigClient,
-                                 config->clients)
+  SILC_SERVER_CONFIG_LIST_DESTROY(SilcServerConfigConnParams,
+                                  config->conn_params)
+    silc_free(di->name);
+    silc_free(di->version_protocol);
+    silc_free(di->version_software);
+    silc_free(di->version_software_vendor);
+    silc_free(di);
+  }
+  SILC_SERVER_CONFIG_LIST_DESTROY(SilcServerConfigClient, config->clients)
     silc_free(di->host);
     CONFIG_FREE_AUTH(di);
     silc_free(di);
@@ -1365,14 +1381,12 @@ void silc_server_config_destroy(SilcServerConfig config)
   SILC_SERVER_CONFIG_LIST_DESTROY(SilcServerConfigServer,
                                  config->servers)
     silc_free(di->host);
-    silc_free(di->version);
     CONFIG_FREE_AUTH(di);
     silc_free(di);
   }
   SILC_SERVER_CONFIG_LIST_DESTROY(SilcServerConfigRouter,
                                  config->routers)
     silc_free(di->host);
-    silc_free(di->version);
     silc_free(di->backup_replace_ip);
     CONFIG_FREE_AUTH(di);
     silc_free(di);
@@ -1637,7 +1651,9 @@ void silc_server_config_setlogfiles(SilcServer server)
   SILC_LOG_DEBUG(("Setting configured log file names and options"));
 
   silc_log_quick = config->logging_quick;
-  silc_log_flushdelay = config->logging_flushdelay;
+  silc_log_flushdelay = (config->logging_flushdelay ? 
+                        config->logging_flushdelay :
+                        SILC_SERVER_LOG_FLUSH_DELAY);
 
   if ((this = config->logging_fatals))
     silc_log_set_file(SILC_LOG_FATAL, this->file, this->maxsize,