Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_SESSION_CLIENT_H_ |
| 16 | #define NC_SESSION_CLIENT_H_ |
| 17 | |
| 18 | #include <libyang/libyang.h> |
| 19 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 20 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 21 | |
| 22 | #include <libssh/libssh.h> |
| 23 | |
| 24 | #endif |
| 25 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 26 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 27 | |
| 28 | #include <openssl/ssl.h> |
| 29 | |
| 30 | #endif |
| 31 | |
| 32 | #include "session.h" |
| 33 | #include "netconf.h" |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 34 | #include "messages_client.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 37 | * @addtogroup client |
| 38 | * @{ |
| 39 | */ |
| 40 | |
| 41 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 42 | * @brief Set location where libnetconf tries to search for YANG/YIN schemas. |
| 43 | * |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 44 | * The location is searched when connecting to a NETCONF server and building |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | * YANG context for further processing of the NETCONF messages and data. |
| 46 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 47 | * @param[in] path Directory where to search for YANG/YIN schemas. |
| 48 | * @return 0 on success, 1 on (memory allocation) failure. |
| 49 | */ |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 50 | int nc_client_set_schema_searchpath(const char *path); |
| 51 | |
| 52 | /** |
| 53 | * @brief Get schema searchpath that was set by nc_client_set_schema_searchpath(). |
| 54 | * |
| 55 | * @return Schema searchpath directory, NULL if not set. |
| 56 | */ |
| 57 | const char *nc_client_get_schema_searchpath(void); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 58 | |
| 59 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 60 | * @brief Initialize libssh and/or libssl/libcrypto for use in the client. |
| 61 | */ |
| 62 | void nc_client_init(void); |
| 63 | |
| 64 | /** |
| 65 | * @brief Destroy all libssh and/or libssl/libcrypto dynamic memory and |
| 66 | * the client options, for both SSH and TLS, and for Call Home too. |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 67 | */ |
| 68 | void nc_client_destroy(void); |
| 69 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 70 | /**@} Client */ |
| 71 | |
| 72 | /** |
| 73 | * @defgroup client_session Client Session |
| 74 | * @ingroup client |
| 75 | * |
| 76 | * @brief Client-side NETCONF session manipulation. |
| 77 | * @{ |
| 78 | */ |
| 79 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 80 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 81 | * @brief Connect to the NETCONF server via proviaded input/output file descriptors. |
| 82 | * |
| 83 | * Transport layer is supposed to be already set. Function do not cover authentication |
| 84 | * or any other manipulation with the transport layer, it only establish NETCONF session |
| 85 | * by sending and processing NETCONF \<hello\> messages. |
| 86 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 87 | * @param[in] fdin Input file descriptor for reading (clear) data from NETCONF server. |
| 88 | * @param[in] fdout Output file descriptor for writing (clear) data for NETCONF server. |
| 89 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 90 | * (ignoring what is actually supported by the server side). If not set, |
| 91 | * YANG context is created for the session using \<get-schema\> (if supported |
| 92 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 93 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 94 | * to connect to a server supporting \<get-schema\> it is possible that |
| 95 | * the session context will not include all the models supported by the server. |
| 96 | * @return Created NETCONF session object or NULL in case of error. |
| 97 | */ |
| 98 | struct nc_session *nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx); |
| 99 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 100 | /**@} Client Session */ |
| 101 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 102 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 105 | * @defgroup client_ssh Client SSH |
| 106 | * @ingroup client |
| 107 | * |
| 108 | * @brief Client-side settings for SSH connections. |
| 109 | * @{ |
| 110 | */ |
| 111 | |
| 112 | /** |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 113 | * @brief Set SSH authentication hostkey check (knownhosts) callback. |
| 114 | * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 115 | * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for |
| 116 | * freeing the private data when necessary (the private data can be obtained by |
| 117 | * nc_client_ssh_get_auth_hostkey_check_clb()). |
| 118 | * |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 119 | * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error. |
| 120 | * If NULL, the default callback is set. |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 121 | * @param[in] priv Optional private data to be passed to the callback function. |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 122 | */ |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 123 | void nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv), |
| 124 | void *priv); |
| 125 | |
| 126 | /** |
| 127 | * @brief Get currently set SSH authentication hostkey check (knownhosts) callback and its private data previously set |
| 128 | * by nc_client_ssh_set_auth_hostkey_check_clb(). |
| 129 | * |
| 130 | * @param[out] auth_hostkey_check Currently set callback, NULL in case of the default callback. |
| 131 | * @param[out] priv Currently set (optional) private data to be passed to the callback function. |
| 132 | */ |
| 133 | void nc_client_ssh_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv), |
| 134 | void **priv); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 135 | |
| 136 | /** |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 137 | * @brief Set SSH password authentication callback. |
| 138 | * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 139 | * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for |
| 140 | * freeing the private data when necessary (the private data can be obtained by |
| 141 | * nc_client_ssh_get_auth_password_clb()). |
| 142 | * |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 143 | * @param[in] auth_password Function to call, returns the password for username\@hostname. |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 144 | * If NULL, the default callback is set. |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 145 | * @param[in] priv Optional private data to be passed to the callback function. |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 146 | */ |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 147 | void nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv), |
| 148 | void *priv); |
| 149 | |
| 150 | /** |
| 151 | * @brief Get currently set SSH password authentication callback and its private data previously set |
| 152 | * by nc_client_ssh_set_auth_password_clb(). |
| 153 | * |
| 154 | * @param[out] auth_password Currently set callback, NULL in case of the default callback. |
| 155 | * @param[out] priv Currently set (optional) private data to be passed to the callback function. |
| 156 | */ |
| 157 | void nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv), |
| 158 | void **priv); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * @brief Set SSH interactive authentication callback. |
| 162 | * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 163 | * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for |
| 164 | * freeing the private data when necessary (the private data can be obtained by |
| 165 | * nc_client_ssh_get_auth_interactive_clb()). |
| 166 | * |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 167 | * @param[in] auth_interactive Function to call for every question, returns the answer for |
| 168 | * authentication name with instruction and echoing prompt. |
| 169 | * If NULL, the default callback is set. |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 170 | * @param[in] priv Optional private data to be passed to the callback function. |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 171 | */ |
| 172 | void nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction, |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 173 | const char *prompt, int echo, void *priv), |
| 174 | void *priv); |
| 175 | |
| 176 | /** |
| 177 | * @brief Get currently set SSH interactive authentication callback and its private data previously set |
| 178 | * by nc_client_ssh_set_auth_interactive_clb(). |
| 179 | * |
| 180 | * @param[out] auth_interactive Currently set callback, NULL in case of the default callback. |
| 181 | * @param[out] priv Currently set (optional) private data to be passed to the callback function. |
| 182 | */ |
| 183 | void nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction, |
| 184 | const char *prompt, int echo, void *priv), |
| 185 | void **priv); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * @brief Set SSH publickey authentication encrypted private key passphrase callback. |
| 189 | * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 190 | * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for |
| 191 | * freeing the private data when necessary (the private data can be obtained by |
| 192 | * nc_client_ssh_get_auth_privkey_passphrase_clb()). |
| 193 | * |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 194 | * @param[in] auth_privkey_passphrase Function to call for every question, returns |
| 195 | * the passphrase for the specific private key. |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 196 | * @param[in] priv Optional private data to be passed to the callback function. |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 197 | */ |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 198 | void nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv), |
| 199 | void *priv); |
| 200 | |
| 201 | /** |
| 202 | * @brief Get currently set SSH publickey authentication encrypted private key passphrase callback and its private data |
| 203 | * previously set by nc_client_ssh_set_auth_privkey_passphrase_clb(). |
| 204 | * |
| 205 | * @param[out] auth_privkey_passphrase Currently set callback, NULL in case of the default callback. |
| 206 | * @param[out] priv Currently set (optional) private data to be passed to the callback function. |
| 207 | */ |
| 208 | void nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv), |
| 209 | void **priv); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 210 | |
| 211 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 212 | * @brief Add an SSH public and private key pair to be used for client authentication. |
| 213 | * |
| 214 | * Private key can be encrypted, the passphrase will be asked for before using it. |
| 215 | * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 216 | * @param[in] pub_key Path to the public key. |
| 217 | * @param[in] priv_key Path to the private key. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 218 | * @return 0 on success, -1 on error. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 219 | */ |
| 220 | int nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key); |
| 221 | |
| 222 | /** |
| 223 | * @brief Remove an SSH public and private key pair that was used for client authentication. |
| 224 | * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 225 | * @param[in] idx Index of the keypair starting with 0. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 226 | * @return 0 on success, -1 on error. |
| 227 | */ |
| 228 | int nc_client_ssh_del_keypair(int idx); |
| 229 | |
| 230 | /** |
| 231 | * @brief Get the number of public an private key pairs set to be used for client authentication. |
| 232 | * |
| 233 | * @return Keypair count. |
| 234 | */ |
| 235 | int nc_client_ssh_get_keypair_count(void); |
| 236 | |
| 237 | /** |
| 238 | * @brief Get a specific keypair set to be used for client authentication. |
| 239 | * |
| 240 | * @param[in] idx Index of the specific keypair. |
| 241 | * @param[out] pub_key Path to the public key. |
| 242 | * @param[out] priv_key Path to the private key. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 243 | * @return 0 on success, -1 on error. |
| 244 | */ |
| 245 | int nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key); |
| 246 | |
| 247 | /** |
| 248 | * @brief Set SSH authentication method preference. |
| 249 | * |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame^] | 250 | * The default preference is as follows: |
| 251 | * - interactive authentication (3) |
| 252 | * - password authentication (2) |
| 253 | * - public key authentication (1) |
| 254 | * |
| 255 | * @param[in] auth_type Authentication method to modify the preference of. |
| 256 | * @param[in] pref Preference of \p auth_type. Higher number increases priority, negative values disable the method. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 257 | */ |
| 258 | void nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref); |
| 259 | |
| 260 | /** |
| 261 | * @brief Get SSH authentication method preference. |
| 262 | * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 263 | * @param[in] auth_type Authentication method to retrieve the prefrence of. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 264 | * @return Preference of the \p auth_type. |
| 265 | */ |
| 266 | int16_t nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type); |
| 267 | |
| 268 | /** |
| 269 | * @brief Set client SSH username used for authentication. |
| 270 | * |
| 271 | * @param[in] username Username to use. |
| 272 | * @return 0 on success, -1 on error. |
| 273 | */ |
| 274 | int nc_client_ssh_set_username(const char *username); |
| 275 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 276 | /** |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 277 | * @brief Get client SSH username used for authentication. |
| 278 | * |
| 279 | * @return Username used. |
| 280 | */ |
| 281 | const char *nc_client_ssh_get_username(void); |
| 282 | |
| 283 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 284 | * @brief Connect to the NETCONF server using SSH transport (via libssh). |
| 285 | * |
| 286 | * SSH session is created with default options. If the caller needs to use specific SSH session properties, |
| 287 | * they are supposed to use nc_connect_libssh(). |
| 288 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 289 | * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server. |
| 290 | * 'localhost' is used by default if NULL is specified. |
| 291 | * @param[in] port Port number of the target server. Default value 830 is used if 0 is specified. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 292 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 293 | * (ignoring what is actually supported by the server side). If not set, |
| 294 | * YANG context is created for the session using \<get-schema\> (if supported |
| 295 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 296 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 297 | * to connect to a server supporting \<get-schema\> it is possible that |
| 298 | * the session context will not include all the models supported by the server. |
| 299 | * @return Created NETCONF session object or NULL on error. |
| 300 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 301 | struct nc_session *nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * @brief Connect to the NETCONF server using the provided SSH (libssh) session. |
| 305 | * |
| 306 | * SSH session can have any options set, they will not be modified. If no options were set, |
| 307 | * host 'localhost', port 22, and the username detected from the EUID is used. If socket is |
| 308 | * set and connected only the host and the username must be set/is detected. Or the \p ssh_session |
| 309 | * can already be authenticated in which case it is used directly. |
| 310 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 311 | * @param[in] ssh_session libssh structure representing SSH session object. After passing it |
| 312 | * to libnetconf2 this way, it is fully managed by it (including freeing!). |
| 313 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 314 | * (ignoring what is actually supported by the server side). If not set, |
| 315 | * YANG context is created for the session using \<get-schema\> (if supported |
| 316 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 317 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 318 | * to connect to a server supporting \<get-schema\> it is possible that |
| 319 | * the session context will not include all the models supported by the server. |
| 320 | * @return Created NETCONF session object or NULL on error. |
| 321 | */ |
| 322 | struct nc_session *nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx); |
| 323 | |
| 324 | /** |
| 325 | * @brief Create another NETCONF session on existing SSH session using separated SSH channel. |
| 326 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 327 | * @param[in] session Existing NETCONF session. The session has to be created on SSH transport layer using libssh - |
| 328 | * it has to be created by nc_connect_ssh(), nc_connect_libssh() or nc_connect_ssh_channel(). |
| 329 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 330 | * (ignoring what is actually supported by the server side). If not set, |
| 331 | * YANG context is created for the session using \<get-schema\> (if supported |
| 332 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 333 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 334 | * to connect to a server supporting \<get-schema\> it is possible that |
| 335 | * the session context will not include all the models supported by the server. |
| 336 | * @return Created NETCONF session object or NULL on error. |
| 337 | */ |
| 338 | struct nc_session *nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx); |
| 339 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 340 | /**@} Client SSH */ |
| 341 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 342 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 343 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 344 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 345 | |
| 346 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 347 | * @defgroup client_tls Client TLS |
| 348 | * @ingroup client |
| 349 | * |
| 350 | * @brief Client-side settings for TLS connections. |
| 351 | * @{ |
| 352 | */ |
| 353 | |
| 354 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 355 | * @brief Set client authentication identity - a certificate and a private key. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 356 | * |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 357 | * @param[in] client_cert Path to the file containing the client certificate. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 358 | * @param[in] client_key Path to the file containing the private key for the \p client_cert. |
| 359 | * If NULL, key is expected to be stored with \p client_cert. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 360 | * @return 0 on success, -1 on error. |
| 361 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 362 | int nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 363 | |
| 364 | /** |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 365 | * @brief Get client authentication identity - a certificate and a private key. |
| 366 | * |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 367 | * @param[out] client_cert Path to the file containing the client certificate. Can be NULL. |
| 368 | * @param[out] client_key Path to the file containing the private key for the \p client_cert. Can be NULL. |
| 369 | */ |
| 370 | void nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key); |
| 371 | |
| 372 | /** |
| 373 | * @brief Set client trusted CA certificates paths. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 374 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 375 | * @param[in] ca_file Location of the CA certificate file used to verify server certificates. |
| 376 | * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL. |
| 377 | * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates. |
| 378 | * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 379 | * @return 0 on success, -1 on error. |
| 380 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 381 | int nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 382 | |
| 383 | /** |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 384 | * @brief Get client trusted CA certificates paths. |
| 385 | * |
| 386 | * @param[out] ca_file Location of the CA certificate file used to verify server certificates. |
| 387 | * Can be NULL. |
| 388 | * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates. |
| 389 | * Can be NULL. |
| 390 | */ |
| 391 | void nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir); |
| 392 | |
| 393 | /** |
| 394 | * @brief Set client Certificate Revocation List paths. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 395 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 396 | * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates. |
| 397 | * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 398 | * @return 0 on success, -1 on error. |
| 399 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 400 | int nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir); |
| 401 | |
| 402 | /** |
| 403 | * @brief Get client Certificate Revocation List paths. |
| 404 | * |
| 405 | * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates. |
| 406 | * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates. |
| 407 | */ |
| 408 | void nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 409 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 410 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 411 | * @brief Connect to the NETCONF server using TLS transport (via libssl) |
| 412 | * |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 413 | * TLS session is created with the certificates set using nc_client_tls_* functions, which must be called beforehand! |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 414 | * If the caller needs to use specific TLS session properties, they are supposed to use nc_connect_libssl(). |
| 415 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 416 | * @param[in] host Hostname or address (both Ipv4 and IPv6 are accepted) of the target server. |
| 417 | * 'localhost' is used by default if NULL is specified. |
| 418 | * @param[in] port Port number of the target server. Default value 6513 is used if 0 is specified. |
| 419 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 420 | * (ignoring what is actually supported by the server side). If not set, |
| 421 | * YANG context is created for the session using \<get-schema\> (if supported |
| 422 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 423 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 424 | * to connect to a server supporting \<get-schema\> it is possible that |
| 425 | * the session context will not include all the models supported by the server. |
| 426 | * @return Created NETCONF session object or NULL on error. |
| 427 | */ |
| 428 | struct nc_session *nc_connect_tls(const char *host, uint16_t port, struct ly_ctx *ctx); |
| 429 | |
| 430 | /** |
| 431 | * @brief Connect to the NETCONF server using the provided TLS (libssl) session. |
| 432 | * |
| 433 | * The TLS session supplied is expected to be fully connected and authenticated! |
| 434 | * |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 435 | * @param[in] tls libssl structure representing the TLS session object. |
| 436 | * @param[in] ctx Optional parameter. If set, provides strict YANG context for the session |
| 437 | * (ignoring what is actually supported by the server side). If not set, |
| 438 | * YANG context is created for the session using \<get-schema\> (if supported |
| 439 | * by the server side) or/and by searching for YANG schemas in the searchpath |
Radek Krejci | c9a6d25 | 2016-03-04 14:50:34 +0100 | [diff] [blame] | 440 | * (see nc_client_schema_searchpath()). In every case except not providing context |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 441 | * to connect to a server supporting \<get-schema\> it is possible that |
| 442 | * the session context will not include all the models supported by the server. |
| 443 | * @return Created NETCONF session object or NULL on error. |
| 444 | */ |
| 445 | struct nc_session *nc_connect_libssl(SSL *tls, struct ly_ctx *ctx); |
| 446 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 447 | /**@} Client TLS */ |
| 448 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 449 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 450 | |
| 451 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 452 | * @addtogroup client_session |
| 453 | * @{ |
| 454 | */ |
| 455 | |
| 456 | /** |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 457 | * @brief Get session capabilities. |
| 458 | * |
| 459 | * @param[in] session Session to get the information from. |
Radek Krejci | 82da781 | 2017-05-25 13:52:04 +0200 | [diff] [blame] | 460 | * @return NULL-terminated array of the \p session capabilities. |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 461 | */ |
| 462 | const char **nc_session_get_cpblts(const struct nc_session *session); |
| 463 | |
| 464 | /** |
| 465 | * @brief Check capability presence in a session. |
| 466 | * |
| 467 | * @param[in] session Session to check. |
| 468 | * @param[in] capab Capability to look for, capability with any additional suffix will match. |
| 469 | * @return Matching capability, NULL if none found. |
| 470 | */ |
| 471 | const char *nc_session_cpblt(const struct nc_session *session, const char *capab); |
| 472 | |
| 473 | /** |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 474 | * @brief Check whether the session has a notification thread running. |
| 475 | * |
| 476 | * @param[in] session Session to check. |
| 477 | * @return 1 if notfication thread is running, 0 otherwise. |
| 478 | */ |
| 479 | int nc_session_ntf_thread_running(const struct nc_session *session); |
| 480 | |
| 481 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 482 | * @brief Receive NETCONF RPC reply. |
| 483 | * |
Michal Vasko | 12257e9 | 2016-11-22 09:30:37 +0100 | [diff] [blame] | 484 | * Be careful, normally there is a whole RPC reply (output) of an RPC in the \p reply. |
| 485 | * However, if a reply to \<get\> or \<get-config\> RPC is received, the \p reply is |
| 486 | * actually the configuration (with either state data or not). This means, for example, |
Michal Vasko | 50d2a5c | 2017-02-14 10:29:49 +0100 | [diff] [blame] | 487 | * that the reply data in these cases should not be validated with \b LYD_OPT_RPCREPLY, |
| 488 | * but \b LYD_OPT_GET and \b LYD_OPT_GETCONFIG, respectively. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 489 | * |
| 490 | * @param[in] session NETCONF session from which the function gets data. It must be the |
| 491 | * client side session object. |
| 492 | * @param[in] rpc Original RPC this should be the reply to. |
| 493 | * @param[in] msgid Expected message ID of the reply. |
| 494 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 495 | * waiting and 0 for immediate return if data are not available on the wire. |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 496 | * @param[in] parseroptions libyang parseroptions flags, do not set the data type, it is set |
Michal Vasko | 50d2a5c | 2017-02-14 10:29:49 +0100 | [diff] [blame] | 497 | * internally. \b LYD_OPT_DESTRUCT and \b LYD_OPT_NOSIBLINGS is ignored. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 498 | * @param[out] reply Resulting object of NETCONF RPC reply. |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 499 | * @return #NC_MSG_REPLY for success, |
| 500 | * #NC_MSG_WOULDBLOCK if \p timeout has elapsed, |
| 501 | * #NC_MSG_ERROR if reading has failed, |
| 502 | * #NC_MSG_NOTIF if a notification was read instead (call this function again to get the reply), and |
| 503 | * #NC_MSG_REPLY_ERR_MSGID if a reply with missing or wrong message-id was received. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 504 | */ |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 505 | NC_MSG_TYPE nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 506 | int parseroptions, struct nc_reply **reply); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * @brief Receive NETCONF Notification. |
| 510 | * |
| 511 | * @param[in] session NETCONF session from which the function gets data. It must be the |
| 512 | * client side session object. |
| 513 | * @param[in] timeout Timeout for reading in milliseconds. Use negative value for infinite |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 514 | * waiting and 0 for immediate return if data are not available on the wire. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 515 | * @param[out] notif Resulting object of NETCONF Notification. |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 516 | * @return #NC_MSG_NOTIF for success, |
| 517 | * #NC_MSG_WOULDBLOCK if \p timeout has elapsed, |
| 518 | * #NC_MSG_ERROR if reading has failed, and |
| 519 | * #NC_MSG_REPLY if a reply was read instead (call this function again to get a notification). |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 520 | */ |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 521 | NC_MSG_TYPE nc_recv_notif(struct nc_session* session, int timeout, struct nc_notif **notif); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 522 | |
| 523 | /** |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 524 | * @brief Receive NETCONF Notifications in a separate thread until the session is terminated |
| 525 | * or \<notificationComplete\> is received. |
| 526 | * |
| 527 | * @param[in] session Netconf session to read notifications from. |
| 528 | * @param[in] notif_clb Function that is called for every received notification (including |
| 529 | * \<notificationComplete\>). Parameters are the session the notification was received on |
| 530 | * and the notification itself. |
| 531 | * @return 0 if the thread was successfully created, -1 on error. |
| 532 | */ |
| 533 | int nc_recv_notif_dispatch(struct nc_session *session, |
| 534 | void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif)); |
| 535 | |
| 536 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 537 | * @brief Send NETCONF RPC message via the session. |
| 538 | * |
| 539 | * @param[in] session NETCONF session where the RPC will be written. |
| 540 | * @param[in] rpc NETCOFN RPC object to send via specified session. Object can be created by |
| 541 | * nc_rpc_lock(), nc_rpc_unlock() and nc_rpc_generic() functions. |
| 542 | * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite |
| 543 | * waiting and 0 for return if data cannot be sent immediately. |
| 544 | * @param[out] msgid If RPC was successfully sent, this is it's message ID. |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 545 | * @return #NC_MSG_RPC on success, |
| 546 | * #NC_MSG_WOULDBLOCK in case of a busy session, and |
| 547 | * #NC_MSG_ERROR on error. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 548 | */ |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 549 | NC_MSG_TYPE nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 550 | |
Michal Vasko | de2946c | 2017-01-12 12:19:26 +0100 | [diff] [blame] | 551 | /** |
| 552 | * @brief Make a session not strict when sending RPCs and receiving RPC replies. In other words, |
| 553 | * it will silently skip unknown nodes without an error. |
| 554 | * |
| 555 | * Generally, no such data should be worked with, so use this function only when you know what you |
| 556 | * are doing and you understand the consequences. |
| 557 | * |
| 558 | * @param[in] session NETCONF client session. |
| 559 | */ |
| 560 | void nc_client_session_set_not_strict(struct nc_session *session); |
| 561 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 562 | /**@} Client Session */ |
| 563 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 564 | #endif /* NC_SESSION_CLIENT_H_ */ |