blob: c4d28c091c81d869d4e02397981dede2309107cb [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 Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
Michal Vasko94d61402023-03-16 08:21:52 +01007 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Michal Vasko45f298f2016-01-29 10:26:26 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko45f298f2016-01-29 10:26:26 +010014 */
15
16#ifndef NC_SESSION_CLIENT_CH_H_
17#define NC_SESSION_CLIENT_CH_H_
18
Michal Vaskoc09730e2019-01-17 10:07:26 +010019#ifdef __cplusplus
20extern "C" {
21#endif
22
Michal Vasko45f298f2016-01-29 10:26:26 +010023#include <libyang/libyang.h>
24
roman2eab4742023-06-06 10:00:26 +020025#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +020026# include <libssh/libssh.h>
roman2eab4742023-06-06 10:00:26 +020027#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskoc446a382021-06-18 08:54:05 +020028
Michal Vasko45f298f2016-01-29 10:26:26 +010029#include "messages_client.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include "netconf.h"
31#include "session.h"
Michal Vasko45f298f2016-01-29 10:26:26 +010032
roman2eab4742023-06-06 10:00:26 +020033#ifdef NC_ENABLED_SSH_TLS
Michal Vasko45f298f2016-01-29 10:26:26 +010034
35/**
Radek Krejci6799a052017-05-19 14:23:23 +020036 * @defgroup client_ch Client-side Call Home
37 * @ingroup client
38 *
39 * @brief Call Home functionality for client-side applications.
40 * @{
41 */
42
43/**
Michal Vasko45f298f2016-01-29 10:26:26 +010044 * @brief Accept a Call Home connection on any of the listening binds.
45 *
46 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko94d61402023-03-16 08:21:52 +010047 * non-blocking call, -1 for infinite waiting.
48 * @param[in,out] ctx Optional custom context to use for the session. If not set, a default context is created.
49 * Any YANG modules not present in the context and supported by the server are loaded using \<get-schema\>
50 * (if supported) and/or by searching the searchpath (see ::nc_client_set_schema_searchpath()).
Michal Vasko45f298f2016-01-29 10:26:26 +010051 * @param[out] session New session.
Michal Vaskof0537d82016-01-29 14:42:38 +010052 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +010053 */
54int nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session);
55
Michal Vaskoc446a382021-06-18 08:54:05 +020056/** @} Client-side Call Home */
Radek Krejci6799a052017-05-19 14:23:23 +020057
Michal Vasko45f298f2016-01-29 10:26:26 +010058/**
Radek Krejci6799a052017-05-19 14:23:23 +020059 * @defgroup client_ch_ssh Client-side Call Home on SSH
60 * @ingroup client_ch
61 *
62 * @brief SSH settings for the Call Home functionality
63 * @{
64 */
65
66/**
roman81242fe2024-08-23 14:16:50 +020067 * @brief Set SSH Call Home behaviour of checking the host key and adding/reading entries to/from the known_hosts file.
68 *
69 * The default mode is ::NC_SSH_KNOWNHOSTS_ASK.
70 *
71 * @param[in] mode Server host key checking mode.
72 */
73void nc_client_ssh_ch_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_MODE mode);
74
75/**
76 * @brief Set SSH Call Home path to the known_hosts file for connections.
77 *
78 * Repetetive calling replaces the value. If the given file doesn't exist and the process has sufficient
79 * rights, it gets created whenever the file is needed, otherwise an error occurs. If NULL is passed or the
80 * path isn't set, the default known_hosts file will be used.
81 *
82 * @param[in] path Path to the known_hosts file.
83 * @return 0 on success, 1 on error.
84 */
85int nc_client_ssh_ch_set_knownhosts_path(const char *path);
86
87/**
Michal Vasko30e2c872016-02-18 10:03:21 +010088 * @brief Set SSH Call Home password authentication callback.
89 *
Radek Krejci90a84a22017-05-25 13:53:00 +020090 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
91 * freeing the private data when necessary (the private data can be obtained by
92 * nc_client_ssh_ch_get_auth_password_clb()).
93 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +010094 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko94d61402023-03-16 08:21:52 +010095 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +020096 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +010097 */
Radek Krejci90a84a22017-05-25 13:53:00 +020098void 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 +020099 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200100
101/**
102 * @brief Get currently set SSH Call Home password authentication callback and its private data
103 * previously set by nc_client_ssh_ch_set_auth_password_clb().
104 *
105 * @param[out] auth_password Currently set callback, NULL in case of the default callback.
106 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
107 */
108void 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 +0200109 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100110
111/**
112 * @brief Set SSH Call Home interactive authentication callback.
113 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200114 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
115 * freeing the private data when necessary (the private data can be obtained by
116 * nc_client_ssh_ch_get_auth_interactive_clb()).
117 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100118 * @param[in] auth_interactive Function to call for every question, returns the answer for
Michal Vasko94d61402023-03-16 08:21:52 +0100119 * authentication name with instruction and echoing prompt. If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +0200120 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100121 */
122void 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 +0200123 const char *prompt, int echo, void *priv),
124 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200125
126/**
127 * @brief Get currently set SSH Call Home interactive authentication callback and its private data
128 * previously set by nc_client_ssh_ch_set_auth_interactive_clb().
129 *
130 * @param[out] auth_interactive 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 */
133void 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 +0200134 const char *prompt, int echo, void *priv),
135 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100136
137/**
138 * @brief Set SSH Call Home publickey authentication encrypted private key passphrase callback.
139 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200140 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
141 * freeing the private data when necessary (the private data can be obtained by
142 * nc_client_ssh_ch_get_auth_privkey_passphrase_clb()).
143 *
Michal Vasko94d61402023-03-16 08:21:52 +0100144 * @param[in] auth_privkey_passphrase Function to call for every question, returns the passphrase for the specific
145 * private key.
Radek Krejci90a84a22017-05-25 13:53:00 +0200146 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +0100147 */
Radek Krejci90a84a22017-05-25 13:53:00 +0200148void 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 +0200149 void *priv);
Radek Krejci90a84a22017-05-25 13:53:00 +0200150
151/**
152 * @brief Get currently set SSH Call Home publickey authentication encrypted private key passphrase callback and its
153 * private data previously set by nc_client_ssh_ch_set_auth_privkey_passphrase_clb().
154 *
155 * @param[out] auth_privkey_passphrase Currently set callback, NULL in case of the default callback.
156 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
157 */
158void 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 +0200159 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100160
161/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100162 * @brief Add a new client bind and start listening on it for SSH Call Home connections.
163 *
164 * @param[in] address IP address to bind to.
165 * @param[in] port Port to bind to.
166 * @return 0 on success, -1 on error.
167 */
168int nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port);
169
170/**
171 * @brief Remove an SSH listening client bind.
172 *
173 * @param[in] address IP address the socket was bound to. NULL matches all.
174 * @param[in] port Port the socket was bound to. 0 matches all.
175 * @return 0 on success, -1 on not found.
176 */
177int nc_client_ssh_ch_del_bind(const char *address, uint16_t port);
178
179/**
180 * @brief Add an SSH public and private key pair to be used for Call Home client authentication.
181 *
182 * Private key can be encrypted, the passphrase will be asked for before using it.
183 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100184 * @param[in] pub_key Path to the public key.
185 * @param[in] priv_key Path to the private key.
Michal Vaskof0537d82016-01-29 14:42:38 +0100186 * @return 0 on success, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +0100187 */
188int nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key);
189
190/**
191 * @brief Remove an SSH public and private key pair that was used for Call Home client authentication.
192 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100193 * @param[in] idx Index of the keypair starting with 0.
Michal Vasko45f298f2016-01-29 10:26:26 +0100194 * @return 0 on success, -1 on error.
195 */
196int nc_client_ssh_ch_del_keypair(int idx);
197
198/**
199 * @brief Get the number of public an private key pairs set to be used for Call Home client authentication.
200 *
201 * @return Keypair count.
202 */
203int nc_client_ssh_ch_get_keypair_count(void);
204
205/**
206 * @brief Get a specific keypair set to be used for Call Home client authentication.
207 *
208 * @param[in] idx Index of the specific keypair.
209 * @param[out] pub_key Path to the public key.
210 * @param[out] priv_key Path to the private key.
Michal Vasko45f298f2016-01-29 10:26:26 +0100211 * @return 0 on success, -1 on error.
212 */
213int nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key);
214
215/**
216 * @brief Set SSH Call Home authentication method preference.
217 *
Radek Krejci62aa0642017-05-25 16:33:49 +0200218 * The default preference is as follows:
219 * - public key authentication (3)
220 * - password authentication (2)
221 * - interactive authentication (1)
222 *
223 * @param[in] auth_type Authentication method to modify the preference of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200224 * @param[in] pref Preference of @p auth_type. Higher number increases priority, negative values disable the method.
Michal Vasko45f298f2016-01-29 10:26:26 +0100225 */
226void nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref);
227
228/**
229 * @brief Get SSH Call Home authentication method preference.
230 *
Michal Vasko45f298f2016-01-29 10:26:26 +0100231 * @param[in] auth_type Authentication method to retrieve the prefrence of.
Michal Vaskoc446a382021-06-18 08:54:05 +0200232 * @return Preference of the @p auth_type.
Michal Vasko45f298f2016-01-29 10:26:26 +0100233 */
234int16_t nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type);
235
236/**
237 * @brief Set client Call Home SSH username used for authentication.
238 *
239 * @param[in] username Username to use.
240 * @return 0 on success, -1 on error.
241 */
242int nc_client_ssh_ch_set_username(const char *username);
243
Michal Vaskoe22c6732016-01-29 11:03:02 +0100244/**
245 * @brief Get client Call Home SSH username used for authentication.
246 *
247 * @return Username used.
248 */
249const char *nc_client_ssh_ch_get_username(void);
250
Michal Vaskoc446a382021-06-18 08:54:05 +0200251/** @} Client-side Call Home on SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200252
Michal Vasko45f298f2016-01-29 10:26:26 +0100253/**
Radek Krejci6799a052017-05-19 14:23:23 +0200254 * @defgroup client_ch_tls Client-side Call Home on TLS
255 * @ingroup client_ch
256 *
257 * @brief TLS settings for the Call Home functionality
258 * @{
259 */
260
261/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100262 * @brief Add a new client bind and start listening on it for TLS Call Home connections.
263 *
264 * @param[in] address IP address to bind to.
265 * @param[in] port Port to bind to.
266 * @return 0 on success, -1 on error.
267 */
268int nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port);
269
270/**
Michal Vasko9d4cca52022-09-07 11:19:57 +0200271 * @brief Add a new client bind and start listening on it for TLS Call Home connections coming from the specified hostname.
272 *
273 * @param[in] address IP address to bind to.
274 * @param[in] port Port to bind to.
Michal Vaskob8f1fa12022-09-07 11:47:42 +0200275 * @param[in] hostname Expected server hostname, verified by TLS when connecting to it. If NULL, the check is skipped.
Michal Vasko9d4cca52022-09-07 11:19:57 +0200276 * @return 0 on success, -1 on error.
277 */
278int nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname);
279
280/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100281 * @brief Remove a TLS listening client bind.
282 *
283 * @param[in] address IP address the socket was bound to. NULL matches all.
284 * @param[in] port Port the socket was bound to. 0 matches all.
285 * @return 0 on success, -1 on not found.
286 */
287int nc_client_tls_ch_del_bind(const char *address, uint16_t port);
288
289/**
290 * @brief Set client Call Home authentication identity - a certificate and a private key.
291 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100292 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vaskoc446a382021-06-18 08:54:05 +0200293 * @param[in] client_key Path to the file containing the private key for the @p client_cert.
Michal Vasko94d61402023-03-16 08:21:52 +0100294 * If NULL, key is expected to be stored with @p client_cert.
Michal Vasko45f298f2016-01-29 10:26:26 +0100295 * @return 0 on success, -1 on error.
296 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100297int nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key);
298
299/**
300 * @brief Get client Call Home authentication identity - a certificate and a private key.
301 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100302 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
Michal Vasko94d61402023-03-16 08:21:52 +0100303 * @param[out] client_key Path to the file containing the private key for the @p client_cert. Can be NULL.
Michal Vaskoe22c6732016-01-29 11:03:02 +0100304 */
305void nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key);
Michal Vasko45f298f2016-01-29 10:26:26 +0100306
307/**
308 * @brief Set client Call Home trusted CA certificates.
309 *
310 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
Michal Vasko94d61402023-03-16 08:21:52 +0100311 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100312 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
Michal Vasko94d61402023-03-16 08:21:52 +0100313 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100314 * @return 0 on success, -1 on error.
315 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100316int nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
317
318/**
319 * @brief Get client Call Home trusted CA certificates.
320 *
Michal Vasko94d61402023-03-16 08:21:52 +0100321 * @param[out] ca_file Location of the CA certificate file used to verify server certificates. Can be NULL.
322 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates. Can be NULL.
Michal Vaskoe22c6732016-01-29 11:03:02 +0100323 */
324void nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100325
326/**
roman15fbc592024-07-02 16:01:23 +0200327 * @brief Deprecated.
Michal Vasko45f298f2016-01-29 10:26:26 +0100328 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100329int nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir);
330
331/**
roman15fbc592024-07-02 16:01:23 +0200332 * @brief Deprecated.
Michal Vaskoe22c6732016-01-29 11:03:02 +0100333 */
334void nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100335
Michal Vaskoc446a382021-06-18 08:54:05 +0200336/** @} Client-side Call Home on TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200337
roman2eab4742023-06-06 10:00:26 +0200338#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +0100339
Michal Vaskoc09730e2019-01-17 10:07:26 +0100340#ifdef __cplusplus
341}
342#endif
343
Michal Vasko45f298f2016-01-29 10:26:26 +0100344#endif /* NC_SESSION_CLIENT_CH_H_ */