-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathserver_config.c
More file actions
6662 lines (5569 loc) · 202 KB
/
server_config.c
File metadata and controls
6662 lines (5569 loc) · 202 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file server_config.c
* @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 server configuration through YANG data implementation
*
* @copyright
* Copyright (c) 2022-2025 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <pthread.h>
#include <pwd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <libyang/libyang.h>
#include <libyang/tree_data.h>
#include "compat.h"
#include "config.h"
#include "log_p.h"
#include "server_config.h"
#include "session_p.h"
/**
* @brief Macro to get the diff operation of a node, defaulting to the parent's operation if not present.
*
* @note Returns 1 on error.
*
* @param[in] node Node to get the operation from.
* @param[in] parent_op Parent node's operation.
* @param[out] op Diff operation of the node.
*/
#define NC_NODE_GET_OP(node, parent_op, op) \
{ \
struct lyd_meta *_meta = lyd_find_meta(node->meta, NULL, "yang:operation"); \
if (_meta) { \
const char *_meta_val = lyd_get_meta_value(_meta); \
if (!strcmp(_meta_val, "create")) { \
*(op) = NC_OP_CREATE; \
} else if (!strcmp(_meta_val, "delete")) { \
*(op) = NC_OP_DELETE; \
} else if (!strcmp(_meta_val, "replace")) { \
*(op) = NC_OP_REPLACE; \
} else if (!strcmp(_meta_val, "none")) { \
*(op) = NC_OP_NONE; \
} else { \
ERR(NULL, "Unknown operation \"%s\" of node \"%s\".", _meta_val, LYD_NAME(node)); \
return 1; \
} \
} else { \
*(op) = parent_op; \
} \
}
/**
* @brief Macro to log unsupported configuration nodes.
*
* @param[in] node Node that is unsupported.
*/
#define CONFIG_LOG_UNSUPPORTED(node) \
WRN(NULL, "Unsupported node \"%s\" in the configuration, ignoring.", LYD_NAME(node))
/**
* @brief Wrapper macro for LY_ARRAY_CREATE_GOTO to disallow zero-size arrays.
*/
#define LN2_LY_ARRAY_CREATE_GOTO_WRAP(ARRAY, SIZE, RET, GOTO) \
if (!(SIZE)) { \
(ARRAY) = NULL; \
} else { \
LY_ARRAY_CREATE_GOTO(NULL, ARRAY, SIZE, RET, GOTO); \
}
/**
* @brief Find a mandatory child node of a given node.
*
* @note Implicit nodes, such as NP containers/leafs with default values, are always expected to be present
* in valid data, however this might not be true for diffs. So use this function only for list keys.
*
* @param[in] ctx_node Context (parent) node.
* @param[in] child Name of the child node to find.
* @param[out] match Found node.
* @return 0 on success, 1 if not found.
*/
static int
nc_lyd_find_child_mandatory(const struct lyd_node *ctx_node, const char *child, struct lyd_node **match)
{
*match = NULL;
lyd_find_path(ctx_node, child, 0, match);
if (!*match) {
ERR(NULL, "Missing mandatory child node \"%s\" of node \"%s\".", child, LYD_NAME(ctx_node));
return 1;
}
return 0;
}
/**
* @brief Find an optional child node of a given node.
*
* @note Almost all nodes are optional, because diff is not valid data.
*
* @param[in] ctx_node Context (parent) node.
* @param[in] child Name of the child node to find.
* @param[out] match Found node, or NULL if not found.
*/
static void
nc_lyd_find_child_optional(const struct lyd_node *ctx_node, const char *child, struct lyd_node **match)
{
*match = NULL;
lyd_find_path(ctx_node, child, 0, match);
}
#ifdef NC_ENABLED_SSH_TLS
/**
* @brief Free SSH server options.
*
* @param[in] opts SSH server options to free.
*/
static void
nc_server_config_ssh_opts_free(struct nc_server_ssh_opts *opts)
{
struct nc_hostkey *hostkey;
struct nc_auth_client *auth_client;
struct nc_public_key *pubkey;
LY_ARRAY_COUNT_TYPE i = 0, j = 0;
if (!opts) {
return;
}
/* free hostkeys */
LY_ARRAY_FOR(opts->hostkeys, i) {
hostkey = &opts->hostkeys[i];
free(hostkey->name);
if (hostkey->store == NC_STORE_LOCAL) {
free(hostkey->key.name);
free(hostkey->key.pubkey.data);
free(hostkey->key.privkey.data);
} else if (hostkey->store == NC_STORE_KEYSTORE) {
free(hostkey->ks_ref);
}
}
LY_ARRAY_FREE(opts->hostkeys);
/* free authorized clients */
LY_ARRAY_FOR(opts->auth_clients, i) {
auth_client = &opts->auth_clients[i];
free(auth_client->username);
if (auth_client->pubkey_store == NC_STORE_LOCAL) {
LY_ARRAY_FOR(auth_client->pubkeys, j) {
pubkey = &auth_client->pubkeys[j];
free(pubkey->name);
free(pubkey->data);
}
LY_ARRAY_FREE(auth_client->pubkeys);
} else if (auth_client->pubkey_store == NC_STORE_TRUSTSTORE) {
free(auth_client->ts_ref);
}
free(auth_client->password);
}
LY_ARRAY_FREE(opts->auth_clients);
free(opts->referenced_endpt_name);
free(opts->hostkey_algs);
free(opts->encryption_algs);
free(opts->kex_algs);
free(opts->mac_algs);
free(opts->banner);
free(opts);
}
/**
* @brief Free TLS server options.
*
* @param[in] opts TLS server options to free.
*/
static void
nc_server_config_tls_opts_free(struct nc_server_tls_opts *opts)
{
struct nc_ctn *ctn, *next;
LY_ARRAY_COUNT_TYPE i = 0;
if (!opts) {
return;
}
/* free server identity */
if (opts->cert_store == NC_STORE_LOCAL) {
free(opts->local.key.name);
free(opts->local.key.pubkey.data);
free(opts->local.key.privkey.data);
free(opts->local.cert.name);
free(opts->local.cert.data);
} else if (opts->cert_store == NC_STORE_KEYSTORE) {
free(opts->keystore.asym_key_ref);
free(opts->keystore.cert_ref);
}
/* free ca certificates */
if (opts->client_auth.ca_certs_store == NC_STORE_LOCAL) {
LY_ARRAY_FOR(opts->client_auth.ca_certs, i) {
free(opts->client_auth.ca_certs[i].name);
free(opts->client_auth.ca_certs[i].data);
}
LY_ARRAY_FREE(opts->client_auth.ca_certs);
} else if (opts->client_auth.ca_certs_store == NC_STORE_TRUSTSTORE) {
free(opts->client_auth.ca_cert_bag_ts_ref);
}
/* free end-entity certificates */
if (opts->client_auth.ee_certs_store == NC_STORE_LOCAL) {
LY_ARRAY_FOR(opts->client_auth.ee_certs, i) {
free(opts->client_auth.ee_certs[i].name);
free(opts->client_auth.ee_certs[i].data);
}
LY_ARRAY_FREE(opts->client_auth.ee_certs);
} else if (opts->client_auth.ee_certs_store == NC_STORE_TRUSTSTORE) {
free(opts->client_auth.ee_cert_bag_ts_ref);
}
/* free cert-to-name entries */
for (ctn = opts->ctn; ctn; ctn = next) {
next = ctn->next;
free(ctn->fingerprint);
free(ctn->name);
free(ctn);
}
free(opts->referenced_endpt_name);
free(opts->cipher_suites);
free(opts);
}
#endif /* NC_ENABLED_SSH_TLS */
/**
* @brief Free UNIX server options.
*
* @param[in] opts UNIX server options to free.
*/
static void
nc_server_config_unix_opts_free(struct nc_server_unix_opts *opts)
{
struct nc_server_unix_user_mapping *mapping;
LY_ARRAY_COUNT_TYPE i = 0, j = 0;
if (!opts) {
return;
}
/* free user mappings */
LY_ARRAY_FOR(opts->user_mappings, i) {
mapping = &opts->user_mappings[i];
free(mapping->system_user);
LY_ARRAY_FOR(mapping->allowed_users, j) {
free(mapping->allowed_users[j]);
}
LY_ARRAY_FREE(mapping->allowed_users);
}
LY_ARRAY_FREE(opts->user_mappings);
free(opts);
}
#ifdef NC_ENABLED_SSH_TLS
/**
* @brief Free keystore configuration data.
*
* @param[in] ks Keystore to free (it's not dynamically allocated).
*/
static void
nc_server_config_keystore_free(struct nc_keystore *ks)
{
struct nc_keystore_entry *entry;
LY_ARRAY_COUNT_TYPE i = 0, j = 0;
if (!ks) {
return;
}
LY_ARRAY_FOR(ks->entries, i) {
entry = &ks->entries[i];
free(entry->asym_key.name);
free(entry->asym_key.pubkey.data);
free(entry->asym_key.privkey.data);
/* free certificates */
LY_ARRAY_FOR(entry->certs, j) {
free(entry->certs[j].name);
free(entry->certs[j].data);
}
LY_ARRAY_FREE(entry->certs);
}
LY_ARRAY_FREE(ks->entries);
memset(ks, 0, sizeof(*ks));
}
/**
* @brief Free truststore configuration data.
*
* @param[in] ts Truststore to free (it's not dynamically allocated).
*/
static void
nc_server_config_truststore_free(struct nc_truststore *ts)
{
struct nc_certificate_bag *cbag;
struct nc_public_key_bag *pkbag;
LY_ARRAY_COUNT_TYPE i = 0, j = 0;
if (!ts) {
return;
}
/* free certificate bags */
LY_ARRAY_FOR(ts->cert_bags, i) {
cbag = &ts->cert_bags[i];
free(cbag->name);
free(cbag->description);
LY_ARRAY_FOR(cbag->certs, j) {
free(cbag->certs[j].name);
free(cbag->certs[j].data);
}
LY_ARRAY_FREE(cbag->certs);
}
LY_ARRAY_FREE(ts->cert_bags);
/* free public key bags */
LY_ARRAY_FOR(ts->pubkey_bags, i) {
pkbag = &ts->pubkey_bags[i];
free(pkbag->name);
free(pkbag->description);
LY_ARRAY_FOR(pkbag->pubkeys, j) {
free(pkbag->pubkeys[j].name);
free(pkbag->pubkeys[j].data);
}
LY_ARRAY_FREE(pkbag->pubkeys);
}
LY_ARRAY_FREE(ts->pubkey_bags);
memset(ts, 0, sizeof(*ts));
}
#endif /* NC_ENABLED_SSH_TLS */
/**
* @brief Free server configuration data.
*
* @param[in] config Server configuration to free.
*/
void
nc_server_config_free(struct nc_server_config *config)
{
struct nc_endpt *endpt;
struct nc_ch_client *ch_client;
struct nc_ch_endpt *ch_endpt;
LY_ARRAY_COUNT_TYPE i = 0, j = 0;
char *socket_path = NULL;
if (!config) {
return;
}
/* free ignored hello modules */
LY_ARRAY_FOR(config->ignored_modules, i) {
free(config->ignored_modules[i]);
}
LY_ARRAY_FREE(config->ignored_modules);
/* free listen endpoints */
LY_ARRAY_FOR(config->endpts, i) {
endpt = &config->endpts[i];
if (endpt->ti == NC_TI_UNIX) {
/* get the socket path before freeing the name */
socket_path = nc_server_unix_get_socket_path(endpt);
}
free(endpt->name);
/* free binds */
LY_ARRAY_FOR(endpt->binds, j) {
if (endpt->binds[j].sock != -1) {
close(endpt->binds[j].sock);
if (socket_path) {
/* remove the UNIX socket file */
unlink(socket_path);
}
}
free(endpt->binds[j].address);
}
LY_ARRAY_FREE(endpt->binds);
if (endpt->ti == NC_TI_UNIX) {
free(socket_path);
socket_path = NULL;
}
/* free transport specific options */
switch (endpt->ti) {
#ifdef NC_ENABLED_SSH_TLS
case NC_TI_SSH:
nc_server_config_ssh_opts_free(endpt->opts.ssh);
break;
case NC_TI_TLS:
nc_server_config_tls_opts_free(endpt->opts.tls);
break;
#endif /* NC_ENABLED_SSH_TLS */
case NC_TI_UNIX:
nc_server_config_unix_opts_free(endpt->opts.unix);
break;
default:
ERRINT;
break;
}
}
LY_ARRAY_FREE(config->endpts);
/* free call home clients */
LY_ARRAY_FOR(config->ch_clients, i) {
ch_client = &config->ch_clients[i];
free(ch_client->name);
/* free call home endpoints */
LY_ARRAY_FOR(ch_client->ch_endpts, j) {
ch_endpt = &ch_client->ch_endpts[j];
free(ch_endpt->name);
free(ch_endpt->src_addr);
free(ch_endpt->dst_addr);
/* free transport specific options */
switch (ch_endpt->ti) {
#ifdef NC_ENABLED_SSH_TLS
case NC_TI_SSH:
nc_server_config_ssh_opts_free(ch_endpt->opts.ssh);
break;
case NC_TI_TLS:
nc_server_config_tls_opts_free(ch_endpt->opts.tls);
break;
#endif /* NC_ENABLED_SSH_TLS */
default:
ERRINT;
break;
}
}
LY_ARRAY_FREE(ch_client->ch_endpts);
}
LY_ARRAY_FREE(config->ch_clients);
#ifdef NC_ENABLED_SSH_TLS
/* free keystore and truststore */
nc_server_config_keystore_free(&config->keystore);
nc_server_config_truststore_free(&config->truststore);
/* free certificate expiration intervals */
LY_ARRAY_FREE(config->cert_exp_notif_intervals);
#endif /* NC_ENABLED_SSH_TLS */
memset(config, 0, sizeof(*config));
}
API int
nc_server_config_load_modules(struct ly_ctx **ctx)
{
int i, new_ctx = 0;
if (!*ctx) {
if (ly_ctx_new(ly_yang_module_dir(), 0, ctx)) {
ERR(NULL, "Failed to create new libyang context.");
goto error;
}
new_ctx = 1;
if (ly_ctx_set_searchdir(*ctx, nc_yang_module_dir())) {
ERR(NULL, "Failed to set new searchdir for a context.");
goto error;
}
}
/* all features */
const char *ietf_nectonf_server[] = {"ssh-listen", "tls-listen", "ssh-call-home", "tls-call-home", "central-netconf-server-supported", NULL};
/* all features */
const char *ietf_x509_cert_to_name[] = {NULL};
/* no private-key-encryption, csr-generation, p10-csr-format, certificate-expiration-notification,
* encrypted-passwords, hidden-symmetric-keys, encrypted-symmetric-keys, hidden-private-keys, encrypted-private-keys,
* one-symmetric-key-format, one-asymmetric-key-format, symmetrically-encrypted-value-format,
* asymmetrically-encrypted-value-format, cms-enveloped-data-format, cms-encrypted-data-format,
* cleartext-symmetric-keys */
const char *ietf_crypto_types[] = {"cleartext-passwords", "cleartext-private-keys", NULL};
/* all features */
const char *ietf_tcp_common[] = {"keepalives-supported", NULL};
/* all features */
const char *ietf_tcp_server[] = {"tcp-server-keepalives", NULL};
/* no proxy-connect, socks5-gss-api, socks5-username-password */
const char *ietf_tcp_client[] = {"local-binding-supported", "tcp-client-keepalives", NULL};
/* no ssh-x509-certs, asymmetric-key-pair-generation */
const char *ietf_ssh_common[] = {"algorithm-discovery", "transport-params", NULL};
/* no ssh-server-keepalives and local-user-auth-hostbased */
const char *ietf_ssh_server[] = {"local-users-supported", "local-user-auth-publickey", "local-user-auth-password", "local-user-auth-none", NULL};
/* all features */
const char *iana_ssh_encryption_algs[] = {NULL};
/* all features */
const char *iana_ssh_key_exchange_algs[] = {NULL};
/* all features */
const char *iana_ssh_mac_algs[] = {NULL};
/* all features */
const char *iana_ssh_public_key_algs[] = {NULL};
/* all features */
const char *iana_crypt_hash[] = {"crypt-hash-md5", "crypt-hash-sha-256", "crypt-hash-sha-512", NULL};
/* no symmetric-keys */
const char *ietf_keystore[] = {"central-keystore-supported", "inline-definitions-supported", "asymmetric-keys", NULL};
/* all features */
const char *ietf_truststore[] = {"central-truststore-supported", "inline-definitions-supported", "certificates", "public-keys", NULL};
/* no asymmetric-key-pair-generation */
const char *ietf_tls_common[] = {"algorithm-discovery", "tls12", "tls13", "hello-params", NULL};
/* no tls-server-keepalives, server-ident-raw-public-key, server-ident-tls12-psk, server-ident-tls13-epsk,
* client-auth-raw-public-key, client-auth-tls12-psk, client-auth-tls13-epsk */
const char *ietf_tls_server[] = {"server-ident-x509-cert", "client-auth-supported", "client-auth-x509-cert", NULL};
/* all features */
const char *iana_tls_cipher_suite_algs[] = {NULL};
/* no unix-socket-path */
const char *libnetconf2_netconf_server[] = {NULL};
const char *module_names[] = {
"ietf-netconf-server", "ietf-x509-cert-to-name", "ietf-crypto-types", "ietf-tcp-common", "ietf-tcp-server",
"ietf-tcp-client", "ietf-ssh-common", "ietf-ssh-server", "iana-ssh-encryption-algs",
"iana-ssh-key-exchange-algs", "iana-ssh-mac-algs", "iana-ssh-public-key-algs", "iana-crypt-hash",
"ietf-keystore", "ietf-truststore", "ietf-tls-common", "ietf-tls-server", "iana-tls-cipher-suite-algs",
"libnetconf2-netconf-server", NULL
};
const char **module_features[] = {
ietf_nectonf_server, ietf_x509_cert_to_name, ietf_crypto_types, ietf_tcp_common,
ietf_tcp_server, ietf_tcp_client, ietf_ssh_common, ietf_ssh_server, iana_ssh_encryption_algs,
iana_ssh_key_exchange_algs, iana_ssh_mac_algs, iana_ssh_public_key_algs, iana_crypt_hash,
ietf_keystore, ietf_truststore, ietf_tls_common, ietf_tls_server, iana_tls_cipher_suite_algs,
libnetconf2_netconf_server, NULL
};
for (i = 0; module_names[i]; i++) {
if (!ly_ctx_load_module(*ctx, module_names[i], NULL, module_features[i])) {
ERR(NULL, "Loading module \"%s\" failed.", module_names[i]);
goto error;
}
}
return 0;
error:
if (new_ctx) {
ly_ctx_destroy(*ctx);
*ctx = NULL;
}
return 1;
}
/*
* =====================================================================================
* ietf-netconf-server handlers
* =====================================================================================
*/
#ifdef NC_ENABLED_SSH_TLS
static int
config_local_address(const struct lyd_node *node, enum nc_operation parent_op, struct nc_bind *bind)
{
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
free(bind->address);
bind->address = NULL;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
free(bind->address);
bind->address = strdup(lyd_get_value(node));
NC_CHECK_ERRMEM_RET(!bind->address, 1);
}
return 0;
}
static int
config_local_port(const struct lyd_node *node, enum nc_operation UNUSED(parent_op), struct nc_bind *bind)
{
/* default value always present */
bind->port = strtoul(lyd_get_value(node), NULL, 10);
return 0;
}
static int
config_local_bind(const struct lyd_node *node, enum nc_operation parent_op, struct nc_endpt *endpt)
{
struct lyd_node *n;
enum nc_operation op;
struct nc_bind *bind = NULL;
const char *local_addr;
LY_ARRAY_COUNT_TYPE i = 0;
NC_NODE_GET_OP(node, parent_op, &op);
/* local address (local-bind list key) */
NC_CHECK_RET(nc_lyd_find_child_mandatory(node, "local-address", &n));
local_addr = lyd_get_value(n);
assert(local_addr);
if ((op == NC_OP_DELETE) || (op == NC_OP_NONE)) {
/* find the bind we are deleting/modifying */
LY_ARRAY_FOR(endpt->binds, i) {
if (!strcmp(endpt->binds[i].address, local_addr)) {
break;
}
}
if (i == LY_ARRAY_COUNT(endpt->binds)) {
ERR(NULL, "Local bind with address \"%s\" not found.", local_addr);
return 1;
}
bind = &endpt->binds[i];
} else if (op == NC_OP_CREATE) {
/* create a new bind */
LY_ARRAY_NEW_RET(LYD_CTX(node), endpt->binds, bind, 1);
/* init the new bind */
bind->sock = -1;
}
/* config local address */
NC_CHECK_RET(config_local_address(n, op, bind));
/* config local port (might not be present in diff) */
nc_lyd_find_child_optional(node, "local-port", &n);
if (n) {
NC_CHECK_RET(config_local_port(n, op, bind));
}
/* all children processed, we can now delete the bind */
if (op == NC_OP_DELETE) {
if (i < LY_ARRAY_COUNT(endpt->binds) - 1) {
endpt->binds[i] = endpt->binds[LY_ARRAY_COUNT(endpt->binds) - 1];
}
LY_ARRAY_DECREMENT_FREE(endpt->binds);
}
return 0;
}
static int
config_idle_time(const struct lyd_node *node, enum nc_operation UNUSED(parent_op), struct nc_keepalives *ka)
{
/* default value always present */
ka->idle_time = strtoul(lyd_get_value(node), NULL, 10);
return 0;
}
static int
config_max_probes(const struct lyd_node *node, enum nc_operation UNUSED(parent_op), struct nc_keepalives *ka)
{
/* default value always present */
ka->max_probes = strtoul(lyd_get_value(node), NULL, 10);
return 0;
}
static int
config_probe_interval(const struct lyd_node *node, enum nc_operation UNUSED(parent_op), struct nc_keepalives *ka)
{
/* default value always present */
ka->probe_interval = strtoul(lyd_get_value(node), NULL, 10);
return 0;
}
static int
config_tcp_keepalives(const struct lyd_node *node, enum nc_operation parent_op, struct nc_keepalives *ka)
{
struct lyd_node *n;
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
/* config idle-time (default value) */
nc_lyd_find_child_optional(node, "idle-time", &n);
if (n) {
config_idle_time(n, op, ka);
}
/* config max-probes (default value) */
nc_lyd_find_child_optional(node, "max-probes", &n);
if (n) {
config_max_probes(n, op, ka);
}
/* config probe-interval (default value) */
nc_lyd_find_child_optional(node, "probe-interval", &n);
if (n) {
config_probe_interval(n, op, ka);
}
/* all children processed */
if (op == NC_OP_DELETE) {
ka->enabled = 0;
} else if (op == NC_OP_CREATE) {
ka->enabled = 1;
}
return 0;
}
static int
config_tcp_server_params(const struct lyd_node *node, enum nc_operation parent_op, struct nc_endpt *endpt)
{
struct lyd_node *n;
enum nc_operation op;
struct ly_set *set = NULL;
uint32_t i;
NC_NODE_GET_OP(node, parent_op, &op);
/* configure all the local binds */
NC_CHECK_RET(lyd_find_xpath(node, "local-bind", &set), 1);
for (i = 0; i < set->count; i++) {
NC_CHECK_GOTO(config_local_bind(set->dnodes[i], op, endpt), cleanup);
}
/* keepalives (presence container) */
nc_lyd_find_child_optional(node, "keepalives", &n);
if (n) {
NC_CHECK_GOTO(config_tcp_keepalives(n, op, &endpt->ka), cleanup);
}
cleanup:
ly_set_free(set, NULL);
return 0;
}
static int
config_hostkey_name(const struct lyd_node *node, enum nc_operation parent_op, struct nc_hostkey *hostkey)
{
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
free(hostkey->name);
hostkey->name = NULL;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
free(hostkey->name);
hostkey->name = strdup(lyd_get_value(node));
NC_CHECK_ERRMEM_RET(!hostkey->name, 1);
}
return 0;
}
static int
config_pubkey_format(const struct lyd_node *node, enum nc_operation parent_op, struct nc_public_key *pubkey)
{
enum nc_operation op;
const char *format;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
pubkey->type = NC_PUBKEY_FORMAT_UNKNOWN;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
format = ((struct lyd_node_term *)node)->value.ident->name;
assert(format);
if (!strcmp(format, "ssh-public-key-format")) {
pubkey->type = NC_PUBKEY_FORMAT_SSH;
} else if (!strcmp(format, "subject-public-key-info-format")) {
pubkey->type = NC_PUBKEY_FORMAT_X509;
} else {
/* do not fail, the key may still be usable, or it may have come from a keystore/truststore
* and have a different purpose other than NETCONF */
WRN(NULL, "Public key format \"%s\" not supported. The key may not be usable.", format);
pubkey->type = NC_PUBKEY_FORMAT_UNKNOWN;
}
}
return 0;
}
static int
config_pubkey_data(const struct lyd_node *node, enum nc_operation parent_op, struct nc_public_key *pubkey)
{
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
free(pubkey->data);
pubkey->data = NULL;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
free(pubkey->data);
pubkey->data = strdup(lyd_get_value(node));
NC_CHECK_ERRMEM_RET(!pubkey->data, 1);
}
return 0;
}
static int
config_privkey_format(const struct lyd_node *node, enum nc_operation parent_op, struct nc_private_key *privkey)
{
enum nc_operation op;
const char *format;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
privkey->type = NC_PRIVKEY_FORMAT_UNKNOWN;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
format = ((struct lyd_node_term *)node)->value.ident->name;
assert(format);
if (!strcmp(format, "rsa-private-key-format")) {
privkey->type = NC_PRIVKEY_FORMAT_RSA;
} else if (!strcmp(format, "ec-private-key-format")) {
privkey->type = NC_PRIVKEY_FORMAT_EC;
} else if (!strcmp(format, "private-key-info-format")) {
privkey->type = NC_PRIVKEY_FORMAT_X509;
} else if (!strcmp(format, "openssh-private-key-format")) {
privkey->type = NC_PRIVKEY_FORMAT_OPENSSH;
} else {
/* do not fail, the key may still be usable, or it may have come from a keystore/truststore
* and have a different purpose other than NETCONF */
WRN(NULL, "Private key format \"%s\" not supported. The key may not be usable.", format);
privkey->type = NC_PRIVKEY_FORMAT_UNKNOWN;
}
}
return 0;
}
static int
config_cleartext_privkey_data(const struct lyd_node *node, enum nc_operation parent_op, struct nc_private_key *privkey)
{
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
free(privkey->data);
privkey->data = NULL;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
free(privkey->data);
privkey->data = strdup(lyd_get_value(node));
NC_CHECK_ERRMEM_RET(!privkey->data, 1);
}
return 0;
}
static int
config_hidden_privkey_data(const struct lyd_node *node, enum nc_operation UNUSED(parent_op))
{
CONFIG_LOG_UNSUPPORTED(node);
return 0;
}
static int
config_encrypted_privkey_data(const struct lyd_node *node, enum nc_operation UNUSED(parent_op))
{
CONFIG_LOG_UNSUPPORTED(node);
return 0;
}
static int
config_hostkey_pubkey_inline(const struct lyd_node *node, enum nc_operation parent_op, struct nc_hostkey *hostkey)
{
struct lyd_node *n, *cleartext = NULL, *hidden = NULL, *encrypted = NULL;
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
/* config pubkey format */
nc_lyd_find_child_optional(node, "public-key-format", &n);
if (n) {
NC_CHECK_RET(config_pubkey_format(n, op, &hostkey->key.pubkey));
}
/* config pubkey data */
nc_lyd_find_child_optional(node, "public-key", &n);
if (n) {
NC_CHECK_RET(config_pubkey_data(n, op, &hostkey->key.pubkey));
}
/* config private key format */
nc_lyd_find_child_optional(node, "private-key-format", &n);
if (n) {
NC_CHECK_RET(config_privkey_format(n, op, &hostkey->key.privkey));
}
/* config privkey data, mandatory case/choice node,
* up to 2 can be present in diff, 1 with delete and 1 with create */
nc_lyd_find_child_optional(node, "cleartext-private-key", &cleartext);
nc_lyd_find_child_optional(node, "hidden-private-key", &hidden);
nc_lyd_find_child_optional(node, "encrypted-private-key", &encrypted);
if (cleartext) {
NC_CHECK_RET(config_cleartext_privkey_data(cleartext, op, &hostkey->key.privkey));
}
if (hidden) {
NC_CHECK_RET(config_hidden_privkey_data(hidden, op));
}
if (encrypted) {
NC_CHECK_RET(config_encrypted_privkey_data(encrypted, op));
}
if (op == NC_OP_DELETE) {
hostkey->store = NC_STORE_UNKNOWN;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
hostkey->store = NC_STORE_LOCAL;
}
return 0;
}
static int
config_hostkey_pubkey_keystore(const struct lyd_node *node, enum nc_operation parent_op, struct nc_hostkey *hostkey)
{
enum nc_operation op;
NC_NODE_GET_OP(node, parent_op, &op);
if (op == NC_OP_DELETE) {
free(hostkey->ks_ref);
hostkey->ks_ref = NULL;
hostkey->store = NC_STORE_UNKNOWN;
} else if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
free(hostkey->ks_ref);
hostkey->ks_ref = strdup(lyd_get_value(node));
NC_CHECK_ERRMEM_RET(!hostkey->ks_ref, 1);
hostkey->store = NC_STORE_KEYSTORE;
}
return 0;
}
static int
config_hostkey_public_key(const struct lyd_node *node, enum nc_operation parent_op, struct nc_hostkey *hostkey)
{
enum nc_operation op;
struct lyd_node *inline_def = NULL, *keystore_ref = NULL;
NC_NODE_GET_OP(node, parent_op, &op);
/* config inline-definition / keystore ref */
nc_lyd_find_child_optional(node, "inline-definition", &inline_def);
nc_lyd_find_child_optional(node, "central-keystore-reference", &keystore_ref);
if (inline_def) {
NC_CHECK_RET(config_hostkey_pubkey_inline(inline_def, op, hostkey));
}
if (keystore_ref) {
NC_CHECK_RET(config_hostkey_pubkey_keystore(keystore_ref, op, hostkey));
}
return 0;
}
static int
config_hostkey_certificate(const struct lyd_node *node, enum nc_operation UNUSED(parent_op))
{
CONFIG_LOG_UNSUPPORTED(node);
return 0;
}
static int
config_ssh_hostkey(const struct lyd_node *node, enum nc_operation parent_op, struct nc_server_ssh_opts *ssh)
{
struct lyd_node *n, *public_key = NULL, *certificate = NULL;
enum nc_operation op;
struct nc_hostkey *hostkey = NULL;
const char *name;
LY_ARRAY_COUNT_TYPE i = 0;
NC_NODE_GET_OP(node, parent_op, &op);
/* hostkey name (list key) */
NC_CHECK_RET(nc_lyd_find_child_mandatory(node, "name", &n));
name = lyd_get_value(n);
assert(name);