Added checking for prv/pub keys in serverconfig.
[silc.git] / apps / silcd / serverconfig.c
index 11f28a0f2f7c9a11ab387ae1b4b2756e5a7f026f..0d217e7c3e39f7c22d9feaf46d94a4b56d887492 100644 (file)
@@ -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")) {
@@ -1195,10 +1199,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 +1212,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 +1245,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;
   }
@@ -1268,8 +1281,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));
   }
 }
 
@@ -1280,13 +1293,13 @@ void silc_server_config_unref(SilcServerConfigRef *ref)
 {
   SilcServerConfig config = ref->config;
 
-  if (config) {
+  if (ref->ref_ptr) {
     config->refcount--;
-    SILC_LOG_DEBUG(("Unreferencing config [%p] New Ref=%hu", config,
-                   config->refcount));
+    SILC_LOG_DEBUG(("Unreferencing config [%p] refcnt %hu->%hu", config,
+                   config->refcount + 1, config->refcount));
     if (!config->refcount)
       silc_server_config_destroy(config);
-    memset(ref, 0, sizeof(*ref));
+    ref->ref_ptr = NULL;
   }
 }
 
@@ -1295,6 +1308,9 @@ void silc_server_config_unref(SilcServerConfigRef *ref)
 void silc_server_config_destroy(SilcServerConfig config)
 {
   void *tmp;
+
+  SILC_LOG_DEBUG(("Freeing config context"));
+
   silc_free(config->module_path);
 
   /* Destroy Logging channels */
@@ -1320,6 +1336,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 */
@@ -1637,7 +1655,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,