Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_server_ch.h |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 Call Home session server 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 |
| 11 | * |
| 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_SERVER_CH_H_ |
| 16 | #define NC_SESSION_SERVER_CH_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <libyang/libyang.h> |
| 20 | |
| 21 | #include "session.h" |
| 22 | #include "netconf.h" |
| 23 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 24 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @brief Establish an SSH Call Home connection with a listening NETCONF client. |
| 28 | * |
| 29 | * @param[in] host Host the client is listening on. |
| 30 | * @param[in] port Port the client is listening on. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 31 | * @param[in] timeout Timeout for transport-related operations in milliseconds. |
| 32 | * 0 for non-blocking call, -1 for infinite waiting. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 33 | * @param[out] session New Call Home session. |
| 34 | * @return 1 on success, 0 on timeout, -1 on error. |
| 35 | */ |
| 36 | int nc_connect_callhome_ssh(const char *host, uint16_t port, int timeout, struct nc_session **session); |
| 37 | |
| 38 | /** |
| 39 | * @brief Set Call Home SSH host keys the server will identify itself with. Each of RSA, DSA, and |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 40 | * ECDSA keys can be set. If the particular type was already set, it is replaced. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 41 | * |
| 42 | * @param[in] privkey_path Path to a private key. |
| 43 | * @return 0 on success, -1 on error. |
| 44 | */ |
| 45 | int nc_server_ssh_ch_set_hostkey(const char *privkey_path); |
| 46 | |
| 47 | /** |
| 48 | * @brief Set Call Home SSH banner the server will send to every client. |
| 49 | * |
| 50 | * @param[in] banner SSH banner. |
| 51 | * @return 0 on success, -1 on error. |
| 52 | */ |
| 53 | int nc_server_ssh_ch_set_banner(const char *banner); |
| 54 | |
| 55 | /** |
| 56 | * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 57 | * are supported by default. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 58 | * |
| 59 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 60 | * @return 0 on success, -1 on error. |
| 61 | */ |
| 62 | int nc_server_ssh_ch_set_auth_methods(int auth_methods); |
| 63 | |
| 64 | /** |
| 65 | * @brief Set Call Home SSH authentication attempts of every client. 3 by default. |
| 66 | * |
| 67 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
| 68 | * @return 0 on success, -1 on error. |
| 69 | */ |
| 70 | int nc_server_ssh_ch_set_auth_attempts(uint16_t auth_attempts); |
| 71 | |
| 72 | /** |
| 73 | * @brief Set Call Home SSH authentication timeout. 10 seconds by default. |
| 74 | * |
| 75 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 76 | * @return 0 on success, -1 on error. |
| 77 | */ |
| 78 | int nc_server_ssh_ch_set_auth_timeout(uint16_t auth_timeout); |
| 79 | |
| 80 | /** |
| 81 | * @brief Add an authorized Call Home client SSH public key. This public key can be used for |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 82 | * publickey authentication afterwards. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 83 | * |
| 84 | * @param[in] pubkey_path Path to the public key. |
| 85 | * @param[in] username Username that the client with the public key must use. |
| 86 | * @return 0 on success, -1 on error. |
| 87 | */ |
| 88 | int nc_server_ssh_ch_add_authkey(const char *pubkey_path, const char *username); |
| 89 | |
| 90 | /** |
| 91 | * @brief Remove an authorized Call Home client SSH public key. |
| 92 | * |
| 93 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 94 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 95 | * @return 0 on success, -1 on not finding any match. |
| 96 | */ |
| 97 | int nc_server_ssh_ch_del_authkey(const char *pubkey_path, const char *username); |
| 98 | |
| 99 | /** |
| 100 | * @brief Clear all the SSH Call Home options. Afterwards a new set of options |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 101 | * can be set for the next client to connect to. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 102 | */ |
| 103 | void nc_server_ssh_ch_clear_opts(void); |
| 104 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 105 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 106 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 107 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * @brief Establish a TLS Call Home connection with a listening NETCONF client. |
| 111 | * |
| 112 | * @param[in] host Host the client is listening on. |
| 113 | * @param[in] port Port the client is listening on. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 114 | * @param[in] timeout Timeout for transport-related operations in milliseconds. |
| 115 | * 0 for non-blocking call, -1 for infinite waiting. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 116 | * @param[out] session New Call Home session. |
| 117 | * @return 1 on success, 0 on timeout, -1 on error. |
| 118 | */ |
| 119 | int nc_connect_callhome_tls(const char *host, uint16_t port, int timeout, struct nc_session **session); |
| 120 | |
| 121 | /** |
| 122 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 123 | * There can only be one certificate for each key type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 124 | * |
| 125 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 126 | * @return 0 on success, -1 on error. |
| 127 | */ |
| 128 | int nc_server_tls_ch_set_cert(const char *cert); |
| 129 | |
| 130 | /** |
| 131 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 132 | * There can only be one certificate for each key type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 133 | * |
| 134 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 135 | * @return 0 on success, -1 on error. |
| 136 | */ |
| 137 | int nc_server_tls_ch_set_cert_path(const char *cert_path); |
| 138 | |
| 139 | /** |
| 140 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 141 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 142 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 143 | * |
| 144 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 145 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 146 | * @return 0 on success, -1 on error. |
| 147 | */ |
| 148 | int nc_server_tls_ch_set_key(const char *privkey, int is_rsa); |
| 149 | |
| 150 | /** |
| 151 | * @brief Set server Call Home TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 152 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 153 | * type, it is replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 154 | * |
| 155 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 156 | * @return 0 on success, -1 on error. |
| 157 | */ |
| 158 | int nc_server_tls_ch_set_key_path(const char *privkey_path); |
| 159 | |
| 160 | /** |
| 161 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 162 | * |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 163 | * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 164 | * @return 0 on success, -1 on error. |
| 165 | */ |
| 166 | int nc_server_tls_ch_add_trusted_cert(const char *cert); |
| 167 | |
| 168 | /** |
| 169 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 170 | * |
| 171 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 172 | * @return 0 on success, -1 on error. |
| 173 | */ |
| 174 | int nc_server_tls_ch_add_trusted_cert_path(const char *cert_path); |
| 175 | |
| 176 | /** |
| 177 | * @brief Set trusted Call Home Certificate Authority certificate locations. There |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 178 | * can only be one file and one directory, they are replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 179 | * |
| 180 | * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 181 | * Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 182 | * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 183 | * (c_rehash utility can be used to create hashes) |
| 184 | * with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 185 | * @return 0 on success, -1 on error. |
| 186 | */ |
| 187 | int nc_server_tls_ch_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path); |
| 188 | |
| 189 | /** |
| 190 | * @brief Destroy and clean all the set Call Home certificates and private keys. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 191 | * CRLs and CTN entries are not affected. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 192 | */ |
| 193 | void nc_server_tls_ch_clear_certs(void); |
| 194 | |
| 195 | /** |
| 196 | * @brief Set Call Home Certificate Revocation List locations. There can only be |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 197 | * one file and one directory, they are replaced if already set. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 198 | * |
| 199 | * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL. |
| 200 | * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 201 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 202 | * @return 0 on success, -1 on error. |
| 203 | */ |
| 204 | int nc_server_tls_ch_set_crl_locations(const char *crl_file_path, const char *crl_dir_path); |
| 205 | |
| 206 | /** |
| 207 | * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys, |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 208 | * and CTN entries are not affected. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 209 | */ |
| 210 | void nc_server_tls_ch_clear_crls(void); |
| 211 | |
| 212 | /** |
| 213 | * @brief Add a Call Home Cert-to-name entry. |
| 214 | * |
| 215 | * @param[in] id Priority of the entry. |
| 216 | * @param[in] fingerprint Matching certificate fingerprint. |
| 217 | * @param[in] map_type Type of username-certificate mapping. |
| 218 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 219 | * @return 0 on success, -1 on error. |
| 220 | */ |
| 221 | int nc_server_tls_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 222 | |
| 223 | /** |
| 224 | * @brief Remove a Call Home Cert-to-name entry. |
| 225 | * |
| 226 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 227 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 228 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 229 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 230 | * @return 0 on success, -1 on not finding any match. |
| 231 | */ |
| 232 | int nc_server_tls_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 233 | |
| 234 | /** |
| 235 | * @brief Clear all the TLS Call Home options. Afterwards a new set of options |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 236 | * can be set for the next client to connect. |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 237 | */ |
| 238 | void nc_server_tls_ch_clear_opts(void); |
| 239 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 240 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 45f298f | 2016-01-29 10:26:26 +0100 | [diff] [blame] | 241 | |
| 242 | #endif /* NC_SESSION_SERVER_CH_H_ */ |