blob: 40ac0d4f4c0ec762b687288c0e0686d40de591a2 [file] [log] [blame]
Michal Vasko45f298f2016-01-29 10:26:26 +01001/**
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 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_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 Krejci53691be2016-02-22 13:58:37 +010024#ifdef NC_ENABLED_SSH
Michal Vasko45f298f2016-01-29 10:26:26 +010025
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 Vasko45f298f2016-01-29 10:26:26 +010031 * @param[out] session New Call Home session.
Michal Vasko71090fc2016-05-24 16:37:28 +020032 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
33 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko45f298f2016-01-29 10:26:26 +010034 */
Michal Vasko71090fc2016-05-24 16:37:28 +020035NC_MSG_TYPE nc_connect_callhome_ssh(const char *host, uint16_t port, struct nc_session **session);
Michal Vasko45f298f2016-01-29 10:26:26 +010036
37/**
Michal Vaskoe2713da2016-08-22 16:06:40 +020038 * @brief Add Call Home SSH host keys the server will identify itself with. Any RSA, DSA, and
39 * ECDSA keys can be added. However, a maximum of one key of each type will be used
40 * during SSH authentication, later keys replacing earlier ones.
41 *
Michal Vaskod6829ce2016-09-22 09:45:13 +020042 * @param[in] privkey_path Path to a private key.
Michal Vaskoe2713da2016-08-22 16:06:40 +020043 * @return 0 on success, -1 on error.
44 */
45int nc_server_ssh_ch_add_hostkey(const char *privkey_path);
46
47/**
48 * @brief Delete Call Home SSH host keys. Their order is preserved.
Michal Vasko45f298f2016-01-29 10:26:26 +010049 *
Michal Vaskod6829ce2016-09-22 09:45:13 +020050 * @param[in] privkey_path Path to a private key. NULL matches all the keys.
Michal Vasko45f298f2016-01-29 10:26:26 +010051 * @return 0 on success, -1 on error.
52 */
Michal Vaskod6829ce2016-09-22 09:45:13 +020053int nc_server_ssh_ch_del_hostkey(const char *privkey_path);
Michal Vasko45f298f2016-01-29 10:26:26 +010054
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 */
61int nc_server_ssh_ch_set_banner(const char *banner);
62
63/**
64 * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +010065 * are supported by default.
Michal Vasko45f298f2016-01-29 10:26:26 +010066 *
67 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
68 * @return 0 on success, -1 on error.
69 */
70int 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 */
78int 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 */
86int 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
Michal Vaskof0537d82016-01-29 14:42:38 +010090 * publickey authentication afterwards.
Michal Vasko45f298f2016-01-29 10:26:26 +010091 *
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 */
96int 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 */
105int 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
Michal Vaskof0537d82016-01-29 14:42:38 +0100109 * can be set for the next client to connect to.
Michal Vasko45f298f2016-01-29 10:26:26 +0100110 */
111void nc_server_ssh_ch_clear_opts(void);
112
Radek Krejci53691be2016-02-22 13:58:37 +0100113#endif /* NC_ENABLED_SSH */
Michal Vasko45f298f2016-01-29 10:26:26 +0100114
Radek Krejci53691be2016-02-22 13:58:37 +0100115#ifdef NC_ENABLED_TLS
Michal Vasko45f298f2016-01-29 10:26:26 +0100116
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.
Michal Vasko45f298f2016-01-29 10:26:26 +0100122 * @param[out] session New Call Home session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200123 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
124 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko45f298f2016-01-29 10:26:26 +0100125 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200126NC_MSG_TYPE nc_connect_callhome_tls(const char *host, uint16_t port, struct nc_session **session);
Michal Vasko45f298f2016-01-29 10:26:26 +0100127
128/**
129 * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path().
Michal Vaskof0537d82016-01-29 14:42:38 +0100130 * There can only be one certificate for each key type, it is replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100131 *
132 * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding.
133 * @return 0 on success, -1 on error.
134 */
135int nc_server_tls_ch_set_cert(const char *cert);
136
137/**
138 * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert().
Michal Vaskof0537d82016-01-29 14:42:38 +0100139 * There can only be one certificate for each key type, it is replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100140 *
141 * @param[in] cert_path Path to a certificate file in PEM format.
142 * @return 0 on success, -1 on error.
143 */
144int nc_server_tls_ch_set_cert_path(const char *cert_path);
145
146/**
147 * @brief Set server Call Home TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100148 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
149 * type, it is replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100150 *
151 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
152 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
153 * @return 0 on success, -1 on error.
154 */
155int nc_server_tls_ch_set_key(const char *privkey, int is_rsa);
156
157/**
158 * @brief Set server Call Home TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100159 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
160 * type, it is replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100161 *
162 * @param[in] privkey_path Path to a private key file in PEM format.
163 * @return 0 on success, -1 on error.
164 */
165int nc_server_tls_ch_set_key_path(const char *privkey_path);
166
167/**
168 * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
169 *
Michal Vaskoe2713da2016-08-22 16:06:40 +0200170 * @param[in] cert_name Arbitary name identifying this certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100171 * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding.
Michal Vasko45f298f2016-01-29 10:26:26 +0100172 * @return 0 on success, -1 on error.
173 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200174int nc_server_tls_ch_add_trusted_cert(const char *cert_name, const char *cert);
Michal Vasko45f298f2016-01-29 10:26:26 +0100175
176/**
177 * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
178 *
Michal Vaskoe2713da2016-08-22 16:06:40 +0200179 * @param[in] cert_name Arbitary name identifying this certificate.
Michal Vasko45f298f2016-01-29 10:26:26 +0100180 * @param[in] cert_path Path to a trusted certificate file in PEM format.
181 * @return 0 on success, -1 on error.
182 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200183int nc_server_tls_ch_add_trusted_cert_path(const char *cert_name, const char *cert_path);
Michal Vasko45f298f2016-01-29 10:26:26 +0100184
185/**
186 * @brief Set trusted Call Home Certificate Authority certificate locations. There
Michal Vaskof0537d82016-01-29 14:42:38 +0100187 * can only be one file and one directory, they are replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100188 *
Michal Vaskob34b3e82016-02-26 15:04:58 +0100189 * @param[in] ca_file Path to a trusted CA cert store file in PEM format.
190 * Can be NULL.
191 * @param[in] ca_dir Path to a trusted CA cert store hashed directory
192 * (c_rehash utility can be used to create hashes)
193 * with PEM files. Can be NULL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100194 * @return 0 on success, -1 on error.
195 */
Michal Vaskob34b3e82016-02-26 15:04:58 +0100196int nc_server_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100197
198/**
199 * @brief Destroy and clean all the set Call Home certificates and private keys.
Michal Vaskof0537d82016-01-29 14:42:38 +0100200 * CRLs and CTN entries are not affected.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200201 *
202 * @param[in] cert_name Name of the certificate to delete. NULL deletes all the certificates.
203 * @return 0 on success, -1 on not found.
Michal Vasko45f298f2016-01-29 10:26:26 +0100204 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200205int nc_server_tls_ch_del_trusted_cert(const char *cert_name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100206
207/**
208 * @brief Set Call Home Certificate Revocation List locations. There can only be
Michal Vaskof0537d82016-01-29 14:42:38 +0100209 * one file and one directory, they are replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100210 *
Michal Vaskob34b3e82016-02-26 15:04:58 +0100211 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
212 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
213 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100214 * @return 0 on success, -1 on error.
215 */
Michal Vaskob34b3e82016-02-26 15:04:58 +0100216int nc_server_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100217
218/**
219 * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys,
Michal Vaskof0537d82016-01-29 14:42:38 +0100220 * and CTN entries are not affected.
Michal Vasko45f298f2016-01-29 10:26:26 +0100221 */
222void nc_server_tls_ch_clear_crls(void);
223
224/**
225 * @brief Add a Call Home Cert-to-name entry.
226 *
227 * @param[in] id Priority of the entry.
228 * @param[in] fingerprint Matching certificate fingerprint.
229 * @param[in] map_type Type of username-certificate mapping.
230 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
231 * @return 0 on success, -1 on error.
232 */
233int nc_server_tls_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
234
235/**
236 * @brief Remove a Call Home Cert-to-name entry.
237 *
238 * @param[in] id Priority of the entry. -1 matches all the priorities.
239 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
240 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
241 * @param[in] name Specific username for the entry. NULL matches all the usernames.
242 * @return 0 on success, -1 on not finding any match.
243 */
244int nc_server_tls_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
245
246/**
247 * @brief Clear all the TLS Call Home options. Afterwards a new set of options
Michal Vaskof0537d82016-01-29 14:42:38 +0100248 * can be set for the next client to connect.
Michal Vasko45f298f2016-01-29 10:26:26 +0100249 */
250void nc_server_tls_ch_clear_opts(void);
251
Radek Krejci53691be2016-02-22 13:58:37 +0100252#endif /* NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +0100253
254#endif /* NC_SESSION_SERVER_CH_H_ */