Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 1 | /** |
| 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 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 | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_SESSION_CLIENT_CH_H_ |
| 16 | #define NC_SESSION_CLIENT_CH_H_ |
| 17 | |
| 18 | #include <libyang/libyang.h> |
| 19 | |
| 20 | #include "session.h" |
| 21 | #include "netconf.h" |
| 22 | #include "messages_client.h" |
| 23 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 24 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @brief Accept a Call Home connection on any of the listening binds. |
| 28 | * |
| 29 | * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for |
| 30 | * non-blocking call, -1 for infinite waiting. |
| 31 | * @param[in] ctx Session context to use. Can be NULL. |
| 32 | * @param[out] session New session. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 33 | * @return 1 on success, 0 on timeout, -1 on error. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 34 | */ |
| 35 | int nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session); |
| 36 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 37 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 38 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 39 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 40 | |
| 41 | /** |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 42 | * @brief Set SSH CALL Home authentication hostkey check (knownhosts) callback. |
| 43 | * |
| 44 | * @param[in] auth_hostkey_check Function to call, returns 0 on success, non-zero in error. |
| 45 | * If NULL, the default callback is set. |
| 46 | */ |
| 47 | void nc_client_ssh_ch_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session)); |
| 48 | |
| 49 | /** |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 50 | * @brief Set SSH Call Home password authentication callback. |
| 51 | * |
| 52 | * @param[in] auth_password Function to call, returns the password for username@hostname. |
| 53 | * If NULL, the default callback is set. |
| 54 | */ |
| 55 | void nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname)); |
| 56 | |
| 57 | /** |
| 58 | * @brief Set SSH Call Home interactive authentication callback. |
| 59 | * |
| 60 | * @param[in] auth_interactive Function to call for every question, returns the answer for |
| 61 | * authentication name with instruction and echoing prompt. |
| 62 | * If NULL, the default callback is set. |
| 63 | */ |
| 64 | void nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction, |
| 65 | const char *prompt, int echo)); |
| 66 | |
| 67 | /** |
| 68 | * @brief Set SSH Call Home publickey authentication encrypted private key passphrase callback. |
| 69 | * |
| 70 | * @param[in] auth_privkey_passphrase Function to call for every question, returns |
| 71 | * the passphrase for the specific private key. |
| 72 | */ |
| 73 | void nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path)); |
| 74 | |
| 75 | /** |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 76 | * @brief Add a new client bind and start listening on it for SSH Call Home connections. |
| 77 | * |
| 78 | * @param[in] address IP address to bind to. |
| 79 | * @param[in] port Port to bind to. |
| 80 | * @return 0 on success, -1 on error. |
| 81 | */ |
| 82 | int nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port); |
| 83 | |
| 84 | /** |
| 85 | * @brief Remove an SSH listening client bind. |
| 86 | * |
| 87 | * @param[in] address IP address the socket was bound to. NULL matches all. |
| 88 | * @param[in] port Port the socket was bound to. 0 matches all. |
| 89 | * @return 0 on success, -1 on not found. |
| 90 | */ |
| 91 | int nc_client_ssh_ch_del_bind(const char *address, uint16_t port); |
| 92 | |
| 93 | /** |
| 94 | * @brief Add an SSH public and private key pair to be used for Call Home client authentication. |
| 95 | * |
| 96 | * Private key can be encrypted, the passphrase will be asked for before using it. |
| 97 | * |
| 98 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 99 | * |
| 100 | * @param[in] pub_key Path to the public key. |
| 101 | * @param[in] priv_key Path to the private key. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 102 | * @return 0 on success, -1 on error. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 103 | */ |
| 104 | int nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key); |
| 105 | |
| 106 | /** |
| 107 | * @brief Remove an SSH public and private key pair that was used for Call Home client authentication. |
| 108 | * |
| 109 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 110 | * |
| 111 | * @param[in] idx Index of the keypair starting with 0. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 112 | * @return 0 on success, -1 on error. |
| 113 | */ |
| 114 | int nc_client_ssh_ch_del_keypair(int idx); |
| 115 | |
| 116 | /** |
| 117 | * @brief Get the number of public an private key pairs set to be used for Call Home client authentication. |
| 118 | * |
| 119 | * @return Keypair count. |
| 120 | */ |
| 121 | int nc_client_ssh_ch_get_keypair_count(void); |
| 122 | |
| 123 | /** |
| 124 | * @brief Get a specific keypair set to be used for Call Home client authentication. |
| 125 | * |
| 126 | * @param[in] idx Index of the specific keypair. |
| 127 | * @param[out] pub_key Path to the public key. |
| 128 | * @param[out] priv_key Path to the private key. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 129 | * @return 0 on success, -1 on error. |
| 130 | */ |
| 131 | int nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key); |
| 132 | |
| 133 | /** |
| 134 | * @brief Set SSH Call Home authentication method preference. |
| 135 | * |
| 136 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 137 | * |
| 138 | * @param[in] auth_type Authentication method to modify the prefrence of. |
| 139 | * @param[in] pref Preference of \p auth_type. Negative values disable the method. |
| 140 | */ |
| 141 | void nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref); |
| 142 | |
| 143 | /** |
| 144 | * @brief Get SSH Call Home authentication method preference. |
| 145 | * |
| 146 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with libssh support. |
| 147 | * |
| 148 | * @param[in] auth_type Authentication method to retrieve the prefrence of. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 149 | * @return Preference of the \p auth_type. |
| 150 | */ |
| 151 | int16_t nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type); |
| 152 | |
| 153 | /** |
| 154 | * @brief Set client Call Home SSH username used for authentication. |
| 155 | * |
| 156 | * @param[in] username Username to use. |
| 157 | * @return 0 on success, -1 on error. |
| 158 | */ |
| 159 | int nc_client_ssh_ch_set_username(const char *username); |
| 160 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 161 | /** |
| 162 | * @brief Get client Call Home SSH username used for authentication. |
| 163 | * |
| 164 | * @return Username used. |
| 165 | */ |
| 166 | const char *nc_client_ssh_ch_get_username(void); |
| 167 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 168 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 169 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 170 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * @brief Add a new client bind and start listening on it for TLS Call Home connections. |
| 174 | * |
| 175 | * @param[in] address IP address to bind to. |
| 176 | * @param[in] port Port to bind to. |
| 177 | * @return 0 on success, -1 on error. |
| 178 | */ |
| 179 | int nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port); |
| 180 | |
| 181 | /** |
| 182 | * @brief Remove a TLS listening client bind. |
| 183 | * |
| 184 | * @param[in] address IP address the socket was bound to. NULL matches all. |
| 185 | * @param[in] port Port the socket was bound to. 0 matches all. |
| 186 | * @return 0 on success, -1 on not found. |
| 187 | */ |
| 188 | int nc_client_tls_ch_del_bind(const char *address, uint16_t port); |
| 189 | |
| 190 | /** |
| 191 | * @brief Set client Call Home authentication identity - a certificate and a private key. |
| 192 | * |
| 193 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support. |
| 194 | * |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 195 | * @param[in] client_cert Path to the file containing the client certificate. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 196 | * @param[in] client_key Path to the file containing the private key for the \p client_cert. |
| 197 | * If NULL, key is expected to be stored with \p client_cert. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 198 | * @return 0 on success, -1 on error. |
| 199 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 200 | int nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key); |
| 201 | |
| 202 | /** |
| 203 | * @brief Get client Call Home authentication identity - a certificate and a private key. |
| 204 | * |
| 205 | * Function is provided only via nc_client.h header file and only when libnetconf2 is compiled with TLS support. |
| 206 | * |
| 207 | * @param[out] client_cert Path to the file containing the client certificate. Can be NULL. |
| 208 | * @param[out] client_key Path to the file containing the private key for the \p client_cert. |
| 209 | * Can be NULL. |
| 210 | */ |
| 211 | void nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * @brief Set client Call Home trusted CA certificates. |
| 215 | * |
| 216 | * @param[in] ca_file Location of the CA certificate file used to verify server certificates. |
| 217 | * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL. |
| 218 | * @param[in] ca_dir Location of the CA certificates directory used to verify the server certificates. |
| 219 | * For more info, see the documentation for SSL_CTX_load_verify_locations() from OpenSSL. |
| 220 | * @return 0 on success, -1 on error. |
| 221 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 222 | int nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir); |
| 223 | |
| 224 | /** |
| 225 | * @brief Get client Call Home trusted CA certificates. |
| 226 | * |
| 227 | * @param[out] ca_file Location of the CA certificate file used to verify server certificates. |
| 228 | * Can be NULL. |
| 229 | * @param[out] ca_dir Location of the CA certificates directory used to verify the server certificates. |
| 230 | * Can be NULL. |
| 231 | */ |
| 232 | void nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * @brief Set client Call Home Certificate Revocation Lists. |
| 236 | * |
| 237 | * @param[in] crl_file Location of the CRL certificate file used to check for revocated certificates. |
| 238 | * @param[in] crl_dir Location of the CRL certificate directory used to check for revocated certificates. |
| 239 | * @return 0 on success, -1 on error. |
| 240 | */ |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 241 | int nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir); |
| 242 | |
| 243 | /** |
| 244 | * @brief Get client Call Home Certificate Revocation Lists. |
| 245 | * |
| 246 | * @param[out] crl_file Location of the CRL certificate file used to check for revocated certificates. |
| 247 | * Can be NULL. |
| 248 | * @param[out] crl_dir Location of the CRL certificate directory used to check for revocated certificates. |
| 249 | * Can be NULL. |
| 250 | */ |
| 251 | void nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir); |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 252 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 253 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 254 | |
| 255 | #endif /* NC_SESSION_CLIENT_CH_H_ */ |