@@ -254,16 +254,28 @@ store_history(void)
254254 free (history_file );
255255}
256256
257+ void
258+ load_auth_pref (struct lyd_node * match , int auth_pref_type )
259+ {
260+ uint16_t pref_value ;
261+ if (!strcmp (lyd_get_value (match ), "disabled" )) {
262+ pref_value = -1 ;
263+ } else {
264+ pref_value = strtoul (lyd_get_value (match ), NULL , 10 );
265+ }
266+ nc_client_ssh_set_auth_pref (auth_pref_type , pref_value );
267+ }
268+
257269void
258270load_config (void )
259271{
260272 char * netconf_dir = NULL , * config_file = NULL ;
261- struct lyd_node * config = NULL , * child ;
273+ struct lyd_node * config = NULL , * match = NULL , * client ;
262274 struct ly_ctx * ctx = NULL ;
263275
264276#ifdef NC_ENABLED_SSH_TLS
265277 const char * key_pub , * key_priv ;
266- struct lyd_node * auth_child , * pref_child , * key_child , * pair_child ;
278+ struct lyd_node * auth_pref , * parent , * key ;
267279#endif
268280
269281 if ((netconf_dir = get_netconf_dir ()) == NULL ) {
@@ -276,6 +288,11 @@ load_config(void)
276288 goto cleanup ;
277289 }
278290
291+ if (lys_parse_mem (ctx , netopeer2_cli_yang , LYS_IN_YANG , NULL )) {
292+ ERROR (__func__ , "Failed to load netopeer2-cli YANG module from memory." );
293+ goto cleanup ;
294+ }
295+
279296 if (asprintf (& config_file , "%s/config.xml" , netconf_dir ) == -1 ) {
280297 ERROR (__func__ , "asprintf() failed (%s:%d)." , __FILE__ , __LINE__ );
281298 ERROR (__func__ , "Unable to load configuration due to the previous error." );
@@ -286,88 +303,92 @@ load_config(void)
286303 goto cleanup ;
287304 }
288305
289- if (lyd_parse_data_path (ctx , config_file , LYD_XML , LYD_PARSE_ONLY | LYD_PARSE_OPAQ , 0 , & config )) {
306+ if (lyd_parse_data_path (ctx , config_file , LYD_XML , 0 , LYD_VALIDATE_PRESENT , & config )) {
290307 ERROR (__func__ , "Failed to load configuration of NETCONF client (lyxml_read_path failed)." );
291308 goto cleanup ;
292309 }
293310
294- if (strcmp (LYD_NAME (config ), "netconf-client" )) {
311+ if (! config || strcmp (LYD_NAME (config ), "netconf-client" )) {
295312 ERROR (__func__ , "Unknown stored configuration data." );
296313 goto cleanup ;
297314 }
298315
299- LY_LIST_FOR (lyd_child (config ), child ) {
300- if (!strcmp (LYD_NAME (child ), "editor" )) {
301- /* <netconf-client> -> <editor> */
302- free (opts .config_editor );
303- opts .config_editor = strdup (lyd_get_value (child ));
304- } else if (!strcmp (LYD_NAME (child ), "searchpath" )) {
305- /* <netconf-client> -> <searchpath> */
306- errno = 0 ;
307- if (!mkdir (lyd_get_value (child ), 00700 ) || (errno == EEXIST )) {
308- if (errno == 0 ) {
309- ERROR (__func__ , "Search path \"%s\" did not exist, created." , lyd_get_value (child ));
310- }
311- nc_client_set_schema_searchpath (lyd_get_value (child ));
312- } else {
313- ERROR (__func__ , "Search path \"%s\" cannot be created (%s)." , lyd_get_value (child ), strerror (errno ));
316+ client = config ;
317+
318+ /* <netconf-client> -> <editor> */
319+ lyd_find_path (client , "editor" , 0 , & match );
320+ if (match ) {
321+ free (opts .config_editor );
322+ opts .config_editor = strdup (lyd_get_value (match ));
323+ }
324+
325+ /* <netconf-client> -> <search-path> */
326+ lyd_find_path (client , "search-path" , 0 , & match );
327+ if (match ) {
328+ errno = 0 ;
329+ if (!mkdir (lyd_get_value (match ), 00700 ) || (errno == EEXIST )) {
330+ if (errno == 0 ) {
331+ ERROR (__func__ , "Search path \"%s\" did not exist, created." , lyd_get_value (match ));
314332 }
315- } else if (!strcmp (LYD_NAME (child ), "output-format" )) {
316- /* <netconf-client> -> <output-format> */
317- if (!strcmp (lyd_get_value (child ), "json" )) {
318- opts .output_format = LYD_JSON ;
319- } /* else default (formatted XML) */
320- } else if (!strcmp (LYD_NAME (child ), "shrink" )) {
321- /* <netconf-client> -> <shrink> */
322- if (!strcmp (lyd_get_value (child ), "true" )) {
323- opts .output_flag = 1 ;
324- } /* else default (formatted XML) */
333+ nc_client_set_schema_searchpath (lyd_get_value (match ));
334+ } else {
335+ ERROR (__func__ , "Search path \"%s\" cannot be created (%s)." , lyd_get_value (match ), strerror (errno ));
325336 }
337+ }
338+
339+ /* <netconf-client> -> <output-format> */
340+ lyd_find_path (client , "output-format" , 0 , & match );
341+ if (!strcmp (lyd_get_value (match ), "json" )) {
342+ opts .output_format = LYD_JSON ;
343+ } /* else default (formatted XML) */
344+
345+ /* <netconf-client> -> <shrink> */
346+ lyd_find_path (client , "shrink" , 0 , & match );
347+ if (!strcmp (lyd_get_value (match ), "true" )) {
348+ opts .output_flag = 1 ;
349+ } /* else default (formatted XML) */
326350#ifdef NC_ENABLED_SSH_TLS
327- else if (!strcmp (LYD_NAME (child ), "authentication ")) {
328- /* <netconf-client> -> <authentication> */
329- LY_LIST_FOR (lyd_child (child ), auth_child ) {
330- if (!strcmp (LYD_NAME (auth_child ), "method-preference" )) {
331- LY_LIST_FOR (lyd_child (auth_child ), pref_child ) {
332- uint16_t pref_value ;
333- if (!strcmp (lyd_get_value (pref_child ), "disabled" )) {
334- pref_value = -1 ;
335- } else {
336- pref_value = strtoul (lyd_get_value (pref_child ), NULL , 10 );
337- }
338-
339- if (!strcmp (LYD_NAME (pref_child ), "publickey" )) {
340- nc_client_ssh_set_auth_pref (NC_SSH_AUTH_PUBLICKEY , pref_value );
341- } else if (!strcmp (LYD_NAME (pref_child ), "interactive" )) {
342- nc_client_ssh_set_auth_pref (NC_SSH_AUTH_INTERACTIVE , pref_value );
343- } else if (!strcmp (LYD_NAME (pref_child ), "password" )) {
344- nc_client_ssh_set_auth_pref (NC_SSH_AUTH_PASSWORD , pref_value );
345- }
346- }
347- } else if (!strcmp (LYD_NAME (auth_child ), "keys" )) {
348- LY_LIST_FOR (lyd_child (auth_child ), key_child ) {
349- if (!strcmp (LYD_NAME (key_child ), "pair" )) {
350- key_pub = NULL ;
351- key_priv = NULL ;
352- LY_LIST_FOR (lyd_child (key_child ), pair_child ) {
353- if (!strcmp (LYD_NAME (pair_child ), "public" )) {
354- key_pub = lyd_get_value (pair_child );
355- } else if (!strcmp (LYD_NAME (pair_child ), "private" )) {
356- key_priv = lyd_get_value (pair_child );
357- }
358- }
359- if (key_pub && key_priv ) {
360- nc_client_ssh_ch_add_keypair (key_pub , key_priv );
361- nc_client_ssh_add_keypair (key_pub , key_priv );
362- }
363- }
364- }
365- }
351+ lyd_find_path (client , "authentication" , 0 , & auth_pref );
352+
353+ /* <netconf-client> -> <authentication> -> <method-preference>*/
354+ lyd_find_path (auth_pref , "method-preference" , 0 , & parent );
355+ lyd_find_path (parent , "publickey" , 0 , & match );
356+ load_auth_pref (match , NC_SSH_AUTH_PUBLICKEY );
357+
358+ lyd_find_path (parent , "interactive" , 0 , & match );
359+ load_auth_pref (match , NC_SSH_AUTH_INTERACTIVE );
360+
361+ lyd_find_path (parent , "password" , 0 , & match );
362+ load_auth_pref (match , NC_SSH_AUTH_PASSWORD );
363+
364+ /* <netconf-client> -> <authentication> -> <keys>*/
365+ lyd_find_path (auth_pref , "keys" , 0 , & parent );
366+ if (parent ) {
367+ LY_LIST_FOR (lyd_child (parent ), key ) {
368+ key_pub = NULL ;
369+ key_priv = NULL ;
370+
371+ lyd_find_path (key , "public" , 0 , & match );
372+ key_pub = lyd_get_value (match );
373+
374+ lyd_find_path (key , "private" , 0 , & match );
375+ key_priv = lyd_get_value (match );
376+
377+ if (key_pub && key_priv ) {
378+ nc_client_ssh_ch_add_keypair (key_pub , key_priv );
379+ nc_client_ssh_add_keypair (key_pub , key_priv );
366380 }
367381 }
368- #endif /* NC_ENABLED_SSH_TLS */
369382 }
370383
384+ /* <netconf-client> -> <authentication> -> <knownhost-mode>*/
385+ lyd_find_path (auth_pref , "knownhost-mode" , 0 , & match );
386+ str2knownhosts_mode (lyd_get_value (match ), & opts .knownhosts_mode );
387+
388+ nc_client_ssh_set_knownhosts_mode (opts .knownhosts_mode );
389+ nc_client_ssh_ch_set_knownhosts_mode (opts .knownhosts_mode );
390+ #endif /* NC_ENABLED_SSH_TLS */
391+
371392cleanup :
372393 lyd_free_tree (config );
373394 ly_ctx_destroy (ctx );
@@ -376,19 +397,19 @@ load_config(void)
376397}
377398
378399int
379- store_pref (int pref_type , struct lyd_node * pref_parent , const char * pref_name )
400+ store_auth_pref (int pref_type , struct lyd_node * auth_pref_parent , const char * auth_pref_name )
380401{
381402 int pref_value ;
382403 char buf [23 ];
383404
384405 pref_value = nc_client_ssh_get_auth_pref (pref_type );
385406 if (pref_value < 0 ) {
386- if (lyd_new_term (pref_parent , NULL , pref_name , "disabled" , 0 , NULL )) {
407+ if (lyd_new_term (auth_pref_parent , NULL , auth_pref_name , "disabled" , 0 , NULL )) {
387408 return 1 ;
388409 }
389410 } else {
390411 sprintf (buf , "%d" , pref_value );
391- if (lyd_new_term (pref_parent , NULL , pref_name , buf , 0 , NULL )) {
412+ if (lyd_new_term (auth_pref_parent , NULL , auth_pref_name , buf , 0 , NULL )) {
392413 return 1 ;
393414 }
394415 }
@@ -466,15 +487,15 @@ store_config(void)
466487 goto cleanup ;
467488 }
468489
469- if (store_pref (NC_SSH_AUTH_PUBLICKEY , pref , "publickey" )) {
490+ if (store_auth_pref (NC_SSH_AUTH_PUBLICKEY , pref , "publickey" )) {
470491 goto cleanup ;
471492 }
472493
473- if (store_pref (NC_SSH_AUTH_PASSWORD , pref , "password" )) {
494+ if (store_auth_pref (NC_SSH_AUTH_PASSWORD , pref , "password" )) {
474495 goto cleanup ;
475496 }
476497
477- if (store_pref (NC_SSH_AUTH_INTERACTIVE , pref , "interactive" )) {
498+ if (store_auth_pref (NC_SSH_AUTH_INTERACTIVE , pref , "interactive" )) {
478499 goto cleanup ;
479500 }
480501
0 commit comments