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