blob: 532a18166a7d95ba06c710fee0fd678ade12a03e [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file session_server.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 session server manipulation
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko4848ac82021-11-09 12:29:14 +01006 * @copyright
Michal Vasko1440a742021-03-31 11:11:03 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * 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
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
16#ifndef NC_SESSION_SERVER_H_
17#define NC_SESSION_SERVER_H_
18
Michal Vaskoc09730e2019-01-17 10:07:26 +010019#ifdef __cplusplus
20extern "C" {
21#endif
22
Michal Vasko05ba9df2016-01-13 14:40:27 +010023#include <libyang/libyang.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020024#include <stdint.h>
Michal Vasko086311b2016-01-08 09:53:11 +010025
Michal Vasko709598e2016-11-28 14:48:32 +010026#ifdef NC_ENABLED_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +020027# include <openssl/x509.h>
Michal Vasko709598e2016-11-28 14:48:32 +010028#endif
29
bhart3bc2f582018-06-05 12:40:32 -050030#ifdef NC_ENABLED_SSH
Michal Vaskoc446a382021-06-18 08:54:05 +020031# include <libssh/callbacks.h>
32# include <libssh/libssh.h>
33# include <libssh/server.h>
bhart3bc2f582018-06-05 12:40:32 -050034#endif
35
Michal Vasko086311b2016-01-08 09:53:11 +010036#include "netconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020037#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010038
Michal Vasko1a38c862016-01-15 15:50:07 +010039/**
Radek Krejci6799a052017-05-19 14:23:23 +020040 * @defgroup server_session Server Session
41 * @ingroup server
42 *
43 * @brief Server-side NETCONF session manipulation.
44 * @{
45 */
46
47/**
Michal Vasko1a38c862016-01-15 15:50:07 +010048 * @brief Prototype of callbacks that are called if some RPCs are received.
49 *
Michal Vaskoc446a382021-06-18 08:54:05 +020050 * If @p session termination reason is changed in the callback, one last reply
Michal Vasko1a38c862016-01-15 15:50:07 +010051 * is sent and then the session is considered invalid.
52 *
Radek Krejci6799a052017-05-19 14:23:23 +020053 * The callback is set via nc_set_global_rpc_clb().
54 *
Michal Vasko1a38c862016-01-15 15:50:07 +010055 * @param[in] rpc Parsed client RPC request.
56 * @param[in] session Session the RPC arrived on.
57 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
58 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010059typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
60
Michal Vasko1a38c862016-01-15 15:50:07 +010061/**
Michal Vasko7b096242016-01-29 15:55:10 +010062 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010063 *
64 * @param[in] session Session to modify.
65 * @param[in] reason Reason of termination.
66 */
67void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
68
69/**
Michal Vasko142cfea2017-08-07 10:12:11 +020070 * @brief Set the session-id of the session responsible for this session's termination.
71 *
72 * @param[in] session Session to modify. Must have term_reason set to #NC_SESSION_TERM_KILLED.
73 * @param[in] sid SID of the killing session.
74 */
75void nc_session_set_killed_by(struct nc_session *session, uint32_t sid);
76
77/**
78 * @brief Set the status of a session.
79 *
80 * @param[in] session Session to modify.
81 * @param[in] status Status of the session.
82 */
83void nc_session_set_status(struct nc_session *session, NC_STATUS status);
84
85/**
Radek Krejci6799a052017-05-19 14:23:23 +020086 * @brief Set a global nc_rpc_clb that is called if the particular RPC request is
87 * received and the private field in the corresponding RPC schema node is NULL.
88 *
89 * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default.
90 */
91void nc_set_global_rpc_clb(nc_rpc_clb clb);
92
Michal Vasko4e6d3242021-05-26 09:13:24 +020093/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +020094
95/**
96 * @addtogroup server
97 * @{
98 */
99
100/**
Michal Vasko395900e2021-11-09 12:14:28 +0100101 * @brief Initialize libssh and/or libssl/libcrypto and the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100102 *
Michal Vasko395900e2021-11-09 12:14:28 +0100103 * Must be called before other nc_server* functions.
Michal Vasko1a38c862016-01-15 15:50:07 +0100104 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100105 * @return 0 on success, -1 on error.
106 */
Michal Vasko395900e2021-11-09 12:14:28 +0100107int nc_server_init(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100108
Michal Vasko1a38c862016-01-15 15:50:07 +0100109/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100110 * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
111 * server resources.
Michal Vaskob48aa812016-01-18 14:13:09 +0100112 */
113void nc_server_destroy(void);
114
115/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100116 * @brief Set the with-defaults capability extra parameters.
117 *
118 * For the capability to be actually advertised, the server context must also
119 * include the ietf-netconf-with-defaults model.
120 *
121 * Changing this option has the same ill effects as changing capabilities while
122 * sessions are already established.
123 *
124 * @param[in] basic_mode basic-mode with-defaults parameter.
125 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
126 * parameter.
127 * @return 0 on success, -1 on error.
128 */
Michal Vasko086311b2016-01-08 09:53:11 +0100129int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
130
Michal Vasko1a38c862016-01-15 15:50:07 +0100131/**
Michal Vasko55f03972016-04-13 08:56:01 +0200132 * @brief Get with-defaults capability extra parameters.
133 *
134 * At least one argument must be non-NULL.
135 *
136 * @param[in,out] basic_mode basic-mode parameter.
137 * @param[in,out] also_supported also-supported parameter.
138 */
139void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
140
141/**
Radek Krejci658782b2016-12-04 22:04:55 +0100142 * @brief Set capability of the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100143 *
Radek Krejci658782b2016-12-04 22:04:55 +0100144 * Capability can be used when some behavior or extension of the server is not defined
145 * as a YANG module. The provided value will be advertised in the server's \<hello\>
146 * messages. Note, that libnetconf only checks that the provided value is non-empty
147 * string.
Michal Vasko1a38c862016-01-15 15:50:07 +0100148 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100149 * @param[in] value Capability string to be advertised in server's \<hello\> messages.
Michal Vasko1a38c862016-01-15 15:50:07 +0100150 */
Radek Krejci658782b2016-12-04 22:04:55 +0100151int nc_server_set_capability(const char *value);
Michal Vasko55f03972016-04-13 08:56:01 +0200152
153/**
Michal Vasko1440a742021-03-31 11:11:03 +0200154 * @brief Set the callback for getting yang-library capability identifier. If none is set, libyang context change count is used.
155 *
156 * @param[in] content_id_clb Callback that should return the yang-library content identifier.
Michal Vaskoc446a382021-06-18 08:54:05 +0200157 * @param[in] user_data Optional arbitrary user data that will be passed to @p content_id_clb.
158 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Michal Vasko1440a742021-03-31 11:11:03 +0200159 */
160void nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data,
161 void (*free_user_data)(void *user_data));
162
163/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100164 * @brief Set server timeout for receiving a hello message.
165 *
166 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
167 */
168void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100169
Michal Vasko1a38c862016-01-15 15:50:07 +0100170/**
Michal Vasko55f03972016-04-13 08:56:01 +0200171 * @brief get server timeout for receiving a hello message.
172 *
173 * @return Hello message timeout, 0 is infinite.
174 */
175uint16_t nc_server_get_hello_timeout(void);
176
177/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100178 * @brief Set server timeout for dropping an idle session.
179 *
180 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100181 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100182 */
183void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100184
Michal Vasko1a38c862016-01-15 15:50:07 +0100185/**
Michal Vasko55f03972016-04-13 08:56:01 +0200186 * @brief Get server timeout for dropping an idle session.
187 *
188 * @return Idle session timeout, 0 for for never dropping
189 * a session because of inactivity.
190 */
191uint16_t nc_server_get_idle_timeout(void);
192
193/**
Radek Krejci24a18412018-05-16 15:09:10 +0200194 * @brief Get all the server capabilities including all the schemas.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200195 *
196 * A few capabilities (with-defaults, interleave) depend on the current
197 * server options.
198 *
199 * @param[in] ctx Context to read most capabilities from.
Michal Vasko395900e2021-11-09 12:14:28 +0100200 * @return Array of capabilities, NULL on error.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200201 */
Michal Vasko395900e2021-11-09 12:14:28 +0100202char **nc_server_get_cpblts(const struct ly_ctx *ctx);
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200203
Radek Krejci24a18412018-05-16 15:09:10 +0200204/**
205 * @brief Get the server capabilities including the schemas with the specified YANG version.
206 *
207 * A few capabilities (with-defaults, interleave) depend on the current
208 * server options.
209 *
210 * @param[in] ctx Context to read most capabilities from.
211 * @param[in] version YANG version of the schemas to be included in result, with
212 * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts().
Michal Vasko395900e2021-11-09 12:14:28 +0100213 * @return Array of capabilities, NULL on error.
Radek Krejci24a18412018-05-16 15:09:10 +0200214 */
Michal Vasko395900e2021-11-09 12:14:28 +0100215char **nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version);
Radek Krejci24a18412018-05-16 15:09:10 +0200216
Michal Vasko1440a742021-03-31 11:11:03 +0200217/** @} Server */
Radek Krejci6799a052017-05-19 14:23:23 +0200218
219/**
220 * @addtogroup server_session
221 * @{
222 */
223
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200224/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100225 * @brief Accept a new session on a pre-established transport session.
226 *
Michal Vasko395900e2021-11-09 12:14:28 +0100227 * For detailed description, look at ::nc_accept().
228 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100229 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
230 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
231 * @param[in] username NETCONF username as provided by the transport protocol.
Michal Vasko395900e2021-11-09 12:14:28 +0100232 * @param[in] ctx Context for the session to use.
Michal Vasko1a38c862016-01-15 15:50:07 +0100233 * @param[out] session New session on success.
Michal Vasko71090fc2016-05-24 16:37:28 +0200234 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
235 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100236 */
Michal Vasko395900e2021-11-09 12:14:28 +0100237NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx,
238 struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100239
Michal Vasko1a38c862016-01-15 15:50:07 +0100240/**
241 * @brief Create an empty structure for polling sessions.
242 *
243 * @return Empty pollsession structure, NULL on error.
244 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100245struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100246
Michal Vasko1a38c862016-01-15 15:50:07 +0100247/**
248 * @brief Free a pollsession structure.
249 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200250 * !IMPORTANT! Make sure that @p ps is not accessible (is not used)
Michal Vasko01082bf2016-04-07 10:44:21 +0200251 * by any thread before and after this call!
252 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100253 * @param[in] ps Pollsession structure to free.
254 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100255void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100256
Michal Vasko1a38c862016-01-15 15:50:07 +0100257/**
258 * @brief Add a session to a pollsession structure.
259 *
260 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200261 * @param[in] session Session to add to @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100262 * @return 0 on success, -1 on error.
263 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100264int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100265
Michal Vasko1a38c862016-01-15 15:50:07 +0100266/**
267 * @brief Remove a session from a pollsession structure.
268 *
269 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200270 * @param[in] session Session to remove from @p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100271 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100272 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100273int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
274
Michal Vasko1a38c862016-01-15 15:50:07 +0100275/**
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100276 * @brief Get a session from a pollsession structure matching the session ID.
277 *
278 * @param[in] ps Pollsession structure to read from.
Michal Vasko4871c9d2017-10-09 14:48:39 +0200279 * @param[in] idx Index of the session.
280 * @return Session on index, NULL if out-of-bounds.
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100281 */
Michal Vasko4871c9d2017-10-09 14:48:39 +0200282struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx);
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100283
284/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100285 * @brief Learn the number of sessions in a pollsession structure.
286 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200287 * Does not lock @p ps structure for efficiency.
Michal Vaskof4462fd2017-02-15 14:29:05 +0100288 *
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100289 * @param[in] ps Pollsession structure to check.
Michal Vaskoc446a382021-06-18 08:54:05 +0200290 * @return Number of sessions (even invalid ones) in @p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100291 */
292uint16_t nc_ps_session_count(struct nc_pollsession *ps);
293
Michal Vasko30a5d6b2017-02-15 14:29:39 +0100294#define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */
295#define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */
296#define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */
297#define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */
298#define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */
299#define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */
300#define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
301#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200302
303#ifdef NC_ENABLED_SSH
Michal Vaskoc446a382021-06-18 08:54:05 +0200304# define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */
305# define NC_PSPOLL_SSH_CHANNEL 0x0200 /**< New SSH channel opened on an existing session (only with SSH support). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200306#endif
307
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100308/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100309 * @brief Poll sessions and process any received RPCs.
310 *
Michal Vaskoe4300a82017-05-24 10:35:42 +0200311 * Only one event on one session is handled in one function call. If this event
312 * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session
Michal Vaskoc446a382021-06-18 08:54:05 +0200313 * should be removed from @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100314 *
315 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100316 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100317 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200318 * @param[in] session Session that was processed and that specific return bits concern.
319 * Can be NULL.
Michal Vaskoade892d2017-02-22 13:40:35 +0100320 * @return Bitfield of NC_PSPOLL_* macros.
Michal Vasko1a38c862016-01-15 15:50:07 +0100321 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200322int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100323
Michal Vasko11d142a2016-01-19 15:58:24 +0100324/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100325 * @brief Remove sessions from a pollsession structure and
326 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100327 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200328 * Calling this function with @p all false makes sense if nc_ps_poll() returned #NC_PSPOLL_SESSION_TERM.
Michal Vaskod09eae62016-02-01 10:32:52 +0100329 *
330 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100331 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100332 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100333 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100334void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100335
Michal Vasko1440a742021-03-31 11:11:03 +0200336/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200337
338/**
339 * @addtogroup server
340 * @{
341 */
342
Michal Vasko1a38c862016-01-15 15:50:07 +0100343/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200344 * @brief Add a new endpoint.
345 *
346 * Before the endpoint can accept any connections, its address and port must
Radek Krejci6799a052017-05-19 14:23:23 +0200347 * be set via nc_server_endpt_set_address() and nc_server_endpt_set_port().
Michal Vaskoe2713da2016-08-22 16:06:40 +0200348 *
349 * @param[in] name Arbitrary unique endpoint name.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200350 * @param[in] ti Transport protocol to use.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200351 * @return 0 on success, -1 on error.
352 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200353int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200354
355/**
356 * @brief Stop listening on and remove an endpoint.
357 *
358 * @param[in] name Endpoint name. NULL matches all endpoints.
Michal Vasko59050372016-11-22 14:33:55 +0100359 * @param[in] ti Endpoint transport protocol. NULL matches any protocol.
Michal Vaskoc446a382021-06-18 08:54:05 +0200360 * Redundant to set if @p name is set, endpoint names are
Michal Vasko59050372016-11-22 14:33:55 +0100361 * unique disregarding their protocol.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200362 * @return 0 on success, -1 on not finding any match.
363 */
Michal Vasko59050372016-11-22 14:33:55 +0100364int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200365
366/**
Michal Vaskoe8e07702017-03-15 10:19:30 +0100367 * @brief Get the number of currently configured listening endpoints.
368 * Note that an ednpoint without address and/or port will be included
369 * even though it is not, in fact, listening.
370 *
371 * @return Number of added listening endpoints.
372 */
373int nc_server_endpt_count(void);
374
375/**
Michal Vasko1b5973e2020-01-30 16:05:46 +0100376 * @brief Check if an endpoint exists.
377 *
378 * @param[in] name Endpoint name.
379 * @return 0 if does not exists, non-zero otherwise.
380 */
381int nc_server_is_endpt(const char *name);
382
383/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200384 * @brief Change endpoint listening address.
385 *
386 * On error the previous listening socket (if any) is left untouched.
387 *
388 * @param[in] endpt_name Existing endpoint name.
389 * @param[in] address New listening address.
390 * @return 0 on success, -1 on error.
391 */
392int nc_server_endpt_set_address(const char *endpt_name, const char *address);
393
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200394#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko946cacb2020-08-12 11:18:08 +0200395
Michal Vasko2e6defd2016-10-07 15:48:15 +0200396/**
397 * @brief Change endpoint listening port.
398 *
Michal Vasko946cacb2020-08-12 11:18:08 +0200399 * This is only valid on SSH/TLS transport endpoint.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200400 * On error the previous listening socket (if any) is left untouched.
401 *
402 * @param[in] endpt_name Existing endpoint name.
403 * @param[in] port New listening port.
404 * @return 0 on success, -1 on error.
405 */
406int nc_server_endpt_set_port(const char *endpt_name, uint16_t port);
Michal Vasko9e036d52016-01-08 10:49:26 +0100407
Michal Vasko946cacb2020-08-12 11:18:08 +0200408#endif
409
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200410/**
411 * @brief Change endpoint permissions.
412 *
Michal Vasko946cacb2020-08-12 11:18:08 +0200413 * This is only valid on UNIX transport endpoint.
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200414 * On error the previous listening socket (if any) is left untouched.
415 *
416 * @param[in] endpt_name Existing endpoint name.
417 * @param[in] mode New mode, -1 to use default.
418 * @param[in] uid New uid, -1 to use default.
419 * @param[in] gid New gid, -1 to use default.
420 * @return 0 on success, -1 on error.
421 */
422int nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid);
423
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200424/**
425 * @brief Change endpoint keepalives state. Affects only new connections.
426 *
427 * @param[in] endpt_name Existing endpoint name.
428 * @param[in] enable Whether to enable or disable keepalives.
429 * @return 0 on success, -1 on error.
430 */
431int nc_server_endpt_enable_keepalives(const char *endpt_name, int enable);
432
433/**
434 * @brief Change endpoint keepalives parameters. Affects only new connections.
435 *
436 * @param[in] endpt_name Existing endpoint name.
437 * @param[in] idle_time Keepalive idle time in seconds, 1 by default, -1 to keep previous value.
438 * @param[in] max_probes Keepalive max probes sent, 10 by default, -1 to keep previous value.
439 * @param[in] probe_interval Keepalive probe interval in seconds, 5 by default, -1 to keep previous value.
440 * @return 0 on success, -1 on error.
441 */
442int nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval);
443
Michal Vasko1440a742021-03-31 11:11:03 +0200444/** @} Server */
Radek Krejci6799a052017-05-19 14:23:23 +0200445
446/**
447 * @addtogroup server_session
Michal Vasko4e6d3242021-05-26 09:13:24 +0200448 * @{
Radek Krejci6799a052017-05-19 14:23:23 +0200449 */
450
Michal Vasko1a38c862016-01-15 15:50:07 +0100451/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100452 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100453 *
Michal Vaskob70c8b82017-03-17 09:09:29 +0100454 * Once a new (TCP/IP) conection is established a different (quite long) timeout
455 * is used for waiting for transport-related data, which means this call can block
Michal Vaskoc446a382021-06-18 08:54:05 +0200456 * for much longer that @p timeout, but only with slow/faulty/malicious clients.
Michal Vaskob70c8b82017-03-17 09:09:29 +0100457 *
Michal Vasko395900e2021-11-09 12:14:28 +0100458 * Server capabilities are generated based on the content of @p ctx. The context must
459 * not be destroyed before the accepted NETCONF session is freed.
460 *
461 * Supported RPCs of models in the context are expected to have their callback
462 * in the corresponding RPC schema node set to a nc_rpc_clb function callback using ::nc_set_rpc_callback().
463 * This callback is called by ::nc_ps_poll() if the particular RPC request is
464 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
465 * only) and ietf-netconf:close-session are set internally if left unset.
466 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100467 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko395900e2021-11-09 12:14:28 +0100468 * non-blocking call, -1 for infinite waiting.
469 * @param[in] ctx Context for the session to use.
Michal Vasko96164bf2016-01-21 15:41:58 +0100470 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200471 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
472 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100473 */
Michal Vasko395900e2021-11-09 12:14:28 +0100474NC_MSG_TYPE nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100475
Radek Krejci53691be2016-02-22 13:58:37 +0100476#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100477
Michal Vasko1a38c862016-01-15 15:50:07 +0100478/**
Michal Vaskoc446a382021-06-18 08:54:05 +0200479 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF @p orig_session.
480 * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on @p orig_session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200481 *
482 * @param[in] orig_session Session that has a new SSH channel ready.
483 * @param[out] session New session.
484 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
485 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
486 */
487NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
488
489/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100490 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskoc446a382021-06-18 08:54:05 +0200491 * that was polled in @p ps. Call this function only when nc_ps_poll() on @p ps returns #NC_PSPOLL_SSH_CHANNEL.
492 * The new session is only returned in @p session, it is not added to @p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100493 *
494 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
495 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200496 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
497 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100498 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200499NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100500
Michal Vasko1440a742021-03-31 11:11:03 +0200501/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200502
503/**
504 * @defgroup server_ssh Server SSH
505 * @ingroup server
506 *
507 * @brief Server-side settings for SSH connections.
508 * @{
509 */
510
Michal Vasko96164bf2016-01-21 15:41:58 +0100511/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100512 * @brief Add an authorized client SSH public key. This public key can be used for
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200513 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100514 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100515 * @param[in] pubkey_base64 Authorized public key binary content encoded in base64.
516 * @param[in] type Authorized public key SSH type.
517 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100518 * @return 0 on success, -1 on error.
519 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100520int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100521
522/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100523 * @brief Add an authorized client SSH public key. This public key can be used for
524 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100525 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100526 * @param[in] pubkey_path Path to the public key.
527 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100528 * @return 0 on success, -1 on error.
529 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100530int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100531
532/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100533 * @brief Remove an authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100534 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100535 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
536 * @param[in] pubkey_base64 Authorized public key content. NULL matches any key.
537 * @param[in] type Authorized public key type. 0 matches all types.
538 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
539 * @return 0 on success, -1 on not finding any match.
Michal Vasko1a38c862016-01-15 15:50:07 +0100540 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100541int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200542 const char *username);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200543
544/**
Michal Vaskoebba7602018-03-23 13:14:08 +0100545 * @brief Set the callback for SSH password authentication. If none is set, local system users are used.
546 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200547 * @param[in] passwd_auth_clb Callback that should authenticate the user. Username can be directly obtained from @p session.
Michal Vasko1440a742021-03-31 11:11:03 +0200548 * Zero return indicates success, non-zero an error.
Michal Vaskoc446a382021-06-18 08:54:05 +0200549 * @param[in] user_data Optional arbitrary user data that will be passed to @p passwd_auth_clb.
550 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Michal Vaskoebba7602018-03-23 13:14:08 +0100551 */
552void nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password,
Michal Vasko1440a742021-03-31 11:11:03 +0200553 void *user_data), void *user_data, void (*free_user_data)(void *user_data));
Michal Vaskoebba7602018-03-23 13:14:08 +0100554
555/**
bhart3bc2f582018-06-05 12:40:32 -0500556 * @brief Set the callback for SSH interactive authentication. If none is set, local system users are used.
557 *
558 * @param[in] interactive_auth_clb Callback that should authenticate the user.
Michal Vasko1440a742021-03-31 11:11:03 +0200559 * Zero return indicates success, non-zero an error.
Michal Vaskoc446a382021-06-18 08:54:05 +0200560 * @param[in] user_data Optional arbitrary user data that will be passed to @p passwd_auth_clb.
561 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
bhart3bc2f582018-06-05 12:40:32 -0500562 */
Michal Vasko1440a742021-03-31 11:11:03 +0200563void nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session,
564 const ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data));
bhart3bc2f582018-06-05 12:40:32 -0500565
566/**
567 * @brief Set the callback for SSH public key authentication. If none is set, local system users are used.
568 *
569 * @param[in] pubkey_auth_clb Callback that should authenticate the user.
Michal Vasko1440a742021-03-31 11:11:03 +0200570 * Zero return indicates success, non-zero an error.
Michal Vaskoc446a382021-06-18 08:54:05 +0200571 * @param[in] user_data Optional arbitrary user data that will be passed to @p passwd_auth_clb.
572 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
bhart3bc2f582018-06-05 12:40:32 -0500573 */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200574void nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key,
Michal Vasko1440a742021-03-31 11:11:03 +0200575 void *user_data), void *user_data, void (*free_user_data)(void *user_data));
bhart3bc2f582018-06-05 12:40:32 -0500576
577/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100578 * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However,
579 * a maximum of one key of each type will be used during SSH authentication, later keys replacing
580 * the earlier ones.
581 *
582 * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero
Michal Vaskoc446a382021-06-18 08:54:05 +0200583 * an error. On success exactly ONE of @p privkey_path or @p privkey_data is expected
Michal Vasko4c1fb492017-01-30 14:31:07 +0100584 * to be set. The one set will be freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200585 * - @p privkey_path expects a PEM file,
586 * - @p privkey_data expects a base-64 encoded ANS.1 DER data,
587 * - @p privkey_type type of the key in @p privkey_data. Use ::NC_SSH_KEY_UNKNOWN for
Michal Vasko68177b72020-04-27 15:46:53 +0200588 * PKCS#8 key that includes the information about the key in its data.
Michal Vaskoc446a382021-06-18 08:54:05 +0200589 * @param[in] user_data Optional arbitrary user data that will be passed to @p hostkey_clb.
590 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100591 */
592void nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200593 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, void (*free_user_data)(void *user_data));
Michal Vasko4c1fb492017-01-30 14:31:07 +0100594
595/**
Michal Vaskoddce1212019-05-24 09:58:49 +0200596 * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself
597 * wil be retrieved using a callback.
598 *
599 * @param[in] endpt_name Existing endpoint name.
600 * @param[in] name Arbitrary name of the host key.
601 * @param[in] idx Optional index where to add the key. -1 adds at the end.
602 * @return 0 on success, -1 on error.
603 */
604int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx);
605
606/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100607 * @brief Delete endpoint SSH host key. Their order is preserved.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200608 *
609 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoc446a382021-06-18 08:54:05 +0200610 * @param[in] name Name of the host key. NULL matches all the keys, but if @p idx != -1 then this must be NULL.
611 * @param[in] idx Index of the hostkey. -1 matches all indices, but if @p name != NULL then this must be -1.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200612 * @return 0 on success, -1 on error.
613 */
Michal Vasko7d255882017-02-09 13:35:08 +0100614int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vasko086311b2016-01-08 09:53:11 +0100615
Michal Vasko1a38c862016-01-15 15:50:07 +0100616/**
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100617 * @brief Move endpoint SSH host key.
618 *
619 * @param[in] endpt_name Exisitng endpoint name.
620 * @param[in] key_mov Name of the host key that will be moved.
Michal Vaskoc446a382021-06-18 08:54:05 +0200621 * @param[in] key_after Name of the key that will preceed @p key_mov. NULL if @p key_mov is to be moved at the beginning.
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100622 * @return 0 in success, -1 on error.
623 */
624int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after);
625
626/**
627 * @brief Modify endpoint SSH host key.
628 *
629 * @param[in] endpt_name Exisitng endpoint name.
630 * @param[in] name Name of an existing host key.
Michal Vaskoc446a382021-06-18 08:54:05 +0200631 * @param[in] new_name New name of the host key @p name.
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100632 * @return 0 in success, -1 on error.
633 */
634int nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100635
Michal Vasko1a38c862016-01-15 15:50:07 +0100636/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100637 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100638 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100639 *
Michal Vaskoda514772016-02-01 11:32:01 +0100640 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100641 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
642 * @return 0 on success, -1 on error.
643 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100644int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100645
Michal Vasko1a38c862016-01-15 15:50:07 +0100646/**
Michal Vaskoddce1212019-05-24 09:58:49 +0200647 * @brief Get endpoint accepted SSH authentication methods.
648 *
649 * @param[in] endpt_name Existing endpoint name.
650 * @return Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
651 */
652int nc_server_ssh_endpt_get_auth_methods(const char *endpt_name);
653
654/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100655 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100656 *
Michal Vaskoda514772016-02-01 11:32:01 +0100657 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100658 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100659 * @return 0 on success, -1 on error.
660 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100661int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100662
Michal Vasko1a38c862016-01-15 15:50:07 +0100663/**
Michal Vaskocbad4c52019-06-27 16:30:35 +0200664 * @brief Set endpoint SSH authentication timeout. 30 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100665 *
Michal Vaskoda514772016-02-01 11:32:01 +0100666 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100667 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
668 * @return 0 on success, -1 on error.
669 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100670int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100671
Michal Vaskoc446a382021-06-18 08:54:05 +0200672/** @} Server SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200673
Radek Krejci53691be2016-02-22 13:58:37 +0100674#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100675
Radek Krejci53691be2016-02-22 13:58:37 +0100676#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100677
Michal Vasko1a38c862016-01-15 15:50:07 +0100678/**
Radek Krejci6799a052017-05-19 14:23:23 +0200679 * @defgroup server_tls Server TLS
680 * @ingroup server
681 *
682 * @brief Server-side settings for TLS connections.
683 * @{
684 */
685
686/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100687 * @brief Set the server TLS certificate. Only the name is set, the certificate itself
688 * wil be retrieved using a callback.
Michal Vaskoda514772016-02-01 11:32:01 +0100689 *
690 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100691 * @param[in] name Arbitrary certificate name.
Michal Vaskoda514772016-02-01 11:32:01 +0100692 * @return 0 on success, -1 on error.
693 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100694int nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name);
Michal Vaskoda514772016-02-01 11:32:01 +0100695
696/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100697 * @brief Set the callback for retrieving server certificate and matching private key.
Michal Vaskoda514772016-02-01 11:32:01 +0100698 *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100699 * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success,
Michal Vaskoc446a382021-06-18 08:54:05 +0200700 * non-zero an error. On success exactly ONE of @p cert_path or @p cert_data and ONE of
701 * @p privkey_path and @p privkey_data is expected to be set. Those set will be freed.
702 * - @p cert_path expects a PEM file,
703 * - @p cert_data expects a base-64 encoded ASN.1 DER data,
704 * - @p privkey_path expects a PEM file,
705 * - @p privkey_data expects a base-64 encoded ANS.1 DER data,
706 * - @p privkey_type type of the key in @p privkey_data.
707 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
708 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Michal Vaskoda514772016-02-01 11:32:01 +0100709 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100710void nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200711 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
712 void (*free_user_data)(void *user_data));
Michal Vaskoda514772016-02-01 11:32:01 +0100713
714/**
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500715 * @brief Set the callback for retrieving server certificate chain
716 *
717 * @param[in] cert_chain_clb Callback that should return all the certificates of the chain. Zero return indicates success,
Michal Vaskoc446a382021-06-18 08:54:05 +0200718 * non-zero an error. On success, @p cert_paths and @p cert_data are expected to be set or left
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500719 * NULL. Both will be (deeply) freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200720 * - @p cert_paths expect an array of PEM files,
721 * - @p cert_path_count number of @p cert_paths array members,
722 * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
723 * - @p cert_data_count number of @p cert_data array members.
724 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
725 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500726 */
727void nc_server_tls_set_server_cert_chain_clb(int (*cert_chain_clb)(const char *name, void *user_data, char ***cert_paths,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200728 int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data));
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500729
730/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100731 * @brief Add a trusted certificate list. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100732 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100733 *
Michal Vaskoda514772016-02-01 11:32:01 +0100734 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100735 * @param[in] name Arbitary name identifying this certificate list.
Michal Vasko1a38c862016-01-15 15:50:07 +0100736 * @return 0 on success, -1 on error.
737 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100738int nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100739
Michal Vasko1a38c862016-01-15 15:50:07 +0100740/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100741 * @brief Set the callback for retrieving trusted certificates.
742 *
743 * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success,
Michal Vaskoc446a382021-06-18 08:54:05 +0200744 * non-zero an error. On success, @p cert_paths and @p cert_data are expected to be set or left
Michal Vasko4c1fb492017-01-30 14:31:07 +0100745 * NULL. Both will be (deeply) freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200746 * - @p cert_paths expect an array of PEM files,
747 * - @p cert_path_count number of @p cert_paths array members,
748 * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
749 * - @p cert_data_count number of @p cert_data array members.
750 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
751 * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100752 */
753void nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200754 int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data));
Michal Vasko4c1fb492017-01-30 14:31:07 +0100755
756/**
757 * @brief Remove a trusted certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100758 *
Michal Vaskoda514772016-02-01 11:32:01 +0100759 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100760 * @param[in] name Name of the certificate list to delete. NULL deletes all the lists.
761 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100762 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100763int nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100764
Michal Vasko1a38c862016-01-15 15:50:07 +0100765/**
766 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100767 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100768 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100769 *
Michal Vaskoda514772016-02-01 11:32:01 +0100770 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100771 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
772 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
773 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100774 * @return 0 on success, -1 on error.
775 */
Michal Vasko96830e32016-02-01 10:54:18 +0100776int 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 +0100777
Michal Vasko1a38c862016-01-15 15:50:07 +0100778/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100779 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100780 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100781 *
Michal Vaskoda514772016-02-01 11:32:01 +0100782 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100783 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
784 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
785 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100786 * @return 0 on success, -1 on error.
787 */
Michal Vasko96830e32016-02-01 10:54:18 +0100788int 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 +0100789
Michal Vasko1a38c862016-01-15 15:50:07 +0100790/**
Michal Vasko96830e32016-02-01 10:54:18 +0100791 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100792 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100793 *
794 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100795 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100796void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100797
Michal Vasko1a38c862016-01-15 15:50:07 +0100798/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100799 * @brief Add a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100800 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200801 * It is possible to add an entry step-by-step, specifying first only @p ip and in later calls
802 * @p fingerprint, @p map_type, and optionally @p name spearately.
Michal Vasko1a38c862016-01-15 15:50:07 +0100803 *
Michal Vaskoda514772016-02-01 11:32:01 +0100804 * @param[in] endpt_name Existing endpoint name.
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100805 * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id
806 * is modified.
807 * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset.
808 * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset.
Michal Vaskoc446a382021-06-18 08:54:05 +0200809 * @param[in] name Specific username used only if @p map_type == NC_TLS_CTN_SPECIFED.
Michal Vasko1a38c862016-01-15 15:50:07 +0100810 * @return 0 on success, -1 on error.
811 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200812int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200813 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100814
Michal Vasko1a38c862016-01-15 15:50:07 +0100815/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100816 * @brief Remove a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100817 *
Michal Vaskoda514772016-02-01 11:32:01 +0100818 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100819 * @param[in] id Priority of the entry. -1 matches all the priorities.
820 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
821 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
822 * @param[in] name Specific username for the entry. NULL matches all the usernames.
823 * @return 0 on success, -1 on not finding any match.
824 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200825int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200826 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100827
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100828/**
829 * @brief Get a cert-to-name entry.
830 *
831 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
832 * it is filled and returned. If the value is set, it is used as a filter.
833 * Returns first matching entry.
834 *
835 * @param[in] endpt_name Existing endpoint name.
836 * @param[in,out] id Priority of the entry.
837 * @param[in,out] fingerprint Fingerprint fo the entry.
838 * @param[in,out] map_type Mapping type of the entry.
839 * @param[in,out] name Specific username for the entry.
840 * @return 0 on success, -1 on not finding any match.
841 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100842int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200843 char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100844
Michal Vasko7060bcf2016-11-28 14:48:11 +0100845/**
Michal Vasko709598e2016-11-28 14:48:32 +0100846 * @brief Get client certificate.
847 *
848 * @param[in] session Session to get the information from.
849 * @return Const session client certificate.
850 */
851const X509 *nc_session_get_client_cert(const struct nc_session *session);
852
853/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100854 * @brief Set TLS authentication additional verify callback.
855 *
856 * Server will always perform cert-to-name based on its configuration. Only after it passes
857 * and this callback is set, it is also called. It should return exactly what OpenSSL
858 * verify callback meaning 1 for success, 0 to deny the user.
859 *
860 * @param[in] verify_clb Additional user verify callback.
861 */
862void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
863
Michal Vaskoc446a382021-06-18 08:54:05 +0200864/** @} Server TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200865
Radek Krejci53691be2016-02-22 13:58:37 +0100866#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100867
Michal Vaskof8352352016-05-24 09:11:36 +0200868/**
Radek Krejci6799a052017-05-19 14:23:23 +0200869 * @addtogroup server_session
870 * @{
871 */
872
873/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200874 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200875 *
876 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200877 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200878 */
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200879time_t nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200880
Michal Vasko3486a7c2017-03-03 13:28:07 +0100881/**
Michal Vasko71dbd772021-03-23 14:08:37 +0100882 * @brief Increase session notification subscription flag count.
883 * Supports multiple subscriptions on one session.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100884 *
885 * It is used only to ignore timeouts, because they are
886 * ignored for sessions with active subscriptions.
887 *
888 * @param[in] session Session to modify.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100889 */
Michal Vasko71dbd772021-03-23 14:08:37 +0100890void nc_session_inc_notif_status(struct nc_session *session);
891
892/**
893 * @brief Decrease session notification subscription flag count.
894 * Supports multiple subscriptions on one session.
895 *
896 * @param[in] session Session to modify.
897 */
898void nc_session_dec_notif_status(struct nc_session *session);
Michal Vasko3486a7c2017-03-03 13:28:07 +0100899
900/**
901 * @brief Get session notification subscription flag.
902 *
903 * @param[in] session Session to get the information from.
904 * @return 0 for no active subscription, non-zero for an active subscription.
905 */
906int nc_session_get_notif_status(const struct nc_session *session);
907
Michal Vasko8f430592019-02-26 08:32:54 +0100908/**
909 * @brief Learn whether a session was created using Call Home or not.
910 * Works only for server sessions.
911 *
912 * @param[in] session Session to get the information from.
913 * @return 0 if a standard session, non-zero if a Call Home session.
914 */
915int nc_session_is_callhome(const struct nc_session *session);
916
Michal Vaskoc446a382021-06-18 08:54:05 +0200917/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200918
Michal Vaskoc09730e2019-01-17 10:07:26 +0100919#ifdef __cplusplus
920}
921#endif
922
Michal Vasko086311b2016-01-08 09:53:11 +0100923#endif /* NC_SESSION_SERVER_H_ */