blob: 6115fcbbdf8e5e109677b2ce23bf0356b596cc35 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file session_client.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 session client manipulation
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
Michal Vaskoc446a382021-06-18 08:54:05 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * 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
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
16#ifndef NC_SESSION_CLIENT_H_
17#define NC_SESSION_CLIENT_H_
18
Michal Vaskoc09730e2019-01-17 10:07:26 +010019#ifdef __cplusplus
20extern "C" {
21#endif
22
Michal Vasko086311b2016-01-08 09:53:11 +010023#include <libyang/libyang.h>
24
Radek Krejci53691be2016-02-22 13:58:37 +010025#ifdef NC_ENABLED_SSH
Michal Vaskoc446a382021-06-18 08:54:05 +020026# include <libssh/libssh.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#endif
28
Radek Krejci53691be2016-02-22 13:58:37 +010029#ifdef NC_ENABLED_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +020030# include <openssl/ssl.h>
Michal Vasko086311b2016-01-08 09:53:11 +010031#endif
32
Michal Vasko7bcb48e2016-01-15 10:28:54 +010033#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020034#include "netconf.h"
35#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010036
37/**
Radek Krejci6799a052017-05-19 14:23:23 +020038 * @addtogroup client
39 * @{
40 */
41
42/**
Michal Vasko086311b2016-01-08 09:53:11 +010043 * @brief Set location where libnetconf tries to search for YANG/YIN schemas.
44 *
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010045 * The location is searched when connecting to a NETCONF server and building
Michal Vasko086311b2016-01-08 09:53:11 +010046 * YANG context for further processing of the NETCONF messages and data.
47 *
Radek Krejcifd5b6682017-06-13 15:52:53 +020048 * The searchpath is also used to store schemas retreived via \<get-schema\>
49 * operation - if the schema is not found in searchpath neither via schema
50 * callback provided via nc_client_set_schema_callback() and server supports
51 * the NETCONF \<get-schema\> operation, the schema is retrieved this way and
52 * stored into the searchpath (if specified).
53 *
Michal Vasko086311b2016-01-08 09:53:11 +010054 * @param[in] path Directory where to search for YANG/YIN schemas.
55 * @return 0 on success, 1 on (memory allocation) failure.
56 */
Michal Vasko7f1c0ef2016-03-11 11:13:06 +010057int nc_client_set_schema_searchpath(const char *path);
58
59/**
60 * @brief Get schema searchpath that was set by nc_client_set_schema_searchpath().
61 *
62 * @return Schema searchpath directory, NULL if not set.
63 */
64const char *nc_client_get_schema_searchpath(void);
Michal Vasko086311b2016-01-08 09:53:11 +010065
66/**
Radek Krejcifd5b6682017-06-13 15:52:53 +020067 * @brief Set callback function to get missing schemas.
68 *
69 * @param[in] clb Callback responsible for returning the missing model.
Michal Vaskoc446a382021-06-18 08:54:05 +020070 * @param[in] user_data Arbitrary data that will always be passed to the callback @p clb.
Radek Krejcifd5b6682017-06-13 15:52:53 +020071 * @return 0 on success, 1 on (memory allocation) failure.
72 */
73int nc_client_set_schema_callback(ly_module_imp_clb clb, void *user_data);
74
75/**
76 * @brief Get callback function used to get missing schemas.
77 *
78 * @param[out] user_data Optionally return the private data set with the callback.
79 * Note that the caller is responsible for freeing the private data, so before
80 * changing the callback, private data used for the previous callback should be
81 * freed.
82 * @return Pointer to the set callback, NULL if no such callback was set.
83 */
84ly_module_imp_clb nc_client_get_schema_callback(void **user_data);
85
86/**
Michal Vaskoa272e092023-07-10 14:34:03 +020087 * @brief Enable/disable loading of all the YANG modules supported by
88 * the server when a new session is created. If disabled, it is expected
89 * that users update the context themselves and load the YANG modules
90 * that are planned to be used. Otherwise even basic RPCs may fail to be sent!
91 *
92 * @param[in] enabled Whether context autofill is enabled or disabled.
93 */
94void nc_client_set_new_session_context_autofill(int enabled);
95
96/**
Radek Krejci5cebc6b2017-05-26 13:24:38 +020097 * @brief Use the provided thread-specific client's context in the current thread.
98 *
99 * Note that from this point the context is shared with the thread from which the context was taken and any
100 * nc_client_*set* functions and functions creating connection in these threads should be protected from the
101 * concurrent execution.
102 *
Radek Krejcifd5b6682017-06-13 15:52:53 +0200103 * Context contains schema searchpath/callback, call home binds, TLS and SSH authentication data (username, keys,
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200104 * various certificates and callbacks).
105 *
106 * @param[in] context Client's thread-specific context provided by nc_client_get_thread_context().
107 */
108void nc_client_set_thread_context(void *context);
109
110/**
111 * @brief Get thread-specific client context for sharing with some other thread using
112 * nc_client_set_thread_context().
113 *
114 * @return Pointer to the client's context of the current thread.
115 */
116void *nc_client_get_thread_context(void);
117
118/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100119 * @brief Initialize libssh and/or libssl/libcrypto for use in the client.
120 */
121void nc_client_init(void);
122
123/**
124 * @brief Destroy all libssh and/or libssl/libcrypto dynamic memory and
Michal Vaskodf0fa452022-03-02 11:06:01 +0100125 * the client options, for both SSH and TLS, and for Call Home too.
Michal Vaskob7558c52016-02-26 15:04:19 +0100126 */
127void nc_client_destroy(void);
128
Michal Vasko4e6d3242021-05-26 09:13:24 +0200129/** @} Client */
Radek Krejci6799a052017-05-19 14:23:23 +0200130
131/**
132 * @defgroup client_session Client Session
133 * @ingroup client
134 *
135 * @brief Client-side NETCONF session manipulation.
136 * @{
137 */
138
Michal Vaskob7558c52016-02-26 15:04:19 +0100139/**
Michal Vasko086311b2016-01-08 09:53:11 +0100140 * @brief Connect to the NETCONF server via proviaded input/output file descriptors.
141 *
142 * Transport layer is supposed to be already set. Function do not cover authentication
143 * or any other manipulation with the transport layer, it only establish NETCONF session
144 * by sending and processing NETCONF \<hello\> messages.
145 *
Michal Vasko086311b2016-01-08 09:53:11 +0100146 * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server.
147 * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server.
Michal Vasko94d61402023-03-16 08:21:52 +0100148 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
149 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
150 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100151 * @return Created NETCONF session object or NULL in case of error.
152 */
153struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx);
154
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200155/**
156 * @brief Connect to the NETCONF server via unix socket.
157 *
158 * Connect to netconf server via an unix socket. Function do not cover authentication
159 * or any other manipulation with the transport layer, it only establish NETCONF session
160 * by sending and processing NETCONF \<hello\> messages.
161 *
162 * @param[in] address Path to the unix socket.
Michal Vasko94d61402023-03-16 08:21:52 +0100163 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
164 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
165 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200166 * @return Created NETCONF session object or NULL in case of error.
167 */
168struct nc_session *nc_connect_unix(const char *address, struct ly_ctx *ctx);
169
Michal Vasko4e6d3242021-05-26 09:13:24 +0200170/** @} Client Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200171
Radek Krejci53691be2016-02-22 13:58:37 +0100172#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100173
174/**
Radek Krejci6799a052017-05-19 14:23:23 +0200175 * @defgroup client_ssh Client SSH
176 * @ingroup client
177 *
178 * @brief Client-side settings for SSH connections.
179 * @{
180 */
181
182/**
Michal Vaskoef112d72016-02-18 13:28:25 +0100183 * @brief Set SSH authentication hostkey check (knownhosts) callback.
184 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200185 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
186 * freeing the private data when necessary (the private data can be obtained by
187 * nc_client_ssh_get_auth_hostkey_check_clb()).
188 *
Michal Vaskoef112d72016-02-18 13:28:25 +0100189 * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error.
190 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +0200191 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vaskoef112d72016-02-18 13:28:25 +0100192 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200193void nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200194 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200195
196/**
197 * @brief Get currently set SSH authentication hostkey check (knownhosts) callback and its private data previously set
198 * by nc_client_ssh_set_auth_hostkey_check_clb().
199 *
200 * @param[out] auth_hostkey_check Currently set callback, NULL in case of the default callback.
201 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
202 */
203void nc_client_ssh_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200204 void **priv);
Michal Vaskoef112d72016-02-18 13:28:25 +0100205
206/**
Michal Vasko30e2c872016-02-18 10:03:21 +0100207 * @brief Set SSH password authentication callback.
208 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200209 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
210 * freeing the private data when necessary (the private data can be obtained by
211 * nc_client_ssh_get_auth_password_clb()).
212 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100213 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko30e2c872016-02-18 10:03:21 +0100214 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +0200215 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100216 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200217void nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200218 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200219
220/**
221 * @brief Get currently set SSH password authentication callback and its private data previously set
222 * by nc_client_ssh_set_auth_password_clb().
223 *
224 * @param[out] auth_password Currently set callback, NULL in case of the default callback.
225 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
226 */
227void nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200228 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100229
230/**
231 * @brief Set SSH interactive authentication callback.
232 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200233 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
234 * freeing the private data when necessary (the private data can be obtained by
235 * nc_client_ssh_get_auth_interactive_clb()).
236 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100237 * @param[in] auth_interactive Function to call for every question, returns the answer for
238 * authentication name with instruction and echoing prompt.
239 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +0200240 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100241 */
242void nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200243 const char *prompt, int echo, void *priv),
244 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200245
246/**
247 * @brief Get currently set SSH interactive authentication callback and its private data previously set
248 * by nc_client_ssh_set_auth_interactive_clb().
249 *
250 * @param[out] auth_interactive Currently set callback, NULL in case of the default callback.
251 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
252 */
253void nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200254 const char *prompt, int echo, void *priv),
255 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100256
257/**
258 * @brief Set SSH publickey authentication encrypted private key passphrase callback.
259 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200260 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
261 * freeing the private data when necessary (the private data can be obtained by
262 * nc_client_ssh_get_auth_privkey_passphrase_clb()).
263 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100264 * @param[in] auth_privkey_passphrase Function to call for every question, returns
265 * the passphrase for the specific private key.
Radek Krejci90a84a22017-05-25 13:53:00 +0200266 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100267 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200268void nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200269 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200270
271/**
272 * @brief Get currently set SSH publickey authentication encrypted private key passphrase callback and its private data
273 * previously set by nc_client_ssh_set_auth_privkey_passphrase_clb().
274 *
275 * @param[out] auth_privkey_passphrase Currently set callback, NULL in case of the default callback.
276 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
277 */
278void nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200279 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100280
281/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100282 * @brief Add an SSH public and private key pair to be used for client authentication.
283 *
284 * Private key can be encrypted, the passphrase will be asked for before using it.
285 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100286 * @param[in] pub_key Path to the public key.
287 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +0100288 * @return 0 on success, -1 on error.
Michal Vasko3031aae2016-01-27 16:07:18 +0100289 */
290int nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key);
291
292/**
293 * @brief Remove an SSH public and private key pair that was used for client authentication.
294 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100295 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko3031aae2016-01-27 16:07:18 +0100296 * @return 0 on success, -1 on error.
297 */
298int nc_client_ssh_del_keypair(int idx);
299
300/**
301 * @brief Get the number of public an private key pairs set to be used for client authentication.
302 *
303 * @return Keypair count.
304 */
305int nc_client_ssh_get_keypair_count(void);
306
307/**
308 * @brief Get a specific keypair set to be used for client authentication.
309 *
310 * @param[in] idx Index of the specific keypair.
311 * @param[out] pub_key Path to the public key.
312 * @param[out] priv_key Path to the private key.
Michal Vasko3031aae2016-01-27 16:07:18 +0100313 * @return 0 on success, -1 on error.
314 */
315int nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key);
316
317/**
318 * @brief Set SSH authentication method preference.
319 *
Radek Krejci62aa0642017-05-25 16:33:49 +0200320 * The default preference is as follows:
321 * - interactive authentication (3)
322 * - password authentication (2)
323 * - public key authentication (1)
324 *
325 * @param[in] auth_type Authentication method to modify the preference of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200326 * @param[in] pref Preference of @p auth_type. Higher number increases priority, negative values disable the method.
Michal Vasko3031aae2016-01-27 16:07:18 +0100327 */
328void nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
329
330/**
331 * @brief Get SSH authentication method preference.
332 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100333 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200334 * @return Preference of the @p auth_type.
Michal Vasko3031aae2016-01-27 16:07:18 +0100335 */
336int16_t nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
337
338/**
339 * @brief Set client SSH username used for authentication.
340 *
341 * @param[in] username Username to use.
342 * @return 0 on success, -1 on error.
343 */
344int nc_client_ssh_set_username(const char *username);
345
Michal Vasko3031aae2016-01-27 16:07:18 +0100346/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100347 * @brief Get client SSH username used for authentication.
348 *
349 * @return Username used.
350 */
351const char *nc_client_ssh_get_username(void);
352
353/**
Michal Vasko086311b2016-01-08 09:53:11 +0100354 * @brief Connect to the NETCONF server using SSH transport (via libssh).
355 *
356 * SSH session is created with default options. If the caller needs to use specific SSH session properties,
357 * they are supposed to use nc_connect_libssh().
358 *
Michal Vasko086311b2016-01-08 09:53:11 +0100359 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
Michal Vaskodf0fa452022-03-02 11:06:01 +0100360 * 'localhost' is used by default if NULL is specified.
Michal Vasko086311b2016-01-08 09:53:11 +0100361 * @param[in] port Port number of the target server. Default value 830 is used if 0 is specified.
Michal Vasko94d61402023-03-16 08:21:52 +0100362 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
363 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
364 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100365 * @return Created NETCONF session object or NULL on error.
366 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100367struct nc_session *nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100368
369/**
370 * @brief Connect to the NETCONF server using the provided SSH (libssh) session.
371 *
372 * SSH session can have any options set, they will not be modified. If no options were set,
373 * host 'localhost', port 22, and the username detected from the EUID is used. If socket is
Michal Vaskoc446a382021-06-18 08:54:05 +0200374 * set and connected only the host and the username must be set/is detected. Or the @p ssh_session
Michal Vasko086311b2016-01-08 09:53:11 +0100375 * can already be authenticated in which case it is used directly.
376 *
Michal Vaskodf0fa452022-03-02 11:06:01 +0100377 * @param[in] ssh_session libssh structure representing SSH session object. It is fully managed by the created session
378 * including freeing it.
Michal Vasko94d61402023-03-16 08:21:52 +0100379 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
380 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
381 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100382 * @return Created NETCONF session object or NULL on error.
383 */
384struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx);
385
386/**
387 * @brief Create another NETCONF session on existing SSH session using separated SSH channel.
388 *
Michal Vasko086311b2016-01-08 09:53:11 +0100389 * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh -
Michal Vaskodf0fa452022-03-02 11:06:01 +0100390 * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel().
Michal Vasko94d61402023-03-16 08:21:52 +0100391 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
392 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
393 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100394 * @return Created NETCONF session object or NULL on error.
395 */
396struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx);
397
Michal Vasko4e6d3242021-05-26 09:13:24 +0200398/** @} Client SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200399
Radek Krejci53691be2016-02-22 13:58:37 +0100400#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100401
Radek Krejci53691be2016-02-22 13:58:37 +0100402#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100403
404/**
Radek Krejci6799a052017-05-19 14:23:23 +0200405 * @defgroup client_tls Client TLS
406 * @ingroup client
407 *
408 * @brief Client-side settings for TLS connections.
409 * @{
410 */
411
412/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100413 * @brief Set client authentication identity - a certificate and a private key.
Michal Vasko086311b2016-01-08 09:53:11 +0100414 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100415 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vaskoc446a382021-06-18 08:54:05 +0200416 * @param[in] client_key Path to the file containing the private key for the @p client_cert.
417 * If NULL, key is expected to be stored with @p client_cert.
Michal Vasko3031aae2016-01-27 16:07:18 +0100418 * @return 0 on success, -1 on error.
419 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100420int nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key);
Michal Vasko3031aae2016-01-27 16:07:18 +0100421
422/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100423 * @brief Get client authentication identity - a certificate and a private key.
424 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100425 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
Michal Vaskoc446a382021-06-18 08:54:05 +0200426 * @param[out] client_key Path to the file containing the private key for the @p client_cert. Can be NULL.
Michal Vaskoe22c6732016-01-29 11:03:02 +0100427 */
428void nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key);
429
430/**
431 * @brief Set client trusted CA certificates paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100432 *
Michal Vasko086311b2016-01-08 09:53:11 +0100433 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
434 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
435 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
436 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100437 * @return 0 on success, -1 on error.
438 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100439int nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100440
441/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100442 * @brief Get client trusted CA certificates paths.
443 *
444 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
445 * Can be NULL.
446 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
447 * Can be NULL.
448 */
449void nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
450
451/**
452 * @brief Set client Certificate Revocation List paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100453 *
Michal Vasko086311b2016-01-08 09:53:11 +0100454 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
455 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
Michal Vasko3031aae2016-01-27 16:07:18 +0100456 * @return 0 on success, -1 on error.
457 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100458int nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir);
459
460/**
461 * @brief Get client Certificate Revocation List paths.
462 *
463 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
464 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
465 */
466void nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100467
Michal Vasko3031aae2016-01-27 16:07:18 +0100468/**
Michal Vasko086311b2016-01-08 09:53:11 +0100469 * @brief Connect to the NETCONF server using TLS transport (via libssl)
470 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100471 * TLS session is created with the certificates set using nc_client_tls_* functions, which must be called beforehand!
Michal Vaskodf0fa452022-03-02 11:06:01 +0100472 * If the caller needs to use specific TLS session properties, they are supposed to use ::nc_connect_libssl().
Michal Vasko086311b2016-01-08 09:53:11 +0100473 *
Michal Vasko086311b2016-01-08 09:53:11 +0100474 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
Michal Vasko9d4cca52022-09-07 11:19:57 +0200475 * 'localhost' is used by default if NULL is specified. It is verified by TLS when connecting to it.
Michal Vasko086311b2016-01-08 09:53:11 +0100476 * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified.
Michal Vasko94d61402023-03-16 08:21:52 +0100477 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
478 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
479 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100480 * @return Created NETCONF session object or NULL on error.
481 */
482struct nc_session *nc_connect_tls(const char *host, uint16_t port, struct ly_ctx *ctx);
483
484/**
485 * @brief Connect to the NETCONF server using the provided TLS (libssl) session.
486 *
487 * The TLS session supplied is expected to be fully connected and authenticated!
488 *
Michal Vasko086311b2016-01-08 09:53:11 +0100489 * @param[in] tls libssl structure representing the TLS session object.
Michal Vasko94d61402023-03-16 08:21:52 +0100490 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
491 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
492 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko086311b2016-01-08 09:53:11 +0100493 * @return Created NETCONF session object or NULL on error.
494 */
495struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx);
496
Michal Vasko4e6d3242021-05-26 09:13:24 +0200497/** @} Client TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200498
Radek Krejci53691be2016-02-22 13:58:37 +0100499#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100500
501/**
Radek Krejci6799a052017-05-19 14:23:23 +0200502 * @addtogroup client_session
503 * @{
504 */
505
506/**
Michal Vaskobdfb5242016-05-24 09:11:01 +0200507 * @brief Get session capabilities.
508 *
509 * @param[in] session Session to get the information from.
Michal Vaskoc446a382021-06-18 08:54:05 +0200510 * @return NULL-terminated array of the @p session capabilities.
Michal Vaskobdfb5242016-05-24 09:11:01 +0200511 */
Michal Vasko1b2ddc92017-05-24 08:59:49 +0200512const char * const *nc_session_get_cpblts(const struct nc_session *session);
Michal Vaskobdfb5242016-05-24 09:11:01 +0200513
514/**
515 * @brief Check capability presence in a session.
516 *
517 * @param[in] session Session to check.
518 * @param[in] capab Capability to look for, capability with any additional suffix will match.
519 * @return Matching capability, NULL if none found.
520 */
521const char *nc_session_cpblt(const struct nc_session *session, const char *capab);
522
523/**
Michal Vasko9cd26a82016-05-31 08:58:48 +0200524 * @brief Check whether the session has a notification thread running.
525 *
526 * @param[in] session Session to check.
527 * @return 1 if notfication thread is running, 0 otherwise.
528 */
529int nc_session_ntf_thread_running(const struct nc_session *session);
530
531/**
Michal Vasko086311b2016-01-08 09:53:11 +0100532 * @brief Receive NETCONF RPC reply.
533 *
tadeas-vintrlik399af302021-08-25 10:32:21 +0200534 * @note This function can be called in a single thread only.
535 *
Michal Vasko086311b2016-01-08 09:53:11 +0100536 * @param[in] session NETCONF session from which the function gets data. It must be the
Michal Vasko77367452021-02-16 16:32:18 +0100537 * client side session object.
Michal Vasko086311b2016-01-08 09:53:11 +0100538 * @param[in] rpc Original RPC this should be the reply to.
539 * @param[in] msgid Expected message ID of the reply.
540 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
Michal Vasko77367452021-02-16 16:32:18 +0100541 * waiting and 0 for immediate return if data are not available on the wire.
542 * @param[out] envp NETCONF rpc-reply XML envelopes.
543 * @param[out] op Parsed NETCONF reply data, if any (none for \<ok\> or error replies). Set only on #NC_MSG_REPLY
544 * and #NC_MSG_REPLY_ERR_MSGID return.
Michal Vasko71ba2da2016-05-04 10:53:16 +0200545 * @return #NC_MSG_REPLY for success,
Michal Vaskoc446a382021-06-18 08:54:05 +0200546 * #NC_MSG_WOULDBLOCK if @p timeout has elapsed,
Michal Vasko71ba2da2016-05-04 10:53:16 +0200547 * #NC_MSG_ERROR if reading has failed,
548 * #NC_MSG_NOTIF if a notification was read instead (call this function again to get the reply), and
549 * #NC_MSG_REPLY_ERR_MSGID if a reply with missing or wrong message-id was received.
Michal Vasko086311b2016-01-08 09:53:11 +0100550 */
Michal Vaskod083db62016-01-19 10:31:29 +0100551NC_MSG_TYPE nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout,
Michal Vasko77367452021-02-16 16:32:18 +0100552 struct lyd_node **envp, struct lyd_node **op);
Michal Vasko086311b2016-01-08 09:53:11 +0100553
554/**
555 * @brief Receive NETCONF Notification.
556 *
557 * @param[in] session NETCONF session from which the function gets data. It must be the
Michal Vaskoffb35e92022-10-20 09:07:25 +0200558 * client side session object.
Michal Vasko086311b2016-01-08 09:53:11 +0100559 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
Michal Vaskoffb35e92022-10-20 09:07:25 +0200560 * waiting and 0 for immediate return if data are not available on the wire.
Michal Vasko77367452021-02-16 16:32:18 +0100561 * @param[out] envp NETCONF notification XML envelopes.
562 * @param[out] op Parsed NETCONF notification data.
Michal Vasko71ba2da2016-05-04 10:53:16 +0200563 * @return #NC_MSG_NOTIF for success,
Michal Vaskoc446a382021-06-18 08:54:05 +0200564 * #NC_MSG_WOULDBLOCK if @p timeout has elapsed,
Michal Vasko71ba2da2016-05-04 10:53:16 +0200565 * #NC_MSG_ERROR if reading has failed, and
566 * #NC_MSG_REPLY if a reply was read instead (call this function again to get a notification).
Michal Vasko086311b2016-01-08 09:53:11 +0100567 */
Michal Vasko77367452021-02-16 16:32:18 +0100568NC_MSG_TYPE nc_recv_notif(struct nc_session *session, int timeout, struct lyd_node **envp, struct lyd_node **op);
Michal Vasko086311b2016-01-08 09:53:11 +0100569
570/**
Michal Vaskoffb35e92022-10-20 09:07:25 +0200571 * @brief Callback for receiving notifications in a separate thread.
572 *
573 * @param[in] session NC session that received the notification.
574 * @param[in] envp Notification envelope data tree.
575 * @param[in] op Notification body data tree.
576 * @param[in] user_data Arbitrary user data passed to the callback.
577 */
578typedef void (*nc_notif_dispatch_clb)(struct nc_session *session, const struct lyd_node *envp, const struct lyd_node *op,
579 void *user_data);
580
581/**
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100582 * @brief Receive NETCONF Notifications in a separate thread until the session is terminated
Michal Vaskoffb35e92022-10-20 09:07:25 +0200583 * or \<notificationComplete\> is received.
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100584 *
585 * @param[in] session Netconf session to read notifications from.
586 * @param[in] notif_clb Function that is called for every received notification (including
Michal Vaskoffb35e92022-10-20 09:07:25 +0200587 * \<notificationComplete\>). Parameters are the session the notification was received on
588 * and the notification data.
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100589 * @return 0 if the thread was successfully created, -1 on error.
590 */
Michal Vaskoffb35e92022-10-20 09:07:25 +0200591int nc_recv_notif_dispatch(struct nc_session *session, nc_notif_dispatch_clb notif_clb);
592
593/**
594 * @brief Receive NETCONF Notifications in a separate thread until the session is terminated
595 * or \<notificationComplete\> is received. Similar to ::nc_recv_notif_dispatch() but allows
596 * to set arbitrary user data that can be freed as well.
597 *
598 * @param[in] session Netconf session to read notifications from.
599 * @param[in] notif_clb Callback that is called for every received notification (including
600 * \<notificationComplete\>). Parameters are the session the notification was received on
601 * and the notification data.
602 * @param[in] user_data Arbitrary user data.
603 * @param[in] free_data Callback for freeing the user data after notif thread exit.
604 * @return 0 if the thread was successfully created, -1 on error.
605 */
606int nc_recv_notif_dispatch_data(struct nc_session *session, nc_notif_dispatch_clb notif_clb, void *user_data,
607 void (*free_data)(void *));
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100608
609/**
Michal Vasko086311b2016-01-08 09:53:11 +0100610 * @brief Send NETCONF RPC message via the session.
611 *
612 * @param[in] session NETCONF session where the RPC will be written.
Michal Vasko66fa9b92021-10-26 10:48:13 +0200613 * @param[in] rpc NETCONF RPC object to send via the specified session.
Michal Vasko086311b2016-01-08 09:53:11 +0100614 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
Michal Vaskoffb35e92022-10-20 09:07:25 +0200615 * waiting and 0 for return if data cannot be sent immediately.
Michal Vasko086311b2016-01-08 09:53:11 +0100616 * @param[out] msgid If RPC was successfully sent, this is it's message ID.
Michal Vasko71ba2da2016-05-04 10:53:16 +0200617 * @return #NC_MSG_RPC on success,
618 * #NC_MSG_WOULDBLOCK in case of a busy session, and
619 * #NC_MSG_ERROR on error.
Michal Vasko086311b2016-01-08 09:53:11 +0100620 */
Michal Vaskod083db62016-01-19 10:31:29 +0100621NC_MSG_TYPE nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid);
Michal Vasko086311b2016-01-08 09:53:11 +0100622
Michal Vaskode2946c2017-01-12 12:19:26 +0100623/**
624 * @brief Make a session not strict when sending RPCs and receiving RPC replies. In other words,
Michal Vaskoffb35e92022-10-20 09:07:25 +0200625 * it will silently skip unknown nodes without an error.
Michal Vaskode2946c2017-01-12 12:19:26 +0100626 *
627 * Generally, no such data should be worked with, so use this function only when you know what you
628 * are doing and you understand the consequences.
629 *
630 * @param[in] session NETCONF client session.
631 */
632void nc_client_session_set_not_strict(struct nc_session *session);
633
Michal Vasko77367452021-02-16 16:32:18 +0100634/** @} Client Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200635
Michal Vaskoc09730e2019-01-17 10:07:26 +0100636#ifdef __cplusplus
637}
638#endif
639
Michal Vasko086311b2016-01-08 09:53:11 +0100640#endif /* NC_SESSION_CLIENT_H_ */