blob: 625a5052069b2db95700f8b95faef5bcc13c0616 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#ifndef NC_SESSION_CLIENT_H_
24#define NC_SESSION_CLIENT_H_
25
26#include <libyang/libyang.h>
27
28#ifdef ENABLE_SSH
29
30#include <libssh/libssh.h>
31
32#endif
33
34#ifdef ENABLE_TLS
35
36#include <openssl/ssl.h>
37
38#endif
39
40#include "session.h"
41#include "netconf.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010042#include "messages_client.h"
Michal Vasko086311b2016-01-08 09:53:11 +010043
44/**
45 * @brief Set location where libnetconf tries to search for YANG/YIN schemas.
46 *
47 * The location is search when connecting to a NETCONF server and building
48 * YANG context for further processing of the NETCONF messages and data.
49 *
50 * Function is provided only via nc_client.h header file.
51 *
52 * @param[in] path Directory where to search for YANG/YIN schemas.
53 * @return 0 on success, 1 on (memory allocation) failure.
54 */
Michal Vasko3031aae2016-01-27 16:07:18 +010055int nc_client_schema_searchpath(const char *path);
Michal Vasko086311b2016-01-08 09:53:11 +010056
57/**
Michal Vasko086311b2016-01-08 09:53:11 +010058 * @brief Connect to the NETCONF server via proviaded input/output file descriptors.
59 *
60 * Transport layer is supposed to be already set. Function do not cover authentication
61 * or any other manipulation with the transport layer, it only establish NETCONF session
62 * by sending and processing NETCONF \<hello\> messages.
63 *
64 * Function is provided only via nc_client.h header file.
65 *
66 * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server.
67 * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server.
68 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
69 * (ignoring what is actually supported by the server side). If not set,
70 * YANG context is created for the session using \<get-schema\> (if supported
71 * by the server side) or/and by searching for YANG schemas in the searchpath
72 * (see nc_schema_searchpath()). In every case except not providing context
73 * to connect to a server supporting \<get-schema\> it is possible that
74 * the session context will not include all the models supported by the server.
75 * @return Created NETCONF session object or NULL in case of error.
76 */
77struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx);
78
79#ifdef ENABLE_SSH
80
81/**
Michal Vasko3031aae2016-01-27 16:07:18 +010082 * @brief Add an SSH public and private key pair to be used for client authentication.
83 *
84 * Private key can be encrypted, the passphrase will be asked for before using it.
85 *
86 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
87 *
88 * @param[in] pub_key Path to the public key.
89 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +010090 * @return 0 on success, -1 on error.
Michal Vasko3031aae2016-01-27 16:07:18 +010091 */
92int nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key);
93
94/**
95 * @brief Remove an SSH public and private key pair that was used for client authentication.
96 *
97 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
98 *
99 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko3031aae2016-01-27 16:07:18 +0100100 * @return 0 on success, -1 on error.
101 */
102int nc_client_ssh_del_keypair(int idx);
103
104/**
105 * @brief Get the number of public an private key pairs set to be used for client authentication.
106 *
107 * @return Keypair count.
108 */
109int nc_client_ssh_get_keypair_count(void);
110
111/**
112 * @brief Get a specific keypair set to be used for client authentication.
113 *
114 * @param[in] idx Index of the specific keypair.
115 * @param[out] pub_key Path to the public key.
116 * @param[out] priv_key Path to the private key.
Michal Vasko3031aae2016-01-27 16:07:18 +0100117 * @return 0 on success, -1 on error.
118 */
119int nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key);
120
121/**
122 * @brief Set SSH authentication method preference.
123 *
124 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
125 *
126 * @param[in] auth_type Authentication method to modify the prefrence of.
127 * @param[in] pref Preference of \p auth_type. Negative values disable the method.
128 */
129void nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
130
131/**
132 * @brief Get SSH authentication method preference.
133 *
134 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
135 *
136 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vasko3031aae2016-01-27 16:07:18 +0100137 * @return Preference of the \p auth_type.
138 */
139int16_t nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
140
141/**
142 * @brief Set client SSH username used for authentication.
143 *
144 * @param[in] username Username to use.
145 * @return 0 on success, -1 on error.
146 */
147int nc_client_ssh_set_username(const char *username);
148
Michal Vasko3031aae2016-01-27 16:07:18 +0100149/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100150 * @brief Get client SSH username used for authentication.
151 *
152 * @return Username used.
153 */
154const char *nc_client_ssh_get_username(void);
155
156/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100157 * @brief Destroy any dynamically allocated SSH-specific client context (including Call Home).
Michal Vasko086311b2016-01-08 09:53:11 +0100158 *
159 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
160 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100161void nc_client_ssh_destroy_opts(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100162
163/**
164 * @brief Connect to the NETCONF server using SSH transport (via libssh).
165 *
166 * SSH session is created with default options. If the caller needs to use specific SSH session properties,
167 * they are supposed to use nc_connect_libssh().
168 *
169 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
170 *
171 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
172 * 'localhost' is used by default if NULL is specified.
173 * @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 +0100174 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
175 * (ignoring what is actually supported by the server side). If not set,
176 * YANG context is created for the session using \<get-schema\> (if supported
177 * by the server side) or/and by searching for YANG schemas in the searchpath
178 * (see nc_schema_searchpath()). In every case except not providing context
179 * to connect to a server supporting \<get-schema\> it is possible that
180 * the session context will not include all the models supported by the server.
181 * @return Created NETCONF session object or NULL on error.
182 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100183struct nc_session *nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx);
Michal Vasko086311b2016-01-08 09:53:11 +0100184
185/**
186 * @brief Connect to the NETCONF server using the provided SSH (libssh) session.
187 *
188 * SSH session can have any options set, they will not be modified. If no options were set,
189 * host 'localhost', port 22, and the username detected from the EUID is used. If socket is
190 * set and connected only the host and the username must be set/is detected. Or the \p ssh_session
191 * can already be authenticated in which case it is used directly.
192 *
193 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
194 *
195 * @param[in] ssh_session libssh structure representing SSH session object. After passing it
196 * to libnetconf2 this way, it is fully managed by it (including freeing!).
197 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
198 * (ignoring what is actually supported by the server side). If not set,
199 * YANG context is created for the session using \<get-schema\> (if supported
200 * by the server side) or/and by searching for YANG schemas in the searchpath
201 * (see nc_schema_searchpath()). In every case except not providing context
202 * to connect to a server supporting \<get-schema\> it is possible that
203 * the session context will not include all the models supported by the server.
204 * @return Created NETCONF session object or NULL on error.
205 */
206struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx);
207
208/**
209 * @brief Create another NETCONF session on existing SSH session using separated SSH channel.
210 *
211 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support.
212 *
213 * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh -
214 * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel().
215 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
216 * (ignoring what is actually supported by the server side). If not set,
217 * YANG context is created for the session using \<get-schema\> (if supported
218 * by the server side) or/and by searching for YANG schemas in the searchpath
219 * (see nc_schema_searchpath()). In every case except not providing context
220 * to connect to a server supporting \<get-schema\> it is possible that
221 * the session context will not include all the models supported by the server.
222 * @return Created NETCONF session object or NULL on error.
223 */
224struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx);
225
Michal Vasko086311b2016-01-08 09:53:11 +0100226#endif /* ENABLE_SSH */
227
228#ifdef ENABLE_TLS
229
230/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100231 * @brief Set client authentication identity - a certificate and a private key.
Michal Vasko086311b2016-01-08 09:53:11 +0100232 *
233 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
234 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100235 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vasko086311b2016-01-08 09:53:11 +0100236 * @param[in] client_key Path to the file containing the private key for the \p client_cert.
237 * If NULL, key is expected to be stored with \p client_cert.
Michal Vasko3031aae2016-01-27 16:07:18 +0100238 * @return 0 on success, -1 on error.
239 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100240int nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key);
Michal Vasko3031aae2016-01-27 16:07:18 +0100241
242/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100243 * @brief Get client authentication identity - a certificate and a private key.
244 *
245 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
246 *
247 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
248 * @param[out] client_key Path to the file containing the private key for the \p client_cert. Can be NULL.
249 */
250void nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key);
251
252/**
253 * @brief Set client trusted CA certificates paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100254 *
Michal Vasko086311b2016-01-08 09:53:11 +0100255 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
256 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
257 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
258 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100259 * @return 0 on success, -1 on error.
260 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100261int nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100262
263/**
Michal Vaskoe22c6732016-01-29 11:03:02 +0100264 * @brief Get client trusted CA certificates paths.
265 *
266 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
267 * Can be NULL.
268 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
269 * Can be NULL.
270 */
271void nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
272
273/**
274 * @brief Set client Certificate Revocation List paths.
Michal Vasko3031aae2016-01-27 16:07:18 +0100275 *
Michal Vasko086311b2016-01-08 09:53:11 +0100276 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
277 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
Michal Vasko3031aae2016-01-27 16:07:18 +0100278 * @return 0 on success, -1 on error.
279 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100280int nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir);
281
282/**
283 * @brief Get client Certificate Revocation List paths.
284 *
285 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
286 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
287 */
288void nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100289
Michal Vasko3031aae2016-01-27 16:07:18 +0100290/**
291 * @brief Destroy any dynamically allocated TLS-specific client data (including Call Home).
Michal Vasko086311b2016-01-08 09:53:11 +0100292 *
293 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
294 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100295void nc_client_tls_destroy_opts(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100296
297/**
298 * @brief Connect to the NETCONF server using TLS transport (via libssl)
299 *
300 * TLS session is created with the certificates set using nc_client_init_tls(), which must be called beforehand!
301 * If the caller needs to use specific TLS session properties, they are supposed to use nc_connect_libssl().
302 *
303 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
304 *
305 * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server.
306 * 'localhost' is used by default if NULL is specified.
307 * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified.
308 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
309 * (ignoring what is actually supported by the server side). If not set,
310 * YANG context is created for the session using \<get-schema\> (if supported
311 * by the server side) or/and by searching for YANG schemas in the searchpath
312 * (see nc_schema_searchpath()). In every case except not providing context
313 * to connect to a server supporting \<get-schema\> it is possible that
314 * the session context will not include all the models supported by the server.
315 * @return Created NETCONF session object or NULL on error.
316 */
317struct nc_session *nc_connect_tls(const char *host, uint16_t port, struct ly_ctx *ctx);
318
319/**
320 * @brief Connect to the NETCONF server using the provided TLS (libssl) session.
321 *
322 * The TLS session supplied is expected to be fully connected and authenticated!
323 *
324 * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support.
325 *
326 * @param[in] tls libssl structure representing the TLS session object.
327 * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session
328 * (ignoring what is actually supported by the server side). If not set,
329 * YANG context is created for the session using \<get-schema\> (if supported
330 * by the server side) or/and by searching for YANG schemas in the searchpath
331 * (see nc_schema_searchpath()). In every case except not providing context
332 * to connect to a server supporting \<get-schema\> it is possible that
333 * the session context will not include all the models supported by the server.
334 * @return Created NETCONF session object or NULL on error.
335 */
336struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx);
337
Michal Vasko086311b2016-01-08 09:53:11 +0100338#endif /* ENABLE_TLS */
339
340/**
341 * @brief Receive NETCONF RPC reply.
342 *
343 * If a reply to \<get\> or \<get-config\> RPCs is received, the data are the whole configuration
344 * parsed (usually results in more top-level nodes), not just a single 'data' anyxml node with
345 * the configuration unparsed inside (which would strictly be according to the model).
346 *
347 * @param[in] session NETCONF session from which the function gets data. It must be the
348 * client side session object.
349 * @param[in] rpc Original RPC this should be the reply to.
350 * @param[in] msgid Expected message ID of the reply.
351 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
352 * waiting and 0 for immediate return if data are not available on wire.
353 * @param[out] reply Resulting object of NETCONF RPC reply.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100354 * @return #NC_MSG_REPLY for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
355 * reading has failed, and #NC_MSG_NOTIF if a notification was read instead (call this
356 * function again to get the reply).
Michal Vasko086311b2016-01-08 09:53:11 +0100357 */
Michal Vaskod083db62016-01-19 10:31:29 +0100358NC_MSG_TYPE nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout,
Michal Vasko086311b2016-01-08 09:53:11 +0100359 struct nc_reply **reply);
360
361/**
362 * @brief Receive NETCONF Notification.
363 *
364 * @param[in] session NETCONF session from which the function gets data. It must be the
365 * client side session object.
366 * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite
367 * waiting and 0 for immediate return if data are not available on wire.
368 * @param[out] notif Resulting object of NETCONF Notification.
Michal Vasko2518b6b2016-01-28 13:24:53 +0100369 * @return #NC_MSG_NOTIF for success, #NC_MSG_WOULDBLOCK if timeout has elapsed, #NC_MSG_ERROR if
370 * reading has failed, and #NC_MSG_REPLY if a reply was read instead (call this function
371 * again to get a notification).
Michal Vasko086311b2016-01-08 09:53:11 +0100372 */
Michal Vaskod083db62016-01-19 10:31:29 +0100373NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif);
Michal Vasko086311b2016-01-08 09:53:11 +0100374
375/**
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100376 * @brief Receive NETCONF Notifications in a separate thread until the session is terminated
377 * or \<notificationComplete\> is received.
378 *
379 * @param[in] session Netconf session to read notifications from.
380 * @param[in] notif_clb Function that is called for every received notification (including
381 * \<notificationComplete\>). Parameters are the session the notification was received on
382 * and the notification itself.
383 * @return 0 if the thread was successfully created, -1 on error.
384 */
385int nc_recv_notif_dispatch(struct nc_session *session,
386 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif));
387
388/**
Michal Vasko086311b2016-01-08 09:53:11 +0100389 * @brief Send NETCONF RPC message via the session.
390 *
391 * @param[in] session NETCONF session where the RPC will be written.
392 * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by
393 * nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions.
394 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
395 * waiting and 0 for return if data cannot be sent immediately.
396 * @param[out] msgid If RPC was successfully sent, this is it's message ID.
397 * @return #NC_MSG_RPC on success, #NC_MSG_WOULDBLOCK in case of busy session
398 * and #NC_MSG_ERROR on error.
399 */
Michal Vaskod083db62016-01-19 10:31:29 +0100400NC_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 +0100401
402#endif /* NC_SESSION_CLIENT_H_ */