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