blob: d70202c3985d7e318522895ff92ee3dacb921769 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 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 Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#ifndef NC_SESSION_SERVER_H_
16#define NC_SESSION_SERVER_H_
17
18#include <stdint.h>
Michal Vasko05ba9df2016-01-13 14:40:27 +010019#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020
21#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010022#include "netconf.h"
23
Michal Vasko1a38c862016-01-15 15:50:07 +010024/**
25 * @brief Prototype of callbacks that are called if some RPCs are received.
26 *
27 * If \p session termination reason is changed in the callback, one last reply
28 * is sent and then the session is considered invalid.
29 *
30 * @param[in] rpc Parsed client RPC request.
31 * @param[in] session Session the RPC arrived on.
32 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
33 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010034typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
35
Michal Vasko1a38c862016-01-15 15:50:07 +010036/**
Michal Vasko7b096242016-01-29 15:55:10 +010037 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010038 *
39 * @param[in] session Session to modify.
40 * @param[in] reason Reason of termination.
41 */
42void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
43
44/**
45 * @brief Initialize the server using a libyang context.
46 *
47 * The context is not modified internally, only its dictionary is used for holding
Michal Vaskoa43589b2016-02-17 13:24:59 +010048 * all the strings, which is thread-safe. Reading models is considered thread-safe
49 * as models cannot be removed and are rarely modified (augments or deviations).
Michal Vasko1a38c862016-01-15 15:50:07 +010050 *
51 * Server capabilities are generated based on its content. Changing the context
52 * in ways that result in changed capabilities (adding models, changing features)
53 * is discouraged after sessions are established as it is not possible to change
54 * capabilities of a session.
55 *
56 * This context can safely be destroyed only after calling the last libnetconf2
57 * function in an application.
58 *
59 * Supported RPCs of models in the context are expected to have the private field
60 * in the corresponding RPC schema node set to a nc_rpc_clb function callback.
61 * This callback is called by nc_ps_poll() if the particular RPC request is
62 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
63 * only) and ietf-netconf:close-session are set internally if left unset.
64 *
Michal Vasko1a38c862016-01-15 15:50:07 +010065 * @param[in] ctx Core NETCONF server context.
66 * @return 0 on success, -1 on error.
67 */
Michal Vasko086311b2016-01-08 09:53:11 +010068int nc_server_init(struct ly_ctx *ctx);
69
Michal Vasko1a38c862016-01-15 15:50:07 +010070/**
Michal Vaskob48aa812016-01-18 14:13:09 +010071 * @brief Destroy any dynamically allocated server resources.
72 */
73void nc_server_destroy(void);
74
75/**
Michal Vasko1a38c862016-01-15 15:50:07 +010076 * @brief Set the with-defaults capability extra parameters.
77 *
78 * For the capability to be actually advertised, the server context must also
79 * include the ietf-netconf-with-defaults model.
80 *
81 * Changing this option has the same ill effects as changing capabilities while
82 * sessions are already established.
83 *
84 * @param[in] basic_mode basic-mode with-defaults parameter.
85 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
86 * parameter.
87 * @return 0 on success, -1 on error.
88 */
Michal Vasko086311b2016-01-08 09:53:11 +010089int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
90
Michal Vasko1a38c862016-01-15 15:50:07 +010091/**
92 * @brief Set the interleave capability.
93 *
94 * For the capability to be actually advertised, the server context must also
95 * include the nc-notifications model.
96 *
97 * Changing this option has the same ill effects as changing capabilities while
98 * sessions are already established.
99 *
100 * @param[in] interleave_support 1 to suport interleave, 0 to not.
101 */
102void nc_server_set_capab_interleave(int interleave_support);
Michal Vasko086311b2016-01-08 09:53:11 +0100103
Michal Vasko1a38c862016-01-15 15:50:07 +0100104/**
105 * @brief Set server timeout for receiving a hello message.
106 *
107 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
108 */
109void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100110
Michal Vasko1a38c862016-01-15 15:50:07 +0100111/**
112 * @brief Set server timeout for dropping an idle session.
113 *
114 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100115 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100116 */
117void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100118
Michal Vasko1a38c862016-01-15 15:50:07 +0100119/**
120 * @brief Accept a new session on a pre-established transport session.
121 *
122 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
123 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
124 * @param[in] username NETCONF username as provided by the transport protocol.
125 * @param[out] session New session on success.
126 * @return 0 on success, -1 on error.
127 */
128int nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100129
Michal Vasko1a38c862016-01-15 15:50:07 +0100130/**
131 * @brief Create an empty structure for polling sessions.
132 *
133 * @return Empty pollsession structure, NULL on error.
134 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100135struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100136
Michal Vasko1a38c862016-01-15 15:50:07 +0100137/**
138 * @brief Free a pollsession structure.
139 *
140 * @param[in] ps Pollsession structure to free.
141 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100142void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100143
Michal Vasko1a38c862016-01-15 15:50:07 +0100144/**
145 * @brief Add a session to a pollsession structure.
146 *
147 * @param[in] ps Pollsession structure to modify.
148 * @param[in] session Session to add to \p ps.
149 * @return 0 on success, -1 on error.
150 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100151int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100152
Michal Vasko1a38c862016-01-15 15:50:07 +0100153/**
154 * @brief Remove a session from a pollsession structure.
155 *
156 * @param[in] ps Pollsession structure to modify.
157 * @param[in] session Session to remove from \p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100158 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100159 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100160int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
161
Michal Vasko1a38c862016-01-15 15:50:07 +0100162/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100163 * @brief Learn the number of sessions in a pollsession structure.
164 *
165 * @param[in] ps Pollsession structure to check.
166 * @return Number of sessions (even invalid ones) in \p ps.
167 */
168uint16_t nc_ps_session_count(struct nc_pollsession *ps);
169
170/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100171 * @brief Poll sessions and process any received RPCs.
172 *
173 * All the sessions must be running. If a session fails causing it to change its
Michal Vasko96164bf2016-01-21 15:41:58 +0100174 * status, it can be learnt from the return value. Only one event on one session
175 * is handled in one function call.
Michal Vasko1a38c862016-01-15 15:50:07 +0100176 *
177 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100178 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100179 * infinite waiting.
Michal Vaskobd8ef262016-01-20 11:09:27 +0100180 * @return 0 on elapsed timeout,
Michal Vaskoca4a2422016-02-02 12:17:14 +0100181 * 1 if an RPC was processed (even if it was not known - it failed to be
182 * parsed into session ctx),
Michal Vaskobd8ef262016-01-20 11:09:27 +0100183 * 2 if an RPC was processed and there are unhandled events on other sessions,
184 * 3 if a session from \p ps changed its status (was invalidated),
Michal Vasko8c748832016-02-03 15:32:16 +0100185 * -1 on error (a session likely changed its status as well).
Michal Vasko96164bf2016-01-21 15:41:58 +0100186 *
187 * Only with SSH support:
188 * 4 if an SSH message was processed,
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100189 * 5 if a new NETCONF SSH channel was created; call nc_ps_accept_ssh_channel()
Michal Vasko96164bf2016-01-21 15:41:58 +0100190 * to establish a new NETCONF session.
Michal Vasko1a38c862016-01-15 15:50:07 +0100191 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100192int nc_ps_poll(struct nc_pollsession *ps, int timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100193
Michal Vasko11d142a2016-01-19 15:58:24 +0100194/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100195 * @brief Remove sessions from a pollsession structure and
196 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100197 *
Michal Vaskocad3ac42016-03-01 09:06:01 +0100198 * Calling this function with \p all false makes sense if nc_ps_poll() returned 3.
Michal Vaskod09eae62016-02-01 10:32:52 +0100199 *
200 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100201 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskod09eae62016-02-01 10:32:52 +0100202 */
Michal Vaskocad3ac42016-03-01 09:06:01 +0100203void nc_ps_clear(struct nc_pollsession *ps, int all);
Michal Vaskod09eae62016-02-01 10:32:52 +0100204
Radek Krejci53691be2016-02-22 13:58:37 +0100205#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100206
Michal Vasko1a38c862016-01-15 15:50:07 +0100207/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100208 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100209 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100210 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
211 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100212 * @param[out] session New session.
Michal Vaskoc61c4492016-01-25 11:13:34 +0100213 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko1a38c862016-01-15 15:50:07 +0100214 */
215int nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100216
Radek Krejci53691be2016-02-22 13:58:37 +0100217#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100218
Radek Krejci53691be2016-02-22 13:58:37 +0100219#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100220
Michal Vasko1a38c862016-01-15 15:50:07 +0100221/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100222 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskof0537d82016-01-29 14:42:38 +0100223 * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns 5.
Michal Vasko96164bf2016-01-21 15:41:58 +0100224 *
225 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
226 * @param[out] session New session.
Michal Vaskof0537d82016-01-29 14:42:38 +0100227 * @return 0 on success, -1 on error.
Michal Vasko96164bf2016-01-21 15:41:58 +0100228 */
229int nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
230
231/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100232 * @brief Add a new SSH endpoint and start listening on it.
233 *
234 * @param[in] name Arbitrary unique endpoint name. There can be a TLS endpoint with
Michal Vaskof0537d82016-01-29 14:42:38 +0100235 * the same name.
Michal Vasko3031aae2016-01-27 16:07:18 +0100236 * @param[in] address IP address to listen on.
237 * @param[in] port Port to listen on.
238 * @return 0 on success, -1 on error.
239 */
240int nc_server_ssh_add_endpt_listen(const char *name, const char *address, uint16_t port);
241
242/**
Michal Vaskoda514772016-02-01 11:32:01 +0100243 * @brief Change SSH endpoint listening address.
244 *
245 * On error the previous listening socket is left untouched.
246 *
247 * @param[in] endpt_name Existing endpoint name.
248 * @param[in] address New listening address.
249 * @return 0 on success, -1 on error.
250 */
251int nc_server_ssh_endpt_set_address(const char *endpt_name, const char *address);
252
253/**
254 * @brief Change SSH endpoint listening port.
255 *
256 * On error the previous listening socket is left untouched.
257 *
258 * @param[in] endpt_name Existing endpoint name.
259 * @param[in] port New listening port.
260 * @return 0 on success, -1 on error.
261 */
262int nc_server_ssh_endpt_set_port(const char *endpt_name, uint16_t port);
263
264/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100265 * @brief Stop listening on and remove an SSH endpoint.
266 *
267 * @param[in] name Endpoint name. NULL matches all (SSH) endpoints.
268 * @return 0 on success, -1 on not finding any match.
269 */
270int nc_server_ssh_del_endpt(const char *name);
271
272/**
273 * @brief Set endpoint SSH host keys the server will identify itself with. Each of RSA, DSA, and
Michal Vaskof0537d82016-01-29 14:42:38 +0100274 * ECDSA keys can be set. If the particular type was already set, it is replaced.
Michal Vasko1a38c862016-01-15 15:50:07 +0100275 *
Michal Vaskoda514772016-02-01 11:32:01 +0100276 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100277 * @param[in] privkey_path Path to a private key.
278 * @return 0 on success, -1 on error.
279 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100280int nc_server_ssh_endpt_set_hostkey(const char *endpt_name, const char *privkey_path);
Michal Vasko086311b2016-01-08 09:53:11 +0100281
Michal Vasko1a38c862016-01-15 15:50:07 +0100282/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100283 * @brief Set endpoint SSH banner the server will send to every client.
Michal Vasko1a38c862016-01-15 15:50:07 +0100284 *
Michal Vaskoda514772016-02-01 11:32:01 +0100285 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100286 * @param[in] banner SSH banner.
287 * @return 0 on success, -1 on error.
288 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100289int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner);
Michal Vasko086311b2016-01-08 09:53:11 +0100290
Michal Vasko1a38c862016-01-15 15:50:07 +0100291/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100292 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100293 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100294 *
Michal Vaskoda514772016-02-01 11:32:01 +0100295 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100296 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
297 * @return 0 on success, -1 on error.
298 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100299int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100300
Michal Vasko1a38c862016-01-15 15:50:07 +0100301/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100302 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100303 *
Michal Vaskoda514772016-02-01 11:32:01 +0100304 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100305 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100306 * @return 0 on success, -1 on error.
307 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100308int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100309
Michal Vasko1a38c862016-01-15 15:50:07 +0100310/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100311 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100312 *
Michal Vaskoda514772016-02-01 11:32:01 +0100313 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100314 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
315 * @return 0 on success, -1 on error.
316 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100317int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100318
Michal Vasko1a38c862016-01-15 15:50:07 +0100319/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100320 * @brief Add an endpoint authorized client SSH public key. This public key can be used for
Michal Vaskof0537d82016-01-29 14:42:38 +0100321 * publickey authentication afterwards.
Michal Vasko1a38c862016-01-15 15:50:07 +0100322 *
Michal Vaskoda514772016-02-01 11:32:01 +0100323 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100324 * @param[in] pubkey_path Path to the public key.
325 * @param[in] username Username that the client with the public key must use.
326 * @return 0 on success, -1 on error.
327 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100328int nc_server_ssh_endpt_add_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
Michal Vasko086311b2016-01-08 09:53:11 +0100329
Michal Vasko1a38c862016-01-15 15:50:07 +0100330/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100331 * @brief Remove an endpoint authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100332 *
Michal Vaskoda514772016-02-01 11:32:01 +0100333 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100334 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
335 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
336 * @return 0 on success, -1 on not finding any match.
337 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100338int nc_server_ssh_endpt_del_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
339
Radek Krejci53691be2016-02-22 13:58:37 +0100340#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100341
Radek Krejci53691be2016-02-22 13:58:37 +0100342#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100343
Michal Vasko1a38c862016-01-15 15:50:07 +0100344/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100345 * @brief Add a new TLS endpoint and start listening on it.
346 *
347 * @param[in] name Arbitrary unique endpoint name. There can be an SSH endpoint with
Michal Vaskof0537d82016-01-29 14:42:38 +0100348 * the same name.
Michal Vasko3031aae2016-01-27 16:07:18 +0100349 * @param[in] address IP address to listen on.
350 * @param[in] port Port to listen on.
351 * @return 0 on success, -1 on error.
352 */
353int nc_server_tls_add_endpt_listen(const char *name, const char *address, uint16_t port);
354
355/**
Michal Vaskoda514772016-02-01 11:32:01 +0100356 * @brief Change TLS endpoint listening address.
357 *
358 * On error the previous listening socket is left untouched.
359 *
360 * @param[in] endpt_name Existing endpoint name.
361 * @param[in] address New listening address.
362 * @return 0 on success, -1 on error.
363 */
364int nc_server_tls_endpt_set_address(const char *endpt_name, const char *address);
365
366/**
367 * @brief Change TLS endpoint listening port.
368 *
369 * On error the previous listening socket is left untouched.
370 *
371 * @param[in] endpt_name Existing endpoint name.
372 * @param[in] port New listening port.
373 * @return 0 on success, -1 on error.
374 */
375int nc_server_tls_endpt_set_port(const char *endpt_name, uint16_t port);
376
377/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100378 * @brief Stop listening on and remove a TLS endpoint.
379 *
380 * @param[in] name Endpoint name. NULL matches all (TLS) endpoints.
381 * @return 0 on success, -1 on not finding any match.
382 */
383int nc_server_tls_del_endpt(const char *name);
384
385/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100386 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert_path().
Michal Vaskof0537d82016-01-29 14:42:38 +0100387 * There can only be one certificate for each key type, it is replaced if
388 * already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100389 *
Michal Vaskoda514772016-02-01 11:32:01 +0100390 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100391 * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding.
392 * @return 0 on success, -1 on error.
393 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100394int nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert);
Michal Vasko086311b2016-01-08 09:53:11 +0100395
Michal Vasko1a38c862016-01-15 15:50:07 +0100396/**
397 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert().
Michal Vaskof0537d82016-01-29 14:42:38 +0100398 * There can only be one certificate for each key type, it is replaced if
399 * already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100400 *
Michal Vaskoda514772016-02-01 11:32:01 +0100401 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100402 * @param[in] cert_path Path to a certificate file in PEM format.
403 * @return 0 on success, -1 on error.
404 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100405int nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100406
Michal Vasko1a38c862016-01-15 15:50:07 +0100407/**
408 * @brief Set server TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100409 * Alternative to nc_tls_server_set_key_path(). There can only be one of
410 * every key type, it is replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100411 *
Michal Vaskoda514772016-02-01 11:32:01 +0100412 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100413 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
414 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
415 * @return 0 on success, -1 on error.
416 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100417int nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100418
Michal Vasko1a38c862016-01-15 15:50:07 +0100419/**
420 * @brief Set server TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100421 * Alternative to nc_tls_server_set_key_path(). There can only be one of
422 * every key type, it is replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100423 *
Michal Vaskoda514772016-02-01 11:32:01 +0100424 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100425 * @param[in] privkey_path Path to a private key file in PEM format.
426 * @return 0 on success, -1 on error.
427 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100428int nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100429
Michal Vasko1a38c862016-01-15 15:50:07 +0100430/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100431 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100432 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100433 *
Michal Vaskoda514772016-02-01 11:32:01 +0100434 * @param[in] endpt_name Existing endpoint name.
Michal Vaskof0537d82016-01-29 14:42:38 +0100435 * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding.
Michal Vasko1a38c862016-01-15 15:50:07 +0100436 * @return 0 on success, -1 on error.
437 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100438int nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert);
Michal Vasko0457bb42016-01-08 15:49:13 +0100439
Michal Vasko1a38c862016-01-15 15:50:07 +0100440/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100441 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100442 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100443 *
Michal Vaskoda514772016-02-01 11:32:01 +0100444 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100445 * @param[in] cert_path Path to a trusted certificate file in PEM format.
446 * @return 0 on success, -1 on error.
447 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100448int nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100449
Michal Vasko1a38c862016-01-15 15:50:07 +0100450/**
451 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100452 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100453 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100454 *
Michal Vaskoda514772016-02-01 11:32:01 +0100455 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100456 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
457 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
458 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100459 * @return 0 on success, -1 on error.
460 */
Michal Vasko96830e32016-02-01 10:54:18 +0100461int nc_server_tls_endpt_set_trusted_ca_paths(const char *endpt_name, const char *ca_file, const char *ca_dir);
Michal Vasko0457bb42016-01-08 15:49:13 +0100462
Michal Vasko1a38c862016-01-15 15:50:07 +0100463/**
464 * @brief Destroy and clean all the set certificates and private keys. CRLs and
Michal Vaskof0537d82016-01-29 14:42:38 +0100465 * CTN entries are not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100466 *
467 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100468 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100469void nc_server_tls_endpt_clear_certs(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100470
Michal Vasko1a38c862016-01-15 15:50:07 +0100471/**
472 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100473 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100474 *
Michal Vaskoda514772016-02-01 11:32:01 +0100475 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100476 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
477 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
478 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100479 * @return 0 on success, -1 on error.
480 */
Michal Vasko96830e32016-02-01 10:54:18 +0100481int nc_server_tls_endpt_set_crl_paths(const char *endpt_name, const char *crl_file, const char *crl_dir);
Michal Vasko0457bb42016-01-08 15:49:13 +0100482
Michal Vasko1a38c862016-01-15 15:50:07 +0100483/**
Michal Vasko96830e32016-02-01 10:54:18 +0100484 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100485 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100486 *
487 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100488 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100489void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100490
Michal Vasko1a38c862016-01-15 15:50:07 +0100491/**
492 * @brief Add a Cert-to-name entry.
493 *
Michal Vaskoda514772016-02-01 11:32:01 +0100494 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100495 * @param[in] id Priority of the entry.
496 * @param[in] fingerprint Matching certificate fingerprint.
497 * @param[in] map_type Type of username-certificate mapping.
498 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
499 * @return 0 on success, -1 on error.
500 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100501int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100502
Michal Vasko1a38c862016-01-15 15:50:07 +0100503/**
504 * @brief Remove a Cert-to-name entry.
505 *
Michal Vaskoda514772016-02-01 11:32:01 +0100506 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100507 * @param[in] id Priority of the entry. -1 matches all the priorities.
508 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
509 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
510 * @param[in] name Specific username for the entry. NULL matches all the usernames.
511 * @return 0 on success, -1 on not finding any match.
512 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100513int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100514
Radek Krejci53691be2016-02-22 13:58:37 +0100515#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100516
517#endif /* NC_SESSION_SERVER_H_ */