roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1 | /** |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2 | * @file server_config.c |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 3 | * @author Roman Janota <janota@cesnet.cz> |
| 4 | * @brief libnetconf2 server configuration functions |
| 5 | * |
| 6 | * @copyright |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 7 | * Copyright (c) 2022-2023 CESNET, z.s.p.o. |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 18 | #include <assert.h> |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <stdint.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 25 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 26 | #include <libyang/libyang.h> |
roman | a3c95c7 | 2023-10-26 11:15:53 +0200 | [diff] [blame] | 27 | #include <libyang/tree_data.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 28 | |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 29 | #ifdef NC_ENABLED_SSH_TLS |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 30 | #include <openssl/err.h> |
| 31 | #include <openssl/evp.h> // EVP_PKEY_free |
| 32 | #include <openssl/x509.h> // d2i_PUBKEY |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 33 | #include <openssl/x509_vfy.h> // X509_STORE_free |
| 34 | #endif |
| 35 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 36 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 37 | #include "config.h" |
| 38 | #include "log_p.h" |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 39 | #include "server_config.h" |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 40 | #include "server_config_p.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 41 | #include "session_p.h" |
| 42 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 43 | #ifdef NC_ENABLED_SSH_TLS |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 44 | |
| 45 | /* All libssh supported host-key, key-exchange, encryption and mac algorithms as of version 0.10.90 */ |
| 46 | |
| 47 | static const char *supported_hostkey_algs[] = { |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 48 | "openssh-ssh-ed25519-cert-v01", "openssh-ecdsa-sha2-nistp521-cert-v01", |
| 49 | "openssh-ecdsa-sha2-nistp384-cert-v01", "openssh-ecdsa-sha2-nistp256-cert-v01", |
| 50 | "openssh-rsa-sha2-512-cert-v01", "openssh-rsa-sha2-256-cert-v01", |
| 51 | "openssh-ssh-rsa-cert-v01", "openssh-ssh-dss-cert-v01", |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 52 | "ssh-ed25519", "ecdsa-sha2-nistp521", "ecdsa-sha2-nistp384", "ecdsa-sha2-nistp256", |
| 53 | "rsa-sha2-512", "rsa-sha2-256", "ssh-rsa", "ssh-dss", NULL |
| 54 | }; |
| 55 | |
| 56 | static const char *supported_kex_algs[] = { |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 57 | "diffie-hellman-group-exchange-sha1", "curve25519-sha256", "libssh-curve25519-sha256", |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 58 | "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521", "diffie-hellman-group18-sha512", |
| 59 | "diffie-hellman-group16-sha512", "diffie-hellman-group-exchange-sha256", "diffie-hellman-group14-sha256", NULL |
| 60 | }; |
| 61 | |
| 62 | static const char *supported_encryption_algs[] = { |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 63 | "openssh-chacha20-poly1305", "openssh-aes256-gcm", "openssh-aes128-gcm", |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 64 | "aes256-ctr", "aes192-ctr", "aes128-ctr", "aes256-cbc", "aes192-cbc", "aes128-cbc", |
roman | 2721524 | 2023-03-10 14:55:00 +0100 | [diff] [blame] | 65 | "blowfish-cbc", "triple-des-cbc", "none", NULL |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static const char *supported_mac_algs[] = { |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 69 | "openssh-hmac-sha2-256-etm", "openssh-hmac-sha2-512-etm", "openssh-hmac-sha1-etm", |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 70 | "hmac-sha2-256", "hmac-sha2-512", "hmac-sha1", NULL |
| 71 | }; |
| 72 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 73 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 74 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 75 | extern struct nc_server_opts server_opts; |
| 76 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 77 | /* returns true if a node is a part of the listen subtree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 78 | static int |
| 79 | is_listen(const struct lyd_node *node) |
| 80 | { |
| 81 | assert(node); |
| 82 | |
| 83 | while (node) { |
| 84 | if (!strcmp(LYD_NAME(node), "listen")) { |
| 85 | break; |
| 86 | } |
| 87 | node = lyd_parent(node); |
| 88 | } |
| 89 | |
| 90 | return node != NULL; |
| 91 | } |
| 92 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 93 | /* returns true if a node is a part of the Call Home subtree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 94 | static int |
| 95 | is_ch(const struct lyd_node *node) |
| 96 | { |
| 97 | assert(node); |
| 98 | |
| 99 | while (node) { |
| 100 | if (!strcmp(LYD_NAME(node), "call-home")) { |
| 101 | break; |
| 102 | } |
| 103 | node = lyd_parent(node); |
| 104 | } |
| 105 | |
| 106 | return node != NULL; |
| 107 | } |
| 108 | |
| 109 | #ifdef NC_ENABLED_SSH_TLS |
| 110 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 111 | /* returns true if a node is a part of the ssh subtree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 112 | static int |
| 113 | is_ssh(const struct lyd_node *node) |
| 114 | { |
| 115 | assert(node); |
| 116 | |
| 117 | while (node) { |
| 118 | if (!strcmp(LYD_NAME(node), "ssh")) { |
| 119 | break; |
| 120 | } |
| 121 | node = lyd_parent(node); |
| 122 | } |
| 123 | |
| 124 | return node != NULL; |
| 125 | } |
| 126 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 127 | /* returns true if a node is a part of the tls subtree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 128 | static int |
| 129 | is_tls(const struct lyd_node *node) |
| 130 | { |
| 131 | assert(node); |
| 132 | |
| 133 | while (node) { |
| 134 | if (!strcmp(LYD_NAME(node), "tls")) { |
| 135 | break; |
| 136 | } |
| 137 | node = lyd_parent(node); |
| 138 | } |
| 139 | |
| 140 | return node != NULL; |
| 141 | } |
| 142 | |
| 143 | #endif /* NC_ENABLED_SSH_TLS */ |
| 144 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 145 | /* gets the endpoint struct (and optionally bind) based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 146 | static int |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 147 | nc_server_config_get_endpt(const struct lyd_node *node, struct nc_endpt **endpt, struct nc_bind **bind) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 148 | { |
| 149 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 150 | const char *name; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 151 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 152 | assert(node && endpt); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 153 | name = LYD_NAME(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 154 | |
| 155 | while (node) { |
| 156 | if (!strcmp(LYD_NAME(node), "endpoint")) { |
| 157 | break; |
| 158 | } |
| 159 | node = lyd_parent(node); |
| 160 | } |
| 161 | |
| 162 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 163 | ERR(NULL, "Node \"%s\" is not contained in an endpoint subtree.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | node = lyd_child(node); |
| 168 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 169 | name = lyd_get_value(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 170 | |
| 171 | for (i = 0; i < server_opts.endpt_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 172 | if (!strcmp(server_opts.endpts[i].name, name)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 173 | *endpt = &server_opts.endpts[i]; |
| 174 | if (bind) { |
| 175 | *bind = &server_opts.binds[i]; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 181 | ERR(NULL, "Endpoint \"%s\" was not found.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 185 | /* gets the ch_client struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 186 | static int |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 187 | nc_server_config_get_ch_client(const struct lyd_node *node, struct nc_ch_client **ch_client) |
| 188 | { |
| 189 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 190 | const char *name; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 191 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 192 | assert(node && ch_client); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 193 | name = LYD_NAME(node); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 194 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 195 | while (node) { |
| 196 | if (!strcmp(LYD_NAME(node), "netconf-client")) { |
| 197 | break; |
| 198 | } |
| 199 | node = lyd_parent(node); |
| 200 | } |
| 201 | |
| 202 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 203 | ERR(NULL, "Node \"%s\" is not contained in a netconf-client subtree.", name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 204 | return 1; |
| 205 | } |
| 206 | |
| 207 | node = lyd_child(node); |
| 208 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 209 | name = lyd_get_value(node); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 210 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 211 | /* LOCK */ |
| 212 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 213 | for (i = 0; i < server_opts.ch_client_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 214 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 215 | *ch_client = &server_opts.ch_clients[i]; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 216 | /* UNLOCK */ |
| 217 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 222 | /* UNLOCK */ |
| 223 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 224 | ERR(NULL, "Call-home client \"%s\" was not found.", name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 228 | /* gets the ch_endpt struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 229 | static int |
| 230 | nc_server_config_get_ch_endpt(const struct lyd_node *node, struct nc_ch_endpt **ch_endpt) |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 231 | { |
| 232 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 233 | const char *name; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 234 | struct nc_ch_client *ch_client; |
| 235 | |
| 236 | assert(node && ch_endpt); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 237 | name = LYD_NAME(node); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 238 | |
| 239 | if (nc_server_config_get_ch_client(node, &ch_client)) { |
| 240 | return 1; |
| 241 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 242 | |
| 243 | while (node) { |
| 244 | if (!strcmp(LYD_NAME(node), "endpoint")) { |
| 245 | break; |
| 246 | } |
| 247 | node = lyd_parent(node); |
| 248 | } |
| 249 | |
| 250 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 251 | ERR(NULL, "Node \"%s\" is not contained in a call-home endpoint subtree.", name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | node = lyd_child(node); |
| 256 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 257 | name = lyd_get_value(node); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 258 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 259 | /* LOCK */ |
| 260 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 261 | for (i = 0; i < ch_client->ch_endpt_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 262 | if (!strcmp(ch_client->ch_endpts[i].name, name)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 263 | *ch_endpt = &ch_client->ch_endpts[i]; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 264 | /* UNLOCK */ |
| 265 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | } |
| 269 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 270 | /* UNLOCK */ |
| 271 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 272 | ERR(NULL, "Call-home client's \"%s\" endpoint \"%s\" was not found.", ch_client->name, name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 273 | return 1; |
| 274 | } |
| 275 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 276 | #ifdef NC_ENABLED_SSH_TLS |
| 277 | |
| 278 | /* gets the ssh_opts struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 279 | static int |
| 280 | nc_server_config_get_ssh_opts(const struct lyd_node *node, struct nc_server_ssh_opts **opts) |
| 281 | { |
| 282 | struct nc_endpt *endpt; |
| 283 | struct nc_ch_endpt *ch_endpt; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 284 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 285 | assert(node && opts); |
| 286 | |
| 287 | if (is_listen(node)) { |
| 288 | if (nc_server_config_get_endpt(node, &endpt, NULL)) { |
| 289 | return 1; |
| 290 | } |
| 291 | *opts = endpt->opts.ssh; |
| 292 | } else { |
| 293 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
| 294 | return 1; |
| 295 | } |
| 296 | *opts = ch_endpt->opts.ssh; |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 302 | /* gets the hostkey struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 303 | static int |
| 304 | nc_server_config_get_hostkey(const struct lyd_node *node, struct nc_hostkey **hostkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 305 | { |
| 306 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 307 | const char *name; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 308 | struct nc_server_ssh_opts *opts; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 309 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 310 | assert(node && hostkey); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 311 | name = LYD_NAME(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 312 | |
| 313 | while (node) { |
| 314 | if (!strcmp(LYD_NAME(node), "host-key")) { |
| 315 | break; |
| 316 | } |
| 317 | node = lyd_parent(node); |
| 318 | } |
| 319 | |
| 320 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 321 | ERR(NULL, "Node \"%s\" is not contained in a host-key subtree.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | node = lyd_child(node); |
| 326 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 327 | name = lyd_get_value(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 328 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 329 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 330 | return 1; |
| 331 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 332 | for (i = 0; i < opts->hostkey_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 333 | if (!strcmp(opts->hostkeys[i].name, name)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 334 | *hostkey = &opts->hostkeys[i]; |
| 335 | return 0; |
| 336 | } |
| 337 | } |
| 338 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 339 | ERR(NULL, "Host-key \"%s\" was not found.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 340 | return 1; |
| 341 | } |
| 342 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 343 | /* gets the client_auth struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 344 | static int |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 345 | nc_server_config_get_auth_client(const struct lyd_node *node, struct nc_auth_client **auth_client) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 346 | { |
| 347 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 348 | const char *name; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 349 | struct nc_server_ssh_opts *opts; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 350 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 351 | assert(node && auth_client); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 352 | name = LYD_NAME(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 353 | |
| 354 | while (node) { |
| 355 | if (!strcmp(LYD_NAME(node), "user")) { |
| 356 | break; |
| 357 | } |
| 358 | node = lyd_parent(node); |
| 359 | } |
| 360 | |
| 361 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 362 | ERR(NULL, "Node \"%s\" is not contained in a client-authentication subtree.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | node = lyd_child(node); |
| 367 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 368 | name = lyd_get_value(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 369 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 370 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 371 | return 1; |
| 372 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 373 | for (i = 0; i < opts->client_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 374 | if (!strcmp(opts->auth_clients[i].username, name)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 375 | *auth_client = &opts->auth_clients[i]; |
| 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 380 | ERR(NULL, "Authorized key \"%s\" was not found.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 381 | return 1; |
| 382 | } |
| 383 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 384 | /* gets the pubkey struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 385 | static int |
| 386 | nc_server_config_get_pubkey(const struct lyd_node *node, struct nc_public_key **pubkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 387 | { |
| 388 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 389 | const char *name; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 390 | struct nc_auth_client *auth_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 391 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 392 | assert(node && pubkey); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 393 | name = LYD_NAME(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 394 | |
| 395 | node = lyd_parent(node); |
| 396 | while (node) { |
| 397 | if (!strcmp(LYD_NAME(node), "public-key")) { |
| 398 | break; |
| 399 | } |
| 400 | node = lyd_parent(node); |
| 401 | } |
| 402 | |
| 403 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 404 | ERR(NULL, "Node \"%s\" is not contained in a public-key subtree.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | node = lyd_child(node); |
| 409 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 410 | name = lyd_get_value(node); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 411 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 412 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
| 413 | return 1; |
| 414 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 415 | for (i = 0; i < auth_client->pubkey_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 416 | if (!strcmp(auth_client->pubkeys[i].name, name)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 417 | *pubkey = &auth_client->pubkeys[i]; |
| 418 | return 0; |
| 419 | } |
| 420 | } |
| 421 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 422 | ERR(NULL, "Public key \"%s\" was not found.", name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 423 | return 1; |
| 424 | } |
| 425 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 426 | /* gets the tls_opts struct based on node's location in the YANG data tree */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 427 | static int |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 428 | nc_server_config_get_tls_opts(const struct lyd_node *node, struct nc_server_tls_opts **opts) |
| 429 | { |
| 430 | struct nc_endpt *endpt; |
| 431 | struct nc_ch_endpt *ch_endpt; |
| 432 | |
| 433 | assert(node && opts); |
| 434 | |
| 435 | if (is_listen(node)) { |
| 436 | if (nc_server_config_get_endpt(node, &endpt, NULL)) { |
| 437 | return 1; |
| 438 | } |
| 439 | *opts = endpt->opts.tls; |
| 440 | } else { |
| 441 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
| 442 | return 1; |
| 443 | } |
| 444 | *opts = ch_endpt->opts.tls; |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 450 | /* gets the cert struct based on node's location in the YANG data tree */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 451 | static int |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 452 | nc_server_config_get_cert(const struct lyd_node *node, struct nc_certificate **cert) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 453 | { |
| 454 | uint16_t i; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 455 | const char *name; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 456 | struct nc_cert_grouping *auth_client; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 457 | struct nc_server_tls_opts *opts; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 458 | int is_cert_end_entity; |
| 459 | struct lyd_node *name_node; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 460 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 461 | assert(node && cert); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 462 | name = LYD_NAME(node); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 463 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 464 | /* check if node is in certificate subtree */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 465 | while (node) { |
| 466 | if (!strcmp(LYD_NAME(node), "certificate")) { |
| 467 | break; |
| 468 | } |
| 469 | node = lyd_parent(node); |
| 470 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 471 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 472 | ERR(NULL, "Node \"%s\" is not contained in a certificate subtree.", name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 476 | /* it's child should be the list's key, check later */ |
| 477 | name_node = lyd_child(node); |
| 478 | |
| 479 | /* it's in certificate node, now check if it's end entity or certificate authority */ |
| 480 | while (node) { |
| 481 | if (!strcmp(LYD_NAME(node), "ee-certs")) { |
| 482 | is_cert_end_entity = 1; |
| 483 | break; |
| 484 | } else if (!strcmp(LYD_NAME(node), "ca-certs")) { |
| 485 | is_cert_end_entity = 0; |
| 486 | break; |
| 487 | } |
| 488 | node = lyd_parent(node); |
| 489 | } |
| 490 | if (!node) { |
| 491 | ERR(NULL, "Node \"%s\" is not contained in ee-certs nor ca-certs subtree.", name); |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | assert(!strcmp(LYD_NAME(name_node), "name")); |
| 496 | name = lyd_get_value(name_node); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 497 | |
| 498 | if (nc_server_config_get_tls_opts(node, &opts)) { |
| 499 | return 1; |
| 500 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 501 | if (is_cert_end_entity) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 502 | auth_client = &opts->ee_certs; |
| 503 | } else { |
| 504 | auth_client = &opts->ca_certs; |
| 505 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 506 | |
| 507 | for (i = 0; i < auth_client->cert_count; i++) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 508 | if (!strcmp(auth_client->certs[i].name, name)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 509 | *cert = &auth_client->certs[i]; |
| 510 | return 0; |
| 511 | } |
| 512 | } |
| 513 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 514 | ERR(NULL, "Certificate \"%s\" was not found.", name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 515 | return 1; |
| 516 | } |
| 517 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 518 | /* gets the ctn struct based on node's location in the YANG data tree */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 519 | static int |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 520 | nc_server_config_get_ctn(const struct lyd_node *node, struct nc_ctn **ctn) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 521 | { |
| 522 | uint32_t id; |
| 523 | struct nc_ctn *iter; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 524 | struct nc_server_tls_opts *opts; |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 525 | const char *name; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 526 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 527 | assert(node && ctn); |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 528 | name = LYD_NAME(node); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 529 | |
| 530 | node = lyd_parent(node); |
| 531 | while (node) { |
| 532 | if (!strcmp(LYD_NAME(node), "cert-to-name")) { |
| 533 | break; |
| 534 | } |
| 535 | node = lyd_parent(node); |
| 536 | } |
| 537 | |
| 538 | if (!node) { |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 539 | ERR(NULL, "Node \"%s\" is not contained in a cert-to-name subtree.", name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | node = lyd_child(node); |
| 544 | assert(!strcmp(LYD_NAME(node), "id")); |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 545 | id = ((struct lyd_node_term *)node)->value.uint32; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 546 | |
roman | b9beb11 | 2023-07-18 09:06:58 +0200 | [diff] [blame] | 547 | if (nc_server_config_get_tls_opts(node, &opts)) { |
| 548 | return 1; |
| 549 | } |
| 550 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 551 | iter = opts->ctn; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 552 | while (iter) { |
| 553 | if (iter->id == id) { |
| 554 | *ctn = iter; |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | iter = iter->next; |
| 559 | } |
| 560 | |
| 561 | ERR(NULL, "Cert-to-name entry with id \"%d\" was not found.", id); |
| 562 | return 1; |
| 563 | } |
| 564 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 565 | NC_PRIVKEY_FORMAT |
| 566 | nc_server_config_get_private_key_type(const char *format) |
| 567 | { |
| 568 | if (!strcmp(format, "rsa-private-key-format")) { |
| 569 | return NC_PRIVKEY_FORMAT_RSA; |
| 570 | } else if (!strcmp(format, "ec-private-key-format")) { |
| 571 | return NC_PRIVKEY_FORMAT_EC; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 572 | } else if (!strcmp(format, "private-key-info-format")) { |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 573 | return NC_PRIVKEY_FORMAT_X509; |
| 574 | } else if (!strcmp(format, "openssh-private-key-format")) { |
| 575 | return NC_PRIVKEY_FORMAT_OPENSSH; |
| 576 | } else { |
| 577 | ERR(NULL, "Private key format (%s) not supported.", format); |
| 578 | return NC_PRIVKEY_FORMAT_UNKNOWN; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 583 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 584 | /* gets the ch_client struct based on node's location in the YANG data tree and locks it for reading */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 585 | static int |
| 586 | nc_server_config_get_ch_client_with_lock(const struct lyd_node *node, struct nc_ch_client **ch_client) |
| 587 | { |
| 588 | uint16_t i; |
| 589 | const char *name; |
| 590 | |
| 591 | assert(node && ch_client); |
| 592 | name = LYD_NAME(node); |
| 593 | |
| 594 | while (node) { |
| 595 | if (!strcmp(LYD_NAME(node), "netconf-client")) { |
| 596 | break; |
| 597 | } |
| 598 | node = lyd_parent(node); |
| 599 | } |
| 600 | |
| 601 | if (!node) { |
| 602 | ERR(NULL, "Node \"%s\" is not contained in a netconf-client subtree.", name); |
| 603 | return 1; |
| 604 | } |
| 605 | |
| 606 | node = lyd_child(node); |
| 607 | assert(!strcmp(LYD_NAME(node), "name")); |
| 608 | name = lyd_get_value(node); |
| 609 | |
| 610 | /* LOCK */ |
| 611 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 612 | for (i = 0; i < server_opts.ch_client_count; i++) { |
| 613 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 614 | /* LOCK */ |
| 615 | pthread_mutex_lock(&server_opts.ch_clients[i].lock); |
| 616 | *ch_client = &server_opts.ch_clients[i]; |
| 617 | return 0; |
| 618 | } |
| 619 | } |
| 620 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 621 | /* UNLOCK */ |
| 622 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 623 | ERR(NULL, "Call-home client \"%s\" was not found.", name); |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | static void |
| 628 | nc_ch_client_unlock(struct nc_ch_client *client) |
| 629 | { |
| 630 | assert(client); |
| 631 | |
| 632 | pthread_mutex_unlock(&client->lock); |
| 633 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 634 | } |
| 635 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 636 | int |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 637 | equal_parent_name(const struct lyd_node *node, uint16_t parent_count, const char *parent_name) |
| 638 | { |
| 639 | uint16_t i; |
| 640 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 641 | assert(node && parent_count && parent_name); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 642 | |
| 643 | node = lyd_parent(node); |
| 644 | for (i = 1; i < parent_count; i++) { |
| 645 | node = lyd_parent(node); |
| 646 | } |
| 647 | |
| 648 | if (!strcmp(LYD_NAME(node), parent_name)) { |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 655 | int |
| 656 | nc_server_config_realloc(const char *key_value, void **ptr, size_t size, uint16_t *count) |
| 657 | { |
| 658 | int ret = 0; |
| 659 | void *tmp; |
| 660 | char **name; |
| 661 | |
| 662 | tmp = realloc(*ptr, (*count + 1) * size); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 663 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 664 | *ptr = tmp; |
| 665 | |
| 666 | /* set the newly allocated memory to 0 */ |
| 667 | memset((char *)(*ptr) + (*count * size), 0, size); |
| 668 | (*count)++; |
| 669 | |
| 670 | /* access the first member of the supposed structure */ |
| 671 | name = (char **)((*ptr) + ((*count - 1) * size)); |
| 672 | |
| 673 | /* and set it's value */ |
| 674 | *name = strdup(key_value); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 675 | NC_CHECK_ERRMEM_GOTO(!*name, ret = 1, cleanup); |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 676 | |
| 677 | cleanup: |
| 678 | return ret; |
| 679 | } |
| 680 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 681 | #ifdef NC_ENABLED_SSH_TLS |
| 682 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 683 | static void |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 684 | nc_server_config_del_hostkey(struct nc_server_ssh_opts *opts, struct nc_hostkey *hostkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 685 | { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 686 | assert(hostkey->store == NC_STORE_LOCAL || hostkey->store == NC_STORE_KEYSTORE); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 687 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 688 | free(hostkey->name); |
| 689 | |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 690 | if (hostkey->store == NC_STORE_LOCAL) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 691 | free(hostkey->key.pubkey_data); |
| 692 | free(hostkey->key.privkey_data); |
roman | dd019a9 | 2023-09-14 10:17:07 +0200 | [diff] [blame] | 693 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 694 | free(hostkey->ks_ref); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 695 | } |
| 696 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 697 | opts->hostkey_count--; |
| 698 | if (!opts->hostkey_count) { |
| 699 | free(opts->hostkeys); |
| 700 | opts->hostkeys = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 701 | } else if (hostkey != &opts->hostkeys[opts->hostkey_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 702 | memcpy(hostkey, &opts->hostkeys[opts->hostkey_count], sizeof *opts->hostkeys); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | static void |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 707 | nc_server_config_del_auth_client_pubkey(struct nc_auth_client *auth_client, struct nc_public_key *pubkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 708 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 709 | free(pubkey->name); |
| 710 | free(pubkey->data); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 711 | |
| 712 | auth_client->pubkey_count--; |
| 713 | if (!auth_client->pubkey_count) { |
| 714 | free(auth_client->pubkeys); |
| 715 | auth_client->pubkeys = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 716 | } else if (pubkey != &auth_client->pubkeys[auth_client->pubkey_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 717 | memcpy(pubkey, &auth_client->pubkeys[auth_client->pubkey_count], sizeof *auth_client->pubkeys); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | static void |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 722 | nc_server_config_del_auth_client(struct nc_server_ssh_opts *opts, struct nc_auth_client *auth_client) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 723 | { |
| 724 | uint16_t i, pubkey_count; |
| 725 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 726 | free(auth_client->username); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 727 | |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 728 | if (auth_client->store == NC_STORE_LOCAL) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 729 | pubkey_count = auth_client->pubkey_count; |
| 730 | for (i = 0; i < pubkey_count; i++) { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 731 | nc_server_config_del_auth_client_pubkey(auth_client, &auth_client->pubkeys[i]); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 732 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 733 | } else { |
| 734 | free(auth_client->ts_ref); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 735 | } |
| 736 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 737 | free(auth_client->password); |
| 738 | free(auth_client->pam_config_name); |
| 739 | free(auth_client->pam_config_dir); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 740 | |
| 741 | opts->client_count--; |
| 742 | if (!opts->client_count) { |
| 743 | free(opts->auth_clients); |
| 744 | opts->auth_clients = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 745 | } else if (auth_client != &opts->auth_clients[opts->client_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 746 | memcpy(auth_client, &opts->auth_clients[opts->client_count], sizeof *opts->auth_clients); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 751 | nc_server_config_del_ssh_opts(struct nc_bind *bind, struct nc_server_ssh_opts *opts) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 752 | { |
| 753 | uint16_t i, hostkey_count, client_count; |
| 754 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 755 | free(bind->address); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 756 | if (bind->sock > -1) { |
| 757 | close(bind->sock); |
| 758 | } |
| 759 | |
| 760 | /* store in variable because it gets decremented in the function call */ |
| 761 | hostkey_count = opts->hostkey_count; |
| 762 | for (i = 0; i < hostkey_count; i++) { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 763 | nc_server_config_del_hostkey(opts, &opts->hostkeys[i]); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | client_count = opts->client_count; |
| 767 | for (i = 0; i < client_count; i++) { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 768 | nc_server_config_del_auth_client(opts, &opts->auth_clients[i]); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 769 | } |
| 770 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 771 | free(opts->hostkey_algs); |
| 772 | free(opts->kex_algs); |
| 773 | free(opts->encryption_algs); |
| 774 | free(opts->mac_algs); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 775 | |
| 776 | free(opts); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 780 | nc_server_config_del_endpt_ssh(struct nc_endpt *endpt, struct nc_bind *bind) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 781 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 782 | free(endpt->name); |
| 783 | free(endpt->referenced_endpt_name); |
| 784 | nc_server_config_del_ssh_opts(bind, endpt->opts.ssh); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 785 | |
| 786 | server_opts.endpt_count--; |
| 787 | if (!server_opts.endpt_count) { |
| 788 | free(server_opts.endpts); |
| 789 | free(server_opts.binds); |
| 790 | server_opts.endpts = NULL; |
| 791 | server_opts.binds = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 792 | } else if (endpt != &server_opts.endpts[server_opts.endpt_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 793 | memcpy(endpt, &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
| 794 | memcpy(bind, &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 798 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 799 | |
roman | 45cec4e | 2023-02-17 10:21:39 +0100 | [diff] [blame] | 800 | void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 801 | nc_server_config_del_unix_socket_opts(struct nc_bind *bind, struct nc_server_unix_opts *opts) |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 802 | { |
| 803 | if (bind->sock > -1) { |
| 804 | close(bind->sock); |
| 805 | } |
| 806 | |
roman | d0b7837 | 2023-09-14 10:06:03 +0200 | [diff] [blame] | 807 | unlink(bind->address); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 808 | free(bind->address); |
| 809 | free(opts->address); |
| 810 | |
| 811 | free(opts); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | void |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 815 | nc_server_config_del_endpt_unix_socket(struct nc_endpt *endpt, struct nc_bind *bind) |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 816 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 817 | free(endpt->name); |
| 818 | nc_server_config_del_unix_socket_opts(bind, endpt->opts.unixsock); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 819 | |
| 820 | server_opts.endpt_count--; |
| 821 | if (!server_opts.endpt_count) { |
| 822 | free(server_opts.endpts); |
| 823 | free(server_opts.binds); |
| 824 | server_opts.endpts = NULL; |
| 825 | server_opts.binds = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 826 | } else if (endpt != &server_opts.endpts[server_opts.endpt_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 827 | memcpy(endpt, &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
| 828 | memcpy(bind, &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 832 | #ifdef NC_ENABLED_SSH_TLS |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 833 | |
| 834 | static void |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 835 | nc_server_config_del_cert(struct nc_cert_grouping *certs, struct nc_certificate *cert) |
| 836 | { |
| 837 | free(cert->name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 838 | free(cert->data); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 839 | |
| 840 | certs->cert_count--; |
| 841 | if (!certs->cert_count) { |
| 842 | free(certs->certs); |
| 843 | certs->certs = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 844 | } else if (cert != &certs->certs[certs->cert_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 845 | memcpy(cert, &certs->certs[certs->cert_count], sizeof *certs->certs); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 850 | nc_server_config_del_certs(struct nc_cert_grouping *certs_grp) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 851 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 852 | uint16_t i; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 853 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 854 | if (certs_grp->store == NC_STORE_LOCAL) { |
| 855 | for (i = 0; i < certs_grp->cert_count; i++) { |
| 856 | free(certs_grp->certs[i].name); |
| 857 | free(certs_grp->certs[i].data); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 858 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 859 | free(certs_grp->certs); |
| 860 | certs_grp->certs = NULL; |
| 861 | } else { |
| 862 | free(certs_grp->ts_ref); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
| 866 | static void |
| 867 | nc_server_config_del_ctn(struct nc_server_tls_opts *opts, struct nc_ctn *ctn) |
| 868 | { |
| 869 | struct nc_ctn *iter; |
| 870 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 871 | free(ctn->name); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 872 | free(ctn->fingerprint); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 873 | |
| 874 | if (opts->ctn == ctn) { |
| 875 | /* it's the first in the list */ |
| 876 | opts->ctn = ctn->next; |
| 877 | free(ctn); |
| 878 | return; |
| 879 | } |
| 880 | |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 881 | for (iter = opts->ctn; iter; iter = iter->next) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 882 | if (iter->next == ctn) { |
| 883 | /* found the ctn */ |
| 884 | break; |
| 885 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | iter->next = ctn->next; |
| 889 | free(ctn); |
| 890 | } |
| 891 | |
| 892 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 893 | nc_server_config_del_ctns(struct nc_server_tls_opts *opts) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 894 | { |
| 895 | struct nc_ctn *cur, *next; |
| 896 | |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 897 | for (cur = opts->ctn; cur; cur = next) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 898 | next = cur->next; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 899 | free(cur->name); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 900 | free(cur->fingerprint); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 901 | free(cur); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 902 | } |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 903 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 904 | opts->ctn = NULL; |
| 905 | } |
| 906 | |
| 907 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 908 | nc_server_config_del_tls_opts(struct nc_bind *bind, struct nc_server_tls_opts *opts) |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 909 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 910 | free(bind->address); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 911 | if (bind->sock > -1) { |
| 912 | close(bind->sock); |
| 913 | } |
| 914 | |
| 915 | if (opts->store == NC_STORE_LOCAL) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 916 | free(opts->pubkey_data); |
| 917 | free(opts->privkey_data); |
| 918 | free(opts->cert_data); |
| 919 | } else { |
| 920 | free(opts->key_ref); |
| 921 | free(opts->cert_ref); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 922 | } |
| 923 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 924 | nc_server_config_del_certs(&opts->ca_certs); |
| 925 | nc_server_config_del_certs(&opts->ee_certs); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 926 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 927 | free(opts->crl_path); |
| 928 | free(opts->crl_url); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 929 | X509_STORE_free(opts->crl_store); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 930 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 931 | nc_server_config_del_ctns(opts); |
| 932 | free(opts->ciphers); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 933 | free(opts); |
| 934 | } |
| 935 | |
| 936 | static void |
| 937 | nc_server_config_del_endpt_tls(struct nc_endpt *endpt, struct nc_bind *bind) |
| 938 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 939 | free(endpt->name); |
| 940 | free(endpt->referenced_endpt_name); |
| 941 | |
| 942 | nc_server_config_del_tls_opts(bind, endpt->opts.tls); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 943 | |
| 944 | server_opts.endpt_count--; |
| 945 | if (!server_opts.endpt_count) { |
| 946 | free(server_opts.endpts); |
| 947 | free(server_opts.binds); |
| 948 | server_opts.endpts = NULL; |
| 949 | server_opts.binds = NULL; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 950 | } else if (endpt != &server_opts.endpts[server_opts.endpt_count]) { |
roman | 3398123 | 2023-07-08 11:55:13 +0200 | [diff] [blame] | 951 | memcpy(endpt, &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
| 952 | memcpy(bind, &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 956 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 957 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 958 | /* presence container */ |
| 959 | int |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 960 | nc_server_config_listen(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 961 | { |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 962 | uint16_t i, endpt_count; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 963 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 964 | (void) node; |
| 965 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 966 | assert(op == NC_OP_CREATE || op == NC_OP_DELETE); |
| 967 | |
| 968 | if (op == NC_OP_DELETE) { |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 969 | endpt_count = server_opts.endpt_count; |
| 970 | for (i = 0; i < endpt_count; i++) { |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 971 | switch (server_opts.endpts[i].ti) { |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 972 | #ifdef NC_ENABLED_SSH_TLS |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 973 | case NC_TI_LIBSSH: |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 974 | nc_server_config_del_endpt_ssh(&server_opts.endpts[i], &server_opts.binds[i]); |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 975 | break; |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 976 | case NC_TI_OPENSSL: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 977 | nc_server_config_del_endpt_tls(&server_opts.endpts[i], &server_opts.binds[i]); |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 978 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 979 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 980 | case NC_TI_UNIX: |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 981 | nc_server_config_del_endpt_unix_socket(&server_opts.endpts[i], &server_opts.binds[i]); |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 982 | break; |
| 983 | case NC_TI_NONE: |
| 984 | case NC_TI_FD: |
| 985 | ERRINT; |
| 986 | return 1; |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 987 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 994 | #ifdef NC_ENABLED_SSH_TLS |
| 995 | |
| 996 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 997 | nc_server_config_ch_del_ssh_opts(struct nc_server_ssh_opts *opts) |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 998 | { |
| 999 | uint16_t i, hostkey_count, client_count; |
| 1000 | |
| 1001 | /* store in variable because it gets decremented in the function call */ |
| 1002 | hostkey_count = opts->hostkey_count; |
| 1003 | for (i = 0; i < hostkey_count; i++) { |
| 1004 | nc_server_config_del_hostkey(opts, &opts->hostkeys[i]); |
| 1005 | } |
| 1006 | |
| 1007 | client_count = opts->client_count; |
| 1008 | for (i = 0; i < client_count; i++) { |
| 1009 | nc_server_config_del_auth_client(opts, &opts->auth_clients[i]); |
| 1010 | } |
| 1011 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1012 | free(opts->hostkey_algs); |
| 1013 | free(opts->kex_algs); |
| 1014 | free(opts->encryption_algs); |
| 1015 | free(opts->mac_algs); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1016 | |
| 1017 | free(opts); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | static void |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1021 | nc_server_config_ch_del_tls_opts(struct nc_server_tls_opts *opts) |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1022 | { |
| 1023 | if (opts->store == NC_STORE_LOCAL) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1024 | free(opts->pubkey_data); |
| 1025 | free(opts->privkey_data); |
| 1026 | free(opts->cert_data); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1029 | nc_server_config_del_certs(&opts->ca_certs); |
| 1030 | nc_server_config_del_certs(&opts->ee_certs); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1031 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1032 | free(opts->crl_path); |
| 1033 | free(opts->crl_url); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1034 | X509_STORE_free(opts->crl_store); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1035 | nc_server_config_del_ctns(opts); |
| 1036 | free(opts->ciphers); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1037 | |
| 1038 | free(opts); |
| 1039 | } |
| 1040 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1041 | #endif /* NC_ENABLED_SSH_TLS */ |
| 1042 | |
| 1043 | static void |
| 1044 | nc_server_config_ch_del_endpt(struct nc_ch_client *ch_client, struct nc_ch_endpt *ch_endpt) |
| 1045 | { |
| 1046 | free(ch_endpt->name); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1047 | |
| 1048 | #ifdef NC_ENABLED_SSH_TLS |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1049 | free(ch_endpt->address); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1050 | if (ch_endpt->sock_pending > -1) { |
| 1051 | close(ch_endpt->sock_pending); |
| 1052 | ch_endpt->sock_pending = -1; |
| 1053 | } |
| 1054 | #endif /* NC_ENABLED_SSH_TLS */ |
| 1055 | |
| 1056 | switch (ch_endpt->ti) { |
| 1057 | #ifdef NC_ENABLED_SSH_TLS |
| 1058 | case NC_TI_LIBSSH: |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1059 | nc_server_config_ch_del_ssh_opts(ch_endpt->opts.ssh); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1060 | break; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1061 | case NC_TI_OPENSSL: |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1062 | nc_server_config_ch_del_tls_opts(ch_endpt->opts.tls); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1063 | break; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1064 | #endif /* NC_ENABLED_SSH_TLS */ |
| 1065 | default: |
| 1066 | ERRINT; |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | ch_client->ch_endpt_count--; |
| 1071 | if (!ch_client->ch_endpt_count) { |
| 1072 | free(ch_client->ch_endpts); |
| 1073 | ch_client->ch_endpts = NULL; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | static void |
| 1078 | nc_server_config_ch_del_client(struct nc_ch_client *ch_client) |
| 1079 | { |
| 1080 | uint16_t i, ch_endpt_count; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1081 | struct nc_ch_client client; |
| 1082 | pthread_t tid; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1083 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1084 | /* WR LOCK */ |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1085 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 1086 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1087 | /* copy the client we want to delete into a local variable */ |
| 1088 | memcpy(&client, ch_client, sizeof *ch_client); |
| 1089 | /* get his tid */ |
| 1090 | tid = client.tid; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1091 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1092 | /* delete the client */ |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1093 | server_opts.ch_client_count--; |
| 1094 | if (!server_opts.ch_client_count) { |
| 1095 | free(server_opts.ch_clients); |
| 1096 | server_opts.ch_clients = NULL; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1097 | } else { |
| 1098 | memcpy(ch_client, &server_opts.ch_clients[server_opts.ch_client_count], sizeof *server_opts.ch_clients); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1101 | /* WR UNLOCK */ |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1102 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* RD LOCK */ |
| 1105 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 1106 | /* MUTEX LOCK */ |
| 1107 | pthread_mutex_lock(&client.lock); |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1108 | |
| 1109 | if (client.thread_data->thread_running) { |
| 1110 | /* CH COND LOCK */ |
| 1111 | pthread_mutex_lock(&client.thread_data->cond_lock); |
| 1112 | client.thread_data->thread_running = 0; |
| 1113 | pthread_cond_signal(&client.thread_data->cond); |
| 1114 | /* CH COND UNLOCK */ |
| 1115 | pthread_mutex_unlock(&client.thread_data->cond_lock); |
| 1116 | |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 1117 | /* MUTEX UNLOCK */ |
| 1118 | pthread_mutex_unlock(&client.lock); |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1119 | /* RD UNLOCK */ |
| 1120 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 1121 | |
| 1122 | /* wait for the thread to terminate */ |
| 1123 | pthread_join(tid, NULL); |
| 1124 | } |
| 1125 | |
| 1126 | /* free its members */ |
| 1127 | free(client.name); |
| 1128 | |
| 1129 | ch_endpt_count = client.ch_endpt_count; |
| 1130 | for (i = 0; i < ch_endpt_count; i++) { |
| 1131 | nc_server_config_ch_del_endpt(&client, &client.ch_endpts[i]); |
| 1132 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1135 | int |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1136 | nc_server_config_ch(const struct lyd_node *node, NC_OPERATION op) |
| 1137 | { |
| 1138 | uint16_t i, ch_client_count; |
| 1139 | |
| 1140 | (void) node; |
| 1141 | |
| 1142 | if (op == NC_OP_DELETE) { |
| 1143 | ch_client_count = server_opts.ch_client_count; |
| 1144 | for (i = 0; i < ch_client_count; i++) { |
| 1145 | nc_server_config_ch_del_client(&server_opts.ch_clients[i]); |
| 1146 | } |
| 1147 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1148 | |
| 1149 | return 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1152 | /* default leaf */ |
| 1153 | static int |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 1154 | nc_server_config_hello_timeout(const struct lyd_node *node, NC_OPERATION op) |
| 1155 | { |
| 1156 | assert(!strcmp(LYD_NAME(node), "hello-timeout")); |
| 1157 | |
| 1158 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1159 | server_opts.hello_timeout = ((struct lyd_node_term *)node)->value.uint16; |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 1160 | } else { |
| 1161 | /* default value */ |
| 1162 | server_opts.hello_timeout = 60; |
| 1163 | } |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | /* default leaf */ |
| 1169 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1170 | nc_server_config_idle_timeout(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1171 | { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1172 | struct nc_ch_client *ch_client; |
| 1173 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1174 | assert(!strcmp(LYD_NAME(node), "idle-timeout")); |
| 1175 | |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 1176 | if (is_ch(node)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1177 | /* call-home idle timeout */ |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1178 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
| 1179 | /* to avoid unlock on fail */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1184 | ch_client->idle_timeout = ((struct lyd_node_term *)node)->value.uint16; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1185 | } else if (op == NC_OP_DELETE) { |
| 1186 | ch_client->idle_timeout = 180; |
| 1187 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1188 | |
| 1189 | nc_ch_client_unlock(ch_client); |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 1190 | } else { |
| 1191 | /* whole server idle timeout */ |
| 1192 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1193 | server_opts.idle_timeout = ((struct lyd_node_term *)node)->value.uint16; |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 1194 | } else { |
| 1195 | /* default value */ |
| 1196 | server_opts.idle_timeout = 0; |
| 1197 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1204 | nc_server_config_create_bind(void) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1205 | { |
| 1206 | int ret = 0; |
| 1207 | void *tmp; |
| 1208 | |
| 1209 | tmp = realloc(server_opts.binds, (server_opts.endpt_count + 1) * sizeof *server_opts.binds); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1210 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1211 | server_opts.binds = tmp; |
| 1212 | memset(&server_opts.binds[server_opts.endpt_count], 0, sizeof *server_opts.binds); |
| 1213 | |
| 1214 | server_opts.binds[server_opts.endpt_count].sock = -1; |
| 1215 | |
| 1216 | cleanup: |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
| 1220 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1221 | nc_server_config_create_endpoint(const struct lyd_node *node) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1222 | { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1223 | if (nc_server_config_create_bind()) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1224 | return 1; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1225 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1226 | |
| 1227 | node = lyd_child(node); |
| 1228 | assert(!strcmp(LYD_NAME(node), "name")); |
| 1229 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1230 | return nc_server_config_realloc(lyd_get_value(node), (void **)&server_opts.endpts, sizeof *server_opts.endpts, &server_opts.endpt_count); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1231 | } |
| 1232 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1233 | static int |
| 1234 | nc_server_config_ch_create_endpoint(const struct lyd_node *node, struct nc_ch_client *ch_client) |
| 1235 | { |
| 1236 | node = lyd_child(node); |
| 1237 | assert(!strcmp(LYD_NAME(node), "name")); |
| 1238 | |
| 1239 | return nc_server_config_realloc(lyd_get_value(node), (void **)&ch_client->ch_endpts, sizeof *ch_client->ch_endpts, &ch_client->ch_endpt_count); |
| 1240 | } |
| 1241 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1242 | /* list */ |
| 1243 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1244 | nc_server_config_endpoint(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1245 | { |
| 1246 | int ret = 0; |
| 1247 | struct nc_endpt *endpt; |
| 1248 | struct nc_bind *bind; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1249 | struct nc_ch_endpt *ch_endpt; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1250 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1251 | |
| 1252 | assert(!strcmp(LYD_NAME(node), "endpoint")); |
| 1253 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1254 | if (is_listen(node)) { |
| 1255 | /* listen */ |
| 1256 | if (op == NC_OP_CREATE) { |
| 1257 | ret = nc_server_config_create_endpoint(node); |
| 1258 | if (ret) { |
| 1259 | goto cleanup; |
| 1260 | } |
| 1261 | } else if (op == NC_OP_DELETE) { |
| 1262 | /* free all children */ |
| 1263 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
| 1264 | ret = 1; |
| 1265 | goto cleanup; |
| 1266 | } |
| 1267 | |
| 1268 | switch (endpt->ti) { |
| 1269 | #ifdef NC_ENABLED_SSH_TLS |
| 1270 | case NC_TI_LIBSSH: |
| 1271 | nc_server_config_del_endpt_ssh(endpt, bind); |
| 1272 | break; |
| 1273 | case NC_TI_OPENSSL: |
| 1274 | nc_server_config_del_endpt_tls(endpt, bind); |
| 1275 | break; |
| 1276 | #endif /* NC_ENABLED_SSH_TLS */ |
| 1277 | case NC_TI_UNIX: |
| 1278 | nc_server_config_del_endpt_unix_socket(endpt, bind); |
| 1279 | break; |
| 1280 | case NC_TI_NONE: |
| 1281 | case NC_TI_FD: |
| 1282 | ERRINT; |
| 1283 | ret = 1; |
| 1284 | goto cleanup; |
| 1285 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1286 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1287 | } else if (is_ch(node)) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1288 | /* LOCK */ |
| 1289 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1290 | /* to avoid unlock on fail */ |
| 1291 | return 1; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1292 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1293 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1294 | if (op == NC_OP_CREATE) { |
| 1295 | ret = nc_server_config_ch_create_endpoint(node, ch_client); |
| 1296 | if (ret) { |
| 1297 | goto cleanup; |
| 1298 | } |
| 1299 | |
| 1300 | /* init ch sock */ |
| 1301 | ch_client->ch_endpts[ch_client->ch_endpt_count - 1].sock_pending = -1; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1302 | } else if (op == NC_OP_DELETE) { |
| 1303 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
| 1304 | ret = 1; |
| 1305 | goto cleanup; |
| 1306 | } |
| 1307 | |
| 1308 | nc_server_config_ch_del_endpt(ch_client, ch_endpt); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1309 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1313 | if (is_ch(node)) { |
| 1314 | /* UNLOCK */ |
| 1315 | nc_ch_client_unlock(ch_client); |
| 1316 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1317 | return ret; |
| 1318 | } |
| 1319 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1320 | #ifdef NC_ENABLED_SSH_TLS |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1321 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1322 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1323 | nc_server_config_create_ssh(struct nc_endpt *endpt) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1324 | { |
| 1325 | endpt->ti = NC_TI_LIBSSH; |
| 1326 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1327 | NC_CHECK_ERRMEM_RET(!endpt->opts.ssh, 1); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1332 | static int |
| 1333 | nc_server_config_ch_create_ssh(struct nc_ch_endpt *ch_endpt) |
| 1334 | { |
| 1335 | ch_endpt->ti = NC_TI_LIBSSH; |
| 1336 | ch_endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1337 | NC_CHECK_ERRMEM_RET(!ch_endpt->opts.ssh, 1); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1342 | /* NP container */ |
| 1343 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1344 | nc_server_config_ssh(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1345 | { |
| 1346 | struct nc_endpt *endpt; |
| 1347 | struct nc_bind *bind; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1348 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1349 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1350 | int ret = 0; |
| 1351 | |
| 1352 | assert(!strcmp(LYD_NAME(node), "ssh")); |
| 1353 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1354 | if (is_listen(node)) { |
| 1355 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
| 1356 | ret = 1; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1357 | goto cleanup; |
| 1358 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1359 | |
| 1360 | if (op == NC_OP_CREATE) { |
| 1361 | ret = nc_server_config_create_ssh(endpt); |
| 1362 | if (ret) { |
| 1363 | goto cleanup; |
| 1364 | } |
| 1365 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1366 | nc_server_config_del_ssh_opts(bind, endpt->opts.ssh); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1367 | } |
| 1368 | } else { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1369 | /* LOCK */ |
| 1370 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1371 | /* to avoid unlock on fail */ |
| 1372 | return 1; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1375 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1376 | ret = 1; |
| 1377 | goto cleanup; |
| 1378 | } |
| 1379 | |
| 1380 | if (op == NC_OP_CREATE) { |
| 1381 | ret = nc_server_config_ch_create_ssh(ch_endpt); |
| 1382 | if (ret) { |
| 1383 | goto cleanup; |
| 1384 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1385 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1386 | nc_server_config_ch_del_ssh_opts(ch_endpt->opts.ssh); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1387 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1391 | if (is_ch(node)) { |
| 1392 | /* UNLOCK */ |
| 1393 | nc_ch_client_unlock(ch_client); |
| 1394 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
| 1397 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1398 | static int |
| 1399 | nc_server_config_create_tls(struct nc_endpt *endpt) |
| 1400 | { |
| 1401 | endpt->ti = NC_TI_OPENSSL; |
| 1402 | endpt->opts.tls = calloc(1, sizeof *endpt->opts.tls); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1403 | NC_CHECK_ERRMEM_RET(!endpt->opts.tls, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | static int |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1409 | nc_server_config_ch_create_tls(struct nc_ch_endpt *ch_endpt) |
| 1410 | { |
| 1411 | ch_endpt->ti = NC_TI_OPENSSL; |
| 1412 | ch_endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1413 | NC_CHECK_ERRMEM_RET(!ch_endpt->opts.tls, 1); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1419 | nc_server_config_tls(const struct lyd_node *node, NC_OPERATION op) |
| 1420 | { |
| 1421 | struct nc_endpt *endpt; |
| 1422 | struct nc_bind *bind; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1423 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1424 | struct nc_ch_client *ch_client; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1425 | int ret = 0; |
| 1426 | |
| 1427 | assert(!strcmp(LYD_NAME(node), "tls")); |
| 1428 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1429 | if (is_listen(node)) { |
| 1430 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
| 1431 | ret = 1; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1432 | goto cleanup; |
| 1433 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1434 | |
| 1435 | if (op == NC_OP_CREATE) { |
| 1436 | ret = nc_server_config_create_tls(endpt); |
| 1437 | if (ret) { |
| 1438 | goto cleanup; |
| 1439 | } |
| 1440 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1441 | nc_server_config_del_tls_opts(bind, endpt->opts.tls); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1442 | } |
| 1443 | } else { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1444 | /* LOCK */ |
| 1445 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1446 | /* to avoid unlock on fail */ |
| 1447 | return 1; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1450 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
| 1451 | ret = 1; |
| 1452 | goto cleanup; |
| 1453 | } |
| 1454 | |
| 1455 | if (op == NC_OP_CREATE) { |
| 1456 | ret = nc_server_config_ch_create_tls(ch_endpt); |
| 1457 | if (ret) { |
| 1458 | goto cleanup; |
| 1459 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1460 | } else if (op == NC_OP_DELETE) { |
| 1461 | nc_server_config_ch_del_tls_opts(ch_endpt->opts.tls); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1462 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1466 | if (is_ch(node)) { |
| 1467 | /* UNLOCK */ |
| 1468 | nc_ch_client_unlock(ch_client); |
| 1469 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1470 | return ret; |
| 1471 | } |
| 1472 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1473 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1474 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1475 | static int |
| 1476 | nc_server_config_set_address_port(struct nc_endpt *endpt, struct nc_bind *bind, const char *address, uint16_t port) |
| 1477 | { |
| 1478 | int sock = -1, set_addr, ret = 0; |
| 1479 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 1480 | assert((address && !port) || (!address && port) || (endpt->ti == NC_TI_UNIX)); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1481 | |
| 1482 | if (address) { |
| 1483 | set_addr = 1; |
| 1484 | } else { |
| 1485 | set_addr = 0; |
| 1486 | } |
| 1487 | |
| 1488 | if (set_addr) { |
| 1489 | port = bind->port; |
| 1490 | } else { |
| 1491 | address = bind->address; |
| 1492 | } |
| 1493 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1494 | /* we have all the information we need to create a listening socket */ |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 1495 | if ((address && port) || (endpt->ti == NC_TI_UNIX)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1496 | /* create new socket, close the old one */ |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 1497 | if (endpt->ti == NC_TI_UNIX) { |
| 1498 | sock = nc_sock_listen_unix(endpt->opts.unixsock); |
| 1499 | } else { |
| 1500 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
| 1501 | } |
| 1502 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1503 | if (sock == -1) { |
| 1504 | ret = 1; |
| 1505 | goto cleanup; |
| 1506 | } |
| 1507 | |
| 1508 | if (bind->sock > -1) { |
| 1509 | close(bind->sock); |
| 1510 | } |
| 1511 | bind->sock = sock; |
| 1512 | } |
| 1513 | |
| 1514 | if (sock > -1) { |
| 1515 | switch (endpt->ti) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 1516 | case NC_TI_UNIX: |
| 1517 | VRB(NULL, "Listening on %s for UNIX connections.", endpt->opts.unixsock->address); |
| 1518 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1519 | #ifdef NC_ENABLED_SSH_TLS |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1520 | case NC_TI_LIBSSH: |
| 1521 | VRB(NULL, "Listening on %s:%u for SSH connections.", address, port); |
| 1522 | break; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1523 | case NC_TI_OPENSSL: |
| 1524 | VRB(NULL, "Listening on %s:%u for TLS connections.", address, port); |
| 1525 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1526 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1527 | default: |
| 1528 | ERRINT; |
| 1529 | ret = 1; |
| 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | cleanup: |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1538 | #ifdef NC_ENABLED_SSH_TLS |
| 1539 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1540 | /* mandatory leaf */ |
| 1541 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1542 | nc_server_config_local_address(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1543 | { |
| 1544 | struct nc_endpt *endpt; |
| 1545 | struct nc_bind *bind; |
| 1546 | int ret = 0; |
| 1547 | |
| 1548 | (void) op; |
| 1549 | |
| 1550 | assert(!strcmp(LYD_NAME(node), "local-address")); |
| 1551 | |
| 1552 | if (equal_parent_name(node, 4, "listen")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1553 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1554 | ret = 1; |
| 1555 | goto cleanup; |
| 1556 | } |
| 1557 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1558 | free(bind->address); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1559 | bind->address = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1560 | NC_CHECK_ERRMEM_GOTO(!bind->address, ret = 1, cleanup); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1561 | |
| 1562 | ret = nc_server_config_set_address_port(endpt, bind, lyd_get_value(node), 0); |
| 1563 | if (ret) { |
| 1564 | goto cleanup; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | cleanup: |
| 1569 | return ret; |
| 1570 | } |
| 1571 | |
| 1572 | /* leaf with default value */ |
| 1573 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1574 | nc_server_config_local_port(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1575 | { |
| 1576 | struct nc_endpt *endpt; |
| 1577 | struct nc_bind *bind; |
| 1578 | int ret = 0; |
| 1579 | |
| 1580 | assert(!strcmp(LYD_NAME(node), "local-port")); |
| 1581 | |
| 1582 | if (equal_parent_name(node, 4, "listen")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1583 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1584 | ret = 1; |
| 1585 | goto cleanup; |
| 1586 | } |
| 1587 | |
| 1588 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1589 | bind->port = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1590 | } else { |
| 1591 | /* delete -> set to default */ |
| 1592 | bind->port = 0; |
| 1593 | } |
| 1594 | |
| 1595 | ret = nc_server_config_set_address_port(endpt, bind, NULL, bind->port); |
| 1596 | if (ret) { |
| 1597 | goto cleanup; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | cleanup: |
| 1602 | return ret; |
| 1603 | } |
| 1604 | |
| 1605 | /* P container */ |
| 1606 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1607 | nc_server_config_keepalives(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1608 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1609 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1610 | struct nc_endpt *endpt; |
| 1611 | struct nc_bind *bind; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1612 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1613 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1614 | |
| 1615 | assert(!strcmp(LYD_NAME(node), "keepalives")); |
| 1616 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1617 | if (is_listen(node) && equal_parent_name(node, 1, "tcp-server-parameters")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1618 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1619 | ret = 1; |
| 1620 | goto cleanup; |
| 1621 | } |
| 1622 | |
| 1623 | if (op == NC_OP_CREATE) { |
| 1624 | endpt->ka.enabled = 1; |
| 1625 | } else { |
| 1626 | endpt->ka.enabled = 0; |
| 1627 | } |
| 1628 | ret = nc_sock_configure_keepalive(bind->sock, &endpt->ka); |
| 1629 | if (ret) { |
| 1630 | goto cleanup; |
| 1631 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1632 | } else if (is_ch(node) && equal_parent_name(node, 1, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1633 | /* LOCK */ |
| 1634 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1635 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |
| 1638 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1639 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1640 | ret = 1; |
| 1641 | goto cleanup; |
| 1642 | } |
| 1643 | |
| 1644 | if (op == NC_OP_CREATE) { |
| 1645 | ch_endpt->ka.enabled = 1; |
| 1646 | } else { |
| 1647 | ch_endpt->ka.enabled = 0; |
| 1648 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1652 | if (is_ch(node) && equal_parent_name(node, 1, "tcp-client-parameters")) { |
| 1653 | /* UNLOCK */ |
| 1654 | nc_ch_client_unlock(ch_client); |
| 1655 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | /* mandatory leaf */ |
| 1660 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1661 | nc_server_config_idle_time(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1662 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1663 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1664 | struct nc_endpt *endpt; |
| 1665 | struct nc_bind *bind; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1666 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1667 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1668 | |
| 1669 | assert(!strcmp(LYD_NAME(node), "idle-time")); |
| 1670 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1671 | if (is_listen(node) && equal_parent_name(node, 2, "tcp-server-parameters")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1672 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1673 | ret = 1; |
| 1674 | goto cleanup; |
| 1675 | } |
| 1676 | |
| 1677 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1678 | endpt->ka.idle_time = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1679 | } else { |
| 1680 | endpt->ka.idle_time = 0; |
| 1681 | } |
| 1682 | ret = nc_sock_configure_keepalive(bind->sock, &endpt->ka); |
| 1683 | if (ret) { |
| 1684 | goto cleanup; |
| 1685 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1686 | } else if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1687 | /* LOCK */ |
| 1688 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1689 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1690 | return 1; |
| 1691 | } |
| 1692 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1693 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1694 | ret = 1; |
| 1695 | goto cleanup; |
| 1696 | } |
| 1697 | |
| 1698 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1699 | ch_endpt->ka.idle_time = ((struct lyd_node_term *)node)->value.uint16; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1700 | } else { |
| 1701 | ch_endpt->ka.idle_time = 0; |
| 1702 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | cleanup: |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1706 | if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1707 | /* UNLOCK */ |
| 1708 | nc_ch_client_unlock(ch_client); |
| 1709 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1710 | return ret; |
| 1711 | } |
| 1712 | |
| 1713 | /* mandatory leaf */ |
| 1714 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1715 | nc_server_config_max_probes(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1716 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1717 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1718 | struct nc_endpt *endpt; |
| 1719 | struct nc_bind *bind; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1720 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1721 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1722 | |
| 1723 | assert(!strcmp(LYD_NAME(node), "max-probes")); |
| 1724 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1725 | if (is_listen(node) && equal_parent_name(node, 2, "tcp-server-parameters")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1726 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1727 | ret = 1; |
| 1728 | goto cleanup; |
| 1729 | } |
| 1730 | |
| 1731 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1732 | endpt->ka.max_probes = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1733 | } else { |
| 1734 | endpt->ka.max_probes = 0; |
| 1735 | } |
| 1736 | ret = nc_sock_configure_keepalive(bind->sock, &endpt->ka); |
| 1737 | if (ret) { |
| 1738 | goto cleanup; |
| 1739 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1740 | } else if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1741 | /* LOCK */ |
| 1742 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1743 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1744 | return 1; |
| 1745 | } |
| 1746 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1747 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1748 | ret = 1; |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | |
| 1752 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1753 | ch_endpt->ka.max_probes = ((struct lyd_node_term *)node)->value.uint16; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1754 | } else { |
| 1755 | ch_endpt->ka.max_probes = 0; |
| 1756 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | cleanup: |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1760 | if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1761 | /* UNLOCK */ |
| 1762 | nc_ch_client_unlock(ch_client); |
| 1763 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1764 | return ret; |
| 1765 | } |
| 1766 | |
| 1767 | /* mandatory leaf */ |
| 1768 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1769 | nc_server_config_probe_interval(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1770 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1771 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1772 | struct nc_endpt *endpt; |
| 1773 | struct nc_bind *bind; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1774 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1775 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1776 | |
| 1777 | assert(!strcmp(LYD_NAME(node), "probe-interval")); |
| 1778 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1779 | if (is_listen(node) && equal_parent_name(node, 2, "tcp-server-parameters")) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1780 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1781 | ret = 1; |
| 1782 | goto cleanup; |
| 1783 | } |
| 1784 | |
| 1785 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1786 | endpt->ka.probe_interval = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1787 | } else { |
| 1788 | endpt->ka.probe_interval = 0; |
| 1789 | } |
| 1790 | ret = nc_sock_configure_keepalive(bind->sock, &endpt->ka); |
| 1791 | if (ret) { |
| 1792 | goto cleanup; |
| 1793 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1794 | } else if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1795 | /* LOCK */ |
| 1796 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1797 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1798 | return 1; |
| 1799 | } |
| 1800 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1801 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1802 | ret = 1; |
| 1803 | goto cleanup; |
| 1804 | } |
| 1805 | |
| 1806 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 1807 | ch_endpt->ka.probe_interval = ((struct lyd_node_term *)node)->value.uint16; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1808 | } else { |
| 1809 | ch_endpt->ka.max_probes = 0; |
| 1810 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | cleanup: |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1814 | if (is_ch(node) && equal_parent_name(node, 2, "tcp-client-parameters")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1815 | /* UNLOCK */ |
| 1816 | nc_ch_client_unlock(ch_client); |
| 1817 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1818 | return ret; |
| 1819 | } |
| 1820 | |
| 1821 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1822 | nc_server_config_create_host_key(const struct lyd_node *node, struct nc_server_ssh_opts *opts) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1823 | { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1824 | node = lyd_child(node); |
| 1825 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1826 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1827 | return nc_server_config_realloc(lyd_get_value(node), (void **)&opts->hostkeys, sizeof *opts->hostkeys, &opts->hostkey_count); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /* list */ |
| 1831 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1832 | nc_server_config_host_key(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1833 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1834 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1835 | struct nc_hostkey *hostkey; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1836 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1837 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1838 | |
| 1839 | assert(!strcmp(LYD_NAME(node), "host-key")); |
| 1840 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1841 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 1842 | ret = 1; |
| 1843 | goto cleanup; |
| 1844 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1845 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1846 | if (equal_parent_name(node, 1, "server-identity")) { |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1847 | /* LOCK */ |
| 1848 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1849 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1850 | return 1; |
| 1851 | } |
| 1852 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1853 | if (op == NC_OP_CREATE) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1854 | ret = nc_server_config_create_host_key(node, opts); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1855 | if (ret) { |
| 1856 | goto cleanup; |
| 1857 | } |
| 1858 | } else if (op == NC_OP_DELETE) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1859 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1860 | ret = 1; |
| 1861 | goto cleanup; |
| 1862 | } |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1863 | nc_server_config_del_hostkey(opts, hostkey); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1864 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1868 | if (is_ch(node) && equal_parent_name(node, 1, "server-identity")) { |
| 1869 | /* UNLOCK */ |
| 1870 | nc_ch_client_unlock(ch_client); |
| 1871 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | /* mandatory leaf */ |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1876 | static int |
| 1877 | nc_server_config_public_key_format(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1878 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1879 | int ret = 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1880 | const char *format; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1881 | NC_PUBKEY_FORMAT pubkey_type; |
roman | 8edee34 | 2023-03-31 13:25:48 +0200 | [diff] [blame] | 1882 | struct nc_public_key *pubkey; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1883 | struct nc_hostkey *hostkey; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1884 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1885 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1886 | |
| 1887 | assert(!strcmp(LYD_NAME(node), "public-key-format")); |
| 1888 | |
| 1889 | format = ((struct lyd_node_term *)node)->value.ident->name; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1890 | if (!strcmp(format, "ssh-public-key-format")) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 1891 | pubkey_type = NC_PUBKEY_FORMAT_SSH; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1892 | } else if (!strcmp(format, "subject-public-key-info-format")) { |
| 1893 | pubkey_type = NC_PUBKEY_FORMAT_X509; |
| 1894 | } else { |
| 1895 | ERR(NULL, "Public key format (%s) not supported.", format); |
| 1896 | ret = 1; |
| 1897 | goto cleanup; |
| 1898 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1899 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1900 | /* LOCK */ |
| 1901 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1902 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1903 | return 1; |
| 1904 | } |
| 1905 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1906 | if (is_ssh(node) && equal_parent_name(node, 4, "server-identity")) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1907 | /* SSH hostkey public key fmt */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1908 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1909 | ret = 1; |
| 1910 | goto cleanup; |
| 1911 | } |
| 1912 | |
| 1913 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 1914 | hostkey->key.pubkey_type = pubkey_type; |
| 1915 | } |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1916 | } else if (is_ssh(node) && equal_parent_name(node, 6, "client-authentication")) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1917 | /* SSH client auth public key fmt */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1918 | if (nc_server_config_get_pubkey(node, &pubkey)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1919 | ret = 1; |
| 1920 | goto cleanup; |
| 1921 | } |
| 1922 | |
| 1923 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1924 | pubkey->type = pubkey_type; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1925 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1926 | } else if (is_tls(node) && equal_parent_name(node, 3, "server-identity")) { |
| 1927 | /* TLS server-identity */ |
| 1928 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 1929 | ret = 1; |
| 1930 | goto cleanup; |
| 1931 | } |
| 1932 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 1933 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1934 | opts->pubkey_type = pubkey_type; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1935 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1939 | if (is_ch(node)) { |
| 1940 | /* UNLOCK */ |
| 1941 | nc_ch_client_unlock(ch_client); |
| 1942 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1943 | return ret; |
| 1944 | } |
| 1945 | |
| 1946 | static int |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 1947 | nc_server_config_create_auth_key_public_key_list(const struct lyd_node *node, struct nc_auth_client *auth_client) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1948 | { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1949 | assert(!strcmp(LYD_NAME(node), "public-key")); |
| 1950 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1951 | node = lyd_child(node); |
| 1952 | assert(!strcmp(LYD_NAME(node), "name")); |
| 1953 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 1954 | return nc_server_config_realloc(lyd_get_value(node), (void **)&auth_client->pubkeys, sizeof *auth_client->pubkeys, &auth_client->pubkey_count); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1958 | nc_server_config_replace_auth_key_public_key_leaf(const struct lyd_node *node, struct nc_public_key *pubkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1959 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1960 | free(pubkey->data); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1961 | pubkey->data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1962 | NC_CHECK_ERRMEM_RET(!pubkey->data, 1); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1963 | |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 1968 | nc_server_config_replace_host_key_public_key(const struct lyd_node *node, struct nc_hostkey *hostkey) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1969 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1970 | free(hostkey->key.pubkey_data); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1971 | hostkey->key.pubkey_data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1972 | NC_CHECK_ERRMEM_RET(!hostkey->key.pubkey_data, 1); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1973 | |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1977 | static int |
| 1978 | nc_server_config_tls_replace_server_public_key(const struct lyd_node *node, struct nc_server_tls_opts *opts) |
| 1979 | { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 1980 | free(opts->pubkey_data); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1981 | opts->pubkey_data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1982 | NC_CHECK_ERRMEM_RET(!opts->pubkey_data, 1); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1987 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 1988 | nc_server_config_public_key(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1989 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 1990 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1991 | struct nc_hostkey *hostkey; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 1992 | struct nc_auth_client *auth_client; |
roman | 8edee34 | 2023-03-31 13:25:48 +0200 | [diff] [blame] | 1993 | struct nc_public_key *pubkey; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 1994 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1995 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1996 | |
| 1997 | assert(!strcmp(LYD_NAME(node), "public-key")); |
| 1998 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 1999 | /* LOCK */ |
| 2000 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2001 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2002 | return 1; |
| 2003 | } |
| 2004 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2005 | if (is_ssh(node) && equal_parent_name(node, 3, "host-key")) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2006 | /* server's public-key, mandatory leaf */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2007 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2008 | ret = 1; |
| 2009 | goto cleanup; |
| 2010 | } |
| 2011 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2012 | /* the public key must not be SubjectPublicKeyInfoFormat, as per the ietf-netconf-server model */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 2013 | if (nc_is_pk_subject_public_key_info(lyd_get_value(node))) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2014 | ERR(NULL, "Using Public Key in the SubjectPublicKeyInfo format as an SSH hostkey is forbidden!"); |
| 2015 | ret = 1; |
| 2016 | goto cleanup; |
| 2017 | } |
| 2018 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2019 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2020 | /* set to local */ |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2021 | hostkey->store = NC_STORE_LOCAL; |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2022 | |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2023 | ret = nc_server_config_replace_host_key_public_key(node, hostkey); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2024 | if (ret) { |
| 2025 | goto cleanup; |
| 2026 | } |
| 2027 | } |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2028 | } else if (is_ssh(node) && equal_parent_name(node, 5, "client-authentication")) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2029 | /* client auth pubkeys, list */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2030 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2031 | ret = 1; |
| 2032 | goto cleanup; |
| 2033 | } |
| 2034 | |
| 2035 | if (op == NC_OP_CREATE) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2036 | /* set to local */ |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2037 | auth_client->store = NC_STORE_LOCAL; |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2038 | |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2039 | ret = nc_server_config_create_auth_key_public_key_list(node, auth_client); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2040 | if (ret) { |
| 2041 | goto cleanup; |
| 2042 | } |
| 2043 | } else if (op == NC_OP_DELETE) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2044 | if (nc_server_config_get_pubkey(node, &pubkey)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2045 | ret = 1; |
| 2046 | goto cleanup; |
| 2047 | } |
| 2048 | |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2049 | nc_server_config_del_auth_client_pubkey(auth_client, pubkey); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2050 | } |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2051 | } else if (is_ssh(node) && equal_parent_name(node, 6, "client-authentication")) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2052 | /* client auth pubkey, leaf */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2053 | if (nc_server_config_get_pubkey(node, &pubkey)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2054 | ret = 1; |
| 2055 | goto cleanup; |
| 2056 | } |
| 2057 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2058 | /* the public key must not be SubjectPublicKeyInfoFormat, as per the ietf-netconf-server model */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 2059 | if (nc_is_pk_subject_public_key_info(lyd_get_value(node))) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2060 | ERR(NULL, "Using Public Key in the SubjectPublicKeyInfo format as an SSH user's key is forbidden!"); |
| 2061 | ret = 1; |
| 2062 | goto cleanup; |
| 2063 | } |
| 2064 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2065 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2066 | ret = nc_server_config_replace_auth_key_public_key_leaf(node, pubkey); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2067 | if (ret) { |
| 2068 | goto cleanup; |
| 2069 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2070 | } else if (op == NC_OP_DELETE) { |
| 2071 | free(pubkey->data); |
| 2072 | pubkey->data = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2073 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2074 | } else if (is_tls(node) && equal_parent_name(node, 3, "server-identity")) { |
| 2075 | /* TLS server-identity */ |
| 2076 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2077 | ret = 1; |
| 2078 | goto cleanup; |
| 2079 | } |
| 2080 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2081 | /* the public key must be SubjectPublicKeyInfoFormat, as per the ietf-netconf-server model */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 2082 | if (!nc_is_pk_subject_public_key_info(lyd_get_value(node))) { |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 2083 | ERR(NULL, "TLS server certificate's Public Key must be in the SubjectPublicKeyInfo format!"); |
| 2084 | ret = 1; |
| 2085 | goto cleanup; |
| 2086 | } |
| 2087 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2088 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 2089 | /* set to local */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2090 | opts->store = NC_STORE_LOCAL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2091 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2092 | ret = nc_server_config_tls_replace_server_public_key(node, opts); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2093 | if (ret) { |
| 2094 | goto cleanup; |
| 2095 | } |
| 2096 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2100 | if (is_ch(node)) { |
| 2101 | /* UNLOCK */ |
| 2102 | nc_ch_client_unlock(ch_client); |
| 2103 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2104 | return ret; |
| 2105 | } |
| 2106 | |
| 2107 | /* leaf */ |
| 2108 | static int |
| 2109 | nc_server_config_private_key_format(const struct lyd_node *node, NC_OPERATION op) |
| 2110 | { |
| 2111 | int ret = 0; |
| 2112 | const char *format; |
| 2113 | NC_PRIVKEY_FORMAT privkey_type; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2114 | struct nc_hostkey *hostkey; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2115 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2116 | struct nc_ch_client *ch_client; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2117 | |
| 2118 | (void) op; |
| 2119 | |
| 2120 | assert(!strcmp(LYD_NAME(node), "private-key-format")); |
| 2121 | |
| 2122 | format = ((struct lyd_node_term *)node)->value.ident->name; |
| 2123 | if (!format) { |
| 2124 | ret = 1; |
| 2125 | goto cleanup; |
| 2126 | } |
| 2127 | |
| 2128 | privkey_type = nc_server_config_get_private_key_type(format); |
| 2129 | if (privkey_type == NC_PRIVKEY_FORMAT_UNKNOWN) { |
| 2130 | ERR(NULL, "Unknown private key format."); |
| 2131 | ret = 1; |
| 2132 | goto cleanup; |
| 2133 | } |
| 2134 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2135 | /* LOCK */ |
| 2136 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2137 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2138 | return 1; |
| 2139 | } |
| 2140 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2141 | if (is_ssh(node)) { |
| 2142 | /* ssh */ |
| 2143 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2144 | ret = 1; |
| 2145 | goto cleanup; |
| 2146 | } |
| 2147 | |
| 2148 | hostkey->key.privkey_type = privkey_type; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2149 | } else if (is_tls(node)) { |
| 2150 | /* tls */ |
| 2151 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2152 | ret = 1; |
| 2153 | goto cleanup; |
| 2154 | } |
| 2155 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2156 | opts->privkey_type = privkey_type; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2160 | if (is_ch(node)) { |
| 2161 | /* UNLOCK */ |
| 2162 | nc_ch_client_unlock(ch_client); |
| 2163 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2164 | return ret; |
| 2165 | } |
| 2166 | |
| 2167 | static int |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2168 | nc_server_config_cleartext_private_key(const struct lyd_node *node, NC_OPERATION op) |
| 2169 | { |
| 2170 | int ret = 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2171 | struct nc_hostkey *hostkey; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2172 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2173 | struct nc_ch_client *ch_client; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2174 | |
| 2175 | assert(!strcmp(LYD_NAME(node), "cleartext-private-key")); |
| 2176 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2177 | /* LOCK */ |
| 2178 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2179 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2180 | return 1; |
| 2181 | } |
| 2182 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2183 | if (is_ssh(node)) { |
| 2184 | /* ssh */ |
| 2185 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2186 | ret = 1; |
| 2187 | goto cleanup; |
| 2188 | } |
| 2189 | |
| 2190 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2191 | free(hostkey->key.privkey_data); |
| 2192 | hostkey->key.privkey_data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2193 | NC_CHECK_ERRMEM_GOTO(!hostkey->key.privkey_data, ret = 1, cleanup); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2194 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2195 | free(hostkey->key.privkey_data); |
| 2196 | hostkey->key.privkey_data = NULL; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2197 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 2198 | } else if (is_tls(node)) { |
| 2199 | /* tls */ |
| 2200 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2201 | ret = 1; |
| 2202 | goto cleanup; |
| 2203 | } |
| 2204 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2205 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2206 | free(opts->privkey_data); |
| 2207 | opts->privkey_data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2208 | NC_CHECK_ERRMEM_GOTO(!opts->privkey_data, ret = 1, cleanup); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2209 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2210 | free(opts->privkey_data); |
| 2211 | opts->privkey_data = NULL; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2212 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2216 | if (is_ch(node)) { |
| 2217 | /* UNLOCK */ |
| 2218 | nc_ch_client_unlock(ch_client); |
| 2219 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2220 | return ret; |
| 2221 | } |
| 2222 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2223 | /* leaf */ |
| 2224 | static int |
| 2225 | nc_server_config_keystore_reference(const struct lyd_node *node, NC_OPERATION op) |
| 2226 | { |
| 2227 | int ret = 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2228 | struct nc_hostkey *hostkey; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2229 | struct nc_ch_client *ch_client; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2230 | |
| 2231 | assert(!strcmp(LYD_NAME(node), "keystore-reference")); |
| 2232 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2233 | /* LOCK */ |
| 2234 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2235 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2236 | return 1; |
| 2237 | } |
| 2238 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2239 | if (is_ssh(node) && equal_parent_name(node, 3, "server-identity")) { |
| 2240 | if (nc_server_config_get_hostkey(node, &hostkey)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2241 | ret = 1; |
| 2242 | goto cleanup; |
| 2243 | } |
| 2244 | |
| 2245 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 2246 | /* set to keystore */ |
| 2247 | hostkey->store = NC_STORE_KEYSTORE; |
| 2248 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2249 | free(hostkey->ks_ref); |
roman | dd019a9 | 2023-09-14 10:17:07 +0200 | [diff] [blame] | 2250 | hostkey->ks_ref = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2251 | NC_CHECK_ERRMEM_GOTO(!hostkey->ks_ref, ret = 1, cleanup); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2252 | } else if (op == NC_OP_DELETE) { |
| 2253 | free(hostkey->ks_ref); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2254 | hostkey->ks_ref = NULL; |
| 2255 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2256 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2257 | |
| 2258 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2259 | if (is_ch(node)) { |
| 2260 | /* UNLOCK */ |
| 2261 | nc_ch_client_unlock(ch_client); |
| 2262 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2263 | return ret; |
| 2264 | } |
| 2265 | |
| 2266 | static int |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2267 | nc_server_config_create_auth_client(const struct lyd_node *node, struct nc_server_ssh_opts *opts) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2268 | { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2269 | node = lyd_child(node); |
| 2270 | assert(!strcmp(LYD_NAME(node), "name")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2271 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2272 | return nc_server_config_realloc(lyd_get_value(node), (void **)&opts->auth_clients, sizeof *opts->auth_clients, &opts->client_count); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /* list */ |
| 2276 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2277 | nc_server_config_user(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2278 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2279 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2280 | struct nc_auth_client *auth_client; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2281 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2282 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2283 | |
| 2284 | assert(!strcmp(LYD_NAME(node), "user")); |
| 2285 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2286 | /* LOCK */ |
| 2287 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2288 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2289 | return 1; |
| 2290 | } |
| 2291 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2292 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2293 | ret = 1; |
| 2294 | goto cleanup; |
| 2295 | } |
| 2296 | |
| 2297 | if (op == NC_OP_CREATE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2298 | ret = nc_server_config_create_auth_client(node, opts); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2299 | if (ret) { |
| 2300 | goto cleanup; |
| 2301 | } |
| 2302 | } else if (op == NC_OP_DELETE) { |
| 2303 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2304 | ret = 1; |
| 2305 | goto cleanup; |
| 2306 | } |
| 2307 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2308 | nc_server_config_del_auth_client(opts, auth_client); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2312 | if (is_ch(node)) { |
| 2313 | /* UNLOCK */ |
| 2314 | nc_ch_client_unlock(ch_client); |
| 2315 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2316 | return ret; |
| 2317 | } |
| 2318 | |
| 2319 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2320 | nc_server_config_auth_attempts(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2321 | { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2322 | int ret = 0; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2323 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2324 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2325 | |
| 2326 | assert(!strcmp(LYD_NAME(node), "auth-attempts")); |
| 2327 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2328 | /* LOCK */ |
| 2329 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2330 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2331 | return 1; |
| 2332 | } |
| 2333 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2334 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2335 | ret = 1; |
| 2336 | goto cleanup; |
| 2337 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2338 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2339 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 2340 | opts->auth_attempts = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2344 | if (is_ch(node)) { |
| 2345 | /* UNLOCK */ |
| 2346 | nc_ch_client_unlock(ch_client); |
| 2347 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2348 | return ret; |
| 2349 | } |
| 2350 | |
| 2351 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2352 | nc_server_config_auth_timeout(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2353 | { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2354 | int ret = 0; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2355 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2356 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2357 | |
| 2358 | assert(!strcmp(LYD_NAME(node), "auth-timeout")); |
| 2359 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2360 | /* LOCK */ |
| 2361 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2362 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2363 | return 1; |
| 2364 | } |
| 2365 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2366 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2367 | ret = 1; |
| 2368 | goto cleanup; |
| 2369 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2370 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2371 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 2372 | opts->auth_timeout = ((struct lyd_node_term *)node)->value.uint16; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2376 | if (is_ch(node)) { |
| 2377 | /* UNLOCK */ |
| 2378 | nc_ch_client_unlock(ch_client); |
| 2379 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2380 | return ret; |
| 2381 | } |
| 2382 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2383 | /* leaf */ |
| 2384 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2385 | nc_server_config_truststore_reference(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2386 | { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2387 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2388 | struct nc_auth_client *auth_client; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2389 | struct nc_ch_client *ch_client; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2390 | struct nc_server_tls_opts *opts; |
| 2391 | struct nc_cert_grouping *certs_grp; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2392 | |
| 2393 | assert(!strcmp(LYD_NAME(node), "truststore-reference")); |
| 2394 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2395 | /* LOCK */ |
| 2396 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2397 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2398 | return 1; |
| 2399 | } |
| 2400 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2401 | if (is_ssh(node) && equal_parent_name(node, 1, "public-keys")) { |
| 2402 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2403 | ret = 1; |
| 2404 | goto cleanup; |
| 2405 | } |
| 2406 | |
| 2407 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2408 | /* set to truststore */ |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2409 | auth_client->store = NC_STORE_TRUSTSTORE; |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2410 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2411 | free(auth_client->ts_ref); |
| 2412 | auth_client->ts_ref = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2413 | NC_CHECK_ERRMEM_GOTO(!auth_client->ts_ref, ret = 1, cleanup); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2414 | } else if (op == NC_OP_DELETE) { |
| 2415 | free(auth_client->ts_ref); |
roman | d57b372 | 2023-04-05 11:26:25 +0200 | [diff] [blame] | 2416 | auth_client->ts_ref = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2417 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2418 | } else if (is_tls(node) && (equal_parent_name(node, 1, "ca-certs") || equal_parent_name(node, 1, "ee-certs"))) { |
| 2419 | /* ee-certs or ca-certs */ |
| 2420 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2421 | ret = 1; |
| 2422 | goto cleanup; |
| 2423 | } |
| 2424 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2425 | if (equal_parent_name(node, 1, "ca-certs")) { |
| 2426 | certs_grp = &opts->ca_certs; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2427 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2428 | certs_grp = &opts->ee_certs; |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2431 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 2432 | /* set to truststore */ |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2433 | certs_grp->store = NC_STORE_TRUSTSTORE; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2434 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2435 | free(certs_grp->ts_ref); |
| 2436 | certs_grp->ts_ref = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2437 | NC_CHECK_ERRMEM_GOTO(!certs_grp->ts_ref, ret = 1, cleanup); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2438 | } else if (op == NC_OP_DELETE) { |
| 2439 | free(certs_grp->ts_ref); |
| 2440 | certs_grp->ts_ref = NULL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2441 | } |
| 2442 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2443 | |
| 2444 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2445 | if (is_ch(node)) { |
| 2446 | /* UNLOCK */ |
| 2447 | nc_ch_client_unlock(ch_client); |
| 2448 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2449 | return ret; |
| 2450 | } |
| 2451 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2452 | /* leaf */ |
| 2453 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2454 | nc_server_config_password(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2455 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2456 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2457 | struct nc_auth_client *auth_client; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2458 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2459 | |
| 2460 | assert(!strcmp(LYD_NAME(node), "password")); |
| 2461 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2462 | /* LOCK */ |
| 2463 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2464 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2465 | return 1; |
| 2466 | } |
| 2467 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2468 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
| 2469 | ret = 1; |
| 2470 | goto cleanup; |
| 2471 | } |
| 2472 | |
| 2473 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2474 | free(auth_client->password); |
| 2475 | auth_client->password = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2476 | NC_CHECK_ERRMEM_GOTO(!auth_client->password, ret = 1, cleanup); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2477 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2478 | free(auth_client->password); |
| 2479 | auth_client->password = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2483 | if (is_ch(node)) { |
| 2484 | /* UNLOCK */ |
| 2485 | nc_ch_client_unlock(ch_client); |
| 2486 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2487 | return ret; |
| 2488 | } |
| 2489 | |
| 2490 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2491 | nc_server_config_pam_name(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2492 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2493 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2494 | struct nc_auth_client *auth_client; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2495 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2496 | |
| 2497 | assert(!strcmp(LYD_NAME(node), "pam-config-file-name")); |
| 2498 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2499 | /* LOCK */ |
| 2500 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2501 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2502 | return 1; |
| 2503 | } |
| 2504 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2505 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
| 2506 | ret = 1; |
| 2507 | goto cleanup; |
| 2508 | } |
| 2509 | |
| 2510 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2511 | free(auth_client->pam_config_name); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2512 | auth_client->pam_config_name = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2513 | NC_CHECK_ERRMEM_GOTO(!auth_client->pam_config_name, ret = 1, cleanup); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2514 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2515 | free(auth_client->pam_config_name); |
| 2516 | auth_client->pam_config_name = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2520 | if (is_ch(node)) { |
| 2521 | /* UNLOCK */ |
| 2522 | nc_ch_client_unlock(ch_client); |
| 2523 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2524 | return ret; |
| 2525 | } |
| 2526 | |
| 2527 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2528 | nc_server_config_pam_dir(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2529 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2530 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2531 | struct nc_auth_client *auth_client; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2532 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2533 | |
| 2534 | assert(!strcmp(LYD_NAME(node), "pam-config-file-dir")); |
| 2535 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2536 | /* LOCK */ |
| 2537 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2538 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2539 | return 1; |
| 2540 | } |
| 2541 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2542 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
| 2543 | ret = 1; |
| 2544 | goto cleanup; |
| 2545 | } |
| 2546 | |
| 2547 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2548 | free(auth_client->pam_config_dir); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2549 | auth_client->pam_config_dir = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2550 | NC_CHECK_ERRMEM_GOTO(!auth_client->pam_config_dir, ret = 1, cleanup); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2551 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2552 | free(auth_client->pam_config_dir); |
| 2553 | auth_client->pam_config_dir = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2557 | if (is_ch(node)) { |
| 2558 | /* UNLOCK */ |
| 2559 | nc_ch_client_unlock(ch_client); |
| 2560 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2561 | return ret; |
| 2562 | } |
| 2563 | |
| 2564 | /* leaf */ |
| 2565 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2566 | nc_server_config_none(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2567 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2568 | int ret = 0; |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2569 | struct nc_auth_client *auth_client; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2570 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2571 | |
| 2572 | assert(!strcmp(LYD_NAME(node), "none")); |
| 2573 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2574 | /* LOCK */ |
| 2575 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 58f79d0 | 2023-10-06 10:20:31 +0200 | [diff] [blame] | 2576 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2577 | return 1; |
| 2578 | } |
| 2579 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2580 | if (nc_server_config_get_auth_client(node, &auth_client)) { |
| 2581 | ret = 1; |
| 2582 | goto cleanup; |
| 2583 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2584 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2585 | if (op == NC_OP_CREATE) { |
| 2586 | auth_client->supports_none = 1; |
| 2587 | } else { |
| 2588 | auth_client->supports_none = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2592 | if (is_ch(node)) { |
| 2593 | /* UNLOCK */ |
| 2594 | nc_ch_client_unlock(ch_client); |
| 2595 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2596 | return ret; |
| 2597 | } |
| 2598 | |
| 2599 | static int |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 2600 | nc_server_config_delete_substring(const char *haystack, const char *needle, const char delim) |
| 2601 | { |
| 2602 | size_t needle_len = strlen(needle); |
| 2603 | char *substr; |
| 2604 | int substr_found = 0, ret = 0; |
| 2605 | |
| 2606 | while ((substr = strstr(haystack, needle))) { |
| 2607 | /* iterate over all the substrings */ |
| 2608 | if (((substr == haystack) && (*(substr + needle_len) == delim)) || |
| 2609 | ((substr != haystack) && (*(substr - 1) == delim) && (*(substr + needle_len) == delim))) { |
| 2610 | /* either the first element of the string or somewhere in the middle */ |
| 2611 | memmove(substr, substr + needle_len + 1, strlen(substr + needle_len + 1)); |
| 2612 | substr_found = 1; |
| 2613 | break; |
| 2614 | } else if ((*(substr - 1) == delim) && (*(substr + needle_len) == '\0')) { |
| 2615 | /* the last element of the string */ |
| 2616 | *(substr - 1) = '\0'; |
| 2617 | substr_found = 1; |
| 2618 | break; |
| 2619 | } |
| 2620 | haystack = substr + 1; |
| 2621 | } |
| 2622 | if (!substr_found) { |
| 2623 | ret = 1; |
| 2624 | } |
| 2625 | |
| 2626 | return ret; |
| 2627 | } |
| 2628 | |
| 2629 | static int |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2630 | nc_server_config_transport_params(const char *algorithm, char **alg_store, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2631 | { |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 2632 | int ret = 0; |
| 2633 | char *alg = NULL; |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2634 | |
| 2635 | if (!strncmp(algorithm, "openssh-", 8)) { |
| 2636 | /* if the name starts with openssh, convert it to it's original libssh accepted form */ |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2637 | ret = asprintf(&alg, "%s@openssh.com", algorithm + 8); |
| 2638 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; alg = NULL, cleanup); |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2639 | } else if (!strncmp(algorithm, "libssh-", 7)) { |
| 2640 | /* if the name starts with libssh, convert it to it's original libssh accepted form */ |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2641 | ret = asprintf(&alg, "%s@libssh.org", algorithm + 7); |
| 2642 | NC_CHECK_ERRMEM_GOTO(ret == -1, ret = 1; alg = NULL, cleanup); |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2643 | } else { |
| 2644 | alg = strdup(algorithm); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2645 | NC_CHECK_ERRMEM_GOTO(!alg, ret = 1, cleanup); |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2646 | } |
| 2647 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2648 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 2649 | if (!*alg_store) { |
| 2650 | /* first call */ |
| 2651 | *alg_store = strdup(alg); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2652 | NC_CHECK_ERRMEM_GOTO(!*alg_store, ret = 1, cleanup); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2653 | } else { |
| 2654 | /* +1 because of ',' between algorithms */ |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2655 | *alg_store = nc_realloc(*alg_store, strlen(*alg_store) + strlen(alg) + 1 + 1); |
| 2656 | NC_CHECK_ERRMEM_GOTO(!*alg_store, ret = 1, cleanup); |
roman | 08f67f4 | 2023-06-08 13:51:54 +0200 | [diff] [blame] | 2657 | strcat(*alg_store, ","); |
| 2658 | strcat(*alg_store, alg); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2659 | } |
| 2660 | } else { |
| 2661 | /* delete */ |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 2662 | ret = nc_server_config_delete_substring(*alg_store, alg, ','); |
| 2663 | if (ret) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2664 | ERR(NULL, "Unable to delete an algorithm (%s), which was not previously added.", alg); |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 2665 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | cleanup: |
roman | a6bf6ab | 2023-05-26 13:26:02 +0200 | [diff] [blame] | 2670 | free(alg); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2671 | return ret; |
| 2672 | } |
| 2673 | |
| 2674 | /* leaf-list */ |
| 2675 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2676 | nc_server_config_host_key_alg(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2677 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2678 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2679 | const char *alg; |
| 2680 | uint8_t i; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2681 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2682 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2683 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2684 | assert(!strcmp(LYD_NAME(node), "host-key-alg")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2685 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2686 | /* LOCK */ |
| 2687 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2688 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2689 | return 1; |
| 2690 | } |
| 2691 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2692 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2693 | ret = 1; |
| 2694 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2697 | /* get the algorithm name and compare it with algs supported by libssh */ |
| 2698 | alg = ((struct lyd_node_term *)node)->value.ident->name; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2699 | i = 0; |
| 2700 | while (supported_hostkey_algs[i]) { |
| 2701 | if (!strcmp(supported_hostkey_algs[i], alg)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2702 | if (nc_server_config_transport_params(alg, &opts->hostkey_algs, op)) { |
| 2703 | ret = 1; |
| 2704 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2705 | } |
| 2706 | break; |
| 2707 | } |
| 2708 | i++; |
| 2709 | } |
| 2710 | if (!supported_hostkey_algs[i]) { |
| 2711 | /* algorithm not supported */ |
| 2712 | ERR(NULL, "Public key algorithm (%s) not supported by libssh.", alg); |
| 2713 | ret = 1; |
| 2714 | } |
| 2715 | |
| 2716 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2717 | if (is_ch(node)) { |
| 2718 | /* UNLOCK */ |
| 2719 | nc_ch_client_unlock(ch_client); |
| 2720 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2721 | return ret; |
| 2722 | } |
| 2723 | |
| 2724 | /* leaf-list */ |
| 2725 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2726 | nc_server_config_kex_alg(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2727 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2728 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2729 | const char *alg; |
| 2730 | uint8_t i; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2731 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2732 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2733 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2734 | assert(!strcmp(LYD_NAME(node), "key-exchange-alg")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2735 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2736 | /* LOCK */ |
| 2737 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2738 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2739 | return 1; |
| 2740 | } |
| 2741 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2742 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2743 | ret = 1; |
| 2744 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2747 | /* get the algorithm name and compare it with algs supported by libssh */ |
| 2748 | alg = ((struct lyd_node_term *)node)->value.ident->name; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2749 | i = 0; |
| 2750 | while (supported_kex_algs[i]) { |
| 2751 | if (!strcmp(supported_kex_algs[i], alg)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2752 | if (nc_server_config_transport_params(alg, &opts->kex_algs, op)) { |
| 2753 | ret = 1; |
| 2754 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2755 | } |
| 2756 | break; |
| 2757 | } |
| 2758 | i++; |
| 2759 | } |
| 2760 | if (!supported_kex_algs[i]) { |
| 2761 | /* algorithm not supported */ |
| 2762 | ERR(NULL, "Key exchange algorithm (%s) not supported by libssh.", alg); |
| 2763 | ret = 1; |
| 2764 | } |
| 2765 | |
| 2766 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2767 | if (is_ch(node)) { |
| 2768 | /* UNLOCK */ |
| 2769 | nc_ch_client_unlock(ch_client); |
| 2770 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2771 | return ret; |
| 2772 | } |
| 2773 | |
| 2774 | /* leaf-list */ |
| 2775 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2776 | nc_server_config_encryption_alg(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2777 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2778 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2779 | const char *alg; |
| 2780 | uint8_t i; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2781 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2782 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2783 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2784 | assert(!strcmp(LYD_NAME(node), "encryption-alg")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2785 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2786 | /* LOCK */ |
| 2787 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2788 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2789 | return 1; |
| 2790 | } |
| 2791 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2792 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2793 | ret = 1; |
| 2794 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2795 | } |
| 2796 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2797 | /* get the algorithm name and compare it with algs supported by libssh */ |
| 2798 | alg = ((struct lyd_node_term *)node)->value.ident->name; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2799 | i = 0; |
| 2800 | while (supported_encryption_algs[i]) { |
| 2801 | if (!strcmp(supported_encryption_algs[i], alg)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2802 | if (nc_server_config_transport_params(alg, &opts->encryption_algs, op)) { |
| 2803 | ret = 1; |
| 2804 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2805 | } |
| 2806 | break; |
| 2807 | } |
| 2808 | i++; |
| 2809 | } |
| 2810 | if (!supported_encryption_algs[i]) { |
| 2811 | /* algorithm not supported */ |
| 2812 | ERR(NULL, "Encryption algorithm (%s) not supported by libssh.", alg); |
| 2813 | ret = 1; |
| 2814 | } |
| 2815 | |
| 2816 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2817 | if (is_ch(node)) { |
| 2818 | /* UNLOCK */ |
| 2819 | nc_ch_client_unlock(ch_client); |
| 2820 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | /* leaf-list */ |
| 2825 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2826 | nc_server_config_mac_alg(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2827 | { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2828 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2829 | const char *alg; |
| 2830 | uint8_t i; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2831 | struct nc_server_ssh_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2832 | struct nc_ch_client *ch_client; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2833 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2834 | assert(!strcmp(LYD_NAME(node), "mac-alg")); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2835 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2836 | /* LOCK */ |
| 2837 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2838 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2839 | return 1; |
| 2840 | } |
| 2841 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 2842 | if (nc_server_config_get_ssh_opts(node, &opts)) { |
| 2843 | ret = 1; |
| 2844 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2847 | /* get the algorithm name and compare it with algs supported by libssh */ |
| 2848 | alg = ((struct lyd_node_term *)node)->value.ident->name; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2849 | i = 0; |
| 2850 | while (supported_mac_algs[i]) { |
| 2851 | if (!strcmp(supported_mac_algs[i], alg)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 2852 | if (nc_server_config_transport_params(alg, &opts->mac_algs, op)) { |
| 2853 | ret = 1; |
| 2854 | goto cleanup; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2855 | } |
| 2856 | break; |
| 2857 | } |
| 2858 | i++; |
| 2859 | } |
| 2860 | if (!supported_mac_algs[i]) { |
| 2861 | /* algorithm not supported */ |
| 2862 | ERR(NULL, "MAC algorithm (%s) not supported by libssh.", alg); |
| 2863 | ret = 1; |
| 2864 | } |
| 2865 | |
| 2866 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 2867 | if (is_ch(node)) { |
| 2868 | /* UNLOCK */ |
| 2869 | nc_ch_client_unlock(ch_client); |
| 2870 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2871 | return ret; |
| 2872 | } |
| 2873 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 2874 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2875 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 2876 | static int |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2877 | nc_server_config_create_unix_socket(struct nc_endpt *endpt) |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2878 | { |
| 2879 | endpt->ti = NC_TI_UNIX; |
| 2880 | endpt->opts.unixsock = calloc(1, sizeof *endpt->opts.unixsock); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2881 | NC_CHECK_ERRMEM_RET(!endpt->opts.unixsock, 1); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2882 | |
| 2883 | /* set default values */ |
| 2884 | endpt->opts.unixsock->mode = -1; |
| 2885 | endpt->opts.unixsock->uid = -1; |
| 2886 | endpt->opts.unixsock->gid = -1; |
| 2887 | |
| 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | static int |
roman | e028ef9 | 2023-02-24 16:33:08 +0100 | [diff] [blame] | 2892 | nc_server_config_unix_socket(const struct lyd_node *node, NC_OPERATION op) |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2893 | { |
| 2894 | int ret = 0; |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 2895 | uint32_t log_options = 0; |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2896 | struct nc_endpt *endpt; |
| 2897 | struct nc_bind *bind; |
| 2898 | struct nc_server_unix_opts *opts; |
| 2899 | struct lyd_node *data = NULL; |
| 2900 | |
| 2901 | assert(!strcmp(LYD_NAME(node), "unix-socket")); |
| 2902 | |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 2903 | if (nc_server_config_get_endpt(node, &endpt, &bind)) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2904 | ret = 1; |
| 2905 | goto cleanup; |
| 2906 | } |
| 2907 | |
| 2908 | if (op == NC_OP_CREATE) { |
roman | 874fed1 | 2023-05-25 10:20:01 +0200 | [diff] [blame] | 2909 | if (nc_server_config_create_unix_socket(endpt)) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2910 | ret = 1; |
| 2911 | goto cleanup; |
| 2912 | } |
| 2913 | |
| 2914 | opts = endpt->opts.unixsock; |
| 2915 | |
| 2916 | lyd_find_path(node, "path", 0, &data); |
| 2917 | assert(data); |
| 2918 | |
| 2919 | opts->address = strdup(lyd_get_value(data)); |
| 2920 | bind->address = strdup(lyd_get_value(data)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 2921 | NC_CHECK_ERRMEM_GOTO(!opts->address || !bind->address, ret = 1, cleanup); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2922 | |
| 2923 | /* silently search for non-mandatory parameters */ |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 2924 | ly_temp_log_options(&log_options); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2925 | ret = lyd_find_path(node, "mode", 0, &data); |
| 2926 | if (!ret) { |
| 2927 | opts->mode = strtol(lyd_get_value(data), NULL, 8); |
| 2928 | } |
| 2929 | |
| 2930 | ret = lyd_find_path(node, "uid", 0, &data); |
| 2931 | if (!ret) { |
| 2932 | opts->uid = strtol(lyd_get_value(data), NULL, 10); |
| 2933 | } |
| 2934 | |
| 2935 | ret = lyd_find_path(node, "gid", 0, &data); |
| 2936 | if (!ret) { |
| 2937 | opts->gid = strtol(lyd_get_value(data), NULL, 10); |
| 2938 | } |
| 2939 | |
| 2940 | /* reset the logging options */ |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 2941 | ly_temp_log_options(NULL); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2942 | |
| 2943 | ret = nc_server_config_set_address_port(endpt, bind, NULL, 0); |
| 2944 | if (ret) { |
| 2945 | goto cleanup; |
| 2946 | } |
| 2947 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2948 | nc_server_config_del_unix_socket_opts(bind, endpt->opts.unixsock); |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | cleanup: |
| 2952 | return ret; |
| 2953 | } |
| 2954 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 2955 | #ifdef NC_ENABLED_SSH_TLS |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 2956 | |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2957 | /** |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2958 | * @brief Set all endpoint client auth references, which couldn't be set while parsing data. |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2959 | * |
| 2960 | * @return 0 on success, 1 on error. |
| 2961 | */ |
| 2962 | static int |
| 2963 | nc_server_config_fill_endpt_client_auth(void) |
| 2964 | { |
| 2965 | uint16_t i, j; |
| 2966 | |
| 2967 | for (i = 0; i < server_opts.endpt_count; i++) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2968 | /* go through all the endpoints */ |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2969 | if (server_opts.endpts[i].referenced_endpt_name) { |
| 2970 | /* endpt has a reference, that hasn't been set yet */ |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2971 | for (j = 0; j < server_opts.endpt_count; j++) { |
| 2972 | /* go through all the endpts */ |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2973 | if (!strcmp(server_opts.endpts[i].referenced_endpt_name, server_opts.endpts[j].name)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 2974 | /* found the endpoint we were looking for, |
| 2975 | * assign the server opts from the referenced endpt */ |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2976 | if (server_opts.endpts[i].ti == NC_TI_LIBSSH) { |
| 2977 | server_opts.endpts[i].opts.ssh->endpt_client_ref = &server_opts.endpts[j]; |
| 2978 | break; |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 2979 | } else if (server_opts.endpts[i].ti == NC_TI_OPENSSL) { |
| 2980 | server_opts.endpts[i].opts.tls->endpt_client_ref = &server_opts.endpts[j]; |
| 2981 | break; |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 2982 | } else { |
| 2983 | ERRINT; |
| 2984 | return 1; |
| 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | /* didn't find the endpoint */ |
| 2990 | if (j == server_opts.endpt_count) { |
| 2991 | ERR(NULL, "Endpoint \"%s\" referenced by \"%s\" not found.", |
| 2992 | server_opts.endpts[i].referenced_endpt_name, server_opts.endpts[i].name); |
| 2993 | return 1; |
| 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | return 0; |
| 2999 | } |
| 3000 | |
| 3001 | static int |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3002 | nc_server_config_endpoint_client_auth_has_cycle(struct nc_endpt *original, struct nc_endpt *next) |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3003 | { |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3004 | if (original->ti == NC_TI_LIBSSH) { |
| 3005 | if (!next->opts.ssh->endpt_client_ref) { |
| 3006 | /* no further reference -> no cycle */ |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3007 | return 0; |
| 3008 | } |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3009 | |
| 3010 | if (next->opts.ssh->endpt_client_ref == original) { |
| 3011 | /* found cycle */ |
| 3012 | return 1; |
| 3013 | } else { |
| 3014 | /* continue further */ |
| 3015 | return nc_server_config_endpoint_client_auth_has_cycle(original, next->opts.ssh->endpt_client_ref); |
| 3016 | } |
| 3017 | } else if (original->ti == NC_TI_OPENSSL) { |
| 3018 | if (!next->opts.tls->endpt_client_ref) { |
| 3019 | /* no further reference -> no cycle */ |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | if (next->opts.tls->endpt_client_ref == original) { |
| 3024 | /* found cycle */ |
| 3025 | return 1; |
| 3026 | } else { |
| 3027 | /* continue further */ |
| 3028 | return nc_server_config_endpoint_client_auth_has_cycle(original, next->opts.tls->endpt_client_ref); |
| 3029 | } |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3030 | } else { |
| 3031 | ERRINT; |
| 3032 | return 1; |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | static int |
| 3037 | nc_server_config_endpoint_client_auth(const struct lyd_node *node, NC_OPERATION op) |
| 3038 | { |
| 3039 | int ret = 0; |
| 3040 | uint16_t i; |
| 3041 | const char *endpt_name; |
| 3042 | struct nc_endpt *endpt; |
| 3043 | |
| 3044 | assert(!strcmp(LYD_NAME(node), "endpoint-client-auth")); |
| 3045 | |
| 3046 | /* get current endpoint */ |
| 3047 | ret = nc_server_config_get_endpt(node, &endpt, NULL); |
| 3048 | if (ret) { |
| 3049 | goto cleanup; |
| 3050 | } |
| 3051 | |
| 3052 | if (op == NC_OP_DELETE) { |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3053 | if (is_ssh(node)) { |
| 3054 | endpt->opts.ssh->endpt_client_ref = NULL; |
| 3055 | } else { |
| 3056 | endpt->opts.tls->endpt_client_ref = NULL; |
| 3057 | } |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3058 | goto cleanup; |
| 3059 | } |
| 3060 | |
| 3061 | /* find the endpoint leafref is referring to */ |
| 3062 | endpt_name = lyd_get_value(node); |
| 3063 | for (i = 0; i < server_opts.endpt_count; i++) { |
| 3064 | if (!strcmp(endpt_name, server_opts.endpts[i].name)) { |
| 3065 | break; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | if (i == server_opts.endpt_count) { |
| 3070 | /* endpt not found, save the name and try to look it up later */ |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3071 | free(endpt->referenced_endpt_name); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3072 | endpt->referenced_endpt_name = strdup(endpt_name); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3073 | NC_CHECK_ERRMEM_GOTO(!endpt->referenced_endpt_name, ret = 1, cleanup); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3074 | goto cleanup; |
| 3075 | } |
| 3076 | |
| 3077 | /* check for self reference */ |
| 3078 | if (endpt == &server_opts.endpts[i]) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3079 | ERR(NULL, "Self endpoint reference detected for endpoint \"%s\".", endpt->name); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3080 | ret = 1; |
| 3081 | goto cleanup; |
| 3082 | } |
| 3083 | |
| 3084 | /* check for cyclic references */ |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3085 | ret = nc_server_config_endpoint_client_auth_has_cycle(endpt, &server_opts.endpts[i]); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3086 | if (ret) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3087 | ERR(NULL, "Cyclic endpoint reference detected for endpoint \"%s\".", endpt->name); |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3088 | goto cleanup; |
| 3089 | } |
| 3090 | |
| 3091 | /* assign the current endpt the referrenced endpt */ |
roman | 2e797ef | 2023-06-19 10:47:49 +0200 | [diff] [blame] | 3092 | if (is_ssh(node)) { |
| 3093 | endpt->opts.ssh->endpt_client_ref = &server_opts.endpts[i]; |
| 3094 | } else { |
| 3095 | endpt->opts.tls->endpt_client_ref = &server_opts.endpts[i]; |
| 3096 | } |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 3097 | |
| 3098 | cleanup: |
| 3099 | return ret; |
| 3100 | } |
| 3101 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3102 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3103 | nc_server_config_cert_data(const struct lyd_node *node, NC_OPERATION op) |
| 3104 | { |
| 3105 | int ret = 0; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3106 | struct nc_certificate *cert; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3107 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3108 | struct nc_ch_client *ch_client; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3109 | |
| 3110 | assert(!strcmp(LYD_NAME(node), "cert-data")); |
| 3111 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3112 | /* LOCK */ |
| 3113 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3114 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3115 | return 1; |
| 3116 | } |
| 3117 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3118 | if (equal_parent_name(node, 3, "server-identity")) { |
| 3119 | if (nc_server_config_get_tls_opts(node, &opts)) { |
| 3120 | ret = 1; |
| 3121 | goto cleanup; |
| 3122 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3123 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3124 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3125 | free(opts->cert_data); |
| 3126 | opts->cert_data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3127 | NC_CHECK_ERRMEM_GOTO(!opts->cert_data, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3128 | } |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3129 | } else if (equal_parent_name(node, 3, "ca-certs") || equal_parent_name(node, 3, "ee-certs")) { |
| 3130 | if (nc_server_config_get_cert(node, &cert)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3131 | ret = 1; |
| 3132 | goto cleanup; |
| 3133 | } |
| 3134 | |
| 3135 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3136 | free(cert->data); |
| 3137 | cert->data = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3138 | NC_CHECK_ERRMEM_GOTO(!cert->data, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3139 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3140 | free(cert->data); |
| 3141 | cert->data = NULL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3146 | if (is_ch(node)) { |
| 3147 | /* UNLOCK */ |
| 3148 | nc_ch_client_unlock(ch_client); |
| 3149 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3150 | return ret; |
| 3151 | } |
| 3152 | |
| 3153 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3154 | nc_server_config_asymmetric_key(const struct lyd_node *node, NC_OPERATION op) |
| 3155 | { |
| 3156 | int ret = 0; |
| 3157 | struct nc_endpt *endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3158 | struct nc_ch_client *ch_client; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3159 | |
| 3160 | assert(!strcmp(LYD_NAME(node), "asymmetric-key")); |
| 3161 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3162 | /* LOCK */ |
| 3163 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3164 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3168 | if (nc_server_config_get_endpt(node, &endpt, NULL)) { |
| 3169 | ret = 1; |
| 3170 | goto cleanup; |
| 3171 | } |
| 3172 | |
| 3173 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 3174 | /* set to keystore */ |
| 3175 | endpt->opts.tls->store = NC_STORE_KEYSTORE; |
| 3176 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3177 | free(endpt->opts.tls->key_ref); |
| 3178 | endpt->opts.tls->key_ref = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3179 | NC_CHECK_ERRMEM_GOTO(!endpt->opts.tls->key_ref, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3180 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3181 | free(endpt->opts.tls->key_ref); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3182 | endpt->opts.tls->key_ref = NULL; |
| 3183 | } |
| 3184 | |
| 3185 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3186 | if (is_ch(node)) { |
| 3187 | /* UNLOCK */ |
| 3188 | nc_ch_client_unlock(ch_client); |
| 3189 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3190 | return ret; |
| 3191 | } |
| 3192 | |
| 3193 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3194 | nc_server_config_create_ca_certs_certificate(const struct lyd_node *node, struct nc_server_tls_opts *opts) |
| 3195 | { |
| 3196 | assert(!strcmp(LYD_NAME(node), "certificate")); |
| 3197 | |
| 3198 | node = lyd_child(node); |
| 3199 | assert(!strcmp(LYD_NAME(node), "name")); |
| 3200 | |
| 3201 | return nc_server_config_realloc(lyd_get_value(node), (void **)&opts->ca_certs.certs, sizeof *opts->ca_certs.certs, &opts->ca_certs.cert_count); |
| 3202 | } |
| 3203 | |
| 3204 | static int |
| 3205 | nc_server_config_create_ee_certs_certificate(const struct lyd_node *node, struct nc_server_tls_opts *opts) |
| 3206 | { |
| 3207 | assert(!strcmp(LYD_NAME(node), "certificate")); |
| 3208 | |
| 3209 | node = lyd_child(node); |
| 3210 | assert(!strcmp(LYD_NAME(node), "name")); |
| 3211 | |
| 3212 | return nc_server_config_realloc(lyd_get_value(node), (void **)&opts->ee_certs.certs, sizeof *opts->ee_certs.certs, &opts->ee_certs.cert_count); |
| 3213 | } |
| 3214 | |
| 3215 | static int |
| 3216 | nc_server_config_certificate(const struct lyd_node *node, NC_OPERATION op) |
| 3217 | { |
| 3218 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3219 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3220 | struct nc_ch_client *ch_client; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3221 | struct nc_certificate *cert; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3222 | |
| 3223 | assert(!strcmp(LYD_NAME(node), "certificate")); |
| 3224 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3225 | /* LOCK */ |
| 3226 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3227 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3228 | return 1; |
| 3229 | } |
| 3230 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3231 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3232 | ret = 1; |
| 3233 | goto cleanup; |
| 3234 | } |
| 3235 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3236 | if (equal_parent_name(node, 1, "keystore-reference")) { |
| 3237 | /* TLS server-identity */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3238 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
| 3239 | /* set to keystore */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3240 | opts->store = NC_STORE_KEYSTORE; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3241 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3242 | free(opts->cert_ref); |
| 3243 | opts->cert_ref = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3244 | NC_CHECK_ERRMEM_GOTO(!opts->cert_ref, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3245 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3246 | free(opts->cert_ref); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3247 | opts->cert_ref = NULL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3248 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3249 | } else if (equal_parent_name(node, 2, "ca-certs")) { |
| 3250 | /* TLS client auth certificate authority */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3251 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3252 | ret = nc_server_config_create_ca_certs_certificate(node, opts); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3253 | if (ret) { |
| 3254 | goto cleanup; |
| 3255 | } |
| 3256 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3257 | if (nc_server_config_get_cert(node, &cert)) { |
| 3258 | ret = 1; |
| 3259 | goto cleanup; |
| 3260 | } |
| 3261 | nc_server_config_del_cert(&opts->ca_certs, cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3262 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3263 | } else if (equal_parent_name(node, 2, "ee-certs")) { |
| 3264 | /* TLS client auth end entity */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3265 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3266 | ret = nc_server_config_create_ee_certs_certificate(node, opts); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3267 | if (ret) { |
| 3268 | goto cleanup; |
| 3269 | } |
| 3270 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3271 | if (nc_server_config_get_cert(node, &cert)) { |
| 3272 | ret = 1; |
| 3273 | goto cleanup; |
| 3274 | } |
| 3275 | nc_server_config_del_cert(&opts->ee_certs, cert); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3280 | if (is_ch(node)) { |
| 3281 | /* UNLOCK */ |
| 3282 | nc_ch_client_unlock(ch_client); |
| 3283 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3284 | return ret; |
| 3285 | } |
| 3286 | |
| 3287 | static int |
| 3288 | nc_server_config_create_cert_to_name(const struct lyd_node *node, struct nc_server_tls_opts *opts) |
| 3289 | { |
| 3290 | int ret = 0; |
| 3291 | struct lyd_node *n; |
| 3292 | struct nc_ctn *new, *iter; |
| 3293 | const char *map_type, *name; |
| 3294 | uint32_t id; |
| 3295 | NC_TLS_CTN_MAPTYPE m_type; |
| 3296 | |
| 3297 | assert(!strcmp(LYD_NAME(node), "cert-to-name")); |
| 3298 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3299 | /* get all the data */ |
| 3300 | /* find the list's key */ |
| 3301 | lyd_find_path(node, "id", 0, &n); |
| 3302 | assert(n); |
roman | 9cdb7b5 | 2023-10-25 13:59:55 +0200 | [diff] [blame] | 3303 | id = ((struct lyd_node_term *)n)->value.uint32; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3304 | |
| 3305 | /* find the ctn's name */ |
| 3306 | lyd_find_path(node, "name", 0, &n); |
| 3307 | assert(n); |
| 3308 | name = lyd_get_value(n); |
| 3309 | |
| 3310 | /* find the ctn's map-type */ |
| 3311 | lyd_find_path(node, "map-type", 0, &n); |
| 3312 | assert(n); |
| 3313 | map_type = ((struct lyd_node_term *)n)->value.ident->name; |
| 3314 | if (!strcmp(map_type, "specified")) { |
| 3315 | m_type = NC_TLS_CTN_SPECIFIED; |
| 3316 | } else if (!strcmp(map_type, "san-rfc822-name")) { |
| 3317 | m_type = NC_TLS_CTN_SAN_RFC822_NAME; |
| 3318 | } else if (!strcmp(map_type, "san-dns-name")) { |
| 3319 | m_type = NC_TLS_CTN_SAN_DNS_NAME; |
| 3320 | } else if (!strcmp(map_type, "san-ip-address")) { |
| 3321 | m_type = NC_TLS_CTN_SAN_IP_ADDRESS; |
| 3322 | } else if (!strcmp(map_type, "san-any")) { |
| 3323 | m_type = NC_TLS_CTN_SAN_ANY; |
| 3324 | } else if (!strcmp(map_type, "common-name")) { |
| 3325 | m_type = NC_TLS_CTN_COMMON_NAME; |
| 3326 | } else { |
| 3327 | ERR(NULL, "Map-type identity \"%s\" not supported.", map_type); |
| 3328 | ret = 1; |
| 3329 | goto cleanup; |
| 3330 | } |
| 3331 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3332 | /* create new ctn */ |
| 3333 | new = calloc(1, sizeof *new); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3334 | NC_CHECK_ERRMEM_GOTO(!new, ret = 1, cleanup); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3335 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3336 | /* find the right place for insertion */ |
| 3337 | if (!opts->ctn) { |
| 3338 | /* inserting the first one */ |
| 3339 | opts->ctn = new; |
Roytak | 421eb0c | 2023-08-01 22:18:27 +0200 | [diff] [blame] | 3340 | } else if (opts->ctn->id > id) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3341 | /* insert at the beginning */ |
| 3342 | new->next = opts->ctn; |
| 3343 | opts->ctn = new; |
| 3344 | } else { |
| 3345 | /* have to find the right place */ |
Roytak | 421eb0c | 2023-08-01 22:18:27 +0200 | [diff] [blame] | 3346 | for (iter = opts->ctn; iter->next && iter->next->id <= id; iter = iter->next) {} |
| 3347 | if (iter->id == id) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3348 | /* collision */ |
| 3349 | new = iter; |
| 3350 | } else { |
| 3351 | new->next = iter->next; |
| 3352 | iter->next = new; |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | /* insert the right data */ |
| 3357 | new->id = id; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3358 | free(new->name); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3359 | new->name = strdup(name); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3360 | NC_CHECK_ERRMEM_GOTO(!new->name, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3361 | new->map_type = m_type; |
| 3362 | |
| 3363 | cleanup: |
| 3364 | return ret; |
| 3365 | } |
| 3366 | |
| 3367 | static int |
| 3368 | nc_server_config_cert_to_name(const struct lyd_node *node, NC_OPERATION op) |
| 3369 | { |
| 3370 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3371 | struct nc_server_tls_opts *opts; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3372 | struct nc_ctn *ctn; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3373 | struct nc_ch_client *ch_client; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3374 | |
| 3375 | assert(!strcmp(LYD_NAME(node), "cert-to-name")); |
| 3376 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3377 | /* LOCK */ |
| 3378 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3379 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3380 | return 1; |
| 3381 | } |
| 3382 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3383 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3384 | ret = 1; |
| 3385 | goto cleanup; |
| 3386 | } |
| 3387 | |
| 3388 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3389 | ret = nc_server_config_create_cert_to_name(node, opts); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3390 | if (ret) { |
| 3391 | goto cleanup; |
| 3392 | } |
| 3393 | } else { |
| 3394 | /* find the given ctn entry */ |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 3395 | if (nc_server_config_get_ctn(node, &ctn)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3396 | ret = 1; |
| 3397 | goto cleanup; |
| 3398 | } |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3399 | nc_server_config_del_ctn(opts, ctn); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3403 | if (is_ch(node)) { |
| 3404 | /* UNLOCK */ |
| 3405 | nc_ch_client_unlock(ch_client); |
| 3406 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3407 | return ret; |
| 3408 | } |
| 3409 | |
| 3410 | static int |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3411 | nc_server_config_fingerprint(const struct lyd_node *node, NC_OPERATION op) |
| 3412 | { |
| 3413 | int ret = 0; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3414 | struct nc_ctn *ctn; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3415 | struct nc_ch_client *ch_client; |
| 3416 | |
| 3417 | assert(!strcmp(LYD_NAME(node), "fingerprint")); |
| 3418 | |
| 3419 | /* LOCK */ |
| 3420 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3421 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3422 | return 1; |
| 3423 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3424 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 3425 | if (nc_server_config_get_ctn(node, &ctn)) { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3426 | ret = 1; |
| 3427 | goto cleanup; |
| 3428 | } |
| 3429 | |
| 3430 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3431 | free(ctn->fingerprint); |
| 3432 | ctn->fingerprint = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3433 | NC_CHECK_ERRMEM_GOTO(!ctn->fingerprint, ret = 1, cleanup); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3434 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3435 | free(ctn->fingerprint); |
| 3436 | ctn->fingerprint = NULL; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3440 | if (is_ch(node)) { |
| 3441 | /* UNLOCK */ |
| 3442 | nc_ch_client_unlock(ch_client); |
| 3443 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3444 | return ret; |
| 3445 | } |
| 3446 | |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3447 | static int |
| 3448 | nc_server_config_tls_version(const struct lyd_node *node, NC_OPERATION op) |
| 3449 | { |
| 3450 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3451 | struct nc_server_tls_opts *opts; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3452 | const char *version = NULL; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3453 | struct nc_ch_client *ch_client; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3454 | NC_TLS_VERSION tls_version; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3455 | |
| 3456 | assert(!strcmp(LYD_NAME(node), "tls-version")); |
| 3457 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3458 | /* LOCK */ |
| 3459 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3460 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3461 | return 1; |
| 3462 | } |
| 3463 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3464 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3465 | ret = 1; |
| 3466 | goto cleanup; |
| 3467 | } |
| 3468 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3469 | /* str to tls_version */ |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3470 | version = ((struct lyd_node_term *)node)->value.ident->name; |
| 3471 | if (!strcmp(version, "tls10")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3472 | tls_version = NC_TLS_VERSION_10; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3473 | } else if (!strcmp(version, "tls11")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3474 | tls_version = NC_TLS_VERSION_11; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3475 | } else if (!strcmp(version, "tls12")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3476 | tls_version = NC_TLS_VERSION_12; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3477 | } else if (!strcmp(version, "tls13")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3478 | tls_version = NC_TLS_VERSION_13; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3479 | } else { |
| 3480 | ERR(NULL, "TLS version \"%s\" not supported.", version); |
| 3481 | ret = 1; |
| 3482 | goto cleanup; |
| 3483 | } |
| 3484 | |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3485 | if (op == NC_OP_CREATE) { |
| 3486 | /* add the version if it isn't there already */ |
| 3487 | opts->tls_versions |= tls_version; |
| 3488 | } else if ((op == NC_OP_DELETE) && (opts->tls_versions & tls_version)) { |
| 3489 | /* delete the version if it is there */ |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 3490 | opts->tls_versions &= ~tls_version; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3493 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3494 | if (is_ch(node)) { |
| 3495 | /* UNLOCK */ |
| 3496 | nc_ch_client_unlock(ch_client); |
| 3497 | } |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3498 | return ret; |
| 3499 | } |
| 3500 | |
| 3501 | static int |
| 3502 | nc_server_config_create_cipher_suite(struct nc_server_tls_opts *opts, const char *cipher) |
| 3503 | { |
| 3504 | int ret = 0; |
| 3505 | char *ssl_cipher = NULL; |
| 3506 | uint16_t i; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3507 | void *tmp; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3508 | |
| 3509 | ssl_cipher = malloc(strlen(cipher) + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3510 | NC_CHECK_ERRMEM_GOTO(!ssl_cipher, ret = 1, cleanup); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3511 | |
| 3512 | for (i = 0; cipher[i]; i++) { |
| 3513 | if (cipher[i] == '-') { |
| 3514 | /* OpenSSL requires _ instead of - in cipher names */ |
| 3515 | ssl_cipher[i] = '_'; |
| 3516 | } else { |
| 3517 | /* and requires uppercase unlike the identities */ |
| 3518 | ssl_cipher[i] = toupper(cipher[i]); |
| 3519 | } |
| 3520 | } |
| 3521 | ssl_cipher[i] = '\0'; |
| 3522 | |
| 3523 | if (!opts->ciphers) { |
| 3524 | /* first entry */ |
| 3525 | opts->ciphers = strdup(ssl_cipher); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3526 | NC_CHECK_ERRMEM_GOTO(!opts->ciphers, ret = 1, cleanup); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3527 | } else { |
| 3528 | /* + 1 because of : between entries */ |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3529 | tmp = nc_realloc(opts->ciphers, strlen(opts->ciphers) + strlen(ssl_cipher) + 1 + 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3530 | NC_CHECK_ERRMEM_GOTO(!tmp, ret = 1, cleanup); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3531 | opts->ciphers = tmp; |
roman | 08f67f4 | 2023-06-08 13:51:54 +0200 | [diff] [blame] | 3532 | strcat(opts->ciphers, ":"); |
| 3533 | strcat(opts->ciphers, ssl_cipher); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | cleanup: |
| 3537 | free(ssl_cipher); |
| 3538 | return ret; |
| 3539 | } |
| 3540 | |
| 3541 | static int |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3542 | nc_server_config_del_cipher_suite(struct nc_server_tls_opts *opts, const char *cipher) |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3543 | { |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 3544 | int ret = 0; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3545 | |
roman | c135c6d | 2023-10-25 13:32:30 +0200 | [diff] [blame] | 3546 | ret = nc_server_config_delete_substring(opts->ciphers, cipher, ':'); |
| 3547 | if (ret) { |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3548 | ERR(NULL, "Unable to delete a cipher (%s), which was not previously added.", cipher); |
| 3549 | return 1; |
| 3550 | } |
| 3551 | |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | static int |
| 3556 | nc_server_config_cipher_suite(const struct lyd_node *node, NC_OPERATION op) |
| 3557 | { |
| 3558 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3559 | struct nc_server_tls_opts *opts; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3560 | const char *cipher = NULL; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3561 | struct nc_ch_client *ch_client; |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3562 | |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3563 | assert(!strcmp(LYD_NAME(node), "cipher-suite")); |
| 3564 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3565 | /* LOCK */ |
| 3566 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3567 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3568 | return 1; |
| 3569 | } |
| 3570 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3571 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3572 | ret = 1; |
| 3573 | goto cleanup; |
| 3574 | } |
| 3575 | |
| 3576 | cipher = ((struct lyd_node_term *)node)->value.ident->name; |
| 3577 | if (op == NC_OP_CREATE) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3578 | ret = nc_server_config_create_cipher_suite(opts, cipher); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3579 | if (ret) { |
| 3580 | goto cleanup; |
| 3581 | } |
| 3582 | } else if (op == NC_OP_DELETE) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3583 | ret = nc_server_config_del_cipher_suite(opts, cipher); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3584 | if (ret) { |
| 3585 | goto cleanup; |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3590 | if (is_ch(node)) { |
| 3591 | /* UNLOCK */ |
| 3592 | nc_ch_client_unlock(ch_client); |
| 3593 | } |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 3594 | return ret; |
| 3595 | } |
| 3596 | |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3597 | static int |
| 3598 | nc_server_config_crl_url(const struct lyd_node *node, NC_OPERATION op) |
| 3599 | { |
| 3600 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3601 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3602 | struct nc_ch_client *ch_client; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3603 | |
| 3604 | assert(!strcmp(LYD_NAME(node), "crl-url")); |
| 3605 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3606 | /* LOCK */ |
| 3607 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3608 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3609 | return 1; |
| 3610 | } |
| 3611 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3612 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3613 | ret = 1; |
| 3614 | goto cleanup; |
| 3615 | } |
| 3616 | |
| 3617 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3618 | free(opts->crl_url); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3619 | opts->crl_url = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3620 | NC_CHECK_ERRMEM_GOTO(!opts->crl_url, ret = 1, cleanup); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3621 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3622 | free(opts->crl_url); |
| 3623 | opts->crl_url = NULL; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3627 | if (is_ch(node)) { |
| 3628 | /* UNLOCK */ |
| 3629 | nc_ch_client_unlock(ch_client); |
| 3630 | } |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3631 | return ret; |
| 3632 | } |
| 3633 | |
| 3634 | static int |
| 3635 | nc_server_config_crl_path(const struct lyd_node *node, NC_OPERATION op) |
| 3636 | { |
| 3637 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3638 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3639 | struct nc_ch_client *ch_client; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3640 | |
| 3641 | assert(!strcmp(LYD_NAME(node), "crl-path")); |
| 3642 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3643 | /* LOCK */ |
| 3644 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3645 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3646 | return 1; |
| 3647 | } |
| 3648 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3649 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3650 | ret = 1; |
| 3651 | goto cleanup; |
| 3652 | } |
| 3653 | |
| 3654 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3655 | free(opts->crl_path); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3656 | opts->crl_path = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3657 | NC_CHECK_ERRMEM_GOTO(!opts->crl_path, ret = 1, cleanup); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3658 | } else if (op == NC_OP_DELETE) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3659 | free(opts->crl_path); |
| 3660 | opts->crl_path = NULL; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3664 | if (is_ch(node)) { |
| 3665 | /* UNLOCK */ |
| 3666 | nc_ch_client_unlock(ch_client); |
| 3667 | } |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3668 | return ret; |
| 3669 | } |
| 3670 | |
| 3671 | static int |
| 3672 | nc_server_config_crl_cert_ext(const struct lyd_node *node, NC_OPERATION op) |
| 3673 | { |
| 3674 | int ret = 0; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3675 | struct nc_server_tls_opts *opts; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3676 | struct nc_ch_client *ch_client; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3677 | |
| 3678 | assert(!strcmp(LYD_NAME(node), "crl-cert-ext")); |
| 3679 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3680 | /* LOCK */ |
| 3681 | if (is_ch(node) && nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3682 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3683 | return 1; |
| 3684 | } |
| 3685 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3686 | if (nc_server_config_get_tls_opts(node, &opts)) { |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3687 | ret = 1; |
| 3688 | goto cleanup; |
| 3689 | } |
| 3690 | |
| 3691 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3692 | opts->crl_cert_ext = 1; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3693 | } else if (op == NC_OP_DELETE) { |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3694 | opts->crl_cert_ext = 0; |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3698 | if (is_ch(node)) { |
| 3699 | /* UNLOCK */ |
| 3700 | nc_ch_client_unlock(ch_client); |
| 3701 | } |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 3702 | return ret; |
| 3703 | } |
| 3704 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 3705 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 3706 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 3707 | static int |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3708 | nc_server_config_create_netconf_client(const struct lyd_node *node) |
| 3709 | { |
| 3710 | int ret = 0; |
| 3711 | |
| 3712 | node = lyd_child(node); |
| 3713 | assert(!strcmp(LYD_NAME(node), "name")); |
| 3714 | |
| 3715 | /* LOCK */ |
| 3716 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 3717 | |
| 3718 | ret = nc_server_config_realloc(lyd_get_value(node), (void **)&server_opts.ch_clients, sizeof *server_opts.ch_clients, &server_opts.ch_client_count); |
| 3719 | if (ret) { |
| 3720 | goto cleanup; |
| 3721 | } |
| 3722 | |
| 3723 | server_opts.ch_clients[server_opts.ch_client_count - 1].id = ATOMIC_INC_RELAXED(server_opts.new_client_id); |
| 3724 | server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED; |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3725 | server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3726 | |
| 3727 | pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL); |
| 3728 | |
| 3729 | cleanup: |
| 3730 | /* UNLOCK */ |
| 3731 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 3732 | return ret; |
| 3733 | } |
| 3734 | |
| 3735 | static int |
| 3736 | nc_server_config_netconf_client(const struct lyd_node *node, NC_OPERATION op) |
| 3737 | { |
| 3738 | int ret = 0; |
| 3739 | struct nc_ch_client *ch_client; |
| 3740 | |
| 3741 | assert(!strcmp(LYD_NAME(node), "netconf-client")); |
| 3742 | |
| 3743 | if (op == NC_OP_CREATE) { |
| 3744 | ret = nc_server_config_create_netconf_client(node); |
| 3745 | if (ret) { |
| 3746 | goto cleanup; |
| 3747 | } |
roman | 450c00b | 2023-11-02 10:31:45 +0100 | [diff] [blame^] | 3748 | |
| 3749 | if (server_opts.ch_dispatch_data.acquire_ctx_cb && server_opts.ch_dispatch_data.release_ctx_cb && |
| 3750 | server_opts.ch_dispatch_data.new_session_cb) { |
| 3751 | /* we have all we need for dispatching a new call home thread */ |
| 3752 | ret = nc_connect_ch_client_dispatch(lyd_get_value(lyd_child(node)), server_opts.ch_dispatch_data.acquire_ctx_cb, |
| 3753 | server_opts.ch_dispatch_data.release_ctx_cb, server_opts.ch_dispatch_data.ctx_cb_data, |
| 3754 | server_opts.ch_dispatch_data.new_session_cb, server_opts.ch_dispatch_data.new_session_cb_data); |
| 3755 | if (ret) { |
| 3756 | ERR(NULL, "Dispatching a new Call Home thread failed for Call Home client \"%s\".", lyd_get_value(lyd_child(node))); |
| 3757 | goto cleanup; |
| 3758 | } |
| 3759 | } |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3760 | } else if (op == NC_OP_DELETE) { |
| 3761 | if (nc_server_config_get_ch_client(node, &ch_client)) { |
| 3762 | ret = 1; |
| 3763 | goto cleanup; |
| 3764 | } |
| 3765 | |
| 3766 | nc_server_config_ch_del_client(ch_client); |
| 3767 | } |
| 3768 | |
| 3769 | cleanup: |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
| 3773 | #ifdef NC_ENABLED_SSH_TLS |
| 3774 | |
| 3775 | static int |
| 3776 | nc_server_config_remote_address(const struct lyd_node *node, NC_OPERATION op) |
| 3777 | { |
| 3778 | int ret = 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3779 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3780 | struct nc_ch_client *ch_client; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3781 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3782 | assert(!strcmp(LYD_NAME(node), "remote-address")); |
| 3783 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3784 | /* LOCK */ |
| 3785 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3786 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3787 | return 1; |
| 3788 | } |
| 3789 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 3790 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3791 | ret = 1; |
| 3792 | goto cleanup; |
| 3793 | } |
| 3794 | |
| 3795 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3796 | free(ch_endpt->address); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3797 | ch_endpt->address = strdup(lyd_get_value(node)); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 3798 | NC_CHECK_ERRMEM_GOTO(!ch_endpt->address, ret = 1, cleanup); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3799 | } else { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3800 | free(ch_endpt->address); |
| 3801 | ch_endpt->address = NULL; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3805 | /* UNLOCK */ |
| 3806 | nc_ch_client_unlock(ch_client); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3807 | return ret; |
| 3808 | } |
| 3809 | |
| 3810 | static int |
| 3811 | nc_server_config_remote_port(const struct lyd_node *node, NC_OPERATION op) |
| 3812 | { |
| 3813 | int ret = 0; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3814 | struct nc_ch_endpt *ch_endpt; |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3815 | struct nc_ch_client *ch_client; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3816 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3817 | assert(!strcmp(LYD_NAME(node), "remote-port")); |
| 3818 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3819 | /* LOCK */ |
| 3820 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3821 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3822 | return 1; |
| 3823 | } |
| 3824 | |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 3825 | if (nc_server_config_get_ch_endpt(node, &ch_endpt)) { |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3826 | ret = 1; |
| 3827 | goto cleanup; |
| 3828 | } |
| 3829 | |
| 3830 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 3831 | ch_endpt->port = ((struct lyd_node_term *)node)->value.uint16; |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3832 | } else { |
| 3833 | ch_endpt->port = 0; |
| 3834 | } |
| 3835 | |
| 3836 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3837 | /* UNLOCK */ |
| 3838 | nc_ch_client_unlock(ch_client); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 3839 | return ret; |
| 3840 | } |
| 3841 | |
| 3842 | #endif /* NC_ENABLED_SSH_TLS */ |
| 3843 | |
| 3844 | static int |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3845 | nc_server_config_persistent(const struct lyd_node *node, NC_OPERATION op) |
| 3846 | { |
| 3847 | int ret = 0; |
| 3848 | struct nc_ch_client *ch_client; |
| 3849 | |
| 3850 | assert(!strcmp(LYD_NAME(node), "persistent")); |
| 3851 | |
| 3852 | (void) op; |
| 3853 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3854 | /* LOCK */ |
| 3855 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3856 | /* to avoid unlock on fail */ |
| 3857 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | ch_client->conn_type = NC_CH_PERSIST; |
| 3861 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3862 | /* UNLOCK */ |
| 3863 | nc_ch_client_unlock(ch_client); |
| 3864 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3865 | return ret; |
| 3866 | } |
| 3867 | |
| 3868 | static int |
| 3869 | nc_server_config_periodic(const struct lyd_node *node, NC_OPERATION op) |
| 3870 | { |
| 3871 | int ret = 0; |
| 3872 | struct nc_ch_client *ch_client; |
| 3873 | |
| 3874 | assert(!strcmp(LYD_NAME(node), "periodic")); |
| 3875 | |
| 3876 | (void) op; |
| 3877 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3878 | /* LOCK */ |
| 3879 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3880 | /* to avoid unlock on fail */ |
| 3881 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | ch_client->conn_type = NC_CH_PERIOD; |
| 3885 | /* set default values */ |
| 3886 | ch_client->period = 60; |
| 3887 | ch_client->anchor_time = 0; |
| 3888 | ch_client->idle_timeout = 180; |
| 3889 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3890 | /* UNLOCK */ |
| 3891 | nc_ch_client_unlock(ch_client); |
| 3892 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3893 | return ret; |
| 3894 | } |
| 3895 | |
| 3896 | static int |
| 3897 | nc_server_config_period(const struct lyd_node *node, NC_OPERATION op) |
| 3898 | { |
| 3899 | int ret = 0; |
| 3900 | struct nc_ch_client *ch_client; |
| 3901 | |
| 3902 | assert(!strcmp(LYD_NAME(node), "period")); |
| 3903 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3904 | /* LOCK */ |
| 3905 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3906 | /* to avoid unlock on fail */ |
| 3907 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 3911 | ch_client->period = ((struct lyd_node_term *)node)->value.uint16; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3912 | } else if (op == NC_OP_DELETE) { |
| 3913 | ch_client->period = 60; |
| 3914 | } |
| 3915 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3916 | /* UNLOCK */ |
| 3917 | nc_ch_client_unlock(ch_client); |
| 3918 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3919 | return ret; |
| 3920 | } |
| 3921 | |
| 3922 | static int |
| 3923 | nc_server_config_anchor_time(const struct lyd_node *node, NC_OPERATION op) |
| 3924 | { |
| 3925 | int ret = 0; |
| 3926 | struct nc_ch_client *ch_client; |
roman | a3c95c7 | 2023-10-26 11:15:53 +0200 | [diff] [blame] | 3927 | struct lyd_value_date_and_time *anchor_time; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3928 | |
| 3929 | assert(!strcmp(LYD_NAME(node), "anchor-time")); |
| 3930 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3931 | /* LOCK */ |
| 3932 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3933 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3934 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3935 | } |
| 3936 | |
roman | a3c95c7 | 2023-10-26 11:15:53 +0200 | [diff] [blame] | 3937 | /* get the value of time from the node directly */ |
| 3938 | LYD_VALUE_GET(&((struct lyd_node_term *)node)->value, anchor_time); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3939 | |
| 3940 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | a3c95c7 | 2023-10-26 11:15:53 +0200 | [diff] [blame] | 3941 | ch_client->anchor_time = anchor_time->time; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3942 | } else if (op == NC_OP_DELETE) { |
| 3943 | ch_client->anchor_time = 0; |
| 3944 | } |
| 3945 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3946 | /* UNLOCK */ |
| 3947 | nc_ch_client_unlock(ch_client); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3948 | return ret; |
| 3949 | } |
| 3950 | |
| 3951 | static int |
| 3952 | nc_server_config_reconnect_strategy(const struct lyd_node *node, NC_OPERATION op) |
| 3953 | { |
| 3954 | int ret = 0; |
| 3955 | struct nc_ch_client *ch_client; |
| 3956 | |
| 3957 | assert(!strcmp(LYD_NAME(node), "reconnect-strategy")); |
| 3958 | |
| 3959 | (void) op; |
| 3960 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3961 | /* LOCK */ |
| 3962 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3963 | /* to avoid unlock on fail */ |
| 3964 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | /* set to default values */ |
| 3968 | ch_client->start_with = NC_CH_FIRST_LISTED; |
| 3969 | ch_client->max_wait = 5; |
| 3970 | ch_client->max_attempts = 3; |
| 3971 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3972 | /* UNLOCK */ |
| 3973 | nc_ch_client_unlock(ch_client); |
| 3974 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3975 | return ret; |
| 3976 | } |
| 3977 | |
| 3978 | static int |
| 3979 | nc_server_config_start_with(const struct lyd_node *node, NC_OPERATION op) |
| 3980 | { |
| 3981 | int ret = 0; |
| 3982 | struct nc_ch_client *ch_client; |
| 3983 | const char *value; |
| 3984 | |
| 3985 | assert(!strcmp(LYD_NAME(node), "start-with")); |
| 3986 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3987 | /* LOCK */ |
| 3988 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 3989 | /* to avoid unlock on fail */ |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 3990 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | if (op == NC_OP_DELETE) { |
| 3994 | ch_client->start_with = NC_CH_FIRST_LISTED; |
| 3995 | goto cleanup; |
| 3996 | } |
| 3997 | |
| 3998 | value = lyd_get_value(node); |
| 3999 | if (!strcmp(value, "first-listed")) { |
| 4000 | ch_client->start_with = NC_CH_FIRST_LISTED; |
| 4001 | } else if (!strcmp(value, "last-connected")) { |
| 4002 | ch_client->start_with = NC_CH_LAST_CONNECTED; |
| 4003 | } else if (!strcmp(value, "random-selection")) { |
| 4004 | ch_client->start_with = NC_CH_RANDOM; |
| 4005 | } else { |
| 4006 | ERR(NULL, "Unexpected start-with value \"%s\".", value); |
| 4007 | ret = 1; |
| 4008 | goto cleanup; |
| 4009 | } |
| 4010 | |
| 4011 | cleanup: |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 4012 | /* UNLOCK */ |
| 4013 | nc_ch_client_unlock(ch_client); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4014 | return ret; |
| 4015 | } |
| 4016 | |
| 4017 | static int |
| 4018 | nc_server_config_max_wait(const struct lyd_node *node, NC_OPERATION op) |
| 4019 | { |
| 4020 | int ret = 0; |
| 4021 | struct nc_ch_client *ch_client; |
| 4022 | |
| 4023 | assert(!strcmp(LYD_NAME(node), "max-wait")); |
| 4024 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 4025 | /* LOCK */ |
| 4026 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4027 | /* to avoid unlock on fail */ |
| 4028 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 4032 | ch_client->max_wait = ((struct lyd_node_term *)node)->value.uint16; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4033 | } else { |
| 4034 | ch_client->max_wait = 5; |
| 4035 | } |
| 4036 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 4037 | /* UNLOCK */ |
| 4038 | nc_ch_client_unlock(ch_client); |
| 4039 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4040 | return ret; |
| 4041 | } |
| 4042 | |
| 4043 | static int |
| 4044 | nc_server_config_max_attempts(const struct lyd_node *node, NC_OPERATION op) |
| 4045 | { |
| 4046 | int ret = 0; |
| 4047 | struct nc_ch_client *ch_client; |
| 4048 | |
| 4049 | assert(!strcmp(LYD_NAME(node), "max-attempts")); |
| 4050 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 4051 | /* LOCK */ |
| 4052 | if (nc_server_config_get_ch_client_with_lock(node, &ch_client)) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4053 | /* to avoid unlock on fail */ |
| 4054 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) { |
roman | 91ffeb4 | 2023-10-25 13:32:03 +0200 | [diff] [blame] | 4058 | ch_client->max_attempts = ((struct lyd_node_term *)node)->value.uint8; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4059 | } else { |
| 4060 | ch_client->max_attempts = 3; |
| 4061 | } |
| 4062 | |
roman | ba93eac | 2023-07-18 14:36:48 +0200 | [diff] [blame] | 4063 | /* UNLOCK */ |
| 4064 | nc_ch_client_unlock(ch_client); |
| 4065 | |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4066 | return ret; |
| 4067 | } |
| 4068 | |
| 4069 | static int |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4070 | nc_server_config_parse_netconf_server(const struct lyd_node *node, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4071 | { |
| 4072 | const char *name = LYD_NAME(node); |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4073 | int ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4074 | |
| 4075 | if (!strcmp(name, "listen")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4076 | ret = nc_server_config_listen(node, op); |
| 4077 | } else if (!strcmp(name, "call-home")) { |
| 4078 | ret = nc_server_config_ch(node, op); |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 4079 | } else if (!strcmp(name, "hello-timeout")) { |
| 4080 | ret = nc_server_config_hello_timeout(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4081 | } else if (!strcmp(name, "endpoint")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4082 | ret = nc_server_config_endpoint(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4083 | } else if (!strcmp(name, "unix-socket")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4084 | ret = nc_server_config_unix_socket(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4085 | } |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4086 | #ifdef NC_ENABLED_SSH_TLS |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4087 | else if (!strcmp(name, "ssh")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4088 | ret = nc_server_config_ssh(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4089 | } else if (!strcmp(name, "local-address")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4090 | ret = nc_server_config_local_address(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4091 | } else if (!strcmp(name, "local-port")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4092 | ret = nc_server_config_local_port(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4093 | } else if (!strcmp(name, "keepalives")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4094 | ret = nc_server_config_keepalives(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4095 | } else if (!strcmp(name, "idle-time")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4096 | ret = nc_server_config_idle_time(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4097 | } else if (!strcmp(name, "max-probes")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4098 | ret = nc_server_config_max_probes(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4099 | } else if (!strcmp(name, "probe-interval")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4100 | ret = nc_server_config_probe_interval(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4101 | } else if (!strcmp(name, "host-key")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4102 | ret = nc_server_config_host_key(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4103 | } else if (!strcmp(name, "public-key-format")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4104 | ret = nc_server_config_public_key_format(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4105 | } else if (!strcmp(name, "public-key")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4106 | ret = nc_server_config_public_key(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4107 | } else if (!strcmp(name, "private-key-format")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4108 | ret = nc_server_config_private_key_format(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4109 | } else if (!strcmp(name, "cleartext-private-key")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4110 | ret = nc_server_config_cleartext_private_key(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4111 | } else if (!strcmp(name, "keystore-reference")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4112 | ret = nc_server_config_keystore_reference(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4113 | } else if (!strcmp(name, "user")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4114 | ret = nc_server_config_user(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4115 | } else if (!strcmp(name, "auth-attempts")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4116 | ret = nc_server_config_auth_attempts(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4117 | } else if (!strcmp(name, "auth-timeout")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4118 | ret = nc_server_config_auth_timeout(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4119 | } else if (!strcmp(name, "truststore-reference")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4120 | ret = nc_server_config_truststore_reference(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4121 | } else if (!strcmp(name, "password")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4122 | ret = nc_server_config_password(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4123 | } else if (!strcmp(name, "pam-config-file-name")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4124 | ret = nc_server_config_pam_name(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4125 | } else if (!strcmp(name, "pam-config-file-dir")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4126 | ret = nc_server_config_pam_dir(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4127 | } else if (!strcmp(name, "none")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4128 | ret = nc_server_config_none(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4129 | } else if (!strcmp(name, "host-key-alg")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4130 | ret = nc_server_config_host_key_alg(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4131 | } else if (!strcmp(name, "key-exchange-alg")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4132 | ret = nc_server_config_kex_alg(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4133 | } else if (!strcmp(name, "encryption-alg")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4134 | ret = nc_server_config_encryption_alg(node, op); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4135 | } else if (!strcmp(name, "mac-alg")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4136 | ret = nc_server_config_mac_alg(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4137 | } else if (!strcmp(name, "endpoint-client-auth")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4138 | ret = nc_server_config_endpoint_client_auth(node, op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4139 | } else if (!strcmp(name, "tls")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4140 | ret = nc_server_config_tls(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4141 | } else if (!strcmp(name, "cert-data")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4142 | ret = nc_server_config_cert_data(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4143 | } else if (!strcmp(name, "asymmetric-key")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4144 | ret = nc_server_config_asymmetric_key(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4145 | } else if (!strcmp(name, "certificate")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4146 | ret = nc_server_config_certificate(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4147 | } else if (!strcmp(name, "cert-to-name")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4148 | ret = nc_server_config_cert_to_name(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4149 | } else if (!strcmp(name, "fingerprint")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4150 | ret = nc_server_config_fingerprint(node, op); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 4151 | } else if (!strcmp(name, "tls-version")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4152 | ret = nc_server_config_tls_version(node, op); |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 4153 | } else if (!strcmp(name, "cipher-suite")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4154 | ret = nc_server_config_cipher_suite(node, op); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 4155 | } else if (!strcmp(name, "crl-url")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4156 | ret = nc_server_config_crl_url(node, op); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 4157 | } else if (!strcmp(name, "crl-path")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4158 | ret = nc_server_config_crl_path(node, op); |
roman | faecc58 | 2023-06-15 16:13:31 +0200 | [diff] [blame] | 4159 | } else if (!strcmp(name, "crl-cert-ext")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4160 | ret = nc_server_config_crl_cert_ext(node, op); |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4161 | } |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4162 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 4163 | else if (!strcmp(name, "netconf-client")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4164 | ret = nc_server_config_netconf_client(node, op); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 4165 | } |
| 4166 | #ifdef NC_ENABLED_SSH_TLS |
| 4167 | else if (!strcmp(name, "remote-address")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4168 | ret = nc_server_config_remote_address(node, op); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 4169 | } else if (!strcmp(name, "remote-port")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4170 | ret = nc_server_config_remote_port(node, op); |
roman | 5cbb653 | 2023-06-22 12:53:17 +0200 | [diff] [blame] | 4171 | } |
| 4172 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4173 | else if (!strcmp(name, "persistent")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4174 | ret = nc_server_config_persistent(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4175 | } else if (!strcmp(name, "periodic")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4176 | ret = nc_server_config_periodic(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4177 | } else if (!strcmp(name, "period")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4178 | ret = nc_server_config_period(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4179 | } else if (!strcmp(name, "anchor-time")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4180 | ret = nc_server_config_anchor_time(node, op); |
roman | eaf84c7 | 2023-10-19 14:38:05 +0200 | [diff] [blame] | 4181 | } else if (!strcmp(name, "idle-timeout")) { |
| 4182 | ret = nc_server_config_idle_timeout(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4183 | } else if (!strcmp(name, "reconnect-strategy")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4184 | ret = nc_server_config_reconnect_strategy(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4185 | } else if (!strcmp(name, "start-with")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4186 | ret = nc_server_config_start_with(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4187 | } else if (!strcmp(name, "max-wait")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4188 | ret = nc_server_config_max_wait(node, op); |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4189 | } else if (!strcmp(name, "max-attempts")) { |
roman | 6430c15 | 2023-10-12 11:28:47 +0200 | [diff] [blame] | 4190 | ret = nc_server_config_max_attempts(node, op); |
| 4191 | } |
| 4192 | |
| 4193 | if (ret) { |
| 4194 | ERR(NULL, "Configuring node \"%s\" failed.", LYD_NAME(node)); |
| 4195 | return 1; |
roman | b6f4403 | 2023-06-30 15:07:56 +0200 | [diff] [blame] | 4196 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4197 | |
| 4198 | return 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | int |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 4202 | nc_server_config_parse_tree(const struct lyd_node *node, NC_OPERATION parent_op, NC_MODULE module) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4203 | { |
| 4204 | struct lyd_node *child; |
| 4205 | struct lyd_meta *m; |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4206 | NC_OPERATION current_op = NC_OP_UNKNOWN; |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4207 | int ret; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4208 | |
| 4209 | assert(node); |
| 4210 | |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4211 | /* get current op if there is any */ |
| 4212 | if ((m = lyd_find_meta(node->meta, NULL, "yang:operation"))) { |
| 4213 | if (!strcmp(lyd_get_meta_value(m), "create")) { |
| 4214 | current_op = NC_OP_CREATE; |
| 4215 | } else if (!strcmp(lyd_get_meta_value(m), "delete")) { |
| 4216 | current_op = NC_OP_DELETE; |
| 4217 | } else if (!strcmp(lyd_get_meta_value(m), "replace")) { |
| 4218 | current_op = NC_OP_REPLACE; |
| 4219 | } else if (!strcmp(lyd_get_meta_value(m), "none")) { |
| 4220 | current_op = NC_OP_NONE; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | /* node has no op, inherit from the parent */ |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4225 | if (!current_op) { |
| 4226 | if (!parent_op) { |
| 4227 | ERR(NULL, "Unknown operation for node \"%s\".", LYD_NAME(node)); |
| 4228 | return 1; |
| 4229 | } |
| 4230 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4231 | current_op = parent_op; |
| 4232 | } |
| 4233 | |
| 4234 | switch (current_op) { |
| 4235 | case NC_OP_NONE: |
| 4236 | break; |
| 4237 | case NC_OP_CREATE: |
| 4238 | case NC_OP_DELETE: |
| 4239 | case NC_OP_REPLACE: |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4240 | #ifdef NC_ENABLED_SSH_TLS |
| 4241 | if (module == NC_MODULE_KEYSTORE) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4242 | ret = nc_server_config_parse_keystore(node, current_op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4243 | } else if (module == NC_MODULE_TRUSTSTORE) { |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4244 | ret = nc_server_config_parse_truststore(node, current_op); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4245 | } else |
| 4246 | #endif /* NC_ENABLED_SSH_TLS */ |
| 4247 | { |
| 4248 | ret = nc_server_config_parse_netconf_server(node, current_op); |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4249 | } |
| 4250 | if (ret) { |
| 4251 | return ret; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4252 | } |
| 4253 | break; |
| 4254 | default: |
| 4255 | break; |
| 4256 | } |
| 4257 | |
| 4258 | if (current_op != NC_OP_DELETE) { |
| 4259 | LY_LIST_FOR(lyd_child(node), child) { |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 4260 | if (nc_server_config_parse_tree(child, current_op, module)) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4261 | return 1; |
| 4262 | } |
| 4263 | } |
| 4264 | } |
| 4265 | return 0; |
| 4266 | } |
| 4267 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4268 | API int |
| 4269 | nc_server_config_load_modules(struct ly_ctx **ctx) |
| 4270 | { |
| 4271 | int i, new_ctx = 0; |
| 4272 | |
| 4273 | if (!*ctx) { |
| 4274 | if (ly_ctx_new(NC_SERVER_SEARCH_DIR, 0, ctx)) { |
| 4275 | ERR(NULL, "Couldn't create new libyang context.\n"); |
| 4276 | goto error; |
| 4277 | } |
| 4278 | new_ctx = 1; |
| 4279 | } |
| 4280 | |
| 4281 | /* all features */ |
| 4282 | const char *ietf_nectonf_server[] = {"ssh-listen", "tls-listen", "ssh-call-home", "tls-call-home", "central-netconf-server-supported", NULL}; |
| 4283 | /* all features */ |
| 4284 | const char *ietf_x509_cert_to_name[] = {NULL}; |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4285 | /* no private-key-encryption, csr-generation, p10-csr-format, certificate-expiration-notification, |
roman | 3c6c7e6 | 2023-09-14 10:19:19 +0200 | [diff] [blame] | 4286 | * encrypted-passwords, hidden-symmetric-keys, encrypted-symmetric-keys, hidden-private-keys, encrypted-private-keys, |
| 4287 | * one-symmetric-key-format, one-asymmetric-key-format, symmetrically-encrypted-value-format, |
| 4288 | * asymmetrically-encrypted-value-format, cms-enveloped-data-format, cms-encrypted-data-format, |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 4289 | * cleartext-symmetric-keys */ |
roman | 3c6c7e6 | 2023-09-14 10:19:19 +0200 | [diff] [blame] | 4290 | const char *ietf_crypto_types[] = {"cleartext-passwords", "cleartext-private-keys", NULL}; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4291 | /* all features */ |
| 4292 | const char *ietf_tcp_common[] = {"keepalives-supported", NULL}; |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4293 | /* all features */ |
| 4294 | const char *ietf_tcp_server[] = {"tcp-server-keepalives", NULL}; |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 4295 | /* no proxy-connect, socks5-gss-api, socks5-username-password, local-binding-supported ? */ |
| 4296 | const char *ietf_tcp_client[] = {"tcp-client-keepalives", NULL}; |
roman | 3c6c7e6 | 2023-09-14 10:19:19 +0200 | [diff] [blame] | 4297 | /* no ssh-x509-certs, public-key-generation */ |
| 4298 | const char *ietf_ssh_common[] = {"transport-params", NULL}; |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4299 | /* no ssh-server-keepalives and local-user-auth-hostbased */ |
| 4300 | const char *ietf_ssh_server[] = {"local-users-supported", "local-user-auth-publickey", "local-user-auth-password", "local-user-auth-none", NULL}; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4301 | /* all features */ |
| 4302 | const char *iana_ssh_encryption_algs[] = {NULL}; |
| 4303 | /* all features */ |
| 4304 | const char *iana_ssh_key_exchange_algs[] = {NULL}; |
| 4305 | /* all features */ |
| 4306 | const char *iana_ssh_mac_algs[] = {NULL}; |
| 4307 | /* all features */ |
| 4308 | const char *iana_ssh_public_key_algs[] = {NULL}; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4309 | /* all features */ |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4310 | const char *iana_crypt_hash[] = {"crypt-hash-md5", "crypt-hash-sha-256", "crypt-hash-sha-512", NULL}; |
| 4311 | /* no symmetric-keys */ |
| 4312 | const char *ietf_keystore[] = {"central-keystore-supported", "inline-definitions-supported", "asymmetric-keys", NULL}; |
| 4313 | /* all features */ |
| 4314 | const char *ietf_truststore[] = {"central-truststore-supported", "inline-definitions-supported", "certificates", "public-keys", NULL}; |
roman | 3c6c7e6 | 2023-09-14 10:19:19 +0200 | [diff] [blame] | 4315 | /* no public-key-generation */ |
| 4316 | const char *ietf_tls_common[] = {"tls10", "tls11", "tls12", "tls13", "hello-params", NULL}; |
| 4317 | /* no tls-server-keepalives, server-ident-raw-public-key, server-ident-tls12-psk, server-ident-tls13-epsk, |
| 4318 | * client-auth-raw-public-key, client-auth-tls12-psk, client-auth-tls13-epsk */ |
| 4319 | const char *ietf_tls_server[] = {"server-ident-x509-cert", "client-auth-supported", "client-auth-x509-cert", NULL}; |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4320 | /* all features */ |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 4321 | const char *iana_tls_cipher_suite_algs[] = {NULL}; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4322 | /* all features */ |
| 4323 | const char *libnetconf2_netconf_server[] = {NULL}; |
| 4324 | |
| 4325 | const char *module_names[] = { |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4326 | "ietf-netconf-server", "ietf-x509-cert-to-name", "ietf-crypto-types", "ietf-tcp-common", "ietf-tcp-server", |
| 4327 | "ietf-tcp-client", "ietf-ssh-common", "ietf-ssh-server", "iana-ssh-encryption-algs", |
| 4328 | "iana-ssh-key-exchange-algs", "iana-ssh-mac-algs", "iana-ssh-public-key-algs", "iana-crypt-hash", |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 4329 | "ietf-keystore", "ietf-truststore", "ietf-tls-common", "ietf-tls-server", "iana-tls-cipher-suite-algs", |
| 4330 | "libnetconf2-netconf-server", NULL |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4331 | }; |
| 4332 | |
| 4333 | const char **module_features[] = { |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 4334 | ietf_nectonf_server, ietf_x509_cert_to_name, ietf_crypto_types, ietf_tcp_common, |
| 4335 | ietf_tcp_server, ietf_tcp_client, ietf_ssh_common, ietf_ssh_server, iana_ssh_encryption_algs, |
| 4336 | iana_ssh_key_exchange_algs, iana_ssh_mac_algs, iana_ssh_public_key_algs, iana_crypt_hash, |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 4337 | ietf_keystore, ietf_truststore, ietf_tls_common, ietf_tls_server, iana_tls_cipher_suite_algs, |
| 4338 | libnetconf2_netconf_server, NULL |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4339 | }; |
| 4340 | |
| 4341 | for (i = 0; module_names[i] != NULL; i++) { |
| 4342 | if (!ly_ctx_load_module(*ctx, module_names[i], NULL, module_features[i])) { |
| 4343 | ERR(NULL, "Loading module \"%s\" failed.\n", module_names[i]); |
| 4344 | goto error; |
| 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | return 0; |
| 4349 | |
| 4350 | error: |
| 4351 | if (new_ctx) { |
| 4352 | ly_ctx_destroy(*ctx); |
| 4353 | *ctx = NULL; |
| 4354 | } |
| 4355 | return 1; |
| 4356 | } |
| 4357 | |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4358 | static int |
| 4359 | nc_server_config_fill_nectonf_server(const struct lyd_node *data, NC_OPERATION op) |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4360 | { |
| 4361 | int ret = 0; |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 4362 | uint32_t log_options = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4363 | struct lyd_node *tree; |
roman | d57b372 | 2023-04-05 11:26:25 +0200 | [diff] [blame] | 4364 | |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4365 | /* silently search for ietf-netconf-server, it may not be present */ |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 4366 | ly_temp_log_options(&log_options); |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4367 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4368 | ret = lyd_find_path(data, "/ietf-netconf-server:netconf-server", 0, &tree); |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4369 | if (ret || (tree->flags & LYD_DEFAULT)) { |
| 4370 | /* not found */ |
| 4371 | ret = 0; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4372 | goto cleanup; |
| 4373 | } |
| 4374 | |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 4375 | if (nc_server_config_parse_tree(tree, op, NC_MODULE_NETCONF_SERVER)) { |
| 4376 | ret = 1; |
| 4377 | goto cleanup; |
| 4378 | } |
| 4379 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4380 | #ifdef NC_ENABLED_SSH_TLS |
| 4381 | /* backward check of client auth reference */ |
roman | 0bbc19c | 2023-05-26 09:59:09 +0200 | [diff] [blame] | 4382 | if (nc_server_config_fill_endpt_client_auth()) { |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4383 | ret = 1; |
| 4384 | goto cleanup; |
| 4385 | } |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4386 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4387 | |
| 4388 | cleanup: |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4389 | /* reset the logging options back to what they were */ |
roman | 84fe325 | 2023-10-25 11:28:32 +0200 | [diff] [blame] | 4390 | ly_temp_log_options(NULL); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4391 | return ret; |
| 4392 | } |
| 4393 | |
| 4394 | API int |
roman | f6f37a5 | 2023-05-25 14:27:51 +0200 | [diff] [blame] | 4395 | nc_server_config_setup_diff(const struct lyd_node *data) |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4396 | { |
| 4397 | int ret = 0; |
| 4398 | |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4399 | NC_CHECK_ARG_RET(NULL, data, 1); |
| 4400 | |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4401 | /* LOCK */ |
| 4402 | pthread_rwlock_wrlock(&server_opts.config_lock); |
| 4403 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4404 | #ifdef NC_ENABLED_SSH_TLS |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4405 | /* configure keystore */ |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4406 | ret = nc_server_config_fill_keystore(data, NC_OP_UNKNOWN); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4407 | if (ret) { |
| 4408 | ERR(NULL, "Filling keystore failed."); |
| 4409 | goto cleanup; |
| 4410 | } |
| 4411 | |
| 4412 | /* configure truststore */ |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4413 | ret = nc_server_config_fill_truststore(data, NC_OP_UNKNOWN); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4414 | if (ret) { |
| 4415 | ERR(NULL, "Filling truststore failed."); |
| 4416 | goto cleanup; |
| 4417 | } |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4418 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4419 | |
| 4420 | /* configure netconf-server */ |
| 4421 | ret = nc_server_config_fill_nectonf_server(data, NC_OP_UNKNOWN); |
| 4422 | if (ret) { |
| 4423 | ERR(NULL, "Filling netconf-server failed."); |
| 4424 | goto cleanup; |
| 4425 | } |
| 4426 | |
| 4427 | cleanup: |
| 4428 | /* UNLOCK */ |
| 4429 | pthread_rwlock_unlock(&server_opts.config_lock); |
| 4430 | return ret; |
| 4431 | } |
| 4432 | |
| 4433 | API int |
roman | f6f37a5 | 2023-05-25 14:27:51 +0200 | [diff] [blame] | 4434 | nc_server_config_setup_data(const struct lyd_node *data) |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4435 | { |
| 4436 | int ret = 0; |
| 4437 | struct lyd_node *tree, *iter, *root; |
| 4438 | |
roman | c9b62d6 | 2023-09-14 10:19:50 +0200 | [diff] [blame] | 4439 | NC_CHECK_ARG_RET(NULL, data, 1); |
| 4440 | |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4441 | /* LOCK */ |
| 4442 | pthread_rwlock_wrlock(&server_opts.config_lock); |
| 4443 | |
| 4444 | /* find the netconf-server node */ |
| 4445 | ret = lyd_find_path(data, "/ietf-netconf-server:netconf-server", 0, &root); |
| 4446 | if (ret) { |
| 4447 | ERR(NULL, "Unable to find the netconf-server container in the YANG data."); |
| 4448 | goto cleanup; |
| 4449 | } |
| 4450 | |
| 4451 | /* iterate through all the nodes and make sure there is no operation attribute */ |
| 4452 | LY_LIST_FOR(root, tree) { |
| 4453 | LYD_TREE_DFS_BEGIN(tree, iter) { |
| 4454 | if (lyd_find_meta(iter->meta, NULL, "yang:operation")) { |
| 4455 | ERR(NULL, "Unexpected operation attribute in the YANG data."); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4456 | ret = 1; |
| 4457 | goto cleanup; |
| 4458 | } |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4459 | LYD_TREE_DFS_END(tree, iter); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4463 | /* delete the current configuration */ |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4464 | nc_server_config_listen(NULL, NC_OP_DELETE); |
roman | 4cb8bb1 | 2023-06-29 09:16:46 +0200 | [diff] [blame] | 4465 | nc_server_config_ch(NULL, NC_OP_DELETE); |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4466 | #ifdef NC_ENABLED_SSH_TLS |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4467 | nc_server_config_ks_keystore(NULL, NC_OP_DELETE); |
| 4468 | nc_server_config_ts_truststore(NULL, NC_OP_DELETE); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4469 | |
| 4470 | /* configure keystore */ |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4471 | ret = nc_server_config_fill_keystore(data, NC_OP_CREATE); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4472 | if (ret) { |
| 4473 | ERR(NULL, "Filling keystore failed."); |
| 4474 | goto cleanup; |
| 4475 | } |
| 4476 | |
| 4477 | /* configure truststore */ |
roman | f02273a | 2023-05-25 09:44:11 +0200 | [diff] [blame] | 4478 | ret = nc_server_config_fill_truststore(data, NC_OP_CREATE); |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4479 | if (ret) { |
| 4480 | ERR(NULL, "Filling truststore failed."); |
| 4481 | goto cleanup; |
| 4482 | } |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 4483 | #endif /* NC_ENABLED_SSH_TLS */ |
roman | f9906b4 | 2023-05-22 14:04:29 +0200 | [diff] [blame] | 4484 | |
| 4485 | /* configure netconf-server */ |
| 4486 | ret = nc_server_config_fill_nectonf_server(data, NC_OP_CREATE); |
| 4487 | if (ret) { |
| 4488 | ERR(NULL, "Filling netconf-server failed."); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 4489 | goto cleanup; |
| 4490 | } |
| 4491 | |
| 4492 | cleanup: |
| 4493 | /* UNLOCK */ |
| 4494 | pthread_rwlock_unlock(&server_opts.config_lock); |
| 4495 | return ret; |
| 4496 | } |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 4497 | |
| 4498 | API int |
| 4499 | nc_server_config_setup_path(const struct ly_ctx *ctx, const char *path) |
| 4500 | { |
| 4501 | struct lyd_node *tree = NULL; |
| 4502 | int ret = 0; |
| 4503 | |
| 4504 | NC_CHECK_ARG_RET(NULL, path, 1); |
| 4505 | |
| 4506 | ret = lyd_parse_data_path(ctx, path, LYD_UNKNOWN, LYD_PARSE_NO_STATE | LYD_PARSE_STRICT, LYD_VALIDATE_NO_STATE, &tree); |
| 4507 | if (ret) { |
| 4508 | goto cleanup; |
| 4509 | } |
| 4510 | |
| 4511 | ret = nc_server_config_setup_data(tree); |
| 4512 | if (ret) { |
| 4513 | goto cleanup; |
| 4514 | } |
| 4515 | |
| 4516 | cleanup: |
| 4517 | lyd_free_all(tree); |
| 4518 | return ret; |
| 4519 | } |