blob: b6059019fd99a7aeb5c82e1e69c2a7e8be313c18 [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
roman9225e912024-04-05 12:33:09 +020027#include "config.h"
28#include "netconf.h"
29#include "session.h"
30
31#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +020032# include <libssh/callbacks.h>
33# include <libssh/libssh.h>
34# include <libssh/server.h>
roman2eab4742023-06-06 10:00:26 +020035#endif /* NC_ENABLED_SSH_TLS */
bhart3bc2f582018-06-05 12:40:32 -050036
Michal Vasko1a38c862016-01-15 15:50:07 +010037/**
Radek Krejci6799a052017-05-19 14:23:23 +020038 * @defgroup server_session Server Session
39 * @ingroup server
40 *
41 * @brief Server-side NETCONF session manipulation.
42 * @{
43 */
44
45/**
Michal Vasko1a38c862016-01-15 15:50:07 +010046 * @brief Prototype of callbacks that are called if some RPCs are received.
47 *
Michal Vaskoc446a382021-06-18 08:54:05 +020048 * If @p session termination reason is changed in the callback, one last reply
Michal Vasko1a38c862016-01-15 15:50:07 +010049 * is sent and then the session is considered invalid.
50 *
Radek Krejci6799a052017-05-19 14:23:23 +020051 * The callback is set via nc_set_global_rpc_clb().
52 *
Michal Vasko1a38c862016-01-15 15:50:07 +010053 * @param[in] rpc Parsed client RPC request.
54 * @param[in] session Session the RPC arrived on.
55 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
56 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010057typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
58
Michal Vasko1a38c862016-01-15 15:50:07 +010059/**
Michal Vasko7b096242016-01-29 15:55:10 +010060 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010061 *
62 * @param[in] session Session to modify.
63 * @param[in] reason Reason of termination.
64 */
65void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
66
67/**
Michal Vasko142cfea2017-08-07 10:12:11 +020068 * @brief Set the session-id of the session responsible for this session's termination.
69 *
70 * @param[in] session Session to modify. Must have term_reason set to #NC_SESSION_TERM_KILLED.
71 * @param[in] sid SID of the killing session.
72 */
73void nc_session_set_killed_by(struct nc_session *session, uint32_t sid);
74
75/**
76 * @brief Set the status of a session.
77 *
78 * @param[in] session Session to modify.
79 * @param[in] status Status of the session.
80 */
81void nc_session_set_status(struct nc_session *session, NC_STATUS status);
82
83/**
Radek Krejci6799a052017-05-19 14:23:23 +020084 * @brief Set a global nc_rpc_clb that is called if the particular RPC request is
85 * received and the private field in the corresponding RPC schema node is NULL.
86 *
Michal Vasko238b6c12021-12-14 15:14:09 +010087 * If this callback is set, the default callbacks for "get-schema" and "close-session" are not used.
88 *
Radek Krejci6799a052017-05-19 14:23:23 +020089 * @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 Vasko238b6c12021-12-14 15:14:09 +010093/**
94 * @brief Default RPC callback used for "ietf-netconf-monitoring:get-schema" RPC if no other specific
95 * or global callback is set.
96 *
97 * @param[in] rpc Received RPC.
98 * @param[in] session NC session @p rpc was received on.
99 * @return Server reply.
100 */
101struct nc_server_reply *nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session);
102
103/**
104 * @brief Default RPC callback used for "ietf-netconf:close-session" RPC if no other specific
105 * or global callback is set.
106 *
107 * @param[in] rpc Received RPC.
108 * @param[in] session NC session @p rpc was received on.
109 * @return Server reply.
110 */
111struct nc_server_reply *nc_clb_default_close_session(struct lyd_node *rpc, struct nc_session *session);
112
Michal Vasko4e6d3242021-05-26 09:13:24 +0200113/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200114
115/**
romanf578cd52023-10-19 09:47:40 +0200116 * @defgroup server_functions Server Functions
117 * @ingroup server
Radek Krejci6799a052017-05-19 14:23:23 +0200118 * @{
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/**
romanf578cd52023-10-19 09:47:40 +0200137 * @brief Initialize a context which can serve as a default server context.
138 *
139 * Loads the default modules ietf-netconf and ietf-netconf-monitoring and their enabled features - ietf-netconf
140 * enabled features are : writable-running, candidate, rollback-on-error, validate, startup, url, xpath, confirmed-commit and
141 * ietf-netconf-monitoring has no features.
142 *
143 * If ctx is :
144 * - NULL: a new context will be created and if the call is successful you have to free it,
145 * - non NULL: context will be searched for the two modules and their features
146 * and if anything is missing, it will be implemented.
147 *
148 * @param[in,out] ctx Optional context in which the modules will be loaded. Created if ctx is null.
149 * @return 0 on success, -1 on error.
150 */
151int nc_server_init_ctx(struct ly_ctx **ctx);
152
153/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100154 * @brief Set the with-defaults capability extra parameters.
155 *
156 * For the capability to be actually advertised, the server context must also
157 * include the ietf-netconf-with-defaults model.
158 *
159 * Changing this option has the same ill effects as changing capabilities while
160 * sessions are already established.
161 *
162 * @param[in] basic_mode basic-mode with-defaults parameter.
163 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
164 * parameter.
165 * @return 0 on success, -1 on error.
166 */
Michal Vasko086311b2016-01-08 09:53:11 +0100167int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
168
Michal Vasko1a38c862016-01-15 15:50:07 +0100169/**
Michal Vasko55f03972016-04-13 08:56:01 +0200170 * @brief Get with-defaults capability extra parameters.
171 *
172 * At least one argument must be non-NULL.
173 *
romanf578cd52023-10-19 09:47:40 +0200174 * @param[out] basic_mode basic-mode parameter.
175 * @param[out] also_supported also-supported parameter.
Michal Vasko55f03972016-04-13 08:56:01 +0200176 */
177void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
178
179/**
Radek Krejci658782b2016-12-04 22:04:55 +0100180 * @brief Set capability of the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100181 *
Radek Krejci658782b2016-12-04 22:04:55 +0100182 * Capability can be used when some behavior or extension of the server is not defined
183 * as a YANG module. The provided value will be advertised in the server's \<hello\>
184 * messages. Note, that libnetconf only checks that the provided value is non-empty
185 * string.
Michal Vasko1a38c862016-01-15 15:50:07 +0100186 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100187 * @param[in] value Capability string to be advertised in server's \<hello\> messages.
Michal Vasko1a38c862016-01-15 15:50:07 +0100188 */
Radek Krejci658782b2016-12-04 22:04:55 +0100189int nc_server_set_capability(const char *value);
Michal Vasko55f03972016-04-13 08:56:01 +0200190
191/**
Michal Vasko1440a742021-03-31 11:11:03 +0200192 * @brief Set the callback for getting yang-library capability identifier. If none is set, libyang context change count is used.
193 *
194 * @param[in] content_id_clb Callback that should return the yang-library content identifier.
Michal Vaskoc446a382021-06-18 08:54:05 +0200195 * @param[in] user_data Optional arbitrary user data that will be passed to @p content_id_clb.
196 * @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 +0200197 */
198void nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data,
199 void (*free_user_data)(void *user_data));
200
201/**
Radek Krejci24a18412018-05-16 15:09:10 +0200202 * @brief Get all the server capabilities including all the schemas.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200203 *
204 * A few capabilities (with-defaults, interleave) depend on the current
205 * server options.
206 *
207 * @param[in] ctx Context to read most capabilities from.
Michal Vasko93224072021-11-09 12:14:28 +0100208 * @return Array of capabilities, NULL on error.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200209 */
Michal Vasko93224072021-11-09 12:14:28 +0100210char **nc_server_get_cpblts(const struct ly_ctx *ctx);
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200211
Radek Krejci24a18412018-05-16 15:09:10 +0200212/**
213 * @brief Get the server capabilities including the schemas with the specified YANG version.
214 *
215 * A few capabilities (with-defaults, interleave) depend on the current
216 * server options.
217 *
218 * @param[in] ctx Context to read most capabilities from.
219 * @param[in] version YANG version of the schemas to be included in result, with
220 * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts().
Michal Vasko93224072021-11-09 12:14:28 +0100221 * @return Array of capabilities, NULL on error.
Radek Krejci24a18412018-05-16 15:09:10 +0200222 */
Michal Vasko93224072021-11-09 12:14:28 +0100223char **nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version);
Radek Krejci24a18412018-05-16 15:09:10 +0200224
romanf578cd52023-10-19 09:47:40 +0200225/** @} Server Functions */
Radek Krejci6799a052017-05-19 14:23:23 +0200226
227/**
228 * @addtogroup server_session
229 * @{
230 */
231
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200232/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100233 * @brief Accept a new session on a pre-established transport session.
234 *
Michal Vasko93224072021-11-09 12:14:28 +0100235 * For detailed description, look at ::nc_accept().
236 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100237 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
238 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
239 * @param[in] username NETCONF username as provided by the transport protocol.
Michal Vasko93224072021-11-09 12:14:28 +0100240 * @param[in] ctx Context for the session to use.
Michal Vasko1a38c862016-01-15 15:50:07 +0100241 * @param[out] session New session on success.
Michal Vasko71090fc2016-05-24 16:37:28 +0200242 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
243 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100244 */
Michal Vasko93224072021-11-09 12:14:28 +0100245NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx,
246 struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100247
Michal Vasko1a38c862016-01-15 15:50:07 +0100248/**
249 * @brief Create an empty structure for polling sessions.
250 *
251 * @return Empty pollsession structure, NULL on error.
252 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100253struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100254
Michal Vasko1a38c862016-01-15 15:50:07 +0100255/**
256 * @brief Free a pollsession structure.
257 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200258 * !IMPORTANT! Make sure that @p ps is not accessible (is not used)
Michal Vasko01082bf2016-04-07 10:44:21 +0200259 * by any thread before and after this call!
260 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100261 * @param[in] ps Pollsession structure to free.
262 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100263void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100264
Michal Vasko1a38c862016-01-15 15:50:07 +0100265/**
266 * @brief Add a session to a pollsession structure.
267 *
268 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200269 * @param[in] session Session to add to @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100270 * @return 0 on success, -1 on error.
271 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100272int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100273
Michal Vasko1a38c862016-01-15 15:50:07 +0100274/**
275 * @brief Remove a session from a pollsession structure.
276 *
277 * @param[in] ps Pollsession structure to modify.
Michal Vaskoc446a382021-06-18 08:54:05 +0200278 * @param[in] session Session to remove from @p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100279 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100280 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100281int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
282
Michal Vasko1a38c862016-01-15 15:50:07 +0100283/**
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100284 * @brief Get a session from a pollsession structure matching the session ID.
285 *
286 * @param[in] ps Pollsession structure to read from.
Michal Vasko4871c9d2017-10-09 14:48:39 +0200287 * @param[in] idx Index of the session.
288 * @return Session on index, NULL if out-of-bounds.
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100289 */
Michal Vasko4871c9d2017-10-09 14:48:39 +0200290struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx);
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100291
292/**
Michal Vasko3ec3b112022-07-21 12:32:33 +0200293 * @brief Callback for finding a session in a pollsession structure.
294 *
295 * @param[in] session Considered NETCONF session.
296 * @param[in] cb_data User data.
297 * @return 0 if the session does not match.
298 * @return non-zero if the session matches and should be returned.
299 */
300typedef int (*nc_ps_session_match_cb)(struct nc_session *session, void *cb_data);
301
302/**
303 * @brief Find a session in a pollsession structure using a matching callback.
304 *
305 * @param[in] ps Pollsession structure to read from.
306 * @param[in] match_cb Matching callback to use.
307 * @param[in] cb_data User data passed to @p cb.
308 * @return Found session, NULL if none matched.
309 */
310struct nc_session *nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data);
311
312/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100313 * @brief Learn the number of sessions in a pollsession structure.
314 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200315 * Does not lock @p ps structure for efficiency.
Michal Vaskof4462fd2017-02-15 14:29:05 +0100316 *
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100317 * @param[in] ps Pollsession structure to check.
Michal Vaskoc446a382021-06-18 08:54:05 +0200318 * @return Number of sessions (even invalid ones) in @p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100319 */
320uint16_t nc_ps_session_count(struct nc_pollsession *ps);
321
Michal Vasko30a5d6b2017-02-15 14:29:39 +0100322#define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */
323#define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */
324#define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */
325#define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */
326#define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */
327#define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */
328#define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
329#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200330
roman2eab4742023-06-06 10:00:26 +0200331#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoc446a382021-06-18 08:54:05 +0200332# define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */
333# define NC_PSPOLL_SSH_CHANNEL 0x0200 /**< New SSH channel opened on an existing session (only with SSH support). */
roman2eab4742023-06-06 10:00:26 +0200334#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko71090fc2016-05-24 16:37:28 +0200335
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100336/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100337 * @brief Poll sessions and process any received RPCs.
338 *
Michal Vaskoe4300a82017-05-24 10:35:42 +0200339 * Only one event on one session is handled in one function call. If this event
340 * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session
Michal Vaskoc446a382021-06-18 08:54:05 +0200341 * should be removed from @p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100342 *
343 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100344 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100345 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200346 * @param[in] session Session that was processed and that specific return bits concern.
347 * Can be NULL.
Michal Vaskoade892d2017-02-22 13:40:35 +0100348 * @return Bitfield of NC_PSPOLL_* macros.
Michal Vasko1a38c862016-01-15 15:50:07 +0100349 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200350int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100351
Michal Vasko11d142a2016-01-19 15:58:24 +0100352/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100353 * @brief Remove sessions from a pollsession structure and
354 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100355 *
Michal Vaskoc446a382021-06-18 08:54:05 +0200356 * 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 +0100357 *
358 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100359 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100360 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100361 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100362void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100363
Michal Vasko1440a742021-03-31 11:11:03 +0200364/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200365
366/**
romanf578cd52023-10-19 09:47:40 +0200367 * @addtogroup server_functions
Radek Krejci6799a052017-05-19 14:23:23 +0200368 * @{
369 */
370
Michal Vasko1a38c862016-01-15 15:50:07 +0100371/**
Michal Vaskoe8e07702017-03-15 10:19:30 +0100372 * @brief Get the number of currently configured listening endpoints.
373 * Note that an ednpoint without address and/or port will be included
374 * even though it is not, in fact, listening.
375 *
376 * @return Number of added listening endpoints.
377 */
378int nc_server_endpt_count(void);
379
romanfb3f7cf2023-11-30 16:10:09 +0100380/**
381 * @brief Create a new UNIX socket endpoint and start listening.
382 *
383 * @param[in] endpt_name Arbitrary unique identifier of the endpoint.
384 * @param[in] unix_socket_path Path to the listening socket.
385 * @param[in] mode New mode, -1 to use default.
386 * @param[in] uid New uid, -1 to use default.
387 * @param[in] gid New gid, -1 to use default.
388 *
389 * @return 0 on success, 1 on error.
390 */
391int nc_server_add_endpt_unix_socket_listen(const char *endpt_name, const char *unix_socket_path, mode_t mode, uid_t uid, gid_t gid);
392
393/**
394 * @brief Deletes a UNIX socket endpoint.
395 *
396 * @param[in] endpt_name Identifier of the endpoint.
397 * Has no effect if the endpoint doesn't exist or if its transport is not UNIX socket.
398 */
399void nc_server_del_endpt_unix_socket(const char *endpt_name);
400
romanf578cd52023-10-19 09:47:40 +0200401/** @} */
Radek Krejci6799a052017-05-19 14:23:23 +0200402
403/**
404 * @addtogroup server_session
Michal Vasko4e6d3242021-05-26 09:13:24 +0200405 * @{
Radek Krejci6799a052017-05-19 14:23:23 +0200406 */
407
Michal Vaskoe2713da2016-08-22 16:06:40 +0200408/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100409 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100410 *
Michal Vaskob70c8b82017-03-17 09:09:29 +0100411 * Once a new (TCP/IP) conection is established a different (quite long) timeout
412 * is used for waiting for transport-related data, which means this call can block
Michal Vaskoc446a382021-06-18 08:54:05 +0200413 * for much longer that @p timeout, but only with slow/faulty/malicious clients.
Michal Vaskob70c8b82017-03-17 09:09:29 +0100414 *
Michal Vasko93224072021-11-09 12:14:28 +0100415 * Server capabilities are generated based on the content of @p ctx. The context must
romanf578cd52023-10-19 09:47:40 +0200416 * not be destroyed before the accepted NETCONF session is freed. Basic usable context may
417 * be created by calling ::nc_server_init_ctx().
Michal Vasko93224072021-11-09 12:14:28 +0100418 *
419 * Supported RPCs of models in the context are expected to have their callback
420 * in the corresponding RPC schema node set to a nc_rpc_clb function callback using ::nc_set_rpc_callback().
421 * This callback is called by ::nc_ps_poll() if the particular RPC request is
422 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
423 * only) and ietf-netconf:close-session are set internally if left unset.
424 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100425 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko93224072021-11-09 12:14:28 +0100426 * non-blocking call, -1 for infinite waiting.
427 * @param[in] ctx Context for the session to use.
Michal Vasko96164bf2016-01-21 15:41:58 +0100428 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200429 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
430 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100431 */
Michal Vasko93224072021-11-09 12:14:28 +0100432NC_MSG_TYPE nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100433
roman2eab4742023-06-06 10:00:26 +0200434#ifdef NC_ENABLED_SSH_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100435
Michal Vasko1a38c862016-01-15 15:50:07 +0100436/**
Michal Vaskoc446a382021-06-18 08:54:05 +0200437 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF @p orig_session.
438 * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on @p orig_session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200439 *
440 * @param[in] orig_session Session that has a new SSH channel ready.
441 * @param[out] session New session.
442 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
443 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
444 */
445NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
446
447/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100448 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskoc446a382021-06-18 08:54:05 +0200449 * that was polled in @p ps. Call this function only when nc_ps_poll() on @p ps returns #NC_PSPOLL_SSH_CHANNEL.
450 * The new session is only returned in @p session, it is not added to @p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100451 *
452 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
453 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200454 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
455 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100456 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200457NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100458
Michal Vasko1440a742021-03-31 11:11:03 +0200459/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200460
461/**
462 * @defgroup server_ssh Server SSH
463 * @ingroup server
464 *
465 * @brief Server-side settings for SSH connections.
466 * @{
467 */
468
Michal Vasko96164bf2016-01-21 15:41:58 +0100469/**
romana9ec3362023-12-21 10:59:57 +0100470 * @brief Set the format of the path to authorized_keys files.
471 *
472 * This path format will be set globally for all clients wishing to authenticate via the
473 * SSH Public Key system authentication.
474 *
475 * @param[in] path Path to authorized_keys files. The path may contain the following tokens:
476 * - %u - replaced by the username of the user trying to authenticate,
477 * - %h - replaced by the home directory of the user trying to authenticate,
478 * - %U - replaced by the UID of the user trying to authenticate,
479 * - %% - a literal '%'.
480 * @return 0 on success, 1 on error.
481 */
482int nc_server_ssh_set_authkey_path_format(const char *path);
483
484/**
romane5675b12024-03-05 14:26:23 +0100485 * @brief Keyboard interactive authentication callback.
bhart3bc2f582018-06-05 12:40:32 -0500486 *
romane5675b12024-03-05 14:26:23 +0100487 * The callback has to handle sending interactive challenges and receiving responses by itself.
488 * An example callback may fit the following description:
489 * Prepare all prompts for the user and send them via `ssh_message_auth_interactive_request()`.
490 * Get the answers either by calling `ssh_message_get()` or `nc_server_ssh_kbdint_get_nanswers()`.
491 * Return value based on your authentication logic and user answers retrieved by
492 * calling `ssh_userauth_kbdint_getanswer()`.
493 *
494 * @param[in] session NETCONF session.
495 * @param[in] ssh_sess libssh session.
496 * @param[in] msg SSH message that contains the interactive request and which expects a reply with prompts.
497 * @param[in] user_data Arbitrary user data.
498 * @return 0 for successful authentication, non-zero to deny the user.
499 */
500typedef int (*nc_server_ssh_interactive_auth_clb)(const struct nc_session *session,
501 ssh_session ssh_sess, ssh_message msg, void *user_data);
502
503/**
504 * @brief Set the callback for SSH interactive authentication.
505 *
506 * @param[in] auth_clb Keyboard interactive authentication callback. This callback is only called once per authentication.
romanf578cd52023-10-19 09:47:40 +0200507 * @param[in] user_data Optional arbitrary user data that will be passed to @p interactive_auth_clb.
Michal Vaskoc446a382021-06-18 08:54:05 +0200508 * @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 -0500509 */
romane5675b12024-03-05 14:26:23 +0100510void nc_server_ssh_set_interactive_auth_clb(nc_server_ssh_interactive_auth_clb auth_clb, void *user_data, void (*free_user_data)(void *user_data));
511
512/**
513 * @brief Get the number of answers to Keyboard interactive authentication prompts.
514 *
515 * The actual answers can later be retrieved by calling `ssh_userauth_kbdint_getanswer()` on
516 * the @p libssh_session.
517 *
518 * @param[in] session NETCONF session.
519 * @param[in] libssh_session libssh session.
520 *
521 * @return Non-negative number of answers on success, -1 on configurable authentication timeout,
522 * disconnect or other error.
523 */
524int nc_server_ssh_kbdint_get_nanswers(const struct nc_session *session, ssh_session libssh_session);
roman41a11e42022-06-22 09:27:08 +0200525
roman808f3f62023-11-23 16:01:04 +0100526/**
527 * @brief Set the name of the PAM configuration file.
528 *
529 * This filename will be set globally for all clients wishing to authenticate via the
530 * SSH Keyboard Interactive authentication method.
531 *
532 * @param[in] filename Name of the PAM configuration file. The file needs to be located in
533 * the default PAM directory (usually /etc/pam.d/).
534 *
535 * @return 0 on success, 1 on error.
536 */
537int nc_server_ssh_set_pam_conf_filename(const char *filename);
538
Michal Vaskoc446a382021-06-18 08:54:05 +0200539/** @} Server SSH */
Radek Krejci6799a052017-05-19 14:23:23 +0200540
Michal Vasko1a38c862016-01-15 15:50:07 +0100541/**
Radek Krejci6799a052017-05-19 14:23:23 +0200542 * @defgroup server_tls Server TLS
543 * @ingroup server
544 *
545 * @brief Server-side settings for TLS connections.
546 * @{
547 */
548
549/**
Michal Vasko709598e2016-11-28 14:48:32 +0100550 * @brief Get client certificate.
551 *
552 * @param[in] session Session to get the information from.
553 * @return Const session client certificate.
554 */
roman9225e912024-04-05 12:33:09 +0200555const void *nc_session_get_client_cert(const struct nc_session *session);
Michal Vasko709598e2016-11-28 14:48:32 +0100556
557/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100558 * @brief Set TLS authentication additional verify callback.
559 *
560 * Server will always perform cert-to-name based on its configuration. Only after it passes
romance435112024-04-23 15:12:09 +0200561 * and this callback is set, it is also called. It should return non-zero for success, 0 to deny the user.
Michal Vasko7060bcf2016-11-28 14:48:11 +0100562 *
563 * @param[in] verify_clb Additional user verify callback.
564 */
565void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
566
Michal Vaskoc446a382021-06-18 08:54:05 +0200567/** @} Server TLS */
Radek Krejci6799a052017-05-19 14:23:23 +0200568
roman2eab4742023-06-06 10:00:26 +0200569#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100570
Michal Vaskof8352352016-05-24 09:11:36 +0200571/**
Radek Krejci6799a052017-05-19 14:23:23 +0200572 * @addtogroup server_session
573 * @{
574 */
575
576/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200577 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200578 *
579 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200580 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200581 */
roman44efa322023-11-03 13:57:25 +0100582struct timespec nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200583
Michal Vasko3486a7c2017-03-03 13:28:07 +0100584/**
Michal Vasko71dbd772021-03-23 14:08:37 +0100585 * @brief Increase session notification subscription flag count.
586 * Supports multiple subscriptions on one session.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100587 *
588 * It is used only to ignore timeouts, because they are
589 * ignored for sessions with active subscriptions.
590 *
591 * @param[in] session Session to modify.
Michal Vasko3486a7c2017-03-03 13:28:07 +0100592 */
Michal Vasko71dbd772021-03-23 14:08:37 +0100593void nc_session_inc_notif_status(struct nc_session *session);
594
595/**
596 * @brief Decrease session notification subscription flag count.
597 * Supports multiple subscriptions on one session.
598 *
599 * @param[in] session Session to modify.
600 */
601void nc_session_dec_notif_status(struct nc_session *session);
Michal Vasko3486a7c2017-03-03 13:28:07 +0100602
603/**
604 * @brief Get session notification subscription flag.
605 *
606 * @param[in] session Session to get the information from.
607 * @return 0 for no active subscription, non-zero for an active subscription.
608 */
609int nc_session_get_notif_status(const struct nc_session *session);
610
Michal Vaskoc446a382021-06-18 08:54:05 +0200611/** @} Server Session */
Radek Krejci6799a052017-05-19 14:23:23 +0200612
Michal Vaskoc09730e2019-01-17 10:07:26 +0100613#ifdef __cplusplus
614}
615#endif
616
Michal Vasko086311b2016-01-08 09:53:11 +0100617#endif /* NC_SESSION_SERVER_H_ */