blob: 800841d3f74f6d05d2098b918c3671ac183bd732 [file] [log] [blame]
Michal Vasko45f298f2016-01-29 10:26:26 +01001/**
2 * \file session_client_ch.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 Call Home session client manipulation
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal 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
24#include "session.h"
25#include "netconf.h"
26#include "messages_client.h"
27
Radek Krejci53691be2016-02-22 13:58:37 +010028#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko45f298f2016-01-29 10:26:26 +010029
30/**
Radek Krejci6799a052017-05-19 14:23:23 +020031 * @defgroup client_ch Client-side Call Home
32 * @ingroup client
33 *
34 * @brief Call Home functionality for client-side applications.
35 * @{
36 */
37
38/**
Michal Vasko45f298f2016-01-29 10:26:26 +010039 * @brief Accept a Call Home connection on any of the listening binds.
40 *
41 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
42 * non-blocking call, -1 for infinite waiting.
43 * @param[in] ctx Session context to use. Can be NULL.
44 * @param[out] session New session.
Michal Vaskof0537d82016-01-29 14:42:38 +010045 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +010046 */
47int nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session);
48
Radek Krejci6799a052017-05-19 14:23:23 +020049/**@} Client-side Call Home */
50
Radek Krejci53691be2016-02-22 13:58:37 +010051#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +010052
Radek Krejci53691be2016-02-22 13:58:37 +010053#ifdef NC_ENABLED_SSH
Michal Vasko45f298f2016-01-29 10:26:26 +010054
55/**
Radek Krejci6799a052017-05-19 14:23:23 +020056 * @defgroup client_ch_ssh Client-side Call Home on SSH
57 * @ingroup client_ch
58 *
59 * @brief SSH settings for the Call Home functionality
60 * @{
61 */
62
63/**
Radek Krejci90a84a22017-05-25 13:53:00 +020064 * @brief Set SSH Call Home authentication hostkey check (knownhosts) callback.
65 *
66 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
67 * freeing the private data when necessary (the private data can be obtained by
68 * nc_client_ssh_ch_get_auth_hostkey_check_clb()).
Michal Vaskoef112d72016-02-18 13:28:25 +010069 *
70 * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error.
71 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +020072 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vaskoef112d72016-02-18 13:28:25 +010073 */
Radek Krejci90a84a22017-05-25 13:53:00 +020074void nc_client_ssh_ch_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
75 void *priv);
Michal Vaskoef112d72016-02-18 13:28:25 +010076
77/**
Radek Krejci90a84a22017-05-25 13:53:00 +020078 * @brief Get currently set SSH Call Home authentication hostkey check (knownhosts) callback and its private data
79 * previously set by nc_client_ssh_ch_set_auth_hostkey_check_clb().
80 *
81 * @param[out] auth_hostkey_check Currently set callback, NULL in case of the default callback.
82 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
83 */
84void nc_client_ssh_ch_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
85 void **priv);
86/**
Michal Vasko30e2c872016-02-18 10:03:21 +010087 * @brief Set SSH Call Home password authentication callback.
88 *
Radek Krejci90a84a22017-05-25 13:53:00 +020089 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
90 * freeing the private data when necessary (the private data can be obtained by
91 * nc_client_ssh_ch_get_auth_password_clb()).
92 *
Michal Vaskofdfd9dd2016-02-29 10:18:46 +010093 * @param[in] auth_password Function to call, returns the password for username\@hostname.
Michal Vasko30e2c872016-02-18 10:03:21 +010094 * If NULL, the default callback is set.
Radek Krejci90a84a22017-05-25 13:53:00 +020095 * @param[in] priv Optional private data to be passed to the callback function.
Michal Vasko30e2c872016-02-18 10:03:21 +010096 */
Radek Krejci90a84a22017-05-25 13:53:00 +020097void nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
98 void *priv);
99
100/**
101 * @brief Get currently set SSH Call Home password authentication callback and its private data
102 * previously set by nc_client_ssh_ch_set_auth_password_clb().
103 *
104 * @param[out] auth_password Currently set callback, NULL in case of the default callback.
105 * @param[out] priv Currently set (optional) private data to be passed to the callback function.
106 */
107void nc_client_ssh_ch_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
108 void **priv);
Michal Vasko30e2c872016-02-18 10:03:21 +0100109
110/**
111 * @brief Set SSH Call Home interactive authentication callback.
112 *
Radek Krejci90a84a22017-05-25 13:53:00 +0200113 * Repetitive calling causes replacing of the previous callback and its private data. Caller is responsible for
114 * freeing the private data when necessary (the private data can be obtained by
115 * nc_client_ssh_ch_get_auth_interactive_clb()).
116 *
Michal Vasko30e2c872016-02-18 10:03:21 +0100117 * @param[in] auth_interactive Function to call for every question, returns the answer for
118 * authentication name with instruction and echoing prompt.
119 * 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,
Radek Krejci90a84a22017-05-25 13:53:00 +0200123 const char *prompt, int echo, void *priv),
124 void *priv);
125
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,
134 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 Vasko30e2c872016-02-18 10:03:21 +0100144 * @param[in] auth_privkey_passphrase Function to call for every question, returns
145 * the passphrase for the specific 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),
149 void *priv);
150
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),
159 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.
224 * @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 Vasko45f298f2016-01-29 10:26:26 +0100232 * @return Preference of the \p auth_type.
233 */
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
Radek Krejci6799a052017-05-19 14:23:23 +0200251/**@} Client-side Call Home on SSH */
252
Radek Krejci53691be2016-02-22 13:58:37 +0100253#endif /* NC_ENABLED_SSH */
Michal Vasko45f298f2016-01-29 10:26:26 +0100254
Radek Krejci53691be2016-02-22 13:58:37 +0100255#ifdef NC_ENABLED_TLS
Michal Vasko45f298f2016-01-29 10:26:26 +0100256
257/**
Radek Krejci6799a052017-05-19 14:23:23 +0200258 * @defgroup client_ch_tls Client-side Call Home on TLS
259 * @ingroup client_ch
260 *
261 * @brief TLS settings for the Call Home functionality
262 * @{
263 */
264
265/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100266 * @brief Add a new client bind and start listening on it for TLS Call Home connections.
267 *
268 * @param[in] address IP address to bind to.
269 * @param[in] port Port to bind to.
270 * @return 0 on success, -1 on error.
271 */
272int nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port);
273
274/**
275 * @brief Remove a TLS listening client bind.
276 *
277 * @param[in] address IP address the socket was bound to. NULL matches all.
278 * @param[in] port Port the socket was bound to. 0 matches all.
279 * @return 0 on success, -1 on not found.
280 */
281int nc_client_tls_ch_del_bind(const char *address, uint16_t port);
282
283/**
284 * @brief Set client Call Home authentication identity - a certificate and a private key.
285 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100286 * @param[in] client_cert Path to the file containing the client certificate.
Michal Vasko45f298f2016-01-29 10:26:26 +0100287 * @param[in] client_key Path to the file containing the private key for the \p client_cert.
288 * If NULL, key is expected to be stored with \p client_cert.
Michal Vasko45f298f2016-01-29 10:26:26 +0100289 * @return 0 on success, -1 on error.
290 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100291int nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key);
292
293/**
294 * @brief Get client Call Home authentication identity - a certificate and a private key.
295 *
Michal Vaskoe22c6732016-01-29 11:03:02 +0100296 * @param[out] client_cert Path to the file containing the client certificate. Can be NULL.
297 * @param[out] client_key Path to the file containing the private key for the \p client_cert.
298 * Can be NULL.
299 */
300void nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key);
Michal Vasko45f298f2016-01-29 10:26:26 +0100301
302/**
303 * @brief Set client Call Home trusted CA certificates.
304 *
305 * @param[in] ca_file Location of the CA certificate file used to verify server certificates.
306 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
307 * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates.
308 * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL.
309 * @return 0 on success, -1 on error.
310 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100311int nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
312
313/**
314 * @brief Get client Call Home trusted CA certificates.
315 *
316 * @param[out] ca_file Location of the CA certificate file used to verify server certificates.
317 * Can be NULL.
318 * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates.
319 * Can be NULL.
320 */
321void nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100322
323/**
324 * @brief Set client Call Home Certificate Revocation Lists.
325 *
326 * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates.
327 * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
328 * @return 0 on success, -1 on error.
329 */
Michal Vaskoe22c6732016-01-29 11:03:02 +0100330int nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir);
331
332/**
333 * @brief Get client Call Home Certificate Revocation Lists.
334 *
335 * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates.
336 * Can be NULL.
337 * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates.
338 * Can be NULL.
339 */
340void nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100341
Radek Krejci6799a052017-05-19 14:23:23 +0200342/**@} Client-side Call Home on TLS */
343
Radek Krejci53691be2016-02-22 13:58:37 +0100344#endif /* NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +0100345
Michal Vaskoc09730e2019-01-17 10:07:26 +0100346#ifdef __cplusplus
347}
348#endif
349
Michal Vasko45f298f2016-01-29 10:26:26 +0100350#endif /* NC_SESSION_CLIENT_CH_H_ */