blob: 63b30eeb3ffe3167cfcf975e49df99b135d79c9a [file] [log] [blame]
romanc1d2b092023-02-02 08:58:27 +01001/**
romane028ef92023-02-24 16:33:08 +01002 * @file server_config.h
romanc1d2b092023-02-02 08:58:27 +01003 * @author Roman Janota <janota@cesnet.cz>
4 * @brief libnetconf2 server configuration
5 *
6 * @copyright
roman3f9b65c2023-06-05 14:26:58 +02007 * Copyright (c) 2023 CESNET, z.s.p.o.
romanc1d2b092023-02-02 08:58:27 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#ifndef NC_CONFIG_SERVER_H_
17#define NC_CONFIG_SERVER_H_
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
roman3f9b65c2023-06-05 14:26:58 +020023#include <stdarg.h>
romanc1d2b092023-02-02 08:58:27 +010024#include <stdint.h>
25
roman3f9b65c2023-06-05 14:26:58 +020026#include <libyang/libyang.h>
27
romanc1d2b092023-02-02 08:58:27 +010028#include "session.h"
romanc1d2b092023-02-02 08:58:27 +010029
30/**
romanf02273a2023-05-25 09:44:11 +020031 * @brief Configure server based on the given diff data.
romanc1d2b092023-02-02 08:58:27 +010032 *
romanf6f37a52023-05-25 14:27:51 +020033 * Expected data are a validated instance of a ietf-netconf-server YANG data.
romanc1d2b092023-02-02 08:58:27 +010034 * The data must be in the diff format and supported operations are: create, replace,
35 * delete and none. Context must already have implemented the required modules, see
36 * ::nc_config_load_modules().
37 *
romanf6f37a52023-05-25 14:27:51 +020038 * @param[in] diff ietf-netconf-server YANG diff data.
romanc1d2b092023-02-02 08:58:27 +010039 * @return 0 on success, 1 on error.
40 */
romanf6f37a52023-05-25 14:27:51 +020041int nc_server_config_setup_diff(const struct lyd_node *diff);
romanc1d2b092023-02-02 08:58:27 +010042
43/**
romanf02273a2023-05-25 09:44:11 +020044 * @brief Configure server based on the given data.
45 *
46 * Expected data is a validated instance of a ietf-netconf-server YANG data.
47 * Behaves as if all the nodes in data had the replace operation. That means that the current configuration will be deleted
48 * and just the given data will all be applied.
romanf6f37a52023-05-25 14:27:51 +020049 * The data must not contain any operation attribute, see ::nc_config_setup_diff() which works with diff.
romanf02273a2023-05-25 09:44:11 +020050 * Context must already have implemented the required modules, see * ::nc_config_load_modules().
51 *
52 * @param[in] data ietf-netconf-server YANG data.
53 * @return 0 on success, 1 on error.
54 */
romanf6f37a52023-05-25 14:27:51 +020055int nc_server_config_setup_data(const struct lyd_node *data);
romanf02273a2023-05-25 09:44:11 +020056
57/**
romanc1d2b092023-02-02 08:58:27 +010058 * @brief Configure server based on the given ietf-netconf-server YANG data.
romanf6f37a52023-05-25 14:27:51 +020059 * Wrapper around ::nc_config_setup_server_data() hiding work with parsing the data.
romanc1d2b092023-02-02 08:58:27 +010060 *
61 * @param[in] ctx libyang context.
62 * @param[in] path Path to the file with YANG data in XML format.
63 * @return 0 on success, 1 on error.
64 */
65int nc_server_config_setup_path(const struct ly_ctx *ctx, const char *path);
66
67/**
68 * @brief Implements all the required modules and their features in the context.
69 * Needs to be called before any other configuration functions.
70 *
71 * If ctx is :
72 * - NULL: a new context will be created and if the call is successful you have to free it,
73 * - non NULL: modules will simply be implemented.
74 *
75 * Implemented modules: ietf-netconf-server, ietf-x509-cert-to-name, ietf-crypto-types,
76 * ietf-tcp-common, ietf-ssh-common, iana-ssh-encryption-algs, iana-ssh-key-exchange-algs,
77 * iana-ssh-mac-algs, iana-ssh-public-key-algs, ietf-keystore, ietf-ssh-server, ietf-truststore,
78 * ietf-tls-server and libnetconf2-netconf-server.
79 *
80 * @param[in, out] ctx Optional context in which the modules will be implemented. Created if ctx is null.
81 * @return 0 on success, 1 on error.
82 */
83int nc_server_config_load_modules(struct ly_ctx **ctx);
84
85/**
roman9b1379c2023-03-31 10:11:10 +020086 * @brief Creates new YANG configuration data nodes for a local-address and local-port.
87 *
roman9b1379c2023-03-31 10:11:10 +020088 * @param[in] ctx libyang context.
89 * @param[in] endpt_name Arbitrary identifier of the endpoint.
roman3f9b65c2023-06-05 14:26:58 +020090 * @param[in] transport Either SSH or TLS transport for the given endpoint.
91 * @param[in] address New listening address.
92 * @param[in] port New listening port.
roman9b1379c2023-03-31 10:11:10 +020093 * If an endpoint with this identifier already exists, it's address and port will be overriden.
94 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
95 * Otherwise the new YANG data will be added to the previous data and may override it.
96 * @return 0 on success, non-zero otherwise.
roman45cec4e2023-02-17 10:21:39 +010097 */
roman3f9b65c2023-06-05 14:26:58 +020098int nc_server_config_new_address_port(const struct ly_ctx *ctx, const char *endpt_name, NC_TRANSPORT_IMPL transport,
99 const char *address, const char *port, struct lyd_node **config);
100
101/**
102 * @brief Creates new YANG configuration data nodes for a hostkey.
103 *
104 * @param[in] ctx libyang context.
105 * @param[in] endpt_name Arbitrary identifier of the endpoint.
106 * If an endpoint with this identifier already exists, it's hostkey might be changed.
107 * @param[in] hostkey_name Arbitrary identifier of the hostkey.
108 * If a hostkey with this identifier already exists, it's contents will be changed.
109 * @param[in] privkey_path Path to a file containing a private key.
110 * The private key has to be in a PEM format. Only RSA and ECDSA keys are supported.
111 * @param[in] pubkey_path Path to a file containing a public key. If NULL, public key will be
112 * generated from the private key.
113 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
114 * Otherwise the new YANG data will be added to the previous data and may override it.
115 * @return 0 on success, non-zero otherwise.
116 */
117int nc_server_config_new_ssh_hostkey(const struct ly_ctx *ctx,
118 const char *endpt_name, const char *hostkey_name, const char *privkey_path, const char *pubkey_path, struct lyd_node **config);
roman9b1379c2023-03-31 10:11:10 +0200119
120/**
121 * @brief Creates new YANG configuration data nodes for host-key algorithms replacing any previous ones.
122 *
123 * Supported algorithms are: ssh-ed25519, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521,
124 * rsa-sha2-512, rsa-sha2-256, ssh-rsa and ssh-dss.
125 *
126 * @param[in] ctx libyang context
127 * @param[in] endpt_name Arbitrary identifier of the endpoint.
128 * If an endpoint with this identifier already exists, it's host-key algorithms will be replaced.
129 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
130 * Otherwise the new YANG data will be added to the previous data and may override it.
131 * @param[in] alg_count Number of following algorithms.
132 * @param[in] ... String literals of host-key algorithms in a decreasing order of preference.
133 * @return 0 on success, non-zero otherwise.
134 */
roman466719d2023-05-05 16:14:37 +0200135int nc_server_config_new_ssh_host_key_algs(const struct ly_ctx *ctx, const char *endpt_name,
roman9b1379c2023-03-31 10:11:10 +0200136 struct lyd_node **config, int alg_count, ...);
137
138/**
139 * @brief Creates new YANG configuration data nodes for key exchange algorithms replacing any previous ones.
140 *
141 * Supported algorithms are: diffie-hellman-group-exchange-sha1, curve25519-sha256, ecdh-sha2-nistp256,
142 * ecdh-sha2-nistp384, ecdh-sha2-nistp521, diffie-hellman-group18-sha512, diffie-hellman-group16-sha512,
143 * diffie-hellman-group-exchange-sha256 and diffie-hellman-group14-sha256.
144 *
145 * @param[in] ctx libyang context
146 * @param[in] endpt_name Arbitrary identifier of the endpoint.
147 * If an endpoint with this identifier already exists, it's key exchange algorithms will be replaced.
148 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
149 * Otherwise the new YANG data will be added to the previous data and may override it.
150 * @param[in] alg_count Number of following algorithms.
151 * @param[in] ... String literals of key exchange algorithms in a decreasing order of preference.
152 * @return 0 on success, non-zero otherwise.
153 */
roman466719d2023-05-05 16:14:37 +0200154int nc_server_config_new_ssh_key_exchange_algs(const struct ly_ctx *ctx, const char *endpt_name, struct lyd_node **config,
roman9b1379c2023-03-31 10:11:10 +0200155 int alg_count, ...);
156
157/**
158 * @brief Creates new YANG configuration data nodes for encryption algorithms replacing any previous ones.
159 *
160 * Supported algorithms are: aes256-ctr, aes192-ctr, aes128-ctr, aes256-cbc, aes192-cbc, aes128-cbc, blowfish-cbc
161 * triple-des-cbc and none.
162 *
163 * @param[in] ctx libyang context
164 * @param[in] endpt_name Arbitrary identifier of the endpoint.
165 * If an endpoint with this identifier already exists, it's encryption algorithms will be replaced.
166 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
167 * Otherwise the new YANG data will be added to the previous data and may override it.
168 * @param[in] alg_count Number of following algorithms.
169 * @param[in] ... String literals of encryption algorithms in a decreasing order of preference.
170 * @return 0 on success, non-zero otherwise.
171 */
roman466719d2023-05-05 16:14:37 +0200172int nc_server_config_new_ssh_encryption_algs(const struct ly_ctx *ctx, const char *endpt_name, struct lyd_node **config,
roman9b1379c2023-03-31 10:11:10 +0200173 int alg_count, ...);
174
175/**
176 * @brief Creates new YANG configuration data nodes for mac algorithms replacing any previous ones.
177 *
178 * Supported algorithms are: hmac-sha2-256, hmac-sha2-512 and hmac-sha1.
179 *
180 * @param[in] ctx libyang context
181 * @param[in] endpt_name Arbitrary identifier of the endpoint.
182 * If an endpoint with this identifier already exists, it's mac algorithms will be replaced.
183 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
184 * Otherwise the new YANG data will be added to the previous data and may override it.
185 * @param[in] alg_count Number of following algorithms.
186 * @param[in] ... String literals of mac algorithms in a decreasing order of preference.
187 * @return 0 on success, non-zero otherwise.
188 */
roman466719d2023-05-05 16:14:37 +0200189int nc_server_config_new_ssh_mac_algs(const struct ly_ctx *ctx, const char *endpt_name, struct lyd_node **config,
roman9b1379c2023-03-31 10:11:10 +0200190 int alg_count, ...);
191
192/**
193 * @brief Creates new YANG configuration data nodes for a client, which supports the public key authentication method.
194 *
roman9b1379c2023-03-31 10:11:10 +0200195 * @param[in] ctx libyang context.
196 * @param[in] endpt_name Arbitrary identifier of the endpoint.
197 * If an endpoint with this identifier already exists, it's user might be changed.
198 * @param[in] user_name Arbitrary identifier of the user.
199 * If an user with this identifier already exists, it's contents will be changed.
200 * @param[in] pubkey_name Arbitrary identifier of the user's public key.
201 * If a public key with this identifier already exists for this user, it's contents will be changed.
roman3f9b65c2023-06-05 14:26:58 +0200202 * @param[in] pubkey_path Path to a file containing the user's public key.
roman9b1379c2023-03-31 10:11:10 +0200203 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
204 * Otherwise the new YANG data will be added to the previous data and may override it.
205 * @return 0 on success, non-zero otherwise.
206 */
roman3f9b65c2023-06-05 14:26:58 +0200207int nc_server_config_new_ssh_client_auth_pubkey(const struct ly_ctx *ctx, const char *endpt_name,
208 const char *user_name, const char *pubkey_name, const char *pubkey_path, struct lyd_node **config);
roman9b1379c2023-03-31 10:11:10 +0200209
210/**
211 * @brief Creates new YANG configuration data nodes for a client, which supports the password authentication method.
212 *
213 * This function sets the password for the given user.
214 *
roman9b1379c2023-03-31 10:11:10 +0200215 * @param[in] ctx libyang context.
216 * @param[in] endpt_name Arbitrary identifier of the endpoint.
217 * If an endpoint with this identifier already exists, it's user might be changed.
218 * @param[in] user_name Arbitrary identifier of the user.
219 * If an user with this identifier already exists, it's contents will be changed.
roman3f9b65c2023-06-05 14:26:58 +0200220 * @param[in] password Cleartext user's password.
roman9b1379c2023-03-31 10:11:10 +0200221 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
222 * Otherwise the new YANG data will be added to the previous data and may override it.
223 * @return 0 on success, non-zero otherwise.
224 */
roman3f9b65c2023-06-05 14:26:58 +0200225int nc_server_config_new_ssh_client_auth_password(const struct ly_ctx *ctx, const char *endpt_name,
226 const char *user_name, const char *password, struct lyd_node **config);
roman9b1379c2023-03-31 10:11:10 +0200227
228/**
229 * @brief Creates new YANG configuration data nodes for a client, which supports the none authentication method.
230 *
231 * @param[in] ctx libyang context.
232 * @param[in] endpt_name Arbitrary identifier of the endpoint.
233 * If an endpoint with this identifier already exists, it's user might be changed.
234 * @param[in] user_name Arbitrary identifier of the user.
235 * If an user with this identifier already exists, it's contents will be changed.
236 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
237 * Otherwise the new YANG data will be added to the previous data and may override it.
238 * @return 0 on success, non-zero otherwise.
239 */
roman466719d2023-05-05 16:14:37 +0200240int nc_server_config_new_ssh_client_auth_none(const struct ly_ctx *ctx, const char *endpt_name,
roman9b1379c2023-03-31 10:11:10 +0200241 const char *user_name, struct lyd_node **config);
242
243/**
244 * @brief Creates new YANG configuration data nodes for a client, which supports the interactive authentication method.
245 *
roman9b1379c2023-03-31 10:11:10 +0200246 * @param[in] ctx libyang context.
247 * @param[in] endpt_name Arbitrary identifier of the endpoint.
248 * If an endpoint with this identifier already exists, it's user might be changed.
249 * @param[in] user_name Arbitrary identifier of the user.
250 * If an user with this identifier already exists, it's contents will be changed.
roman3f9b65c2023-06-05 14:26:58 +0200251 * @param[in] pam_config_name Name of the PAM configuration file.
252 * @param[in] pam_config_name Optional. The absolute path to the directory in which the configuration file
253 * with the name conf_name is located. A newer version (>= 1.4) of PAM library is required to be able to specify
254 * the path. If NULL is passed, then the PAM's system directories will be searched (usually /etc/pam.d/).
roman9b1379c2023-03-31 10:11:10 +0200255 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
256 * Otherwise the new YANG data will be added to the previous data and may override it.
257 * @return 0 on success, non-zero otherwise.
258 */
roman3f9b65c2023-06-05 14:26:58 +0200259int nc_server_config_new_ssh_client_auth_interactive(const struct ly_ctx *ctx, const char *endpt_name,
260 const char *user_name, const char *pam_config_name, const char *pam_config_dir, struct lyd_node **config);
261
262#ifdef NC_ENABLED_TLS
263/**
264 * @brief Creates new YANG configuration data nodes for a server's certificate.
265 *
266 * @param[in] ctx libyang context.
267 * @param[in] endpt_name Arbitrary identifier of the endpoint.
268 * If an endpoint with this identifier already exists, it's server certificate will be changed.
269 * @param[in] pubkey_path Optional path to the server's public key file. If not provided,
270 * it will be generated from the private key.
271 * @param[in] privkey_path Path to the server's private key file.
272 * @param[in] certificate_path Path to the server's certificate file.
273 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
274 * Otherwise the new YANG data will be added to the previous data and may override it.
275 * @return 0 on success, non-zero otherwise.
276 */
277int nc_server_config_new_tls_server_certificate(const struct ly_ctx *ctx, const char *endpt_name, const char *pubkey_path,
278 const char *privkey_path, const char *certificate_path, struct lyd_node **config);
279
280/**
281 * @brief Creates new YANG configuration data nodes for a client's (end-entity) certificate.
282 *
283 * @param[in] ctx libyang context.
284 * @param[in] endpt_name Arbitrary identifier of the endpoint.
285 * If an endpoint with this identifier already exists, it's contents will be changed.
286 * @param[in] cert_name Arbitrary identifier of the client's certificate.
287 * If a client certificate with this indetifier already exists, it will be changed.
288 * @param[in] cert_path Path to the client's certificate file.
289 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
290 * Otherwise the new YANG data will be added to the previous data and may override it.
291 * @return 0 on success, non-zero otherwise.
292 */
293int nc_server_config_new_tls_client_certificate(const struct ly_ctx *ctx, const char *endpt_name, const char *cert_name,
294 const char *cert_path, struct lyd_node **config);
295
296/**
297 * @brief Creates new YANG configuration data nodes for a client certificate authority (trust-anchor) certificate.
298 *
299 * @param[in] ctx libyang context.
300 * @param[in] endpt_name Arbitrary identifier of the endpoint.
301 * If an endpoint with this identifier already exists, it's contents will be changed.
302 * @param[in] cert_name Arbitrary identifier of the certificate authority certificate.
303 * If a CA with this indetifier already exists, it will be changed.
304 * @param[in] cert_path Path to the CA certificate file.
305 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
306 * Otherwise the new YANG data will be added to the previous data and may override it.
307 * @return 0 on success, non-zero otherwise.
308 */
309int nc_server_config_new_tls_client_ca(const struct ly_ctx *ctx, const char *endpt_name, const char *cert_name,
310 const char *cert_path, struct lyd_node **config);
311
312/**
313 * @brief Creates new YANG configuration data nodes for a cert-to-name entry.
314 *
315 * @param[in] ctx libyang context.
316 * @param[in] endpt_name Arbitrary identifier of the endpoint.
317 * If an endpoint with this identifier already exists, it's contents will be changed.
318 * @param[in] id ID of the entry. The lower the ID, the higher the priority of the entry (it will be checked earlier).
319 * @param[in] fingerprint Optional fingerprint of the entry. The fingerprint should always be set, however if it is
320 * not set, it will match any certificate. Entry with no fingerprint should therefore be placed only as the last entry.
321 * @param[in] map_type Mapping username to the certificate option.
322 * @param[in] name Username for this cert-to-name entry.
323 * @param[in,out] config Configuration YANG data tree. If *config is NULL, it will be created.
324 * Otherwise the new YANG data will be added to the previous data and may override it.
325 * @return 0 on success, non-zero otherwise.
326 */
327int nc_server_config_new_tls_ctn(const struct ly_ctx *ctx, const char *endpt_name, uint32_t id, const char *fingerprint,
328 NC_TLS_CTN_MAPTYPE map_type, const char *name, struct lyd_node **config);
329
330#endif /* NC_ENABLED_TLS */
roman45cec4e2023-02-17 10:21:39 +0100331
romanc1d2b092023-02-02 08:58:27 +0100332#ifdef __cplusplus
333}
334#endif
335
336#endif /* NC_SESSION_SERVER_H_ */