server session FEATURE callhome

Only SSH settings implemented,
the rest as prototypes only.
diff --git a/src/session_p.h b/src/session_p.h
index ce8aef5..5de37ca 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -211,6 +211,7 @@
     struct ly_ctx *ctx;            /**< libyang context of the session */
     uint8_t flags;                 /**< various flags of the session - TODO combine with status and/or side */
 #define NC_SESSION_SHAREDCTX 0x01
+#define NC_SESSION_CALLHOME 0x02
 
     /* client side only data */
     uint64_t msgid;
@@ -222,13 +223,13 @@
     time_t last_rpc;               /**< time the last RPC was received on this session */
 #ifdef ENABLE_SSH
     /* SSH session authenticated */
-#   define NC_SESSION_SSH_AUTHENTICATED 0x02
+#   define NC_SESSION_SSH_AUTHENTICATED 0x04
     /* netconf subsystem requested */
-#   define NC_SESSION_SSH_SUBSYS_NETCONF 0x04
+#   define NC_SESSION_SSH_SUBSYS_NETCONF 0x08
     /* new SSH message arrived */
-#   define NC_SESSION_SSH_NEW_MSG 0x08
+#   define NC_SESSION_SSH_NEW_MSG 0x10
     /* this session is passed to nc_sshcb_msg() */
-#   define NC_SESSION_SSH_MSG_CB 0x10
+#   define NC_SESSION_SSH_MSG_CB 0x20
 
     uint16_t ssh_auth_attempts;    /**< number of failed SSH authentication attempts */
 #endif
diff --git a/src/session_server.c b/src/session_server.c
index c27f34f..f1e464b 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -947,4 +947,79 @@
     return -1;
 }
 
+API int
+nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session)
+{
+    int sock, ret;
+
+    sock = nc_sock_connect(host, port);
+    if (sock == -1) {
+        goto fail;
+    }
+
+    *session = calloc(1, sizeof **session);
+    if (!(*session)) {
+        ERRMEM;
+        close(sock);
+        return -1;
+    }
+    (*session)->status = NC_STATUS_STARTING;
+    (*session)->side = NC_SERVER;
+    (*session)->ctx = server_opts.ctx;
+    (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
+    nc_ctx_lock(-1, NULL);
+    (*session)->host = lydict_insert(server_opts.ctx, host, 0);
+    nc_ctx_unlock();
+    (*session)->port = port;
+
+    /* transport lock */
+    (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
+    if (!(*session)->ti_lock) {
+        ERRMEM;
+        close(sock);
+        ret = -1;
+        goto fail;
+    }
+    pthread_mutex_init((*session)->ti_lock, NULL);
+
+    /* sock gets assigned to session or closed */
+    if (ti == NC_TI_LIBSSH) {
+        ret = nc_accept_ssh_session(*session, sock, timeout);
+        if (ret < 1) {
+            goto fail;
+        }
+    } else if (ti == NC_TI_OPENSSL) {
+        ret = nc_accept_tls_session(*session, sock, timeout);
+        if (ret < 1) {
+            goto fail;
+        }
+    } else {
+        ERRINT;
+        close(sock);
+        ret = -1;
+        goto fail;
+    }
+
+    /* assign new SID atomically */
+    /* LOCK */
+    pthread_spin_lock(&server_opts.sid_lock);
+    (*session)->id = server_opts.new_session_id++;
+    /* UNLOCK */
+    pthread_spin_unlock(&server_opts.sid_lock);
+
+    /* NETCONF handshake */
+    if (nc_handshake(*session)) {
+        ret = -1;
+        goto fail;
+    }
+    (*session)->status = NC_STATUS_RUNNING;
+
+    return 1;
+
+fail:
+    nc_session_free(*session);
+    *session = NULL;
+    return -1;
+}
+
 #endif /* ENABLE_SSH || ENABLE_TLS */
diff --git a/src/session_server.h b/src/session_server.h
index aa72311..6c74e07 100644
--- a/src/session_server.h
+++ b/src/session_server.h
@@ -244,6 +244,8 @@
  */
 int nc_accept(int timeout, struct nc_session **session);
 
+int nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session);
+
 #endif /* ENABLE_SSH || ENABLE_TLS */
 
 #ifdef ENABLE_SSH
@@ -319,8 +321,73 @@
  */
 int nc_ssh_server_del_authkey(const char *pubkey_path, const char *username);
 
+/*
+ * Call Home
+ */
+
 /**
- * @brief Free all the various SSH server options.
+ * @brief Set Call Home SSH host keys the server will identify itself with. Each of RSA, DSA, and
+ * ECDSA keys can be set. If the particular type was already set, it is replaced.
+ *
+ * @param[in] privkey_path Path to a private key.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_set_hostkey(const char *privkey_path);
+
+/**
+ * @brief Set Call Home SSH banner the server will send to every client.
+ *
+ * @param[in] banner SSH banner.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_set_banner(const char *banner);
+
+/**
+ * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive)
+ * are supported by default.
+ *
+ * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_set_auth_methods(int auth_methods);
+
+/**
+ * @brief Set Call Home SSH authentication attempts of every client. 3 by default.
+ *
+ * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_set_auth_attempts(uint16_t auth_attempts);
+
+/**
+ * @brief Set Call Home SSH authentication timeout. 10 seconds by default.
+ *
+ * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_set_auth_timeout(uint16_t auth_timeout);
+
+/**
+ * @brief Add an authorized Call Home client SSH public key. This public key can be used for
+ * publickey authentication afterwards.
+ *
+ * @param[in] pubkey_path Path to the public key.
+ * @param[in] username Username that the client with the public key must use.
+ * @return 0 on success, -1 on error.
+ */
+int nc_ssh_server_ch_add_authkey(const char *pubkey_path, const char *username);
+
+/**
+ * @brief Remove an authorized Call Home client SSH public key.
+ *
+ * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
+ * @param[in] username Username for an authorized public key. NULL matches all the usernames.
+ * @return 0 on success, -1 on not finding any match.
+ */
+int nc_ssh_server_ch_del_authkey(const char *pubkey_path, const char *username);
+
+/**
+ * @brief Free all the various SSH server options (including Call Home ones).
  */
 void nc_ssh_server_free_opts(void);
 
@@ -440,8 +507,124 @@
  */
 int nc_tls_server_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
 
+/*
+ * Call Home
+ */
+
 /**
- * @brief Free all the various TLS server options.
+ * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path().
+ * There can only be one certificate for each key type, it is replaced if already set.
+ *
+ * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_cert(const char *cert);
+
+/**
+ * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert().
+ * There can only be one certificate for each key type, it is replaced if already set.
+ *
+ * @param[in] cert_path Path to a certificate file in PEM format.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_cert_path(const char *cert_path);
+
+/**
+ * @brief Set server Call Home TLS private key matching the certificate.
+ * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
+ * type, it is replaced if already set.
+ *
+ * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
+ * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_key(const char *privkey, int is_rsa);
+
+/**
+ * @brief Set server Call Home TLS private key matching the certificate.
+ * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
+ * type, it is replaced if already set.
+ *
+ * @param[in] privkey_path Path to a private key file in PEM format.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_key_path(const char *privkey_path);
+
+/**
+ * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
+ *
+ * @param[in] cert Base64-enocded certificate in ASN.1DER encoding.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_add_trusted_cert(const char *cert);
+
+/**
+ * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
+ *
+ * @param[in] cert_path Path to a trusted certificate file in PEM format.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_add_trusted_cert_path(const char *cert_path);
+
+/**
+ * @brief Set trusted Call Home Certificate Authority certificate locations. There
+ * can only be one file and one directory, they are replaced if already set.
+ *
+ * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format.
+ * Can be NULL.
+ * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory
+ * (c_rehash utility can be used to create hashes) with PEM files. Can be NULL.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path);
+
+/**
+ * @brief Destroy and clean all the set Call Home certificates and private keys.
+ * CRLs and CTN entries are not affected.
+ */
+void nc_tls_server_ch_destroy_certs(void);
+
+/**
+ * @brief Set Call Home Certificate Revocation List locations. There can only be
+ * one file and one directory, they are replaced if already set.
+ *
+ * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL.
+ * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility
+ * can be used to create hashes) with PEM files. Can be NULL.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_set_crl_locations(const char *crl_file_path, const char *crl_dir_path);
+
+/**
+ * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys,
+ * and CTN entries are not affected.
+ */
+void nc_tls_server_ch_destroy_crls(void);
+
+/**
+ * @brief Add a Call Home Cert-to-name entry.
+ *
+ * @param[in] id Priority of the entry.
+ * @param[in] fingerprint Matching certificate fingerprint.
+ * @param[in] map_type Type of username-certificate mapping.
+ * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
+ * @return 0 on success, -1 on error.
+ */
+int nc_tls_server_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
+
+/**
+ * @brief Remove a Call Home Cert-to-name entry.
+ *
+ * @param[in] id Priority of the entry. -1 matches all the priorities.
+ * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
+ * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
+ * @param[in] name Specific username for the entry. NULL matches all the usernames.
+ * @return 0 on success, -1 on not finding any match.
+ */
+int nc_tls_server_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
+
+/**
+ * @brief Free all the various TLS server options (including Call Home ones).
  */
 void nc_tls_server_free_opts(void);
 
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index a541ae3..4351407 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -34,6 +34,7 @@
 #include "session_server.h"
 
 extern struct nc_server_opts server_opts;
+
 struct nc_ssh_server_opts ssh_opts = {
     .sshbind_lock = PTHREAD_MUTEX_INITIALIZER,
     .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
@@ -42,165 +43,272 @@
     .auth_timeout = 10
 };
 
-API int
-nc_ssh_server_set_hostkey(const char *privkey_path)
+struct nc_ssh_server_opts ssh_ch_opts = {
+    .sshbind_lock = PTHREAD_MUTEX_INITIALIZER,
+    .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
+    .auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE,
+    .auth_attempts = 3,
+    .auth_timeout = 10
+};
+
+static int
+_nc_ssh_server_set_hostkey(const char *privkey_path, int ch)
 {
+    struct nc_ssh_server_opts *opts;
+
     if (!privkey_path) {
         ERRARG;
         return -1;
     }
 
-    /* LOCK */
-    pthread_mutex_lock(&ssh_opts.sshbind_lock);
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
 
-    if (!ssh_opts.sshbind) {
-        ssh_opts.sshbind = ssh_bind_new();
-        if (!ssh_opts.sshbind) {
+    /* LOCK */
+    pthread_mutex_lock(&opts->sshbind_lock);
+
+    if (!opts->sshbind) {
+        opts->sshbind = ssh_bind_new();
+        if (!opts->sshbind) {
             ERR("Failed to create a new ssh_bind.");
             goto fail;
         }
     }
 
-    if (ssh_bind_options_set(ssh_opts.sshbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path) != SSH_OK) {
-        ERR("Failed to set host key (%s).", ssh_get_error(ssh_opts.sshbind));
+    if (ssh_bind_options_set(opts->sshbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path) != SSH_OK) {
+        ERR("Failed to set host key (%s).", ssh_get_error(opts->sshbind));
         goto fail;
     }
 
     /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.sshbind_lock);
+    pthread_mutex_unlock(&opts->sshbind_lock);
     return 0;
 
 fail:
     /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.sshbind_lock);
+    pthread_mutex_unlock(&opts->sshbind_lock);
+    return -1;
+}
+
+API int
+nc_ssh_server_set_hostkey(const char *privkey_path)
+{
+    return _nc_ssh_server_set_hostkey(privkey_path, 0);
+}
+
+API int
+nc_ssh_server_ch_set_hostkey(const char *privkey_path)
+{
+    return _nc_ssh_server_set_hostkey(privkey_path, 1);
+}
+
+static int
+_nc_ssh_server_set_banner(const char *banner, int ch)
+{
+    struct nc_ssh_server_opts *opts;
+
+    if (!banner) {
+        ERRARG;
+        return -1;
+    }
+
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
+    /* LOCK */
+    pthread_mutex_lock(&opts->sshbind_lock);
+
+    if (!opts->sshbind) {
+        opts->sshbind = ssh_bind_new();
+        if (!opts->sshbind) {
+            ERR("Failed to create a new ssh_bind.");
+            goto fail;
+        }
+    }
+
+    ssh_bind_options_set(opts->sshbind, SSH_BIND_OPTIONS_BANNER, banner);
+
+    /* UNLOCK */
+    pthread_mutex_unlock(&opts->sshbind_lock);
+    return 0;
+
+fail:
+    /* UNLOCK */
+    pthread_mutex_unlock(&opts->sshbind_lock);
     return -1;
 }
 
 API int
 nc_ssh_server_set_banner(const char *banner)
 {
-    if (!banner) {
-        ERRARG;
-        return -1;
-    }
-
-    /* LOCK */
-    pthread_mutex_lock(&ssh_opts.sshbind_lock);
-
-    if (!ssh_opts.sshbind) {
-        ssh_opts.sshbind = ssh_bind_new();
-        if (!ssh_opts.sshbind) {
-            ERR("Failed to create a new ssh_bind.");
-            goto fail;
-        }
-    }
-
-    ssh_bind_options_set(ssh_opts.sshbind, SSH_BIND_OPTIONS_BANNER, banner);
-
-    /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.sshbind_lock);
-    return 0;
-
-fail:
-    /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.sshbind_lock);
-    return -1;
+    return _nc_ssh_server_set_banner(banner, 0);
 }
 
 API int
-nc_ssh_server_set_auth_methods(int auth_methods)
+nc_ssh_server_ch_set_banner(const char *banner)
 {
+    return _nc_ssh_server_set_banner(banner, 1);
+}
+
+static int
+_nc_ssh_server_set_auth_methods(int auth_methods, int ch)
+{
+    struct nc_ssh_server_opts *opts;
+
     if (!(auth_methods & NC_SSH_AUTH_PUBLICKEY) && !(auth_methods & NC_SSH_AUTH_PASSWORD)
             && !(auth_methods & NC_SSH_AUTH_INTERACTIVE)) {
         ERRARG;
         return -1;
     }
 
-    ssh_opts.auth_methods = auth_methods;
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
+    opts->auth_methods = auth_methods;
+    return 0;
+}
+
+API int
+nc_ssh_server_set_auth_methods(int auth_methods)
+{
+    return _nc_ssh_server_set_auth_methods(auth_methods, 0);
+}
+
+API int
+nc_ssh_server_ch_set_auth_methods(int auth_methods)
+{
+    return _nc_ssh_server_set_auth_methods(auth_methods, 1);
+}
+
+static int
+_nc_ssh_server_set_auth_attempts(uint16_t auth_attempts, int ch)
+{
+    struct nc_ssh_server_opts *opts;
+
+    if (!auth_attempts) {
+        ERRARG;
+        return -1;
+    }
+
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
+    opts->auth_attempts = auth_attempts;
     return 0;
 }
 
 API int
 nc_ssh_server_set_auth_attempts(uint16_t auth_attempts)
 {
-    if (!auth_attempts) {
+    return _nc_ssh_server_set_auth_attempts(auth_attempts, 0);
+}
+
+API int
+nc_ssh_server_set_ch_auth_attempts(uint16_t auth_attempts)
+{
+    return _nc_ssh_server_set_auth_attempts(auth_attempts, 1);
+}
+
+static int
+_nc_ssh_server_set_auth_timeout(uint16_t auth_timeout, int ch)
+{
+    struct nc_ssh_server_opts *opts;
+
+    if (!auth_timeout) {
         ERRARG;
         return -1;
     }
 
-    ssh_opts.auth_attempts = auth_attempts;
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
+    opts->auth_timeout = auth_timeout;
     return 0;
 }
 
 API int
 nc_ssh_server_set_auth_timeout(uint16_t auth_timeout)
 {
-    if (!auth_timeout) {
+    return _nc_ssh_server_set_auth_timeout(auth_timeout, 0);
+}
+
+API int
+nc_ssh_server_ch_set_auth_timeout(uint16_t auth_timeout)
+{
+    return _nc_ssh_server_set_auth_timeout(auth_timeout, 1);
+}
+
+static int
+_nc_ssh_server_add_authkey(const char *pubkey_path, const char *username, int ch)
+{
+    struct nc_ssh_server_opts *opts;
+
+    if (!pubkey_path || !username) {
         ERRARG;
         return -1;
     }
 
-    ssh_opts.auth_timeout = auth_timeout;
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
+    /* LOCK */
+    pthread_mutex_lock(&opts->authkey_lock);
+
+    ++opts->authkey_count;
+    opts->authkeys = realloc(opts->authkeys, opts->authkey_count * sizeof *opts->authkeys);
+
+    nc_ctx_lock(-1, NULL);
+    opts->authkeys[opts->authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0);
+    opts->authkeys[opts->authkey_count - 1].username = lydict_insert(server_opts.ctx, username, 0);
+    nc_ctx_unlock();
+
+    /* UNLOCK */
+    pthread_mutex_unlock(&opts->authkey_lock);
+
     return 0;
 }
 
 API int
 nc_ssh_server_add_authkey(const char *pubkey_path, const char *username)
 {
-    if (!pubkey_path || !username) {
-        ERRARG;
-        return -1;
-    }
-
-    /* LOCK */
-    pthread_mutex_lock(&ssh_opts.authkey_lock);
-
-    ++ssh_opts.authkey_count;
-    ssh_opts.authkeys = realloc(ssh_opts.authkeys, ssh_opts.authkey_count * sizeof *ssh_opts.authkeys);
-
-    nc_ctx_lock(-1, NULL);
-    ssh_opts.authkeys[ssh_opts.authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0);
-    ssh_opts.authkeys[ssh_opts.authkey_count - 1].username = lydict_insert(server_opts.ctx, username, 0);
-    nc_ctx_unlock();
-
-    /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.authkey_lock);
-
-    return 0;
+    return _nc_ssh_server_add_authkey(pubkey_path, username, 0);
 }
 
 API int
-nc_ssh_server_del_authkey(const char *pubkey_path, const char *username)
+nc_ssh_server_ch_add_authkey(const char *pubkey_path, const char *username)
 {
+    return _nc_ssh_server_add_authkey(pubkey_path, username, 1);
+}
+
+static int
+_nc_ssh_server_del_authkey(const char *pubkey_path, const char *username, int ch)
+{
+    struct nc_ssh_server_opts *opts;
     uint32_t i;
     int ret = -1;
 
+    opts = (ch ? &ssh_ch_opts : &ssh_opts);
+
     /* LOCK */
-    pthread_mutex_lock(&ssh_opts.authkey_lock);
+    pthread_mutex_lock(&opts->authkey_lock);
 
     if (!pubkey_path && !username) {
         nc_ctx_lock(-1, NULL);
-        for (i = 0; i < ssh_opts.authkey_count; ++i) {
-            lydict_remove(server_opts.ctx, ssh_opts.authkeys[i].path);
-            lydict_remove(server_opts.ctx, ssh_opts.authkeys[i].username);
+        for (i = 0; i < opts->authkey_count; ++i) {
+            lydict_remove(server_opts.ctx, opts->authkeys[i].path);
+            lydict_remove(server_opts.ctx, opts->authkeys[i].username);
 
             ret = 0;
         }
         nc_ctx_unlock();
-        free(ssh_opts.authkeys);
-        ssh_opts.authkeys = NULL;
-        ssh_opts.authkey_count = 0;
+        free(opts->authkeys);
+        opts->authkeys = NULL;
+        opts->authkey_count = 0;
     } else {
-        for (i = 0; i < ssh_opts.authkey_count; ++i) {
-            if ((!pubkey_path || !strcmp(ssh_opts.authkeys[i].path, pubkey_path))
-                    && (!username || !strcmp(ssh_opts.authkeys[i].username, username))) {
+        for (i = 0; i < opts->authkey_count; ++i) {
+            if ((!pubkey_path || !strcmp(opts->authkeys[i].path, pubkey_path))
+                    && (!username || !strcmp(opts->authkeys[i].username, username))) {
                 nc_ctx_lock(-1, NULL);
-                lydict_remove(server_opts.ctx, ssh_opts.authkeys[i].path);
-                lydict_remove(server_opts.ctx, ssh_opts.authkeys[i].username);
+                lydict_remove(server_opts.ctx, opts->authkeys[i].path);
+                lydict_remove(server_opts.ctx, opts->authkeys[i].username);
                 nc_ctx_unlock();
 
-                --ssh_opts.authkey_count;
-                memcpy(&ssh_opts.authkeys[i], &ssh_opts.authkeys[ssh_opts.authkey_count], sizeof *ssh_opts.authkeys);
+                --opts->authkey_count;
+                memcpy(&opts->authkeys[i], &opts->authkeys[opts->authkey_count], sizeof *opts->authkeys);
 
                 ret = 0;
             }
@@ -208,11 +316,23 @@
     }
 
     /* UNLOCK */
-    pthread_mutex_unlock(&ssh_opts.authkey_lock);
+    pthread_mutex_unlock(&opts->authkey_lock);
 
     return ret;
 }
 
+API int
+nc_ssh_server_del_authkey(const char *pubkey_path, const char *username)
+{
+    return _nc_ssh_server_del_authkey(pubkey_path, username, 0);
+}
+
+API int
+nc_ssh_server_ch_del_authkey(const char *pubkey_path, const char *username)
+{
+    return _nc_ssh_server_del_authkey(pubkey_path, username, 1);
+}
+
 API void
 nc_ssh_server_free_opts(void)
 {
@@ -228,6 +348,21 @@
     pthread_mutex_unlock(&ssh_opts.sshbind_lock);
 
     nc_ssh_server_del_authkey(NULL, NULL);
+
+    /* Call Home */
+
+    /* LOCK */
+    pthread_mutex_lock(&ssh_ch_opts.sshbind_lock);
+
+    if (ssh_ch_opts.sshbind) {
+        ssh_bind_free(ssh_ch_opts.sshbind);
+        ssh_ch_opts.sshbind = NULL;
+    }
+
+    /* UNLOCK */
+    pthread_mutex_unlock(&ssh_ch_opts.sshbind_lock);
+
+    nc_ssh_server_ch_del_authkey(NULL, NULL);
 }
 
 static char *