blob: 612bb929e8ac37ae78168c10b12e2940c90dc361 [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 Vasko95ea9ff2021-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 Vaskocca9ba62021-11-25 13:26:17 +010025#include <sys/types.h>
Michal Vasko086311b2016-01-08 09:53:11 +010026
roman2eab4742023-06-06 10:00:26 +020027#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +020028# include <openssl/x509.h>
Michal Vasko709598e2016-11-28 14:48:32 +010029
Michal Vaskoc446a382021-06-18 08:54:05 +020030# include <libssh/callbacks.h>
31# include <libssh/libssh.h>
32# include <libssh/server.h>
roman2eab4742023-06-06 10:00:26 +020033#endif /* NC_ENABLED_SSH_TLS */
bhart3bc2f582018-06-05 12:40:32 -050034
Michal Vasko086311b2016-01-08 09:53:11 +010035#include "netconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020036#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037
Michal Vasko1a38c862016-01-15 15:50:07 +010038/**
Radek Krejci6799a052017-05-19 14:23:23 +020039 * @defgroup server_session Server Session
40 * @ingroup server
41 *
42 * @brief Server-side NETCONF session manipulation.
43 * @{
44 */
45
46/**
Michal Vasko1a38c862016-01-15 15:50:07 +010047 * @brief Prototype of callbacks that are called if some RPCs are received.
48 *
Michal Vaskoc446a382021-06-18 08:54:05 +020049 * If @p session termination reason is changed in the callback, one last reply
Michal Vasko1a38c862016-01-15 15:50:07 +010050 * is sent and then the session is considered invalid.
51 *
Radek Krejci6799a052017-05-19 14:23:23 +020052 * The callback is set via nc_set_global_rpc_clb().
53 *
Michal Vasko1a38c862016-01-15 15:50:07 +010054 * @param[in] rpc Parsed client RPC request.
55 * @param[in] session Session the RPC arrived on.
56 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
57 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010058typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
59
Michal Vasko1a38c862016-01-15 15:50:07 +010060/**
Michal Vasko7b096242016-01-29 15:55:10 +010061 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010062 *
63 * @param[in] session Session to modify.
64 * @param[in] reason Reason of termination.
65 */
66void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
67
68/**
Michal Vasko142cfea2017-08-07 10:12:11 +020069 * @brief Set the session-id of the session responsible for this session's termination.
70 *
71 * @param[in] session Session to modify. Must have term_reason set to #NC_SESSION_TERM_KILLED.
72 * @param[in] sid SID of the killing session.
73 */
74void nc_session_set_killed_by(struct nc_session *session, uint32_t sid);
75
76/**
77 * @brief Set the status of a session.
78 *
79 * @param[in] session Session to modify.
80 * @param[in] status Status of the session.
81 */
82void nc_session_set_status(struct nc_session *session, NC_STATUS status);
83
84/**
Radek Krejci6799a052017-05-19 14:23:23 +020085 * @brief Set a global nc_rpc_clb that is called if the particular RPC request is
86 * received and the private field in the corresponding RPC schema node is NULL.
87 *
Michal Vasko238b6c12021-12-14 15:14:09 +010088 * If this callback is set, the default callbacks for "get-schema" and "close-session" are not used.
89 *
Radek Krejci6799a052017-05-19 14:23:23 +020090 * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default.
91 */
92void nc_set_global_rpc_clb(nc_rpc_clb clb);
93
Michal Vasko238b6c12021-12-14 15:14:09 +010094/**
95 * @brief Default RPC callback used for "ietf-netconf-monitoring:get-schema" RPC if no other specific
96 * or global callback is set.
97 *
98 * @param[in] rpc Received RPC.
99 * @param[in] session NC session @p rpc was received on.
100 * @return Server reply.
101 */
102struct nc_server_reply *nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session);
103
104/**
105 * @brief Default RPC callback used for "ietf-netconf:close-session" RPC if no other specific
106 * or global callback is set.
107 *
108 * @param[in] rpc Received RPC.
109 * @param[in] session NC session @p rpc was received on.
110 * @return Server reply.
111 */
112struct nc_server_reply *nc_clb_default_close_session(struct lyd_node *rpc, struct nc_session *session);
113
Michal Vasko4e6d3242021-05-26 09:13:24 +0200114/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200115
116/**
117 * @addtogroup server
118 * @{
119 */
120
121/**
Michal Vasko93224072021-11-09 12:14:28 +0100122 * @brief Initialize libssh and/or libssl/libcrypto and the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100123 *
Michal Vasko93224072021-11-09 12:14:28 +0100124 * Must be called before other nc_server* functions.
Michal Vasko1a38c862016-01-15 15:50:07 +0100125 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100126 * @return 0 on success, -1 on error.
127 */
Michal Vasko93224072021-11-09 12:14:28 +0100128int nc_server_init(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100129
Michal Vasko1a38c862016-01-15 15:50:07 +0100130/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100131 * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
132 * server resources.
Michal Vaskob48aa812016-01-18 14:13:09 +0100133 */
134void nc_server_destroy(void);
135
136/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100137 * @brief Set the with-defaults capability extra parameters.
138 *
139 * For the capability to be actually advertised, the server context must also
140 * include the ietf-netconf-with-defaults model.
141 *
142 * Changing this option has the same ill effects as changing capabilities while
143 * sessions are already established.
144 *
145 * @param[in] basic_mode basic-mode with-defaults parameter.
146 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
147 * parameter.
148 * @return 0 on success, -1 on error.
149 */
Michal Vasko086311b2016-01-08 09:53:11 +0100150int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
151
Michal Vasko1a38c862016-01-15 15:50:07 +0100152/**
Michal Vasko55f03972016-04-13 08:56:01 +0200153 * @brief Get with-defaults capability extra parameters.
154 *
155 * At least one argument must be non-NULL.
156 *
157 * @param[in,out] basic_mode basic-mode parameter.
158 * @param[in,out] also_supported also-supported parameter.
159 */
160void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
161
162/**
Radek Krejci658782b2016-12-04 22:04:55 +0100163 * @brief Set capability of the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100164 *
Radek Krejci658782b2016-12-04 22:04:55 +0100165 * Capability can be used when some behavior or extension of the server is not defined
166 * as a YANG module. The provided value will be advertised in the server's \<hello\>
167 * messages. Note, that libnetconf only checks that the provided value is non-empty
168 * string.
Michal Vasko1a38c862016-01-15 15:50:07 +0100169 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100170 * @param[in] value Capability string to be advertised in server's \<hello\> messages.
Michal Vasko1a38c862016-01-15 15:50:07 +0100171 */
Radek Krejci658782b2016-12-04 22:04:55 +0100172int nc_server_set_capability(const char *value);
Michal Vasko55f03972016-04-13 08:56:01 +0200173
174/**
Michal Vasko1440a742021-03-31 11:11:03 +0200175 * @brief Set the callback for getting yang-library capability identifier. If none is set, libyang context change count is used.
176 *
177 * @param[in] content_id_clb Callback that should return the yang-library content identifier.
Michal Vaskoc446a382021-06-18 08:54:05 +0200178 * @param[in] user_data Optional arbitrary user data that will be passed to @p content_id_clb.
179 * @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 +0200180 */
181void nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data,
182 void (*free_user_data)(void *user_data));
183
184/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100185 * @brief Set server timeout for receiving a hello message.
186 *
187 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
188 */
189void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100190
Michal Vasko1a38c862016-01-15 15:50:07 +0100191/**
Michal Vasko55f03972016-04-13 08:56:01 +0200192 * @brief get server timeout for receiving a hello message.
193 *
194 * @return Hello message timeout, 0 is infinite.
195 */
196uint16_t nc_server_get_hello_timeout(void);
197
198/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100199 * @brief Set server timeout for dropping an idle session.
200 *
201 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100202 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100203 */
204void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100205
Michal Vasko1a38c862016-01-15 15:50:07 +0100206/**
Michal Vasko55f03972016-04-13 08:56:01 +0200207 * @brief Get server timeout for dropping an idle session.
208 *
209 * @return Idle session timeout, 0 for for never dropping
210 * a session because of inactivity.
211 */
212uint16_t nc_server_get_idle_timeout(void);
213
214/**
Radek Krejci24a18412018-05-16 15:09:10 +0200215 * @brief Get all the server capabilities including all the schemas.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200216 *
217 * A few capabilities (with-defaults, interleave) depend on the current
218 * server options.
219 *
220 * @param[in] ctx Context to read most capabilities from.
Michal Vasko93224072021-11-09 12:14:28 +0100221 * @return Array of capabilities, NULL on error.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200222 */
Michal Vasko93224072021-11-09 12:14:28 +0100223char **nc_server_get_cpblts(const struct ly_ctx *ctx);
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200224
Radek Krejci24a18412018-05-16 15:09:10 +0200225/**
226 * @brief Get the server capabilities including the schemas with the specified YANG version.
227 *
228 * A few capabilities (with-defaults, interleave) depend on the current
229 * server options.
230 *
231 * @param[in] ctx Context to read most capabilities from.
232 * @param[in] version YANG version of the schemas to be included in result, with
233 * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts().
Michal Vasko93224072021-11-09 12:14:28 +0100234 * @return Array of capabilities, NULL on error.
Radek Krejci24a18412018-05-16 15:09:10 +0200235 */
Michal Vasko93224072021-11-09 12:14:28 +0100236char **nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version);
Radek Krejci24a18412018-05-16 15:09:10 +0200237
Michal Vasko1440a742021-03-31 11:11:03 +0200238/** @} Server */
Radek Krejci6799a052017-05-19 14:23:23 +0200239
240/**
241 * @addtogroup server_session
242 * @{
243 */
244
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200245/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100246 * @brief Accept a new session on a pre-established transport session.
247 *
Michal Vasko93224072021-11-09 12:14:28 +0100248 * For detailed description, look at ::nc_accept().
249 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100250 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
251 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
252 * @param[in] username NETCONF username as provided by the transport protocol.
Michal Vasko93224072021-11-09 12:14:28 +0100253 * @param[in] ctx Context for the session to use.
Michal Vasko1a38c862016-01-15 15:50:07 +0100254 * @param[out] session New session on success.
Michal Vasko71090fc2016-05-24 16:37:28 +0200255 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
256 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100257 */
Michal Vasko93224072021-11-09 12:14:28 +0100258NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx,
259 struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100260
Michal Vasko1a38c862016-01-15 15:50:07 +0100261/**
262 * @brief Create an empty structure for polling sessions.
263 *
264 * @return Empty pollsession structure, NULL on error.
265 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100266struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100267
Michal Vasko1a38c862016-01-15 15:50:07 +0100268/**
269 * @brief Free a pollsession structure.
270 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200271 * !IMPORTANT! Make sure that @p ps is not accessible (is not used)
Michal Vasko01082bf2016-04-07 10:44:21 +0200272 * by any thread before and after this call!
273 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100274 * @param[in] ps Pollsession structure to free.
275 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100276void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100277
Michal Vasko1a38c862016-01-15 15:50:07 +0100278/**
279 * @brief Add a session to a pollsession structure.
280 *
281 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200282 * @param[in] session Session to add to @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100283 * @return 0 on success, -1 on error.
284 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100285int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100286
Michal Vasko1a38c862016-01-15 15:50:07 +0100287/**
288 * @brief Remove a session from a pollsession structure.
289 *
290 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200291 * @param[in] session Session to remove from @p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100292 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100293 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100294int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
295
Michal Vasko1a38c862016-01-15 15:50:07 +0100296/**
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100297 * @brief Get a session from a pollsession structure matching the session ID.
298 *
299 * @param[in] ps Pollsession structure to read from.
Michal Vasko4871c9d2017-10-09 14:48:39 +0200300 * @param[in] idx Index of the session.
301 * @return Session on index, NULL if out-of-bounds.
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100302 */
Michal Vasko4871c9d2017-10-09 14:48:39 +0200303struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx);
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100304
305/**
Michal Vasko3ec3b112022-07-21 12:32:33 +0200306 * @brief Callback for finding a session in a pollsession structure.
307 *
308 * @param[in] session Considered NETCONF session.
309 * @param[in] cb_data User data.
310 * @return 0 if the session does not match.
311 * @return non-zero if the session matches and should be returned.
312 */
313typedef int (*nc_ps_session_match_cb)(struct nc_session *session, void *cb_data);
314
315/**
316 * @brief Find a session in a pollsession structure using a matching callback.
317 *
318 * @param[in] ps Pollsession structure to read from.
319 * @param[in] match_cb Matching callback to use.
320 * @param[in] cb_data User data passed to @p cb.
321 * @return Found session, NULL if none matched.
322 */
323struct nc_session *nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data);
324
325/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100326 * @brief Learn the number of sessions in a pollsession structure.
327 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200328 * Does not lock @p ps structure for efficiency.
Michal Vaskof4462fd2017-02-15 14:29:05 +0100329 *
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100330 * @param[in] ps Pollsession structure to check.
Michal Vaskoc446a382021-06-18 08:54:05 +0200331 * @return Number of sessions (even invalid ones) in @p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100332 */
333uint16_t nc_ps_session_count(struct nc_pollsession *ps);
334
Michal Vasko30a5d6b2017-02-15 14:29:39 +0100335#define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */
336#define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */
337#define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */
338#define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */
339#define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */
340#define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */
341#define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
342#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200343
roman2eab4742023-06-06 10:00:26 +0200344#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +0200345# define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */
346# define NC_PSPOLL_SSH_CHANNEL 0x0200 /**< New SSH channel opened on an existing session (only with SSH support). */
roman2eab4742023-06-06 10:00:26 +0200347#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko71090fc2016-05-24 16:37:28 +0200348
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100349/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100350 * @brief Poll sessions and process any received RPCs.
351 *
Michal Vaskoe4300a82017-05-24 10:35:42 +0200352 * Only one event on one session is handled in one function call. If this event
353 * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session
Michal Vaskoc446a382021-06-18 08:54:05 +0200354 * should be removed from @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100355 *
356 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100357 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100358 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200359 * @param[in] session Session that was processed and that specific return bits concern.
360 * Can be NULL.
Michal Vaskoade892d2017-02-22 13:40:35 +0100361 * @return Bitfield of NC_PSPOLL_* macros.
Michal Vasko1a38c862016-01-15 15:50:07 +0100362 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200363int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100364
Michal Vasko11d142a2016-01-19 15:58:24 +0100365/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100366 * @brief Remove sessions from a pollsession structure and
367 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100368 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200369 * 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 +0100370 *
371 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100372 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100373 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100374 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100375void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100376
Michal Vasko1440a742021-03-31 11:11:03 +0200377/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200378
379/**
380 * @addtogroup server
381 * @{
382 */
383
Michal Vasko1a38c862016-01-15 15:50:07 +0100384/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200385 * @brief Add a new endpoint.
386 *
387 * Before the endpoint can accept any connections, its address and port must
Radek Krejci6799a052017-05-19 14:23:23 +0200388 * be set via nc_server_endpt_set_address() and nc_server_endpt_set_port().
Michal Vaskoe2713da2016-08-22 16:06:40 +0200389 *
390 * @param[in] name Arbitrary unique endpoint name.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200391 * @param[in] ti Transport protocol to use.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200392 * @return 0 on success, -1 on error.
393 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200394int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200395
396/**
397 * @brief Stop listening on and remove an endpoint.
398 *
399 * @param[in] name Endpoint name. NULL matches all endpoints.
Michal Vasko59050372016-11-22 14:33:55 +0100400 * @param[in] ti Endpoint transport protocol. NULL matches any protocol.
Michal Vaskoc446a382021-06-18 08:54:05 +0200401 * Redundant to set if @p name is set, endpoint names are
Michal Vasko59050372016-11-22 14:33:55 +0100402 * unique disregarding their protocol.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200403 * @return 0 on success, -1 on not finding any match.
404 */
Michal Vasko59050372016-11-22 14:33:55 +0100405int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200406
407/**
Michal Vaskoe8e07702017-03-15 10:19:30 +0100408 * @brief Get the number of currently configured listening endpoints.
409 * Note that an ednpoint without address and/or port will be included
410 * even though it is not, in fact, listening.
411 *
412 * @return Number of added listening endpoints.
413 */
414int nc_server_endpt_count(void);
415
416/**
Michal Vasko1b5973e2020-01-30 16:05:46 +0100417 * @brief Check if an endpoint exists.
418 *
419 * @param[in] name Endpoint name.
420 * @return 0 if does not exists, non-zero otherwise.
421 */
422int nc_server_is_endpt(const char *name);
423
424/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200425 * @brief Change endpoint listening address.
426 *
427 * On error the previous listening socket (if any) is left untouched.
428 *
429 * @param[in] endpt_name Existing endpoint name.
430 * @param[in] address New listening address.
431 * @return 0 on success, -1 on error.
432 */
433int nc_server_endpt_set_address(const char *endpt_name, const char *address);
434
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200435#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko946cacb2020-08-12 11:18:08 +0200436
Michal Vasko2e6defd2016-10-07 15:48:15 +0200437/**
438 * @brief Change endpoint listening port.
439 *
Michal Vasko946cacb2020-08-12 11:18:08 +0200440 * This is only valid on SSH/TLS transport endpoint.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200441 * On error the previous listening socket (if any) is left untouched.
442 *
443 * @param[in] endpt_name Existing endpoint name.
444 * @param[in] port New listening port.
445 * @return 0 on success, -1 on error.
446 */
447int nc_server_endpt_set_port(const char *endpt_name, uint16_t port);
Michal Vasko9e036d52016-01-08 10:49:26 +0100448
Michal Vasko946cacb2020-08-12 11:18:08 +0200449#endif
450
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200451/**
452 * @brief Change endpoint permissions.
453 *
Michal Vasko946cacb2020-08-12 11:18:08 +0200454 * This is only valid on UNIX transport endpoint.
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200455 * On error the previous listening socket (if any) is left untouched.
456 *
457 * @param[in] endpt_name Existing endpoint name.
458 * @param[in] mode New mode, -1 to use default.
459 * @param[in] uid New uid, -1 to use default.
460 * @param[in] gid New gid, -1 to use default.
461 * @return 0 on success, -1 on error.
462 */
463int nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid);
464
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200465/**
466 * @brief Change endpoint keepalives state. Affects only new connections.
467 *
468 * @param[in] endpt_name Existing endpoint name.
469 * @param[in] enable Whether to enable or disable keepalives.
470 * @return 0 on success, -1 on error.
471 */
472int nc_server_endpt_enable_keepalives(const char *endpt_name, int enable);
473
474/**
475 * @brief Change endpoint keepalives parameters. Affects only new connections.
476 *
477 * @param[in] endpt_name Existing endpoint name.
478 * @param[in] idle_time Keepalive idle time in seconds, 1 by default, -1 to keep previous value.
479 * @param[in] max_probes Keepalive max probes sent, 10 by default, -1 to keep previous value.
480 * @param[in] probe_interval Keepalive probe interval in seconds, 5 by default, -1 to keep previous value.
481 * @return 0 on success, -1 on error.
482 */
483int nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval);
484
Michal Vasko1440a742021-03-31 11:11:03 +0200485/** @} Server */
Radek Krejci6799a052017-05-19 14:23:23 +0200486
487/**
488 * @addtogroup server_session
Michal Vasko4e6d3242021-05-26 09:13:24 +0200489 * @{
Radek Krejci6799a052017-05-19 14:23:23 +0200490 */
491
Michal Vasko1a38c862016-01-15 15:50:07 +0100492/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100493 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100494 *
Michal Vaskob70c8b82017-03-17 09:09:29 +0100495 * Once a new (TCP/IP) conection is established a different (quite long) timeout
496 * is used for waiting for transport-related data, which means this call can block
Michal Vaskoc446a382021-06-18 08:54:05 +0200497 * for much longer that @p timeout, but only with slow/faulty/malicious clients.
Michal Vaskob70c8b82017-03-17 09:09:29 +0100498 *
Michal Vasko93224072021-11-09 12:14:28 +0100499 * Server capabilities are generated based on the content of @p ctx. The context must
500 * not be destroyed before the accepted NETCONF session is freed.
501 *
502 * Supported RPCs of models in the context are expected to have their callback
503 * in the corresponding RPC schema node set to a nc_rpc_clb function callback using ::nc_set_rpc_callback().
504 * This callback is called by ::nc_ps_poll() if the particular RPC request is
505 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
506 * only) and ietf-netconf:close-session are set internally if left unset.
507 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100508 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko93224072021-11-09 12:14:28 +0100509 * non-blocking call, -1 for infinite waiting.
510 * @param[in] ctx Context for the session to use.
Michal Vasko96164bf2016-01-21 15:41:58 +0100511 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200512 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
513 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100514 */
Michal Vasko93224072021-11-09 12:14:28 +0100515NC_MSG_TYPE nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100516
roman2eab4742023-06-06 10:00:26 +0200517#ifdef NC_ENABLED_SSH_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100518
Michal Vasko1a38c862016-01-15 15:50:07 +0100519/**
Michal Vaskoc446a382021-06-18 08:54:05 +0200520 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF @p orig_session.
521 * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on @p orig_session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200522 *
523 * @param[in] orig_session Session that has a new SSH channel ready.
524 * @param[out] session New session.
525 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
526 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
527 */
528NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
529
530/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100531 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskoc446a382021-06-18 08:54:05 +0200532 * that was polled in @p ps. Call this function only when nc_ps_poll() on @p ps returns #NC_PSPOLL_SSH_CHANNEL.
533 * The new session is only returned in @p session, it is not added to @p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100534 *
535 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
536 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200537 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
538 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100539 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200540NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100541
Michal Vasko1440a742021-03-31 11:11:03 +0200542/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200543
544/**
545 * @defgroup server_ssh Server SSH
546 * @ingroup server
547 *
548 * @brief Server-side settings for SSH connections.
549 * @{
550 */
551
Michal Vasko96164bf2016-01-21 15:41:58 +0100552/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100553 * @brief Add an authorized client SSH public key. This public key can be used for
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200554 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100555 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100556 * @param[in] pubkey_base64 Authorized public key binary content encoded in base64.
557 * @param[in] type Authorized public key SSH type.
558 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100559 * @return 0 on success, -1 on error.
560 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100561int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100562
563/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100564 * @brief Add an authorized client SSH public key. This public key can be used for
565 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100566 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100567 * @param[in] pubkey_path Path to the public key.
568 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100569 * @return 0 on success, -1 on error.
570 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100571int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100572
573/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100574 * @brief Remove an authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100575 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100576 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
577 * @param[in] pubkey_base64 Authorized public key content. NULL matches any key.
578 * @param[in] type Authorized public key type. 0 matches all types.
579 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
580 * @return 0 on success, -1 on not finding any match.
Michal Vasko1a38c862016-01-15 15:50:07 +0100581 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100582int 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 +0200583 const char *username);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200584
585/**
Michal Vaskoebba7602018-03-23 13:14:08 +0100586 * @brief Set the callback for SSH password authentication. If none is set, local system users are used.
587 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200588 * @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 +0200589 * Zero return indicates success, non-zero an error.
Michal Vaskoc446a382021-06-18 08:54:05 +0200590 * @param[in] user_data Optional arbitrary user data that will be passed to @p passwd_auth_clb.
591 * @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 +0100592 */
593void 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 +0200594 void *user_data), void *user_data, void (*free_user_data)(void *user_data));
Michal Vaskoebba7602018-03-23 13:14:08 +0100595
596/**
Michal Vasko1c2d2652023-10-17 08:53:36 +0200597 * @brief Set the callback for SSH interactive authentication. If none is set, local PAM-based authentication is used.
bhart3bc2f582018-06-05 12:40:32 -0500598 *
Michal Vasko1c2d2652023-10-17 08:53:36 +0200599 * @param[in] interactive_auth_sess_clb Callback that should authenticate the user.
Michal Vasko1440a742021-03-31 11:11:03 +0200600 * Zero return indicates success, non-zero an error.
Michal Vasko1c2d2652023-10-17 08:53:36 +0200601 * @param[in] user_data Optional arbitrary user data that will be passed to @p interactive_auth_sess_clb.
Michal Vaskoc446a382021-06-18 08:54:05 +0200602 * @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 -0500603 */
Michal Vasko1c2d2652023-10-17 08:53:36 +0200604void nc_server_ssh_set_interactive_auth_sess_clb(int (*interactive_auth_sess_clb)(const struct nc_session *session,
605 ssh_session ssh_sess, ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data));
606
607/**
608 * @brief Deprecated, use ::nc_server_ssh_set_interactive_auth_sess_clb() instead.
609 */
Michal Vasko1440a742021-03-31 11:11:03 +0200610void nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session,
611 const ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data));
bhart3bc2f582018-06-05 12:40:32 -0500612
613/**
roman41a11e42022-06-22 09:27:08 +0200614 * @brief Set the name and a path to a PAM configuration file.
615 *
Michal Vasko1c2d2652023-10-17 08:53:36 +0200616 * @p conf_name has to be set via this function prior to using PAM keyboard-interactive authentication method.
roman41a11e42022-06-22 09:27:08 +0200617 *
618 * @param[in] conf_name Name of the configuration file.
619 * @param[in] conf_dir Optional. The absolute path to the directory in which the configuration file
620 * with the name @p conf_name is located. A newer version (>= 1.4) of PAM library is required to be
621 * able to specify the path. If NULL is passed,
622 * then the PAM's system directories will be searched (usually /etc/pam.d/).
623 * @return 0 on success, -1 on error.
624 */
625int nc_server_ssh_set_pam_conf_path(const char *conf_name, const char *conf_dir);
626
627/**
bhart3bc2f582018-06-05 12:40:32 -0500628 * @brief Set the callback for SSH public key authentication. If none is set, local system users are used.
629 *
630 * @param[in] pubkey_auth_clb Callback that should authenticate the user.
Michal Vasko1440a742021-03-31 11:11:03 +0200631 * Zero return indicates success, non-zero an error.
roman41a11e42022-06-22 09:27:08 +0200632 * @param[in] user_data Optional arbitrary user data that will be passed to @p pubkey_auth_clb.
Michal Vaskoc446a382021-06-18 08:54:05 +0200633 * @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 -0500634 */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200635void 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 +0200636 void *user_data), void *user_data, void (*free_user_data)(void *user_data));
bhart3bc2f582018-06-05 12:40:32 -0500637
638/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100639 * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However,
640 * a maximum of one key of each type will be used during SSH authentication, later keys replacing
641 * the earlier ones.
642 *
643 * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero
Michal Vaskoc446a382021-06-18 08:54:05 +0200644 * an error. On success exactly ONE of @p privkey_path or @p privkey_data is expected
Michal Vasko4c1fb492017-01-30 14:31:07 +0100645 * to be set. The one set will be freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200646 * - @p privkey_path expects a PEM file,
647 * - @p privkey_data expects a base-64 encoded ANS.1 DER data,
648 * - @p privkey_type type of the key in @p privkey_data. Use ::NC_SSH_KEY_UNKNOWN for
Michal Vasko68177b72020-04-27 15:46:53 +0200649 * PKCS#8 key that includes the information about the key in its data.
Michal Vaskoc446a382021-06-18 08:54:05 +0200650 * @param[in] user_data Optional arbitrary user data that will be passed to @p hostkey_clb.
651 * @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 +0100652 */
653void 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 +0200654 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 +0100655
656/**
Michal Vaskoddce1212019-05-24 09:58:49 +0200657 * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself
658 * wil be retrieved using a callback.
659 *
660 * @param[in] endpt_name Existing endpoint name.
661 * @param[in] name Arbitrary name of the host key.
662 * @param[in] idx Optional index where to add the key. -1 adds at the end.
663 * @return 0 on success, -1 on error.
664 */
665int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx);
666
667/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100668 * @brief Delete endpoint SSH host key. Their order is preserved.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200669 *
670 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoc446a382021-06-18 08:54:05 +0200671 * @param[in] name Name of the host key. NULL matches all the keys, but if @p idx != -1 then this must be NULL.
672 * @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 +0200673 * @return 0 on success, -1 on error.
674 */
Michal Vasko7d255882017-02-09 13:35:08 +0100675int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vasko086311b2016-01-08 09:53:11 +0100676
Michal Vasko1a38c862016-01-15 15:50:07 +0100677/**
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100678 * @brief Move endpoint SSH host key.
679 *
680 * @param[in] endpt_name Exisitng endpoint name.
681 * @param[in] key_mov Name of the host key that will be moved.
Michal Vaskoc446a382021-06-18 08:54:05 +0200682 * @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.
roman41a11e42022-06-22 09:27:08 +0200683 * @return 0 on success, -1 on error.
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100684 */
685int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after);
686
687/**
688 * @brief Modify endpoint SSH host key.
689 *
690 * @param[in] endpt_name Exisitng endpoint name.
691 * @param[in] name Name of an existing host key.
Michal Vaskoc446a382021-06-18 08:54:05 +0200692 * @param[in] new_name New name of the host key @p name.
roman41a11e42022-06-22 09:27:08 +0200693 * @return 0 on success, -1 on error.
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100694 */
695int nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100696
Michal Vasko1a38c862016-01-15 15:50:07 +0100697/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100698 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100699 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100700 *
Michal Vaskoda514772016-02-01 11:32:01 +0100701 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100702 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
703 * @return 0 on success, -1 on error.
704 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100705int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100706
Michal Vasko1a38c862016-01-15 15:50:07 +0100707/**
Michal Vaskoddce1212019-05-24 09:58:49 +0200708 * @brief Get endpoint accepted SSH authentication methods.
709 *
710 * @param[in] endpt_name Existing endpoint name.
711 * @return Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
712 */
713int nc_server_ssh_endpt_get_auth_methods(const char *endpt_name);
714
715/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100716 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100717 *
Michal Vaskoda514772016-02-01 11:32:01 +0100718 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100719 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100720 * @return 0 on success, -1 on error.
721 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100722int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100723
Michal Vasko1a38c862016-01-15 15:50:07 +0100724/**
Michal Vaskocbad4c52019-06-27 16:30:35 +0200725 * @brief Set endpoint SSH authentication timeout. 30 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100726 *
Michal Vaskoda514772016-02-01 11:32:01 +0100727 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100728 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
729 * @return 0 on success, -1 on error.
730 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100731int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100732
Michal Vaskoc446a382021-06-18 08:54:05 +0200733/** @} Server SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200734
Michal Vasko1a38c862016-01-15 15:50:07 +0100735/**
Radek Krejci6799a052017-05-19 14:23:23 +0200736 * @defgroup server_tls Server TLS
737 * @ingroup server
738 *
739 * @brief Server-side settings for TLS connections.
740 * @{
741 */
742
743/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100744 * @brief Set the server TLS certificate. Only the name is set, the certificate itself
745 * wil be retrieved using a callback.
Michal Vaskoda514772016-02-01 11:32:01 +0100746 *
747 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100748 * @param[in] name Arbitrary certificate name.
Michal Vaskoda514772016-02-01 11:32:01 +0100749 * @return 0 on success, -1 on error.
750 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100751int nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name);
Michal Vaskoda514772016-02-01 11:32:01 +0100752
753/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100754 * @brief Set the callback for retrieving server certificate and matching private key.
Michal Vaskoda514772016-02-01 11:32:01 +0100755 *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100756 * @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 +0200757 * non-zero an error. On success exactly ONE of @p cert_path or @p cert_data and ONE of
758 * @p privkey_path and @p privkey_data is expected to be set. Those set will be freed.
759 * - @p cert_path expects a PEM file,
760 * - @p cert_data expects a base-64 encoded ASN.1 DER data,
761 * - @p privkey_path expects a PEM file,
762 * - @p privkey_data expects a base-64 encoded ANS.1 DER data,
763 * - @p privkey_type type of the key in @p privkey_data.
764 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
765 * @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 +0100766 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100767void 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 +0200768 char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data,
769 void (*free_user_data)(void *user_data));
Michal Vaskoda514772016-02-01 11:32:01 +0100770
771/**
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500772 * @brief Set the callback for retrieving server certificate chain
773 *
774 * @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 +0200775 * 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 -0500776 * NULL. Both will be (deeply) freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200777 * - @p cert_paths expect an array of PEM files,
778 * - @p cert_path_count number of @p cert_paths array members,
779 * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
780 * - @p cert_data_count number of @p cert_data array members.
781 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
782 * @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 -0500783 */
784void 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 +0200785 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 -0500786
787/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100788 * @brief Add a trusted certificate list. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100789 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100790 *
Michal Vaskoda514772016-02-01 11:32:01 +0100791 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100792 * @param[in] name Arbitary name identifying this certificate list.
Michal Vasko1a38c862016-01-15 15:50:07 +0100793 * @return 0 on success, -1 on error.
794 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100795int nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100796
Michal Vasko1a38c862016-01-15 15:50:07 +0100797/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100798 * @brief Set the callback for retrieving trusted certificates.
799 *
800 * @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 +0200801 * 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 +0100802 * NULL. Both will be (deeply) freed.
Michal Vaskoc446a382021-06-18 08:54:05 +0200803 * - @p cert_paths expect an array of PEM files,
804 * - @p cert_path_count number of @p cert_paths array members,
805 * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
806 * - @p cert_data_count number of @p cert_data array members.
807 * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb.
808 * @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 +0100809 */
810void 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 +0200811 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 +0100812
813/**
814 * @brief Remove a trusted certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100815 *
Michal Vaskoda514772016-02-01 11:32:01 +0100816 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100817 * @param[in] name Name of the certificate list to delete. NULL deletes all the lists.
818 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100819 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100820int nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100821
Michal Vasko1a38c862016-01-15 15:50:07 +0100822/**
823 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100824 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100825 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100826 *
Michal Vaskoda514772016-02-01 11:32:01 +0100827 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100828 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
829 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
830 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100831 * @return 0 on success, -1 on error.
832 */
Michal Vasko96830e32016-02-01 10:54:18 +0100833int 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 +0100834
Michal Vasko1a38c862016-01-15 15:50:07 +0100835/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100836 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100837 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100838 *
Michal Vaskoda514772016-02-01 11:32:01 +0100839 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100840 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
841 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
842 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100843 * @return 0 on success, -1 on error.
844 */
Michal Vasko96830e32016-02-01 10:54:18 +0100845int 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 +0100846
Michal Vasko1a38c862016-01-15 15:50:07 +0100847/**
Michal Vasko96830e32016-02-01 10:54:18 +0100848 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100849 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100850 *
851 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100852 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100853void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100854
Michal Vasko1a38c862016-01-15 15:50:07 +0100855/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100856 * @brief Add a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100857 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200858 * It is possible to add an entry step-by-step, specifying first only @p ip and in later calls
859 * @p fingerprint, @p map_type, and optionally @p name spearately.
Michal Vasko1a38c862016-01-15 15:50:07 +0100860 *
Michal Vaskoda514772016-02-01 11:32:01 +0100861 * @param[in] endpt_name Existing endpoint name.
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100862 * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id
863 * is modified.
864 * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset.
865 * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset.
Michal Vaskoc446a382021-06-18 08:54:05 +0200866 * @param[in] name Specific username used only if @p map_type == NC_TLS_CTN_SPECIFED.
Michal Vasko1a38c862016-01-15 15:50:07 +0100867 * @return 0 on success, -1 on error.
868 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200869int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200870 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100871
Michal Vasko1a38c862016-01-15 15:50:07 +0100872/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100873 * @brief Remove a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100874 *
Michal Vaskoda514772016-02-01 11:32:01 +0100875 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100876 * @param[in] id Priority of the entry. -1 matches all the priorities.
877 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
878 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
879 * @param[in] name Specific username for the entry. NULL matches all the usernames.
880 * @return 0 on success, -1 on not finding any match.
881 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200882int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200883 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100884
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100885/**
886 * @brief Get a cert-to-name entry.
887 *
888 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
889 * it is filled and returned. If the value is set, it is used as a filter.
890 * Returns first matching entry.
891 *
892 * @param[in] endpt_name Existing endpoint name.
893 * @param[in,out] id Priority of the entry.
894 * @param[in,out] fingerprint Fingerprint fo the entry.
895 * @param[in,out] map_type Mapping type of the entry.
896 * @param[in,out] name Specific username for the entry.
897 * @return 0 on success, -1 on not finding any match.
898 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100899int 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 +0200900 char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100901
Michal Vasko7060bcf2016-11-28 14:48:11 +0100902/**
Michal Vasko709598e2016-11-28 14:48:32 +0100903 * @brief Get client certificate.
904 *
905 * @param[in] session Session to get the information from.
906 * @return Const session client certificate.
907 */
908const X509 *nc_session_get_client_cert(const struct nc_session *session);
909
910/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100911 * @brief Set TLS authentication additional verify callback.
912 *
913 * Server will always perform cert-to-name based on its configuration. Only after it passes
914 * and this callback is set, it is also called. It should return exactly what OpenSSL
915 * verify callback meaning 1 for success, 0 to deny the user.
916 *
917 * @param[in] verify_clb Additional user verify callback.
918 */
919void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
920
Michal Vaskoc446a382021-06-18 08:54:05 +0200921/** @} Server TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200922
roman2eab4742023-06-06 10:00:26 +0200923#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100924
Michal Vaskof8352352016-05-24 09:11:36 +0200925/**
Radek Krejci6799a052017-05-19 14:23:23 +0200926 * @addtogroup server_session
927 * @{
928 */
929
930/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200931 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200932 *
933 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200934 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200935 */
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200936time_t nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200937
Michal Vasko3486a7c2017-03-03 13:28:07 +0100938/**
Michal Vasko71dbd772021-03-23 14:08:37 +0100939 * @brief Increase session notification subscription flag count.
940 * Supports multiple subscriptions on one session.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100941 *
942 * It is used only to ignore timeouts, because they are
943 * ignored for sessions with active subscriptions.
944 *
945 * @param[in] session Session to modify.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100946 */
Michal Vasko71dbd772021-03-23 14:08:37 +0100947void nc_session_inc_notif_status(struct nc_session *session);
948
949/**
950 * @brief Decrease session notification subscription flag count.
951 * Supports multiple subscriptions on one session.
952 *
953 * @param[in] session Session to modify.
954 */
955void nc_session_dec_notif_status(struct nc_session *session);
Michal Vasko3486a7c2017-03-03 13:28:07 +0100956
957/**
958 * @brief Get session notification subscription flag.
959 *
960 * @param[in] session Session to get the information from.
961 * @return 0 for no active subscription, non-zero for an active subscription.
962 */
963int nc_session_get_notif_status(const struct nc_session *session);
964
Michal Vaskoc446a382021-06-18 08:54:05 +0200965/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200966
Michal Vaskoc09730e2019-01-17 10:07:26 +0100967#ifdef __cplusplus
968}
969#endif
970
Michal Vasko086311b2016-01-08 09:53:11 +0100971#endif /* NC_SESSION_SERVER_H_ */