blob: c445304932a622dbb369f7701e66c3daf8bd19e1 [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/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +010045 * @brief Initialize libssh and/or libssl/libcrypto and the server using a libyang context.
Michal Vasko1a38c862016-01-15 15:50:07 +010046 *
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 *
Michal Vasko5494f512016-03-01 12:13:44 +010051 * If the callbacks on schema nodes (their private data) are modified after
52 * server initialization with that particular context, they will be called (changes
53 * will take effect). However, there could be race conditions as the access to
54 * these callbacks is not thread-safe.
55 *
Michal Vasko1a38c862016-01-15 15:50:07 +010056 * Server capabilities are generated based on its content. Changing the context
57 * in ways that result in changed capabilities (adding models, changing features)
58 * is discouraged after sessions are established as it is not possible to change
59 * capabilities of a session.
60 *
61 * This context can safely be destroyed only after calling the last libnetconf2
62 * function in an application.
63 *
64 * Supported RPCs of models in the context are expected to have the private field
65 * in the corresponding RPC schema node set to a nc_rpc_clb function callback.
66 * This callback is called by nc_ps_poll() if the particular RPC request is
67 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
68 * only) and ietf-netconf:close-session are set internally if left unset.
69 *
Michal Vasko1a38c862016-01-15 15:50:07 +010070 * @param[in] ctx Core NETCONF server context.
71 * @return 0 on success, -1 on error.
72 */
Michal Vasko086311b2016-01-08 09:53:11 +010073int nc_server_init(struct ly_ctx *ctx);
74
Michal Vasko1a38c862016-01-15 15:50:07 +010075/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +010076 * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
77 * server resources.
Michal Vaskob48aa812016-01-18 14:13:09 +010078 */
79void nc_server_destroy(void);
80
81/**
Michal Vasko1a38c862016-01-15 15:50:07 +010082 * @brief Set the with-defaults capability extra parameters.
83 *
84 * For the capability to be actually advertised, the server context must also
85 * include the ietf-netconf-with-defaults model.
86 *
87 * Changing this option has the same ill effects as changing capabilities while
88 * sessions are already established.
89 *
90 * @param[in] basic_mode basic-mode with-defaults parameter.
91 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
92 * parameter.
93 * @return 0 on success, -1 on error.
94 */
Michal Vasko086311b2016-01-08 09:53:11 +010095int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
96
Michal Vasko1a38c862016-01-15 15:50:07 +010097/**
Michal Vasko55f03972016-04-13 08:56:01 +020098 * @brief Get with-defaults capability extra parameters.
99 *
100 * At least one argument must be non-NULL.
101 *
102 * @param[in,out] basic_mode basic-mode parameter.
103 * @param[in,out] also_supported also-supported parameter.
104 */
105void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
106
107/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100108 * @brief Set the interleave capability.
109 *
110 * For the capability to be actually advertised, the server context must also
111 * include the nc-notifications model.
112 *
113 * Changing this option has the same ill effects as changing capabilities while
114 * sessions are already established.
115 *
116 * @param[in] interleave_support 1 to suport interleave, 0 to not.
117 */
118void nc_server_set_capab_interleave(int interleave_support);
Michal Vasko086311b2016-01-08 09:53:11 +0100119
Michal Vasko1a38c862016-01-15 15:50:07 +0100120/**
Michal Vasko55f03972016-04-13 08:56:01 +0200121 * @brief Get the interleave capability state.
122 *
123 * @return 1 for supported, 0 for not supported.
124 */
125int nc_server_get_capab_interleave(void);
126
127/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100128 * @brief Set server timeout for receiving a hello message.
129 *
130 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
131 */
132void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100133
Michal Vasko1a38c862016-01-15 15:50:07 +0100134/**
Michal Vasko55f03972016-04-13 08:56:01 +0200135 * @brief get server timeout for receiving a hello message.
136 *
137 * @return Hello message timeout, 0 is infinite.
138 */
139uint16_t nc_server_get_hello_timeout(void);
140
141/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100142 * @brief Set server timeout for dropping an idle session.
143 *
144 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100145 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100146 */
147void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100148
Michal Vasko1a38c862016-01-15 15:50:07 +0100149/**
Michal Vasko55f03972016-04-13 08:56:01 +0200150 * @brief Get server timeout for dropping an idle session.
151 *
152 * @return Idle session timeout, 0 for for never dropping
153 * a session because of inactivity.
154 */
155uint16_t nc_server_get_idle_timeout(void);
156
157/**
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200158 * @brief Get all the server capabilities as will be sent to every client.
159 *
160 * A few capabilities (with-defaults, interleave) depend on the current
161 * server options.
162 *
163 * @param[in] ctx Context to read most capabilities from.
164 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
165 */
166const char **nc_server_get_cpblts(struct ly_ctx *ctx);
167
168/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100169 * @brief Accept a new session on a pre-established transport session.
170 *
171 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
172 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
173 * @param[in] username NETCONF username as provided by the transport protocol.
174 * @param[out] session New session on success.
175 * @return 0 on success, -1 on error.
176 */
177int nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100178
Michal Vasko1a38c862016-01-15 15:50:07 +0100179/**
180 * @brief Create an empty structure for polling sessions.
181 *
182 * @return Empty pollsession structure, NULL on error.
183 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100184struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100185
Michal Vasko1a38c862016-01-15 15:50:07 +0100186/**
187 * @brief Free a pollsession structure.
188 *
Michal Vasko01082bf2016-04-07 10:44:21 +0200189 * !IMPORTANT! Make sure that \p ps is not accessible (is not used)
190 * by any thread before and after this call!
191 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100192 * @param[in] ps Pollsession structure to free.
193 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100194void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100195
Michal Vasko1a38c862016-01-15 15:50:07 +0100196/**
197 * @brief Add a session to a pollsession structure.
198 *
199 * @param[in] ps Pollsession structure to modify.
200 * @param[in] session Session to add to \p ps.
201 * @return 0 on success, -1 on error.
202 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100203int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100204
Michal Vasko1a38c862016-01-15 15:50:07 +0100205/**
206 * @brief Remove a session from a pollsession structure.
207 *
208 * @param[in] ps Pollsession structure to modify.
209 * @param[in] session Session to remove from \p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100210 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100211 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100212int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
213
Michal Vasko1a38c862016-01-15 15:50:07 +0100214/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100215 * @brief Learn the number of sessions in a pollsession structure.
216 *
217 * @param[in] ps Pollsession structure to check.
218 * @return Number of sessions (even invalid ones) in \p ps.
219 */
220uint16_t nc_ps_session_count(struct nc_pollsession *ps);
221
222/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100223 * @brief Poll sessions and process any received RPCs.
224 *
225 * All the sessions must be running. If a session fails causing it to change its
Michal Vasko96164bf2016-01-21 15:41:58 +0100226 * status, it can be learnt from the return value. Only one event on one session
227 * is handled in one function call.
Michal Vasko1a38c862016-01-15 15:50:07 +0100228 *
229 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100230 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100231 * infinite waiting.
Michal Vaskobd8ef262016-01-20 11:09:27 +0100232 * @return 0 on elapsed timeout,
Michal Vaskoca4a2422016-02-02 12:17:14 +0100233 * 1 if an RPC was processed (even if it was not known - it failed to be
234 * parsed into session ctx),
Michal Vaskobd8ef262016-01-20 11:09:27 +0100235 * 2 if an RPC was processed and there are unhandled events on other sessions,
236 * 3 if a session from \p ps changed its status (was invalidated),
Michal Vasko8c748832016-02-03 15:32:16 +0100237 * -1 on error (a session likely changed its status as well).
Michal Vasko96164bf2016-01-21 15:41:58 +0100238 *
239 * Only with SSH support:
240 * 4 if an SSH message was processed,
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100241 * 5 if a new NETCONF SSH channel was created; call nc_ps_accept_ssh_channel()
Michal Vasko96164bf2016-01-21 15:41:58 +0100242 * to establish a new NETCONF session.
Michal Vasko1a38c862016-01-15 15:50:07 +0100243 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100244int nc_ps_poll(struct nc_pollsession *ps, int timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100245
Michal Vasko11d142a2016-01-19 15:58:24 +0100246/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100247 * @brief Remove sessions from a pollsession structure and
248 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100249 *
Michal Vaskocad3ac42016-03-01 09:06:01 +0100250 * Calling this function with \p all false makes sense if nc_ps_poll() returned 3.
Michal Vaskod09eae62016-02-01 10:32:52 +0100251 *
252 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100253 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100254 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100255 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100256void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100257
Radek Krejci53691be2016-02-22 13:58:37 +0100258#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100259
Michal Vasko1a38c862016-01-15 15:50:07 +0100260/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100261 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100262 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100263 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
264 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100265 * @param[out] session New session.
Michal Vaskoc61c4492016-01-25 11:13:34 +0100266 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko1a38c862016-01-15 15:50:07 +0100267 */
268int nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100269
Radek Krejci53691be2016-02-22 13:58:37 +0100270#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100271
Radek Krejci53691be2016-02-22 13:58:37 +0100272#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100273
Michal Vasko1a38c862016-01-15 15:50:07 +0100274/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100275 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskof0537d82016-01-29 14:42:38 +0100276 * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns 5.
Michal Vaskoc0fde012016-03-01 09:07:23 +0100277 * The new session is only returned in \p session, it is not added to \p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100278 *
279 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
280 * @param[out] session New session.
Michal Vaskof0537d82016-01-29 14:42:38 +0100281 * @return 0 on success, -1 on error.
Michal Vasko96164bf2016-01-21 15:41:58 +0100282 */
283int nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
284
285/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100286 * @brief Add a new SSH endpoint and start listening on it.
287 *
288 * @param[in] name Arbitrary unique endpoint name. There can be a TLS endpoint with
Michal Vaskof0537d82016-01-29 14:42:38 +0100289 * the same name.
Michal Vasko3031aae2016-01-27 16:07:18 +0100290 * @param[in] address IP address to listen on.
291 * @param[in] port Port to listen on.
292 * @return 0 on success, -1 on error.
293 */
294int nc_server_ssh_add_endpt_listen(const char *name, const char *address, uint16_t port);
295
296/**
Michal Vaskoda514772016-02-01 11:32:01 +0100297 * @brief Change SSH endpoint listening address.
298 *
299 * On error the previous listening socket is left untouched.
300 *
301 * @param[in] endpt_name Existing endpoint name.
302 * @param[in] address New listening address.
303 * @return 0 on success, -1 on error.
304 */
305int nc_server_ssh_endpt_set_address(const char *endpt_name, const char *address);
306
307/**
308 * @brief Change SSH endpoint listening port.
309 *
310 * On error the previous listening socket is left untouched.
311 *
312 * @param[in] endpt_name Existing endpoint name.
313 * @param[in] port New listening port.
314 * @return 0 on success, -1 on error.
315 */
316int nc_server_ssh_endpt_set_port(const char *endpt_name, uint16_t port);
317
318/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100319 * @brief Stop listening on and remove an SSH endpoint.
320 *
321 * @param[in] name Endpoint name. NULL matches all (SSH) endpoints.
322 * @return 0 on success, -1 on not finding any match.
323 */
324int nc_server_ssh_del_endpt(const char *name);
325
326/**
327 * @brief Set endpoint SSH host keys the server will identify itself with. Each of RSA, DSA, and
Michal Vaskof0537d82016-01-29 14:42:38 +0100328 * ECDSA keys can be set. If the particular type was already set, it is replaced.
Michal Vasko1a38c862016-01-15 15:50:07 +0100329 *
Michal Vaskoda514772016-02-01 11:32:01 +0100330 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100331 * @param[in] privkey_path Path to a private key.
332 * @return 0 on success, -1 on error.
333 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100334int nc_server_ssh_endpt_set_hostkey(const char *endpt_name, const char *privkey_path);
Michal Vasko086311b2016-01-08 09:53:11 +0100335
Michal Vasko1a38c862016-01-15 15:50:07 +0100336/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100337 * @brief Set endpoint SSH banner the server will send to every client.
Michal Vasko1a38c862016-01-15 15:50:07 +0100338 *
Michal Vaskoda514772016-02-01 11:32:01 +0100339 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100340 * @param[in] banner SSH banner.
341 * @return 0 on success, -1 on error.
342 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100343int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner);
Michal Vasko086311b2016-01-08 09:53:11 +0100344
Michal Vasko1a38c862016-01-15 15:50:07 +0100345/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100346 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100347 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100348 *
Michal Vaskoda514772016-02-01 11:32:01 +0100349 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100350 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
351 * @return 0 on success, -1 on error.
352 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100353int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100354
Michal Vasko1a38c862016-01-15 15:50:07 +0100355/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100356 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100357 *
Michal Vaskoda514772016-02-01 11:32:01 +0100358 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100359 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100360 * @return 0 on success, -1 on error.
361 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100362int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100363
Michal Vasko1a38c862016-01-15 15:50:07 +0100364/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100365 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100366 *
Michal Vaskoda514772016-02-01 11:32:01 +0100367 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100368 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
369 * @return 0 on success, -1 on error.
370 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100371int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100372
Michal Vasko1a38c862016-01-15 15:50:07 +0100373/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100374 * @brief Add an endpoint authorized client SSH public key. This public key can be used for
Michal Vaskof0537d82016-01-29 14:42:38 +0100375 * publickey authentication afterwards.
Michal Vasko1a38c862016-01-15 15:50:07 +0100376 *
Michal Vaskoda514772016-02-01 11:32:01 +0100377 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100378 * @param[in] pubkey_path Path to the public key.
379 * @param[in] username Username that the client with the public key must use.
380 * @return 0 on success, -1 on error.
381 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100382int nc_server_ssh_endpt_add_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
Michal Vasko086311b2016-01-08 09:53:11 +0100383
Michal Vasko1a38c862016-01-15 15:50:07 +0100384/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100385 * @brief Remove an endpoint authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100386 *
Michal Vaskoda514772016-02-01 11:32:01 +0100387 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100388 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
389 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
390 * @return 0 on success, -1 on not finding any match.
391 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100392int nc_server_ssh_endpt_del_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
393
Radek Krejci53691be2016-02-22 13:58:37 +0100394#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100395
Radek Krejci53691be2016-02-22 13:58:37 +0100396#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100397
Michal Vasko1a38c862016-01-15 15:50:07 +0100398/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100399 * @brief Add a new TLS endpoint and start listening on it.
400 *
401 * @param[in] name Arbitrary unique endpoint name. There can be an SSH endpoint with
Michal Vaskof0537d82016-01-29 14:42:38 +0100402 * the same name.
Michal Vasko3031aae2016-01-27 16:07:18 +0100403 * @param[in] address IP address to listen on.
404 * @param[in] port Port to listen on.
405 * @return 0 on success, -1 on error.
406 */
407int nc_server_tls_add_endpt_listen(const char *name, const char *address, uint16_t port);
408
409/**
Michal Vaskoda514772016-02-01 11:32:01 +0100410 * @brief Change TLS endpoint listening address.
411 *
412 * On error the previous listening socket is left untouched.
413 *
414 * @param[in] endpt_name Existing endpoint name.
415 * @param[in] address New listening address.
416 * @return 0 on success, -1 on error.
417 */
418int nc_server_tls_endpt_set_address(const char *endpt_name, const char *address);
419
420/**
421 * @brief Change TLS endpoint listening port.
422 *
423 * On error the previous listening socket is left untouched.
424 *
425 * @param[in] endpt_name Existing endpoint name.
426 * @param[in] port New listening port.
427 * @return 0 on success, -1 on error.
428 */
429int nc_server_tls_endpt_set_port(const char *endpt_name, uint16_t port);
430
431/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100432 * @brief Stop listening on and remove a TLS endpoint.
433 *
434 * @param[in] name Endpoint name. NULL matches all (TLS) endpoints.
435 * @return 0 on success, -1 on not finding any match.
436 */
437int nc_server_tls_del_endpt(const char *name);
438
439/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100440 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert_path().
Michal Vaskof0537d82016-01-29 14:42:38 +0100441 * There can only be one certificate for each key type, it is replaced if
442 * already set.
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 Base64-encoded certificate in ASN.1 DER encoding.
446 * @return 0 on success, -1 on error.
447 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100448int nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert);
Michal Vasko086311b2016-01-08 09:53:11 +0100449
Michal Vasko1a38c862016-01-15 15:50:07 +0100450/**
451 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert().
Michal Vaskof0537d82016-01-29 14:42:38 +0100452 * There can only be one certificate for each key type, it is replaced if
453 * already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100454 *
Michal Vaskoda514772016-02-01 11:32:01 +0100455 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100456 * @param[in] cert_path Path to a certificate file in PEM format.
457 * @return 0 on success, -1 on error.
458 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100459int nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100460
Michal Vasko1a38c862016-01-15 15:50:07 +0100461/**
462 * @brief Set server TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100463 * Alternative to nc_tls_server_set_key_path(). There can only be one of
464 * every key type, it is replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100465 *
Michal Vaskoda514772016-02-01 11:32:01 +0100466 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100467 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
468 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
469 * @return 0 on success, -1 on error.
470 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100471int nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100472
Michal Vasko1a38c862016-01-15 15:50:07 +0100473/**
474 * @brief Set server TLS private key matching the certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100475 * Alternative to nc_tls_server_set_key_path(). There can only be one of
476 * every key type, it is replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100477 *
Michal Vaskoda514772016-02-01 11:32:01 +0100478 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100479 * @param[in] privkey_path Path to a private key file in PEM format.
480 * @return 0 on success, -1 on error.
481 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100482int nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100483
Michal Vasko1a38c862016-01-15 15:50:07 +0100484/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100485 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100486 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100487 *
Michal Vaskoda514772016-02-01 11:32:01 +0100488 * @param[in] endpt_name Existing endpoint name.
Michal Vaskof0537d82016-01-29 14:42:38 +0100489 * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding.
Michal Vasko1a38c862016-01-15 15:50:07 +0100490 * @return 0 on success, -1 on error.
491 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100492int nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert);
Michal Vasko0457bb42016-01-08 15:49:13 +0100493
Michal Vasko1a38c862016-01-15 15:50:07 +0100494/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100495 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100496 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100497 *
Michal Vaskoda514772016-02-01 11:32:01 +0100498 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100499 * @param[in] cert_path Path to a trusted certificate file in PEM format.
500 * @return 0 on success, -1 on error.
501 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100502int nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100503
Michal Vasko1a38c862016-01-15 15:50:07 +0100504/**
505 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100506 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100507 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100508 *
Michal Vaskoda514772016-02-01 11:32:01 +0100509 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100510 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
511 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
512 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100513 * @return 0 on success, -1 on error.
514 */
Michal Vasko96830e32016-02-01 10:54:18 +0100515int 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 +0100516
Michal Vasko1a38c862016-01-15 15:50:07 +0100517/**
518 * @brief Destroy and clean all the set certificates and private keys. CRLs and
Michal Vaskof0537d82016-01-29 14:42:38 +0100519 * CTN entries are not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100520 *
521 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100522 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100523void nc_server_tls_endpt_clear_certs(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100524
Michal Vasko1a38c862016-01-15 15:50:07 +0100525/**
526 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100527 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100528 *
Michal Vaskoda514772016-02-01 11:32:01 +0100529 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100530 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
531 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
532 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100533 * @return 0 on success, -1 on error.
534 */
Michal Vasko96830e32016-02-01 10:54:18 +0100535int 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 +0100536
Michal Vasko1a38c862016-01-15 15:50:07 +0100537/**
Michal Vasko96830e32016-02-01 10:54:18 +0100538 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100539 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100540 *
541 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100542 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100543void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100544
Michal Vasko1a38c862016-01-15 15:50:07 +0100545/**
546 * @brief Add a Cert-to-name entry.
547 *
Michal Vaskoda514772016-02-01 11:32:01 +0100548 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100549 * @param[in] id Priority of the entry.
550 * @param[in] fingerprint Matching certificate fingerprint.
551 * @param[in] map_type Type of username-certificate mapping.
552 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
553 * @return 0 on success, -1 on error.
554 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100555int 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 +0100556
Michal Vasko1a38c862016-01-15 15:50:07 +0100557/**
558 * @brief Remove a Cert-to-name entry.
559 *
Michal Vaskoda514772016-02-01 11:32:01 +0100560 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100561 * @param[in] id Priority of the entry. -1 matches all the priorities.
562 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
563 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
564 * @param[in] name Specific username for the entry. NULL matches all the usernames.
565 * @return 0 on success, -1 on not finding any match.
566 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100567int 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 +0100568
Radek Krejci53691be2016-02-22 13:58:37 +0100569#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100570
Michal Vaskof8352352016-05-24 09:11:36 +0200571/**
572 * @brief Get session start time in YANG datetime format.
573 *
574 * @param[in] session Session to get the information from.
575 * @return Session start time, must be freed, NULL on error.
576 */
577char *nc_session_get_session_start(const struct nc_session *session);
578
Michal Vasko086311b2016-01-08 09:53:11 +0100579#endif /* NC_SESSION_SERVER_H_ */