blob: 374dbfc5032fea64618189c38734ce1c9eb659df [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 Vaskoa7b8ca52016-03-01 12:09:29 +010048 * @brief Initialize libssh and/or libssl/libcrypto for use in the client.
49 */
50void nc_client_init(void);
51
52/**
53 * @brief Destroy all libssh and/or libssl/libcrypto dynamic memory and
54 * the client options, for both SSH and TLS, and for Call Home too.
Michal Vaskob7558c52016-02-26 15:04:19 +010055 */
56void nc_client_destroy(void);
57
58/**
Michal Vasko086311b2016-01-08 09:53:11 +010059 * @brief Connect to the NETCONF server via proviaded input/output file descriptors.
60 *
61 * Transport layer is supposed to be already set. Function do not cover authentication
62 * or any other manipulation with the transport layer, it only establish NETCONF session
63 * by sending and processing NETCONF \<hello\> messages.
64 *
Michal Vasko086311b2016-01-08 09:53:11 +010065 * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server.
66 * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server.
67 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
68 * (ignoring what is actually supported by the server side). If not set,
69 * YANG context is created for the session using \<get-schema\> (if supported
70 * by the server side) or/and by searching for YANG schemas in the searchpath
71 * (see nc_schema_searchpath()). In every case except not providing context
72 * to connect to a server supporting \<get-schema\> it is possible that
73 * the session context will not include all the models supported by the server.
74 * @return Created NETCONF session object or NULL in case of error.
75 */
76struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx);
77
Radek Krejci53691be2016-02-22 13:58:37 +010078#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +010079
80/**
Michal Vaskoef112d72016-02-18 13:28:25 +010081 * @brief Set SSH authentication hostkey check (knownhosts) callback.
82 *
83 * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error.
84 * If NULL, the default callback is set.
85 */
86void nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session));
87
88/**
Michal Vasko30e2c872016-02-18 10:03:21 +010089 * @brief Set SSH password authentication callback.
90 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +010091 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko30e2c872016-02-18 10:03:21 +010092 * If NULL, the default callback is set.
93 */
94void nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname));
95
96/**
97 * @brief Set SSH interactive authentication callback.
98 *
99 * @param[in] auth_interactive Function to call for every question, returns the answer for
100 * authentication name with instruction and echoing prompt.
101 * If NULL, the default callback is set.
102 */
103void nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
104 const char *prompt, int echo));
105
106/**
107 * @brief Set SSH publickey authentication encrypted private key passphrase callback.
108 *
109 * @param[in] auth_privkey_passphrase Function to call for every question, returns
110 * the passphrase for the specific private key.
111 */
112void nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path));
113
114/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100115 * @brief Add an SSH public and private key pair to be used for client authentication.
116 *
117 * Private key can be encrypted, the passphrase will be asked for before using it.
118 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100119 * @param[in] pub_key Path to the public key.
120 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +0100121 * @return 0 on success, -1 on error.
Michal Vasko3031aae2016-01-27 16:07:18 +0100122 */
123int nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key);
124
125/**
126 * @brief Remove an SSH public and private key pair that was used for client authentication.
127 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100128 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko3031aae2016-01-27 16:07:18 +0100129 * @return 0 on success, -1 on error.
130 */
131int nc_client_ssh_del_keypair(int idx);
132
133/**
134 * @brief Get the number of public an private key pairs set to be used for client authentication.
135 *
136 * @return Keypair count.
137 */
138int nc_client_ssh_get_keypair_count(void);
139
140/**
141 * @brief Get a specific keypair set to be used for client authentication.
142 *
143 * @param[in] idx Index of the specific keypair.
144 * @param[out] pub_key Path to the public key.
145 * @param[out] priv_key Path to the private key.
Michal Vasko3031aae2016-01-27 16:07:18 +0100146 * @return 0 on success, -1 on error.
147 */
148int nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key);
149
150/**
151 * @brief Set SSH authentication method preference.
152 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100153 * @param[in] auth_type Authentication method to modify the prefrence of.
154 * @param[in] pref Preference of \p auth_type. Negative values disable the method.
155 */
156void nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
157
158/**
159 * @brief Get SSH authentication method preference.
160 *
Michal Vasko3031aae2016-01-27 16:07:18 +0100161 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vasko3031aae2016-01-27 16:07:18 +0100162 * @return Preference of the \p auth_type.
163 */
164int16_t nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
165
166/**
167 * @brief Set client SSH username used for authentication.
168 *
169 * @param[in] username Username to use.
170 * @return 0 on success, -1 on error.
171 */
172int nc_client_ssh_set_username(const char *username);
173
Michal Vasko3031aae2016-01-27 16:07:18 +0100174/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100175 * @brief Get client SSH username used for authentication.
176 *
177 * @return Username used.
178 */
179const char *nc_client_ssh_get_username(void);
180
181/**
Michal Vasko086311b2016-01-08 09:53:11 +0100182 * @brief Connect to the NETCONF server using SSH transport (via libssh).
183 *
184 * SSH session is created with default options. If the caller needs to use specific SSH session properties,
185 * they are supposed to use nc_connect_libssh().
186 *
Michal Vasko086311b2016-01-08 09:53:11 +0100187 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
188 * 'localhost' is used by default if NULL is specified.
189 * @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 +0100190 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
191 * (ignoring what is actually supported by the server side). If not set,
192 * YANG context is created for the session using \<get-schema\> (if supported
193 * by the server side) or/and by searching for YANG schemas in the searchpath
194 * (see nc_schema_searchpath()). In every case except not providing context
195 * to connect to a server supporting \<get-schema\> it is possible that
196 * the session context will not include all the models supported by the server.
197 * @return Created NETCONF session object or NULL on error.
198 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100199struct nc_session *nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100200
201/**
202 * @brief Connect to the NETCONF server using the provided SSH (libssh) session.
203 *
204 * SSH session can have any options set, they will not be modified. If no options were set,
205 * host 'localhost', port 22, and the username detected from the EUID is used. If socket is
206 * set and connected only the host and the username must be set/is detected. Or the \p ssh_session
207 * can already be authenticated in which case it is used directly.
208 *
Michal Vasko086311b2016-01-08 09:53:11 +0100209 * @param[in] ssh_session libssh structure representing SSH session object. After passing it
210 * to libnetconf2 this way, it is fully managed by it (including freeing!).
211 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
212 * (ignoring what is actually supported by the server side). If not set,
213 * YANG context is created for the session using \<get-schema\> (if supported
214 * by the server side) or/and by searching for YANG schemas in the searchpath
215 * (see nc_schema_searchpath()). In every case except not providing context
216 * to connect to a server supporting \<get-schema\> it is possible that
217 * the session context will not include all the models supported by the server.
218 * @return Created NETCONF session object or NULL on error.
219 */
220struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx);
221
222/**
223 * @brief Create another NETCONF session on existing SSH session using separated SSH channel.
224 *
Michal Vasko086311b2016-01-08 09:53:11 +0100225 * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh -
226 * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel().
227 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
228 * (ignoring what is actually supported by the server side). If not set,
229 * YANG context is created for the session using \<get-schema\> (if supported
230 * by the server side) or/and by searching for YANG schemas in the searchpath
231 * (see nc_schema_searchpath()). In every case except not providing context
232 * to connect to a server supporting \<get-schema\> it is possible that
233 * the session context will not include all the models supported by the server.
234 * @return Created NETCONF session object or NULL on error.
235 */
236struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx);
237
Radek Krejci53691be2016-02-22 13:58:37 +0100238#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100239
Radek Krejci53691be2016-02-22 13:58:37 +0100240#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100241
242/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100243 * @brief Set client authentication identity - a certificate and a private key.
Michal Vasko086311b2016-01-08 09:53:11 +0100244 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100245 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vasko086311b2016-01-08 09:53:11 +0100246 * @param[in] client_key Path to the file containing the private key for the \p client_cert.
247 * If NULL, key is expected to be stored with \p client_cert.
Michal Vasko3031aae2016-01-27 16:07:18 +0100248 * @return 0 on success, -1 on error.
249 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100250int nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key);
Michal Vasko3031aae2016-01-27 16:07:18 +0100251
252/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100253 * @brief Get client authentication identity - a certificate and a private key.
254 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100255 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
256 * @param[out] client_key Path to the file containing the private key for the \p client_cert. Can be NULL.
257 */
258void nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key);
259
260/**
261 * @brief Set client trusted CA certificates paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100262 *
Michal Vasko086311b2016-01-08 09:53:11 +0100263 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
264 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
265 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
266 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100267 * @return 0 on success, -1 on error.
268 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100269int nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100270
271/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100272 * @brief Get client trusted CA certificates paths.
273 *
274 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
275 * Can be NULL.
276 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
277 * Can be NULL.
278 */
279void nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
280
281/**
282 * @brief Set client Certificate Revocation List paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100283 *
Michal Vasko086311b2016-01-08 09:53:11 +0100284 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
285 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
Michal Vasko3031aae2016-01-27 16:07:18 +0100286 * @return 0 on success, -1 on error.
287 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100288int nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir);
289
290/**
291 * @brief Get client Certificate Revocation List paths.
292 *
293 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
294 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
295 */
296void nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100297
Michal Vasko3031aae2016-01-27 16:07:18 +0100298/**
Michal Vasko086311b2016-01-08 09:53:11 +0100299 * @brief Connect to the NETCONF server using TLS transport (via libssl)
300 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100301 * 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 +0100302 * If the caller needs to use specific TLS session properties, they are supposed to use nc_connect_libssl().
303 *
Michal Vasko086311b2016-01-08 09:53:11 +0100304 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
305 * 'localhost' is used by default if NULL is specified.
306 * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified.
307 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
308 * (ignoring what is actually supported by the server side). If not set,
309 * YANG context is created for the session using \<get-schema\> (if supported
310 * by the server side) or/and by searching for YANG schemas in the searchpath
311 * (see nc_schema_searchpath()). In every case except not providing context
312 * to connect to a server supporting \<get-schema\> it is possible that
313 * the session context will not include all the models supported by the server.
314 * @return Created NETCONF session object or NULL on error.
315 */
316struct nc_session *nc_connect_tls(const char *host, uint16_t port, struct ly_ctx *ctx);
317
318/**
319 * @brief Connect to the NETCONF server using the provided TLS (libssl) session.
320 *
321 * The TLS session supplied is expected to be fully connected and authenticated!
322 *
Michal Vasko086311b2016-01-08 09:53:11 +0100323 * @param[in] tls libssl structure representing the TLS session object.
324 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
325 * (ignoring what is actually supported by the server side). If not set,
326 * YANG context is created for the session using \<get-schema\> (if supported
327 * by the server side) or/and by searching for YANG schemas in the searchpath
328 * (see nc_schema_searchpath()). In every case except not providing context
329 * to connect to a server supporting \<get-schema\> it is possible that
330 * the session context will not include all the models supported by the server.
331 * @return Created NETCONF session object or NULL on error.
332 */
333struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx);
334
Radek Krejci53691be2016-02-22 13:58:37 +0100335#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100336
337/**
338 * @brief Receive NETCONF RPC reply.
339 *
340 * If a reply to \<get\> or \<get-config\> RPCs is received, the data are the whole configuration
341 * parsed (usually results in more top-level nodes), not just a single 'data' anyxml node with
342 * the configuration unparsed inside (which would strictly be according to the model).
343 *
344 * @param[in] session NETCONF session from which the function gets data. It must be the
345 * client side session object.
346 * @param[in] rpc Original RPC this should be the reply to.
347 * @param[in] msgid Expected message ID of the reply.
348 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
349 * waiting and 0 for immediate return if data are not available on wire.
Michal Vaskoeb7080e2016-02-18 13:27:05 +0100350 * @param[in] parseroptions libyang parseroptions flags, do not set the data type, it is set
351 * internally. LYD_OPT_DESTRUCT and LYD_OPT_NOSIBLINGS is ignored.
Michal Vasko086311b2016-01-08 09:53:11 +0100352 * @param[out] reply Resulting object of NETCONF RPC reply.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100353 * @return #NC_MSG_REPLY for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
354 * reading has failed, and #NC_MSG_NOTIF if a notification was read instead (call this
355 * function again to get the reply).
Michal Vasko086311b2016-01-08 09:53:11 +0100356 */
Michal Vaskod083db62016-01-19 10:31:29 +0100357NC_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 +0100358 int parseroptions, struct nc_reply **reply);
Michal Vasko086311b2016-01-08 09:53:11 +0100359
360/**
361 * @brief Receive NETCONF Notification.
362 *
363 * @param[in] session NETCONF session from which the function gets data. It must be the
364 * client side session object.
365 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
366 * waiting and 0 for immediate return if data are not available on wire.
367 * @param[out] notif Resulting object of NETCONF Notification.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100368 * @return #NC_MSG_NOTIF for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
369 * reading has failed, and #NC_MSG_REPLY if a reply was read instead (call this function
370 * again to get a notification).
Michal Vasko086311b2016-01-08 09:53:11 +0100371 */
Michal Vaskod083db62016-01-19 10:31:29 +0100372NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif);
Michal Vasko086311b2016-01-08 09:53:11 +0100373
374/**
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100375 * @brief Receive NETCONF Notifications in a separate thread until the session is terminated
376 * or \<notificationComplete\> is received.
377 *
378 * @param[in] session Netconf session to read notifications from.
379 * @param[in] notif_clb Function that is called for every received notification (including
380 * \<notificationComplete\>). Parameters are the session the notification was received on
381 * and the notification itself.
382 * @return 0 if the thread was successfully created, -1 on error.
383 */
384int nc_recv_notif_dispatch(struct nc_session *session,
385 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif));
386
387/**
Michal Vasko086311b2016-01-08 09:53:11 +0100388 * @brief Send NETCONF RPC message via the session.
389 *
390 * @param[in] session NETCONF session where the RPC will be written.
391 * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by
392 * nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions.
393 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
394 * waiting and 0 for return if data cannot be sent immediately.
395 * @param[out] msgid If RPC was successfully sent, this is it's message ID.
396 * @return #NC_MSG_RPC on success, #NC_MSG_WOULDBLOCK in case of busy session
397 * and #NC_MSG_ERROR on error.
398 */
Michal Vaskod083db62016-01-19 10:31:29 +0100399NC_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 +0100400
401#endif /* NC_SESSION_CLIENT_H_ */