blob: d8db141fac407473d1f11c1ec2fcb2fad05a44f7 [file] [log] [blame]
Michal Vasko45f298f2016-01-29 10:26:26 +01001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file session_client_ch.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 Call Home session client manipulation
Michal Vasko45f298f2016-01-29 10:26:26 +01005 *
Michal Vaskoc446a382021-06-18 08:54:05 +02006 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko45f298f2016-01-29 10:26:26 +01007 *
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 Vasko45f298f2016-01-29 10:26:26 +010013 */
14
15#ifndef NC_SESSION_CLIENT_CH_H_
16#define NC_SESSION_CLIENT_CH_H_
17
Michal Vaskoc09730e2019-01-17 10:07:26 +010018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michal Vasko45f298f2016-01-29 10:26:26 +010022#include <libyang/libyang.h>
23
Michal Vaskoc446a382021-06-18 08:54:05 +020024#ifdef NC_ENABLED_SSH
25# include <libssh/libssh.h>
26#endif
27
Michal Vasko45f298f2016-01-29 10:26:26 +010028#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include "netconf.h"
30#include "session.h"
Michal Vasko45f298f2016-01-29 10:26:26 +010031
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko45f298f2016-01-29 10:26:26 +010033
34/**
Radek Krejci6799a052017-05-19 14:23:23 +020035 * @defgroup client_ch Client-side Call Home
36 * @ingroup client
37 *
38 * @brief Call Home functionality for client-side applications.
39 * @{
40 */
41
42/**
Michal Vasko45f298f2016-01-29 10:26:26 +010043 * @brief Accept a Call Home connection on any of the listening binds.
44 *
45 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
46 * non-blocking call, -1 for infinite waiting.
47 * @param[in] ctx Session context to use. Can be NULL.
48 * @param[out] session New session.
Michal Vaskof0537d82016-01-29 14:42:38 +010049 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +010050 */
51int nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session);
52
Michal Vaskoc446a382021-06-18 08:54:05 +020053/** @} Client-side Call Home */
Radek Krejci6799a052017-05-19 14:23:23 +020054
Radek Krejci53691be2016-02-22 13:58:37 +010055#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +010056
Radek Krejci53691be2016-02-22 13:58:37 +010057#ifdef NC_ENABLED_SSH
Michal Vasko45f298f2016-01-29 10:26:26 +010058
59/**
Radek Krejci6799a052017-05-19 14:23:23 +020060 * @defgroup client_ch_ssh Client-side Call Home on SSH
61 * @ingroup client_ch
62 *
63 * @brief SSH settings for the Call Home functionality
64 * @{
65 */
66
67/**
Radek Krejci90a84a22017-05-25 13:53:00 +020068 * @brief Set SSH Call Home authentication hostkey check (knownhosts) callback.
69 *
70 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
71 * freeing the private data when necessary (the private data can be obtained by
72 * nc_client_ssh_ch_get_auth_hostkey_check_clb()).
Michal Vaskoef112d72016-02-18 13:28:25 +010073 *
74 * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error.
75 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +020076 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vaskoef112d72016-02-18 13:28:25 +010077 */
Radek Krejci90a84a22017-05-25 13:53:00 +020078void nc_client_ssh_ch_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +020079 void *priv);
Michal Vaskoef112d72016-02-18 13:28:25 +010080
81/**
Radek Krejci90a84a22017-05-25 13:53:00 +020082 * @brief Get currently set SSH Call Home authentication hostkey check (knownhosts) callback and its private data
83 * previously set by nc_client_ssh_ch_set_auth_hostkey_check_clb().
84 *
85 * @param[out] auth_hostkey_check Currently set callback, NULL in case of the default callback.
86 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
87 */
88void nc_client_ssh_ch_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +020089 void **priv);
Radek Krejci90a84a22017-05-25 13:53:00 +020090/**
Michal Vasko30e2c872016-02-18 10:03:21 +010091 * @brief Set SSH Call Home password authentication callback.
92 *
Radek Krejci90a84a22017-05-25 13:53:00 +020093 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
94 * freeing the private data when necessary (the private data can be obtained by
95 * nc_client_ssh_ch_get_auth_password_clb()).
96 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +010097 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko30e2c872016-02-18 10:03:21 +010098 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +020099 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100100 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200101void nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200102 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200103
104/**
105 * @brief Get currently set SSH Call Home password authentication callback and its private data
106 * previously set by nc_client_ssh_ch_set_auth_password_clb().
107 *
108 * @param[out] auth_password Currently set callback, NULL in case of the default callback.
109 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
110 */
111void nc_client_ssh_ch_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200112 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100113
114/**
115 * @brief Set SSH Call Home interactive authentication callback.
116 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200117 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
118 * freeing the private data when necessary (the private data can be obtained by
119 * nc_client_ssh_ch_get_auth_interactive_clb()).
120 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100121 * @param[in] auth_interactive Function to call for every question, returns the answer for
122 * authentication name with instruction and echoing prompt.
123 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +0200124 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100125 */
126void nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200127 const char *prompt, int echo, void *priv),
128 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200129
130/**
131 * @brief Get currently set SSH Call Home interactive authentication callback and its private data
132 * previously set by nc_client_ssh_ch_set_auth_interactive_clb().
133 *
134 * @param[out] auth_interactive Currently set callback, NULL in case of the default callback.
135 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
136 */
137void nc_client_ssh_ch_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200138 const char *prompt, int echo, void *priv),
139 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100140
141/**
142 * @brief Set SSH Call Home publickey authentication encrypted private key passphrase callback.
143 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200144 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
145 * freeing the private data when necessary (the private data can be obtained by
146 * nc_client_ssh_ch_get_auth_privkey_passphrase_clb()).
147 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100148 * @param[in] auth_privkey_passphrase Function to call for every question, returns
149 * the passphrase for the specific private key.
Radek Krejci90a84a22017-05-25 13:53:00 +0200150 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100151 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200152void nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200153 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200154
155/**
156 * @brief Get currently set SSH Call Home publickey authentication encrypted private key passphrase callback and its
157 * private data previously set by nc_client_ssh_ch_set_auth_privkey_passphrase_clb().
158 *
159 * @param[out] auth_privkey_passphrase Currently set callback, NULL in case of the default callback.
160 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
161 */
162void nc_client_ssh_ch_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200163 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100164
165/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100166 * @brief Add a new client bind and start listening on it for SSH Call Home connections.
167 *
168 * @param[in] address IP address to bind to.
169 * @param[in] port Port to bind to.
170 * @return 0 on success, -1 on error.
171 */
172int nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port);
173
174/**
175 * @brief Remove an SSH listening client bind.
176 *
177 * @param[in] address IP address the socket was bound to. NULL matches all.
178 * @param[in] port Port the socket was bound to. 0 matches all.
179 * @return 0 on success, -1 on not found.
180 */
181int nc_client_ssh_ch_del_bind(const char *address, uint16_t port);
182
183/**
184 * @brief Add an SSH public and private key pair to be used for Call Home client authentication.
185 *
186 * Private key can be encrypted, the passphrase will be asked for before using it.
187 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100188 * @param[in] pub_key Path to the public key.
189 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +0100190 * @return 0 on success, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +0100191 */
192int nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key);
193
194/**
195 * @brief Remove an SSH public and private key pair that was used for Call Home client authentication.
196 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100197 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko45f298f2016-01-29 10:26:26 +0100198 * @return 0 on success, -1 on error.
199 */
200int nc_client_ssh_ch_del_keypair(int idx);
201
202/**
203 * @brief Get the number of public an private key pairs set to be used for Call Home client authentication.
204 *
205 * @return Keypair count.
206 */
207int nc_client_ssh_ch_get_keypair_count(void);
208
209/**
210 * @brief Get a specific keypair set to be used for Call Home client authentication.
211 *
212 * @param[in] idx Index of the specific keypair.
213 * @param[out] pub_key Path to the public key.
214 * @param[out] priv_key Path to the private key.
Michal Vasko45f298f2016-01-29 10:26:26 +0100215 * @return 0 on success, -1 on error.
216 */
217int nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key);
218
219/**
220 * @brief Set SSH Call Home authentication method preference.
221 *
Radek Krejci62aa0642017-05-25 16:33:49 +0200222 * The default preference is as follows:
223 * - public key authentication (3)
224 * - password authentication (2)
225 * - interactive authentication (1)
226 *
227 * @param[in] auth_type Authentication method to modify the preference of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200228 * @param[in] pref Preference of @p auth_type. Higher number increases priority, negative values disable the method.
Michal Vasko45f298f2016-01-29 10:26:26 +0100229 */
230void nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
231
232/**
233 * @brief Get SSH Call Home authentication method preference.
234 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100235 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200236 * @return Preference of the @p auth_type.
Michal Vasko45f298f2016-01-29 10:26:26 +0100237 */
238int16_t nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
239
240/**
241 * @brief Set client Call Home SSH username used for authentication.
242 *
243 * @param[in] username Username to use.
244 * @return 0 on success, -1 on error.
245 */
246int nc_client_ssh_ch_set_username(const char *username);
247
Michal Vaskoe22c6732016-01-29 11:03:02 +0100248/**
249 * @brief Get client Call Home SSH username used for authentication.
250 *
251 * @return Username used.
252 */
253const char *nc_client_ssh_ch_get_username(void);
254
Michal Vaskoc446a382021-06-18 08:54:05 +0200255/** @} Client-side Call Home on SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200256
Radek Krejci53691be2016-02-22 13:58:37 +0100257#endif /* NC_ENABLED_SSH */
Michal Vasko45f298f2016-01-29 10:26:26 +0100258
Radek Krejci53691be2016-02-22 13:58:37 +0100259#ifdef NC_ENABLED_TLS
Michal Vasko45f298f2016-01-29 10:26:26 +0100260
261/**
Radek Krejci6799a052017-05-19 14:23:23 +0200262 * @defgroup client_ch_tls Client-side Call Home on TLS
263 * @ingroup client_ch
264 *
265 * @brief TLS settings for the Call Home functionality
266 * @{
267 */
268
269/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100270 * @brief Add a new client bind and start listening on it for TLS Call Home connections.
271 *
272 * @param[in] address IP address to bind to.
273 * @param[in] port Port to bind to.
274 * @return 0 on success, -1 on error.
275 */
276int nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port);
277
278/**
279 * @brief Remove a TLS listening client bind.
280 *
281 * @param[in] address IP address the socket was bound to. NULL matches all.
282 * @param[in] port Port the socket was bound to. 0 matches all.
283 * @return 0 on success, -1 on not found.
284 */
285int nc_client_tls_ch_del_bind(const char *address, uint16_t port);
286
287/**
288 * @brief Set client Call Home authentication identity - a certificate and a private key.
289 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100290 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vaskoc446a382021-06-18 08:54:05 +0200291 * @param[in] client_key Path to the file containing the private key for the @p client_cert.
292 * If NULL, key is expected to be stored with @p client_cert.
Michal Vasko45f298f2016-01-29 10:26:26 +0100293 * @return 0 on success, -1 on error.
294 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100295int nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key);
296
297/**
298 * @brief Get client Call Home authentication identity - a certificate and a private key.
299 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100300 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
Michal Vaskoc446a382021-06-18 08:54:05 +0200301 * @param[out] client_key Path to the file containing the private key for the @p client_cert.
Michal Vaskoe22c6732016-01-29 11:03:02 +0100302 * Can be NULL.
303 */
304void nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key);
Michal Vasko45f298f2016-01-29 10:26:26 +0100305
306/**
307 * @brief Set client Call Home trusted CA certificates.
308 *
309 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
310 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
311 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
312 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
313 * @return 0 on success, -1 on error.
314 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100315int nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
316
317/**
318 * @brief Get client Call Home trusted CA certificates.
319 *
320 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
321 * Can be NULL.
322 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
323 * Can be NULL.
324 */
325void nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100326
327/**
328 * @brief Set client Call Home Certificate Revocation Lists.
329 *
330 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
331 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
332 * @return 0 on success, -1 on error.
333 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100334int nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir);
335
336/**
337 * @brief Get client Call Home Certificate Revocation Lists.
338 *
339 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
340 * Can be NULL.
341 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
342 * Can be NULL.
343 */
344void nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100345
Michal Vaskoc446a382021-06-18 08:54:05 +0200346/** @} Client-side Call Home on TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200347
Radek Krejci53691be2016-02-22 13:58:37 +0100348#endif /* NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +0100349
Michal Vaskoc09730e2019-01-17 10:07:26 +0100350#ifdef __cplusplus
351}
352#endif
353
Michal Vasko45f298f2016-01-29 10:26:26 +0100354#endif /* NC_SESSION_CLIENT_CH_H_ */