forked from CESNET/libnetconf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_client.c
3219 lines (2800 loc) · 106 KB
/
session_client.c
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 session_client.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief libnetconf2 session client functions
*
* @copyright
* Copyright (c) 2015 - 2021 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
#ifdef __linux__
# include <sys/syscall.h>
#endif
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#ifdef NC_ENABLED_SSH_TLS
#include <libssh/libssh.h>
#endif
#include <libyang/libyang.h>
#include "compat.h"
#include "config.h"
#include "log_p.h"
#include "messages_p.h"
#include "session_client.h"
#include "session_client_ch.h"
#include "session_p.h"
#include "../modules/ietf_netconf@2013-09-29_yang.h"
#include "../modules/ietf_netconf_monitoring@2010-10-04_yang.h"
static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"};
#ifdef NC_ENABLED_SSH_TLS
char *sshauth_password(const char *username, const char *hostname, void *priv);
char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv);
char *sshauth_privkey_passphrase(const char *privkey_path, void *priv);
#endif /* NC_ENABLED_SSH_TLS */
static pthread_once_t nc_client_context_once = PTHREAD_ONCE_INIT;
static pthread_key_t nc_client_context_key;
#ifdef __linux__
static struct nc_client_context context_main = {
.opts.ka = {
.enabled = 1,
.idle_time = 1,
.max_probes = 10,
.probe_interval = 5
},
#ifdef NC_ENABLED_SSH_TLS
.ssh_opts = {
.auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
.auth_password = sshauth_password,
.auth_interactive = sshauth_interactive,
.auth_privkey_passphrase = sshauth_privkey_passphrase,
.knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK
},
.ssh_ch_opts = {
.auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
.auth_password = sshauth_password,
.auth_interactive = sshauth_interactive,
.auth_privkey_passphrase = sshauth_privkey_passphrase,
.knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK
},
#endif /* NC_ENABLED_SSH_TLS */
/* .tls_ structures zeroed */
.refcount = 0
};
#endif
static void
nc_client_context_free(void *ptr)
{
struct nc_client_context *c = (struct nc_client_context *)ptr;
if (--(c->refcount)) {
/* still used */
return;
}
#ifdef __linux__
/* in __linux__ we use static memory in the main thread,
* so this check is for programs terminating the main()
* function by pthread_exit() :)
*/
if (c != &context_main)
#endif
{
/* for the main thread the same is done in nc_client_destroy() */
free(c->opts.schema_searchpath);
#ifdef NC_ENABLED_SSH_TLS
int i;
for (i = 0; i < c->opts.ch_bind_count; ++i) {
close(c->opts.ch_binds[i].sock);
free((char *)c->opts.ch_binds[i].address);
}
free(c->opts.ch_binds);
c->opts.ch_binds = NULL;
c->opts.ch_bind_count = 0;
_nc_client_ssh_destroy_opts(&c->ssh_opts);
_nc_client_ssh_destroy_opts(&c->ssh_ch_opts);
_nc_client_tls_destroy_opts(&c->tls_opts);
_nc_client_tls_destroy_opts(&c->tls_ch_opts);
#endif /* NC_ENABLED_SSH_TLS */
free(c);
}
}
static void
nc_client_context_createkey(void)
{
int r;
/* initiate */
while ((r = pthread_key_create(&nc_client_context_key, nc_client_context_free)) == EAGAIN) {}
pthread_setspecific(nc_client_context_key, NULL);
}
struct nc_client_context *
nc_client_context_location(void)
{
struct nc_client_context *e;
pthread_once(&nc_client_context_once, nc_client_context_createkey);
e = pthread_getspecific(nc_client_context_key);
if (!e) {
/* prepare ly_err storage */
#ifdef __linux__
if (getpid() == syscall(SYS_gettid)) {
/* main thread - use global variable instead of thread-specific variable. */
e = &context_main;
} else
#endif /* __linux__ */
{
e = calloc(1, sizeof *e);
/* set default values */
e->refcount = 1;
#ifdef NC_ENABLED_SSH_TLS
# ifdef HAVE_TERMIOS
e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK;
# else
e->ssh_opts.knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT;
# endif
e->ssh_opts.auth_pref[0].type = NC_SSH_AUTH_INTERACTIVE;
e->ssh_opts.auth_pref[0].value = 1;
e->ssh_opts.auth_pref[1].type = NC_SSH_AUTH_PASSWORD;
e->ssh_opts.auth_pref[1].value = 2;
e->ssh_opts.auth_pref[2].type = NC_SSH_AUTH_PUBLICKEY;
e->ssh_opts.auth_pref[2].value = 3;
e->ssh_opts.auth_password = sshauth_password;
e->ssh_opts.auth_interactive = sshauth_interactive;
e->ssh_opts.auth_privkey_passphrase = sshauth_privkey_passphrase;
/* callhome settings are the same */
memcpy(&e->ssh_ch_opts, &e->ssh_opts, sizeof e->ssh_ch_opts);
e->ssh_ch_opts.auth_pref[0].value = 1;
e->ssh_ch_opts.auth_pref[1].value = 2;
e->ssh_ch_opts.auth_pref[2].value = 3;
#endif /* NC_ENABLED_SSH_TLS */
}
pthread_setspecific(nc_client_context_key, e);
}
return e;
}
#define client_opts nc_client_context_location()->opts
API void *
nc_client_get_thread_context(void)
{
return nc_client_context_location();
}
API void
nc_client_set_thread_context(void *context)
{
struct nc_client_context *old, *new;
if (!context) {
ERRARG(NULL, "context");
return;
}
new = (struct nc_client_context *)context;
old = nc_client_context_location();
if (old == new) {
/* nothing to change */
return;
}
/* replace old by new, increase reference counter in the newly set context */
nc_client_context_free(old);
new->refcount++;
pthread_setspecific(nc_client_context_key, new);
}
/**
* @brief Ext data callback for a context to provide schema mount data.
*/
static LY_ERR
nc_ly_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
{
struct nc_session *session = user_data;
if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
return LY_EINVAL;
}
if (!session->opts.client.ext_data) {
ERR(session, "Unable to parse mounted data, no operational schema-mounts data received from the server.");
return LY_ENOTFOUND;
}
/* return ext data */
*ext_data = session->opts.client.ext_data;
*ext_data_free = 0;
return LY_SUCCESS;
}
int
nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx)
{
/* assign context (dicionary needed for handshake) */
if (!ctx) {
if (ly_ctx_new(NULL, LY_CTX_NO_YANGLIBRARY, &ctx)) {
return EXIT_FAILURE;
}
/* user path must be first, the first path is used to store modules retreived via get-schema */
if (client_opts.schema_searchpath) {
ly_ctx_set_searchdir(ctx, client_opts.schema_searchpath);
}
if (!access(NC_CLIENT_SEARCH_DIR, F_OK)) {
ly_ctx_set_searchdir(ctx, NC_CLIENT_SEARCH_DIR);
}
/* set callback for getting modules, if provided */
ly_ctx_set_module_imp_clb(ctx, client_opts.schema_clb, client_opts.schema_clb_data);
/* set ext data callback to avoid errors that no callback is set, the data are stored later, if any */
ly_ctx_set_ext_data_clb(ctx, nc_ly_ext_data_clb, session);
} else {
session->flags |= NC_SESSION_SHAREDCTX;
}
session->ctx = ctx;
return EXIT_SUCCESS;
}
API int
nc_client_set_schema_searchpath(const char *path)
{
if (client_opts.schema_searchpath) {
free(client_opts.schema_searchpath);
}
if (path) {
client_opts.schema_searchpath = strdup(path);
NC_CHECK_ERRMEM_RET(!client_opts.schema_searchpath, 1);
} else {
client_opts.schema_searchpath = NULL;
}
return 0;
}
API const char *
nc_client_get_schema_searchpath(void)
{
return client_opts.schema_searchpath;
}
API int
nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data)
{
client_opts.schema_clb = clb;
if (clb) {
client_opts.schema_clb_data = user_data;
} else {
client_opts.schema_clb_data = NULL;
}
return 0;
}
API ly_module_imp_clb
nc_client_get_schema_callback(void **user_data)
{
if (user_data) {
(*user_data) = client_opts.schema_clb_data;
}
return client_opts.schema_clb;
}
API void
nc_client_set_new_session_context_autofill(int enabled)
{
client_opts.auto_context_fill_disabled = !enabled;
}
struct module_info {
char *name;
char *revision;
struct {
char *name;
char *revision;
} *submodules;
char **features;
int implemented;
};
struct clb_data_s {
void *user_data;
ly_module_imp_clb user_clb;
struct module_info *modules;
struct nc_session *session;
int has_get_schema;
};
/**
* @brief Retrieve YANG module content from a local file.
*
* @param[in] name Module name.
* @param[in] rev Module revision.
* @param[in] clb_data get-schema callback data.
* @param[out] format Module format.
* @return Module content.
*/
static char *
retrieve_module_data_localfile(const char *name, const char *rev, struct clb_data_s *clb_data,
LYS_INFORMAT *format)
{
char *localfile = NULL, *model_data = NULL;
const char *ptr;
FILE *f;
long length, l;
if (lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx),
!(ly_ctx_get_options(clb_data->session->ctx) & LY_CTX_DISABLE_SEARCHDIR_CWD),
name, rev, &localfile, format)) {
return NULL;
}
if (localfile && rev) {
ptr = strrchr(localfile, '/');
if (!strchr(ptr, '@')) {
/* we do not know the revision of the module and we require a specific one, so ignore this module */
localfile = NULL;
}
}
if (!localfile) {
return NULL;
}
VRB(clb_data->session, "Reading module \"%s@%s\" from local file \"%s\".", name, rev ? rev : "<latest>",
localfile);
f = fopen(localfile, "r");
if (!f) {
ERR(clb_data->session, "Unable to open file \"%s\" (%s).", localfile, strerror(errno));
free(localfile);
return NULL;
}
fseek(f, 0, SEEK_END);
length = ftell(f);
if (length < 0) {
ERR(clb_data->session, "Unable to get the size of module file \"%s\".", localfile);
free(localfile);
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_SET);
model_data = malloc(length + 1);
if (!model_data) {
ERRMEM;
} else if ((l = fread(model_data, 1, length, f)) != length) {
ERR(clb_data->session, "Reading module from \"%s\" failed (%d bytes read, but %d expected).", localfile, l,
length);
free(model_data);
model_data = NULL;
} else {
/* terminating NULL byte */
model_data[length] = '\0';
}
fclose(f);
free(localfile);
return model_data;
}
/**
* @brief Retrieve YANG module content from a reply to get-schema RPC.
*
* @param[in] name Module name.
* @param[in] rev Module revision.
* @param[in] clb_data get-schema callback data.
* @param[out] format Module format.
* @return Module content.
*/
static char *
retrieve_module_data_getschema(const char *name, const char *rev, struct clb_data_s *clb_data,
LYS_INFORMAT *format)
{
struct nc_rpc *rpc;
struct lyd_node *envp = NULL, *op = NULL;
struct lyd_node_any *get_schema_data;
NC_MSG_TYPE msg;
uint64_t msgid;
char *localfile = NULL, *envp_str = NULL, *model_data = NULL;
FILE *f;
VRB(clb_data->session, "Reading module \"%s@%s\" from server via get-schema.", name, rev ? rev : "<latest>");
rpc = nc_rpc_getschema(name, rev, "yang", NC_PARAMTYPE_CONST);
while ((msg = nc_send_rpc(clb_data->session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
usleep(1000);
}
if (msg == NC_MSG_ERROR) {
ERR(clb_data->session, "Failed to send the <get-schema> RPC.");
nc_rpc_free(rpc);
return NULL;
}
do {
msg = nc_recv_reply(clb_data->session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
} while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
nc_rpc_free(rpc);
if (msg == NC_MSG_WOULDBLOCK) {
ERR(clb_data->session, "Timeout for receiving reply to a <get-schema> expired.");
goto cleanup;
} else if (msg == NC_MSG_ERROR) {
ERR(clb_data->session, "Failed to receive a reply to <get-schema>.");
goto cleanup;
} else if (!op) {
assert(envp);
lyd_print_mem(&envp_str, envp, LYD_XML, 0);
WRN(clb_data->session, "Received an unexpected reply to <get-schema>:\n%s", envp_str);
free(envp_str);
goto cleanup;
}
if (!lyd_child(op) || (lyd_child(op)->schema->nodetype != LYS_ANYXML)) {
ERR(clb_data->session, "Unexpected data in reply to a <get-schema> RPC.");
goto cleanup;
}
get_schema_data = (struct lyd_node_any *)lyd_child(op);
switch (get_schema_data->value_type) {
case LYD_ANYDATA_STRING:
case LYD_ANYDATA_XML:
model_data = strdup(get_schema_data->value.str);
break;
case LYD_ANYDATA_DATATREE:
lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
break;
case LYD_ANYDATA_JSON:
case LYD_ANYDATA_LYB:
ERRINT;
break;
}
if (model_data && !model_data[0]) {
/* empty data */
free(model_data);
model_data = NULL;
}
if (!model_data) {
goto cleanup;
}
/* set format */
*format = LYS_IN_YANG;
/* try to store the model_data into local module repository */
lys_search_localfile(ly_ctx_get_searchdirs(clb_data->session->ctx), 0, name, rev, &localfile, NULL);
if (client_opts.schema_searchpath && !localfile) {
if (asprintf(&localfile, "%s/%s%s%s.yang", client_opts.schema_searchpath, name, rev ? "@" : "",
rev ? rev : "") == -1) {
ERRMEM;
} else {
f = fopen(localfile, "w");
if (!f) {
WRN(clb_data->session, "Unable to store \"%s\" as a local copy of module retrieved via <get-schema> (%s).",
localfile, strerror(errno));
} else {
fputs(model_data, f);
fclose(f);
}
}
}
free(localfile);
cleanup:
lyd_free_tree(envp);
lyd_free_tree(op);
return model_data;
}
static void
free_with_user_data(void *data, void *user_data)
{
free(data);
(void)user_data;
}
/**
* @brief Retrieve YANG module content.
*
* @param[in] mod_name Module name.
* @param[in] mod_rev Module revision.
* @param[in] user_data get-schema callback data.
* @param[out] format Module format.
* @param[out] module_data Module content.
* @param[out] free_module_data Callback for freeing @p module_data.
* @return LY_ERR value.
*/
static LY_ERR
retrieve_module_data(const char *mod_name, const char *mod_rev, void *user_data, LYS_INFORMAT *format,
const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
{
struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
char *model_data = NULL;
/* 1. try to get data locally */
model_data = retrieve_module_data_localfile(mod_name, mod_rev, clb_data, format);
/* 2. try to use <get-schema> */
if (!model_data && clb_data->has_get_schema) {
model_data = retrieve_module_data_getschema(mod_name, mod_rev, clb_data, format);
}
/* 3. try to use user callback */
if (!model_data && clb_data->user_clb) {
VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", mod_name, mod_rev ? mod_rev : "<latest>");
clb_data->user_clb(mod_name, mod_rev, NULL, NULL, clb_data->user_data, format, (const char **)&model_data,
free_module_data);
}
*free_module_data = free_with_user_data;
*module_data = model_data;
return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
}
/**
* @brief Retrieve YANG import module content.
*
* @param[in] mod_name Module name.
* @param[in] mod_rev Module revision.
* @param[in] submod_name Optional submodule name.
* @param[in] sub_rev Submodule revision.
* @param[in] user_data get-schema callback data.
* @param[out] format Module format.
* @param[out] module_data Module content.
* @param[out] free_module_data Callback for freeing @p module_data.
* @return LY_ERR value.
*/
static LY_ERR
retrieve_module_data_imp(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
void *user_data, LYS_INFORMAT *format, const char **module_data,
void (**free_module_data)(void *model_data, void *user_data))
{
struct clb_data_s *clb_data = (struct clb_data_s *)user_data;
uint32_t u, v, match = 1;
const char *name = NULL, *rev = NULL;
char *model_data = NULL;
/* get and check the final name and revision of the module to be retrieved */
if (!mod_rev || !mod_rev[0]) {
/* newest revision requested - get the newest revision from the list of available modules on server */
match = 0;
for (u = 0; clb_data->modules[u].name; ++u) {
if (strcmp(mod_name, clb_data->modules[u].name)) {
continue;
}
if (!match || (strcmp(mod_rev, clb_data->modules[u].revision) > 0)) {
mod_rev = clb_data->modules[u].revision;
}
match = u + 1;
}
if (!match) {
/* valid situation if we are retrieving YANG 1.1 module and have only capabilities for now
* (when loading ietf-datastore for ietf-yang-library) */
VRB(clb_data->session, "Unable to identify revision of the import module \"%s\" from "
"the available server side information.", mod_name);
}
}
if (submod_name) {
name = submod_name;
if (sub_rev) {
rev = sub_rev;
} else if (match) {
if (!clb_data->modules[match - 1].submodules) {
VRB(clb_data->session, "Unable to identify revision of the requested submodule \"%s\", "
"in import module \"%s\", from the available server side information.", submod_name, mod_name);
} else {
for (v = 0; clb_data->modules[match - 1].submodules[v].name; ++v) {
if (!strcmp(submod_name, clb_data->modules[match - 1].submodules[v].name)) {
rev = sub_rev = clb_data->modules[match - 1].submodules[v].revision;
}
}
if (!rev) {
ERR(clb_data->session, "Requested submodule \"%s\" is not found in import module \"%s\" on server side.",
submod_name, mod_name);
return LY_ENOTFOUND;
}
}
}
} else {
name = mod_name;
rev = mod_rev;
}
if (match) {
/* we have enough information to avoid communication with server and try to get the module locally */
/* 1. try to get data locally */
model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
/* 2. try to use <get-schema> */
if (!model_data && clb_data->has_get_schema) {
model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
}
} else {
/* we are unsure which revision of the module we should load, so first try to get
* the newest revision from the server via get-schema and only if the server does not
* implement get-schema, try to load the newest revision locally. This is imperfect
* solution, but there are situation when a client does not know what revision is
* actually implemented by the server. */
/* 1. try to use <get-schema> */
if (clb_data->has_get_schema) {
model_data = retrieve_module_data_getschema(name, rev, clb_data, format);
}
/* 2. try to get data locally */
if (!model_data) {
model_data = retrieve_module_data_localfile(name, rev, clb_data, format);
}
}
/* 3. try to use user callback */
if (!model_data && clb_data->user_clb) {
VRB(clb_data->session, "Reading module \"%s@%s\" via user callback.", name, rev ? rev : "<latest>");
clb_data->user_clb(mod_name, mod_rev, submod_name, sub_rev, clb_data->user_data, format,
(const char **)&model_data, free_module_data);
}
*free_module_data = free_with_user_data;
*module_data = model_data;
return *module_data ? LY_SUCCESS : LY_ENOTFOUND;
}
/**
* @brief Load a YANG module into context.
*
* @param[in] session NC session.
* @param[in] name Module name.
* @param[in] revision Module revision.
* @param[in] features Enabled module features.
* @param[in] modules Server module info built from capabilities.
* @param[in] user_clb User callback for retrieving module data.
* @param[in] user_data User data for @p user_clb.
* @param[in] has_get_schema Whether the server supports get-schema.
* @param[out] mod Loaded module.
* @return 0 on success.
* @return -1 on error.
*/
static int
nc_ctx_load_module(struct nc_session *session, const char *name, const char *revision, const char **features,
struct module_info *modules, ly_module_imp_clb user_clb, void *user_data, int has_get_schema, struct lys_module **mod)
{
int ret = 0;
const struct ly_err_item *eitem;
const char *module_data = NULL;
struct ly_in *in;
LYS_INFORMAT format;
uint32_t temp_lo = LY_LOSTORE, *prev_lo;
void (*free_module_data)(void *, void *) = NULL;
struct clb_data_s clb_data;
/* try to use a module from the context */
*mod = ly_ctx_get_module_implemented(session->ctx, name);
if (!*mod) {
if (revision) {
*mod = ly_ctx_get_module(session->ctx, name, revision);
} else {
*mod = ly_ctx_get_module_latest(session->ctx, name);
}
} else if (revision && (!(*mod)->revision || strcmp((*mod)->revision, revision))) {
WRN(session, "Server implements module \"%s\" in revision \"%s\" but revision \"%s\" is already implemented"
" and will be used instead.", name, revision, (*mod)->revision ? (*mod)->revision : "<none>");
}
if (*mod) {
/* make the present module implemented and/or enable all its features */
if (lys_set_implemented(*mod, features)) {
ERR(session, "Failed to implement module \"%s\".", (*mod)->name);
return -1;
}
return 0;
}
/* missing implemented module, load it ... */
clb_data.has_get_schema = has_get_schema;
clb_data.modules = modules;
clb_data.session = session;
clb_data.user_clb = user_clb;
clb_data.user_data = user_data;
/* clear all the errors and just collect them for now */
ly_err_clean(session->ctx, NULL);
prev_lo = ly_temp_log_options(&temp_lo);
/* get module data */
if (!retrieve_module_data(name, revision, &clb_data, &format, &module_data, &free_module_data)) {
/* set import callback */
ly_ctx_set_module_imp_clb(session->ctx, retrieve_module_data_imp, &clb_data);
/* parse the module */
ly_in_new_memory(module_data, &in);
lys_parse(session->ctx, in, format, features, mod);
ly_in_free(in, 0);
if (free_module_data) {
free_module_data((char *)module_data, user_data);
}
ly_ctx_set_module_imp_clb(session->ctx, NULL, NULL);
}
/* restore logging options, then print errors on definite failure */
ly_temp_log_options(prev_lo);
if (!(*mod)) {
for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
ly_err_print(session->ctx, eitem);
}
ret = -1;
} else {
/* print only warnings */
for (eitem = ly_err_first(session->ctx); eitem; eitem = eitem->next) {
if (eitem->level == LY_LLWRN) {
ly_err_print(session->ctx, eitem);
}
}
}
/* clean the errors */
ly_err_clean(session->ctx, NULL);
return ret;
}
static void
free_module_info(struct module_info *list)
{
uint32_t u, v;
if (!list) {
return;
}
for (u = 0; list[u].name; ++u) {
free(list[u].name);
free(list[u].revision);
if (list[u].features) {
for (v = 0; list[u].features[v]; ++v) {
free(list[u].features[v]);
}
free(list[u].features);
}
if (list[u].submodules) {
for (v = 0; list[u].submodules[v].name; ++v) {
free(list[u].submodules[v].name);
free(list[u].submodules[v].revision);
}
free(list[u].submodules);
}
}
free(list);
}
/**
* @brief Retrieve yang-library and schema-mounts operational data from the server.
*
* @param[in] session NC session.
* @param[in] has_get_data Whether get-data RPC is available or only get.
* @param[in] filter Filter to use.
* @param[out] oper_data Received data.
* @return 0 on success.
* @return -1 on error.
*/
static int
get_oper_data(struct nc_session *session, int has_get_data, const char *filter, struct lyd_node **oper_data)
{
struct nc_rpc *rpc = NULL;
struct lyd_node *op = NULL, *envp = NULL;
struct lyd_node_any *data;
NC_MSG_TYPE msg;
uint64_t msgid;
int ret = 0;
const char *rpc_name;
/* get data from the server */
if (has_get_data) {
rpc_name = "<get-data>";
rpc = nc_rpc_getdata("ietf-datastores:operational", filter, "false", NULL, 0, 0, 0, 0, 0, NC_PARAMTYPE_CONST);
} else {
rpc_name = "<get>";
rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
}
if (!rpc) {
goto cleanup;
}
while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) {
usleep(1000);
}
if (msg == NC_MSG_ERROR) {
WRN(session, "Failed to send %s RPC.", rpc_name);
goto cleanup;
}
do {
lyd_free_tree(envp);
lyd_free_tree(op);
msg = nc_recv_reply(session, rpc, msgid, NC_READ_ACT_TIMEOUT * 1000, &envp, &op);
} while (msg == NC_MSG_NOTIF || msg == NC_MSG_REPLY_ERR_MSGID);
if (msg == NC_MSG_WOULDBLOCK) {
WRN(session, "Timeout for receiving reply to a %s RPC expired.", rpc_name);
goto cleanup;
} else if (msg == NC_MSG_ERROR) {
WRN(session, "Failed to receive a reply to %s RPC.", rpc_name);
goto cleanup;
} else if (!op || !lyd_child(op) || !lyd_child(op)->schema || strcmp(lyd_child(op)->schema->name, "data")) {
WRN(session, "Unexpected reply without data to a %s RPC.", rpc_name);
goto cleanup;
}
data = (struct lyd_node_any *)lyd_child(op);
if (data->value_type != LYD_ANYDATA_DATATREE) {
WRN(session, "Unexpected data in reply to a %s RPC.", rpc_name);
goto cleanup;
} else if (!data->value.tree) {
WRN(session, "No data in reply to a %s RPC.", rpc_name);
goto cleanup;
}
*oper_data = data->value.tree;
data->value.tree = NULL;
cleanup:
nc_rpc_free(rpc);
lyd_free_tree(envp);
lyd_free_tree(op);
if (session->status != NC_STATUS_RUNNING) {
/* something bad happened, discard the session */
ERR(session, "Invalid session, discarding.");
ret = -1;
}
return ret;
}
/**
* @brief Build server module info from ietf-yang-library data.
*
* @param[in] session NC session.
* @param[in] get_data_sup Whether get-data RPC is available or only get.
* @param[in] xpath_sup Whether XPath filter is supported or only subtree filter.
* @param[out] result Server modules.
* @return 0 on success.
* @return -1 on error.
*/
static int
build_module_info_yl(struct nc_session *session, int get_data_sup, int xpath_sup, struct module_info **result)
{
struct ly_set *modules = NULL;
uint32_t u, v, submodules_count, feature_count;
struct lyd_node *iter, *child, *oper_data = NULL;
struct lys_module *mod;
int ret = 0;
uint8_t notifications_found = 0;
uint8_t nc_notifications_found = 0;
/* get yang-library operational data */
if (xpath_sup) {
if (get_oper_data(session, get_data_sup, "/ietf-yang-library:*", &oper_data)) {
goto cleanup;
}
} else {
if (get_oper_data(session, get_data_sup,
"<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\"/>", &oper_data)) {
goto cleanup;
}
}
if (!oper_data) {
goto cleanup;
}
if (lyd_find_xpath(oper_data, "/ietf-yang-library:modules-state/module", &modules)) {
WRN(NULL, "No yang-library module information found.");
goto cleanup;
}
(*result) = calloc(modules->count + 1, sizeof **result);
NC_CHECK_ERRMEM_GOTO(!(*result), ret = -1, cleanup);
for (u = 0; u < modules->count; ++u) {
submodules_count = 0;
feature_count = 0;
mod = ((struct lyd_node *)modules->dnodes[u])->schema->module;
LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
if (!iter->schema || (iter->schema->module != mod)) {
/* ignore node from other schemas (augments) */
continue;
}
if (!lyd_get_value(iter) || !lyd_get_value(iter)[0]) {
/* ignore empty nodes */
continue;
}
if (!strcmp(iter->schema->name, "name")) {
(*result)[u].name = strdup(lyd_get_value(iter));
if (!strcmp((*result)[u].name, "notifications")) {
notifications_found = 1;
} else if (!strcmp((*result)[u].name, "nc-notifications")) {
nc_notifications_found = 1;
}
} else if (!strcmp(iter->schema->name, "revision")) {
(*result)[u].revision = strdup(lyd_get_value(iter));
} else if (!strcmp(iter->schema->name, "conformance-type")) {
(*result)[u].implemented = !strcmp(lyd_get_value(iter), "implement");
} else if (!strcmp(iter->schema->name, "feature")) {
(*result)[u].features = nc_realloc((*result)[u].features, (feature_count + 2) * sizeof *(*result)[u].features);
NC_CHECK_ERRMEM_GOTO(!(*result)[u].features, free_module_info(*result); *result = NULL; ret = -1, cleanup);
(*result)[u].features[feature_count] = strdup(lyd_get_value(iter));
(*result)[u].features[feature_count + 1] = NULL;
++feature_count;
} else if (!strcmp(iter->schema->name, "submodule")) {
submodules_count++;
}
}
if (submodules_count) {
(*result)[u].submodules = calloc(submodules_count + 1, sizeof *(*result)[u].submodules);
NC_CHECK_ERRMEM_GOTO(!(*result)[u].submodules, free_module_info(*result); *result = NULL; ret = -1, cleanup);
v = 0;
LY_LIST_FOR(lyd_child(modules->dnodes[u]), iter) {
mod = modules->dnodes[u]->schema->module;
if ((mod == iter->schema->module) && !strcmp(iter->schema->name, "submodule")) {
LY_LIST_FOR(lyd_child(iter), child) {
if (mod != child->schema->module) {
continue;
} else if (!strcmp(child->schema->name, "name")) {
(*result)[u].submodules[v].name = strdup(lyd_get_value(child));
} else if (!strcmp(child->schema->name, "revision")) {
(*result)[u].submodules[v].revision = strdup(lyd_get_value(child));
}
}
}
}
}
}
/* If NETCONF server supports RFC5277 notification capability and libnetconf2