blob: 1d8504c205e4970a6329970cb2c9ef2006cc3456 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_client.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session client manipulation
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#ifndef NC_SESSION_CLIENT_H_
16#define NC_SESSION_CLIENT_H_
17
18#include <libyang/libyang.h>
19
Radek Krejci53691be2016-02-22 13:58:37 +010020#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +010021
22#include <libssh/libssh.h>
23
24#endif
25
Radek Krejci53691be2016-02-22 13:58:37 +010026#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +010027
28#include <openssl/ssl.h>
29
30#endif
31
32#include "session.h"
33#include "netconf.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010034#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010035
36/**
37 * @brief Set location where libnetconf tries to search for YANG/YIN schemas.
38 *
39 * The location is search when connecting to a NETCONF server and building
40 * YANG context for further processing of the NETCONF messages and data.
41 *
Michal Vasko086311b2016-01-08 09:53:11 +010042 * @param[in] path Directory where to search for YANG/YIN schemas.
43 * @return 0 on success, 1 on (memory allocation) failure.
44 */
Michal Vasko3031aae2016-01-27 16:07:18 +010045int nc_client_schema_searchpath(const char *path);
Michal Vasko086311b2016-01-08 09:53:11 +010046
47/**
Michal Vaskob7558c52016-02-26 15:04:19 +010048 * @brief Destroy all the client options, for both SSH and TLS, and for Call Home too.
49 */
50void nc_client_destroy(void);
51
52/**
Michal Vasko086311b2016-01-08 09:53:11 +010053 * @brief Connect to the NETCONF server via proviaded input/output file descriptors.
54 *
55 * Transport layer is supposed to be already set. Function do not cover authentication
56 * or any other manipulation with the transport layer, it only establish NETCONF session
57 * by sending and processing NETCONF \<hello\> messages.
58 *
Michal Vasko086311b2016-01-08 09:53:11 +010059 * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server.
60 * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server.
61 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
62 * (ignoring what is actually supported by the server side). If not set,
63 * YANG context is created for the session using \<get-schema\> (if supported
64 * by the server side) or/and by searching for YANG schemas in the searchpath
65 * (see nc_schema_searchpath()). In every case except not providing context
66 * to connect to a server supporting \<get-schema\> it is possible that
67 * the session context will not include all the models supported by the server.
68 * @return Created NETCONF session object or NULL in case of error.
69 */
70struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx);
71
Radek Krejci53691be2016-02-22 13:58:37 +010072#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +010073
74/**
Michal Vaskoef112d72016-02-18 13:28:25 +010075 * @brief Set SSH authentication hostkey check (knownhosts) callback.
76 *
77 * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error.
78 * If NULL, the default callback is set.
79 */
80void nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session));
81
82/**
Michal Vasko30e2c872016-02-18 10:03:21 +010083 * @brief Set SSH password authentication callback.
84 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +010085 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko30e2c872016-02-18 10:03:21 +010086 * If NULL, the default callback is set.
87 */
88void nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname));
89
90/**
91 * @brief Set SSH interactive authentication callback.
92 *
93 * @param[in] auth_interactive Function to call for every question, returns the answer for
94 * authentication name with instruction and echoing prompt.
95 * If NULL, the default callback is set.
96 */
97void nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
98 const char *prompt, int echo));
99
100/**
101 * @brief Set SSH publickey authentication encrypted private key passphrase callback.
102 *
103 * @param[in] auth_privkey_passphrase Function to call for every question, returns
104 * the passphrase for the specific private key.
105 */
106void nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path));
107
108/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100109 * @brief Add an SSH public and private key pair to be used for client authentication.
110 *
111 * Private key can be encrypted, the passphrase will be asked for before using it.
112 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100113 * @param[in] pub_key Path to the public key.
114 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +0100115 * @return 0 on success, -1 on error.
Michal Vasko3031aae2016-01-27 16:07:18 +0100116 */
117int nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key);
118
119/**
120 * @brief Remove an SSH public and private key pair that was used for client authentication.
121 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100122 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko3031aae2016-01-27 16:07:18 +0100123 * @return 0 on success, -1 on error.
124 */
125int nc_client_ssh_del_keypair(int idx);
126
127/**
128 * @brief Get the number of public an private key pairs set to be used for client authentication.
129 *
130 * @return Keypair count.
131 */
132int nc_client_ssh_get_keypair_count(void);
133
134/**
135 * @brief Get a specific keypair set to be used for client authentication.
136 *
137 * @param[in] idx Index of the specific keypair.
138 * @param[out] pub_key Path to the public key.
139 * @param[out] priv_key Path to the private key.
Michal Vasko3031aae2016-01-27 16:07:18 +0100140 * @return 0 on success, -1 on error.
141 */
142int nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key);
143
144/**
145 * @brief Set SSH authentication method preference.
146 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100147 * @param[in] auth_type Authentication method to modify the prefrence of.
148 * @param[in] pref Preference of \p auth_type. Negative values disable the method.
149 */
150void nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
151
152/**
153 * @brief Get SSH authentication method preference.
154 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100155 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vasko3031aae2016-01-27 16:07:18 +0100156 * @return Preference of the \p auth_type.
157 */
158int16_t nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
159
160/**
161 * @brief Set client SSH username used for authentication.
162 *
163 * @param[in] username Username to use.
164 * @return 0 on success, -1 on error.
165 */
166int nc_client_ssh_set_username(const char *username);
167
Michal Vasko3031aae2016-01-27 16:07:18 +0100168/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100169 * @brief Get client SSH username used for authentication.
170 *
171 * @return Username used.
172 */
173const char *nc_client_ssh_get_username(void);
174
175/**
Michal Vasko086311b2016-01-08 09:53:11 +0100176 * @brief Connect to the NETCONF server using SSH transport (via libssh).
177 *
178 * SSH session is created with default options. If the caller needs to use specific SSH session properties,
179 * they are supposed to use nc_connect_libssh().
180 *
Michal Vasko086311b2016-01-08 09:53:11 +0100181 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
182 * 'localhost' is used by default if NULL is specified.
183 * @param[in] port Port number of the target server. Default value 830 is used if 0 is specified.
Michal Vasko086311b2016-01-08 09:53:11 +0100184 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
185 * (ignoring what is actually supported by the server side). If not set,
186 * YANG context is created for the session using \<get-schema\> (if supported
187 * by the server side) or/and by searching for YANG schemas in the searchpath
188 * (see nc_schema_searchpath()). In every case except not providing context
189 * to connect to a server supporting \<get-schema\> it is possible that
190 * the session context will not include all the models supported by the server.
191 * @return Created NETCONF session object or NULL on error.
192 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100193struct nc_session *nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100194
195/**
196 * @brief Connect to the NETCONF server using the provided SSH (libssh) session.
197 *
198 * SSH session can have any options set, they will not be modified. If no options were set,
199 * host 'localhost', port 22, and the username detected from the EUID is used. If socket is
200 * set and connected only the host and the username must be set/is detected. Or the \p ssh_session
201 * can already be authenticated in which case it is used directly.
202 *
Michal Vasko086311b2016-01-08 09:53:11 +0100203 * @param[in] ssh_session libssh structure representing SSH session object. After passing it
204 * to libnetconf2 this way, it is fully managed by it (including freeing!).
205 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
206 * (ignoring what is actually supported by the server side). If not set,
207 * YANG context is created for the session using \<get-schema\> (if supported
208 * by the server side) or/and by searching for YANG schemas in the searchpath
209 * (see nc_schema_searchpath()). In every case except not providing context
210 * to connect to a server supporting \<get-schema\> it is possible that
211 * the session context will not include all the models supported by the server.
212 * @return Created NETCONF session object or NULL on error.
213 */
214struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx);
215
216/**
217 * @brief Create another NETCONF session on existing SSH session using separated SSH channel.
218 *
Michal Vasko086311b2016-01-08 09:53:11 +0100219 * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh -
220 * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel().
221 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
222 * (ignoring what is actually supported by the server side). If not set,
223 * YANG context is created for the session using \<get-schema\> (if supported
224 * by the server side) or/and by searching for YANG schemas in the searchpath
225 * (see nc_schema_searchpath()). In every case except not providing context
226 * to connect to a server supporting \<get-schema\> it is possible that
227 * the session context will not include all the models supported by the server.
228 * @return Created NETCONF session object or NULL on error.
229 */
230struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx);
231
Radek Krejci53691be2016-02-22 13:58:37 +0100232#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100233
Radek Krejci53691be2016-02-22 13:58:37 +0100234#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100235
236/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100237 * @brief Set client authentication identity - a certificate and a private key.
Michal Vasko086311b2016-01-08 09:53:11 +0100238 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100239 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vasko086311b2016-01-08 09:53:11 +0100240 * @param[in] client_key Path to the file containing the private key for the \p client_cert.
241 * If NULL, key is expected to be stored with \p client_cert.
Michal Vasko3031aae2016-01-27 16:07:18 +0100242 * @return 0 on success, -1 on error.
243 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100244int nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key);
Michal Vasko3031aae2016-01-27 16:07:18 +0100245
246/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100247 * @brief Get client authentication identity - a certificate and a private key.
248 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100249 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
250 * @param[out] client_key Path to the file containing the private key for the \p client_cert. Can be NULL.
251 */
252void nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key);
253
254/**
255 * @brief Set client trusted CA certificates paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100256 *
Michal Vasko086311b2016-01-08 09:53:11 +0100257 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
258 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
259 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
260 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100261 * @return 0 on success, -1 on error.
262 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100263int nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100264
265/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100266 * @brief Get client trusted CA certificates paths.
267 *
268 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
269 * Can be NULL.
270 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
271 * Can be NULL.
272 */
273void nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
274
275/**
276 * @brief Set client Certificate Revocation List paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100277 *
Michal Vasko086311b2016-01-08 09:53:11 +0100278 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
279 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
Michal Vasko3031aae2016-01-27 16:07:18 +0100280 * @return 0 on success, -1 on error.
281 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100282int nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir);
283
284/**
285 * @brief Get client Certificate Revocation List paths.
286 *
287 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
288 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
289 */
290void nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100291
Michal Vasko3031aae2016-01-27 16:07:18 +0100292/**
Michal Vasko086311b2016-01-08 09:53:11 +0100293 * @brief Connect to the NETCONF server using TLS transport (via libssl)
294 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100295 * TLS session is created with the certificates set using nc_client_tls_* functions, which must be called beforehand!
Michal Vasko086311b2016-01-08 09:53:11 +0100296 * If the caller needs to use specific TLS session properties, they are supposed to use nc_connect_libssl().
297 *
Michal Vasko086311b2016-01-08 09:53:11 +0100298 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
299 * 'localhost' is used by default if NULL is specified.
300 * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified.
301 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
302 * (ignoring what is actually supported by the server side). If not set,
303 * YANG context is created for the session using \<get-schema\> (if supported
304 * by the server side) or/and by searching for YANG schemas in the searchpath
305 * (see nc_schema_searchpath()). In every case except not providing context
306 * to connect to a server supporting \<get-schema\> it is possible that
307 * the session context will not include all the models supported by the server.
308 * @return Created NETCONF session object or NULL on error.
309 */
310struct nc_session *nc_connect_tls(const char *host, uint16_t port, struct ly_ctx *ctx);
311
312/**
313 * @brief Connect to the NETCONF server using the provided TLS (libssl) session.
314 *
315 * The TLS session supplied is expected to be fully connected and authenticated!
316 *
Michal Vasko086311b2016-01-08 09:53:11 +0100317 * @param[in] tls libssl structure representing the TLS session object.
318 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
319 * (ignoring what is actually supported by the server side). If not set,
320 * YANG context is created for the session using \<get-schema\> (if supported
321 * by the server side) or/and by searching for YANG schemas in the searchpath
322 * (see nc_schema_searchpath()). In every case except not providing context
323 * to connect to a server supporting \<get-schema\> it is possible that
324 * the session context will not include all the models supported by the server.
325 * @return Created NETCONF session object or NULL on error.
326 */
327struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx);
328
Radek Krejci53691be2016-02-22 13:58:37 +0100329#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100330
331/**
332 * @brief Receive NETCONF RPC reply.
333 *
334 * If a reply to \<get\> or \<get-config\> RPCs is received, the data are the whole configuration
335 * parsed (usually results in more top-level nodes), not just a single 'data' anyxml node with
336 * the configuration unparsed inside (which would strictly be according to the model).
337 *
338 * @param[in] session NETCONF session from which the function gets data. It must be the
339 * client side session object.
340 * @param[in] rpc Original RPC this should be the reply to.
341 * @param[in] msgid Expected message ID of the reply.
342 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
343 * waiting and 0 for immediate return if data are not available on wire.
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100344 * @param[in] parseroptions libyang parseroptions flags, do not set the data type, it is set
345 * internally. LYD_OPT_DESTRUCT and LYD_OPT_NOSIBLINGS is ignored.
Michal Vasko086311b2016-01-08 09:53:11 +0100346 * @param[out] reply Resulting object of NETCONF RPC reply.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100347 * @return #NC_MSG_REPLY for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
348 * reading has failed, and #NC_MSG_NOTIF if a notification was read instead (call this
349 * function again to get the reply).
Michal Vasko086311b2016-01-08 09:53:11 +0100350 */
Michal Vaskod083db62016-01-19 10:31:29 +0100351NC_MSG_TYPE nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout,
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100352 int parseroptions, struct nc_reply **reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100353
354/**
355 * @brief Receive NETCONF Notification.
356 *
357 * @param[in] session NETCONF session from which the function gets data. It must be the
358 * client side session object.
359 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
360 * waiting and 0 for immediate return if data are not available on wire.
361 * @param[out] notif Resulting object of NETCONF Notification.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100362 * @return #NC_MSG_NOTIF for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
363 * reading has failed, and #NC_MSG_REPLY if a reply was read instead (call this function
364 * again to get a notification).
Michal Vasko086311b2016-01-08 09:53:11 +0100365 */
Michal Vaskod083db62016-01-19 10:31:29 +0100366NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif);
Michal Vasko086311b2016-01-08 09:53:11 +0100367
368/**
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100369 * @brief Receive NETCONF Notifications in a separate thread until the session is terminated
370 * or \<notificationComplete\> is received.
371 *
372 * @param[in] session Netconf session to read notifications from.
373 * @param[in] notif_clb Function that is called for every received notification (including
374 * \<notificationComplete\>). Parameters are the session the notification was received on
375 * and the notification itself.
376 * @return 0 if the thread was successfully created, -1 on error.
377 */
378int nc_recv_notif_dispatch(struct nc_session *session,
379 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif));
380
381/**
Michal Vasko086311b2016-01-08 09:53:11 +0100382 * @brief Send NETCONF RPC message via the session.
383 *
384 * @param[in] session NETCONF session where the RPC will be written.
385 * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by
386 * nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions.
387 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
388 * waiting and 0 for return if data cannot be sent immediately.
389 * @param[out] msgid If RPC was successfully sent, this is it's message ID.
390 * @return #NC_MSG_RPC on success, #NC_MSG_WOULDBLOCK in case of busy session
391 * and #NC_MSG_ERROR on error.
392 */
Michal Vaskod083db62016-01-19 10:31:29 +0100393NC_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 +0100394
395#endif /* NC_SESSION_CLIENT_H_ */