blob: c3e584c3840ad4f2f81990c24c701a1a993693f9 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session server manipulation
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#ifndef NC_SESSION_SERVER_H_
16#define NC_SESSION_SERVER_H_
17
Michal Vaskoc09730e2019-01-17 10:07:26 +010018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michal Vasko086311b2016-01-08 09:53:11 +010022#include <stdint.h>
Michal Vasko05ba9df2016-01-13 14:40:27 +010023#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010024
Michal Vasko709598e2016-11-28 14:48:32 +010025#ifdef NC_ENABLED_TLS
26# include <openssl/x509.h>
27#endif
28
bhart3bc2f582018-06-05 12:40:32 -050029#ifdef NC_ENABLED_SSH
30# include <libssh/libssh.h>
31# include <libssh/callbacks.h>
32# include <libssh/server.h>
33#endif
34
Michal Vasko086311b2016-01-08 09:53:11 +010035#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010036#include "netconf.h"
37
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 *
49 * If \p session termination reason is changed in the callback, one last reply
50 * 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 *
88 * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default.
89 */
90void nc_set_global_rpc_clb(nc_rpc_clb clb);
91
92/**@} Server Session */
93
94/**
95 * @addtogroup server
96 * @{
97 */
98
99/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100100 * @brief Initialize libssh and/or libssl/libcrypto and the server using a libyang context.
Michal Vasko1a38c862016-01-15 15:50:07 +0100101 *
102 * The context is not modified internally, only its dictionary is used for holding
Michal Vaskoa43589b2016-02-17 13:24:59 +0100103 * all the strings, which is thread-safe. Reading models is considered thread-safe
104 * as models cannot be removed and are rarely modified (augments or deviations).
Michal Vasko1a38c862016-01-15 15:50:07 +0100105 *
Michal Vasko3a889fd2016-09-30 12:16:37 +0200106 * If the RPC callbacks on schema nodes (mentioned in @ref howtoserver) are modified after
Michal Vasko5494f512016-03-01 12:13:44 +0100107 * server initialization with that particular context, they will be called (changes
108 * will take effect). However, there could be race conditions as the access to
109 * these callbacks is not thread-safe.
110 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100111 * Server capabilities are generated based on its content. Changing the context
112 * in ways that result in changed capabilities (adding models, changing features)
113 * is discouraged after sessions are established as it is not possible to change
114 * capabilities of a session.
115 *
116 * This context can safely be destroyed only after calling the last libnetconf2
117 * function in an application.
118 *
Michal Vasko3a889fd2016-09-30 12:16:37 +0200119 * Supported RPCs of models in the context are expected to have their callback
120 * in the corresponding RPC schema node set to a nc_rpc_clb function callback using nc_set_rpc_callback().
Michal Vasko1a38c862016-01-15 15:50:07 +0100121 * This callback is called by nc_ps_poll() if the particular RPC request is
122 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
123 * only) and ietf-netconf:close-session are set internally if left unset.
124 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100125 * @param[in] ctx Core NETCONF server context.
126 * @return 0 on success, -1 on error.
127 */
Michal Vasko086311b2016-01-08 09:53:11 +0100128int nc_server_init(struct ly_ctx *ctx);
129
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 Vasko1a38c862016-01-15 15:50:07 +0100175 * @brief Set server timeout for receiving a hello message.
176 *
177 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
178 */
179void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100180
Michal Vasko1a38c862016-01-15 15:50:07 +0100181/**
Michal Vasko55f03972016-04-13 08:56:01 +0200182 * @brief get server timeout for receiving a hello message.
183 *
184 * @return Hello message timeout, 0 is infinite.
185 */
186uint16_t nc_server_get_hello_timeout(void);
187
188/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100189 * @brief Set server timeout for dropping an idle session.
190 *
191 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100192 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100193 */
194void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100195
Michal Vasko1a38c862016-01-15 15:50:07 +0100196/**
Michal Vasko55f03972016-04-13 08:56:01 +0200197 * @brief Get server timeout for dropping an idle session.
198 *
199 * @return Idle session timeout, 0 for for never dropping
200 * a session because of inactivity.
201 */
202uint16_t nc_server_get_idle_timeout(void);
203
204/**
Radek Krejci24a18412018-05-16 15:09:10 +0200205 * @brief Get all the server capabilities including all the schemas.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200206 *
207 * A few capabilities (with-defaults, interleave) depend on the current
208 * server options.
209 *
210 * @param[in] ctx Context to read most capabilities from.
211 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
212 */
213const char **nc_server_get_cpblts(struct ly_ctx *ctx);
214
Radek Krejci24a18412018-05-16 15:09:10 +0200215/**
216 * @brief Get the server capabilities including the schemas with the specified YANG version.
217 *
218 * A few capabilities (with-defaults, interleave) depend on the current
219 * server options.
220 *
221 * @param[in] ctx Context to read most capabilities from.
222 * @param[in] version YANG version of the schemas to be included in result, with
223 * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts().
224 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
225 */
226const char **nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version);
227
Radek Krejci6799a052017-05-19 14:23:23 +0200228/**@} Server */
229
230/**
231 * @addtogroup server_session
232 * @{
233 */
234
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200235/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100236 * @brief Accept a new session on a pre-established transport session.
237 *
238 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
239 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
240 * @param[in] username NETCONF username as provided by the transport protocol.
241 * @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 Vasko71090fc2016-05-24 16:37:28 +0200245NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100246
Michal Vasko1a38c862016-01-15 15:50:07 +0100247/**
248 * @brief Create an empty structure for polling sessions.
249 *
250 * @return Empty pollsession structure, NULL on error.
251 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100252struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100253
Michal Vasko1a38c862016-01-15 15:50:07 +0100254/**
255 * @brief Free a pollsession structure.
256 *
Michal Vasko01082bf2016-04-07 10:44:21 +0200257 * !IMPORTANT! Make sure that \p ps is not accessible (is not used)
258 * by any thread before and after this call!
259 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100260 * @param[in] ps Pollsession structure to free.
261 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100262void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100263
Michal Vasko1a38c862016-01-15 15:50:07 +0100264/**
265 * @brief Add a session to a pollsession structure.
266 *
267 * @param[in] ps Pollsession structure to modify.
268 * @param[in] session Session to add to \p ps.
269 * @return 0 on success, -1 on error.
270 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100271int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100272
Michal Vasko1a38c862016-01-15 15:50:07 +0100273/**
274 * @brief Remove a session from a pollsession structure.
275 *
276 * @param[in] ps Pollsession structure to modify.
277 * @param[in] session Session to remove from \p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100278 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100279 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100280int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
281
Michal Vasko1a38c862016-01-15 15:50:07 +0100282/**
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100283 * @brief Get a session from a pollsession structure matching the session ID.
284 *
285 * @param[in] ps Pollsession structure to read from.
Michal Vasko4871c9d2017-10-09 14:48:39 +0200286 * @param[in] idx Index of the session.
287 * @return Session on index, NULL if out-of-bounds.
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100288 */
Michal Vasko4871c9d2017-10-09 14:48:39 +0200289struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx);
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100290
291/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100292 * @brief Learn the number of sessions in a pollsession structure.
293 *
Michal Vaskof4462fd2017-02-15 14:29:05 +0100294 * Does not lock \p ps structure for efficiency.
295 *
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100296 * @param[in] ps Pollsession structure to check.
Michal Vaskoc72d0e62016-07-26 11:36:11 +0200297 * @return Number of sessions (even invalid ones) in \p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100298 */
299uint16_t nc_ps_session_count(struct nc_pollsession *ps);
300
Michal Vasko30a5d6b2017-02-15 14:29:39 +0100301#define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */
302#define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */
303#define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */
304#define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */
305#define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */
306#define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */
307#define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
308#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200309
310#ifdef NC_ENABLED_SSH
Michal Vaskoe645de32017-05-26 14:02:50 +0200311# define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */
312# define NC_PSPOLL_SSH_CHANNEL 0x0200 /**< New SSH channel opened on an existing session (only with SSH support). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200313#endif
314
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100315/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100316 * @brief Poll sessions and process any received RPCs.
317 *
Michal Vaskoe4300a82017-05-24 10:35:42 +0200318 * Only one event on one session is handled in one function call. If this event
319 * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session
320 * should be removed from \p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100321 *
322 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100323 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100324 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200325 * @param[in] session Session that was processed and that specific return bits concern.
326 * Can be NULL.
Michal Vaskoade892d2017-02-22 13:40:35 +0100327 * @return Bitfield of NC_PSPOLL_* macros.
Michal Vasko1a38c862016-01-15 15:50:07 +0100328 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200329int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100330
Michal Vasko11d142a2016-01-19 15:58:24 +0100331/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100332 * @brief Remove sessions from a pollsession structure and
333 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100334 *
Michal Vaskoade892d2017-02-22 13:40:35 +0100335 * 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 +0100336 *
337 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100338 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100339 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100340 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100341void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100342
Radek Krejci53691be2016-02-22 13:58:37 +0100343#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100344
Radek Krejci6799a052017-05-19 14:23:23 +0200345/**@} Server Session */
346
347/**
348 * @addtogroup server
349 * @{
350 */
351
Michal Vasko1a38c862016-01-15 15:50:07 +0100352/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200353 * @brief Add a new endpoint.
354 *
355 * Before the endpoint can accept any connections, its address and port must
Radek Krejci6799a052017-05-19 14:23:23 +0200356 * be set via nc_server_endpt_set_address() and nc_server_endpt_set_port().
Michal Vaskoe2713da2016-08-22 16:06:40 +0200357 *
358 * @param[in] name Arbitrary unique endpoint name.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200359 * @param[in] ti Transport protocol to use.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200360 * @return 0 on success, -1 on error.
361 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200362int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200363
364/**
365 * @brief Stop listening on and remove an endpoint.
366 *
367 * @param[in] name Endpoint name. NULL matches all endpoints.
Michal Vasko59050372016-11-22 14:33:55 +0100368 * @param[in] ti Endpoint transport protocol. NULL matches any protocol.
369 * Redundant to set if \p name is set, endpoint names are
370 * unique disregarding their protocol.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200371 * @return 0 on success, -1 on not finding any match.
372 */
Michal Vasko59050372016-11-22 14:33:55 +0100373int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200374
375/**
Michal Vaskoe8e07702017-03-15 10:19:30 +0100376 * @brief Get the number of currently configured listening endpoints.
377 * Note that an ednpoint without address and/or port will be included
378 * even though it is not, in fact, listening.
379 *
380 * @return Number of added listening endpoints.
381 */
382int nc_server_endpt_count(void);
383
384/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200385 * @brief Change endpoint listening address.
386 *
387 * On error the previous listening socket (if any) is left untouched.
388 *
389 * @param[in] endpt_name Existing endpoint name.
390 * @param[in] address New listening address.
391 * @return 0 on success, -1 on error.
392 */
393int nc_server_endpt_set_address(const char *endpt_name, const char *address);
394
395/**
396 * @brief Change endpoint listening port.
397 *
398 * On error the previous listening socket (if any) is left untouched.
399 *
400 * @param[in] endpt_name Existing endpoint name.
401 * @param[in] port New listening port.
402 * @return 0 on success, -1 on error.
403 */
404int nc_server_endpt_set_port(const char *endpt_name, uint16_t port);
Michal Vasko9e036d52016-01-08 10:49:26 +0100405
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200406/**
407 * @brief Change endpoint permissions.
408 *
409 * This is only valid on unix transport endpoint.
410 * On error the previous listening socket (if any) is left untouched.
411 *
412 * @param[in] endpt_name Existing endpoint name.
413 * @param[in] mode New mode, -1 to use default.
414 * @param[in] uid New uid, -1 to use default.
415 * @param[in] gid New gid, -1 to use default.
416 * @return 0 on success, -1 on error.
417 */
418int nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid);
419
Radek Krejci6799a052017-05-19 14:23:23 +0200420/**@} Server */
421
422/**
423 * @addtogroup server_session
424 */
425
Michal Vasko1a38c862016-01-15 15:50:07 +0100426/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100427 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100428 *
Michal Vaskob70c8b82017-03-17 09:09:29 +0100429 * Once a new (TCP/IP) conection is established a different (quite long) timeout
430 * is used for waiting for transport-related data, which means this call can block
431 * for much longer that \p timeout, but only with slow/faulty/malicious clients.
432 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100433 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko71090fc2016-05-24 16:37:28 +0200434 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100435 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200436 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
437 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100438 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200439NC_MSG_TYPE nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100440
Radek Krejci53691be2016-02-22 13:58:37 +0100441#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100442
Radek Krejci53691be2016-02-22 13:58:37 +0100443#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100444
Michal Vasko1a38c862016-01-15 15:50:07 +0100445/**
Michal Vasko71090fc2016-05-24 16:37:28 +0200446 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF \p orig_session.
Michal Vaskoade892d2017-02-22 13:40:35 +0100447 * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on \p orig_session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200448 *
449 * @param[in] orig_session Session that has a new SSH channel ready.
450 * @param[out] session New session.
451 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
452 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
453 */
454NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
455
456/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100457 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskoade892d2017-02-22 13:40:35 +0100458 * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns #NC_PSPOLL_SSH_CHANNEL.
Michal Vaskoc0fde012016-03-01 09:07:23 +0100459 * The new session is only returned in \p session, it is not added to \p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100460 *
461 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
462 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200463 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
464 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100465 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200466NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100467
Radek Krejci6799a052017-05-19 14:23:23 +0200468/**@} Server Session */
469
470/**
471 * @defgroup server_ssh Server SSH
472 * @ingroup server
473 *
474 * @brief Server-side settings for SSH connections.
475 * @{
476 */
477
Michal Vasko96164bf2016-01-21 15:41:58 +0100478/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100479 * @brief Add an authorized client SSH public key. This public key can be used for
480 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100481 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100482 * @param[in] pubkey_base64 Authorized public key binary content encoded in base64.
483 * @param[in] type Authorized public key SSH type.
484 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100485 * @return 0 on success, -1 on error.
486 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100487int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100488
489/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100490 * @brief Add an authorized client SSH public key. This public key can be used for
491 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100492 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100493 * @param[in] pubkey_path Path to the public key.
494 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100495 * @return 0 on success, -1 on error.
496 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100497int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100498
499/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100500 * @brief Remove an authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100501 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100502 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
503 * @param[in] pubkey_base64 Authorized public key content. NULL matches any key.
504 * @param[in] type Authorized public key type. 0 matches all types.
505 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
506 * @return 0 on success, -1 on not finding any match.
Michal Vasko1a38c862016-01-15 15:50:07 +0100507 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100508int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
509 const char *username);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200510
511/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100512 * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself
513 * wil be retrieved using a callback.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200514 *
515 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100516 * @param[in] name Arbitrary name of the host key.
Michal Vasko7d255882017-02-09 13:35:08 +0100517 * @param[in] idx Optional index where to add the key. -1 adds at the end.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200518 * @return 0 on success, -1 on error.
519 */
Michal Vasko7d255882017-02-09 13:35:08 +0100520int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200521
522/**
Michal Vaskoebba7602018-03-23 13:14:08 +0100523 * @brief Set the callback for SSH password authentication. If none is set, local system users are used.
524 *
525 * @param[in] passwd_auth_clb Callback that should authenticate the user. Username can be directly obtained from \p session.
526 * Zero return indicates success, non-zero an error.
527 * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb.
528 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
529 */
530void nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password,
531 void *user_data),
532 void *user_data, void (*free_user_data)(void *user_data));
533
534/**
bhart3bc2f582018-06-05 12:40:32 -0500535 * @brief Set the callback for SSH interactive authentication. If none is set, local system users are used.
536 *
537 * @param[in] interactive_auth_clb Callback that should authenticate the user.
538 * Zero return indicates success, non-zero an error.
539 * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb.
540 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
541 */
David Sedlák9d21a662018-08-23 10:57:55 +0200542void nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, const ssh_message msg,
bhart3bc2f582018-06-05 12:40:32 -0500543 void *user_data),
544 void *user_data, void (*free_user_data)(void *user_data));
545
546/**
547 * @brief Set the callback for SSH public key authentication. If none is set, local system users are used.
548 *
549 * @param[in] pubkey_auth_clb Callback that should authenticate the user.
550 * Zero return indicates success, non-zero an error.
551 * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb.
552 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
553 */
David Sedlák9d21a662018-08-23 10:57:55 +0200554 void nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data),
bhart3bc2f582018-06-05 12:40:32 -0500555 void *user_data, void (*free_user_data)(void *user_data));
556
557/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100558 * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However,
559 * a maximum of one key of each type will be used during SSH authentication, later keys replacing
560 * the earlier ones.
561 *
562 * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero
563 * an error. On success exactly ONE of \p privkey_path or \p privkey_data is expected
564 * to be set. The one set will be freed.
565 * - \p privkey_path expects a PEM file,
566 * - \p privkey_data expects a base-64 encoded ANS.1 DER data,
567 * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key.
568 * @param[in] user_data Optional arbitrary user data that will be passed to \p hostkey_clb.
569 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
570 */
571void nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
572 char **privkey_data, int *privkey_data_rsa),
573 void *user_data, void (*free_user_data)(void *user_data));
574
575/**
576 * @brief Delete endpoint SSH host key. Their order is preserved.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200577 *
578 * @param[in] endpt_name Existing endpoint name.
Michal Vasko7d255882017-02-09 13:35:08 +0100579 * @param[in] name Name of the host key. NULL matches all the keys, but if \p idx != -1 then this must be NULL.
580 * @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 +0200581 * @return 0 on success, -1 on error.
582 */
Michal Vasko7d255882017-02-09 13:35:08 +0100583int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vasko086311b2016-01-08 09:53:11 +0100584
Michal Vasko1a38c862016-01-15 15:50:07 +0100585/**
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100586 * @brief Move endpoint SSH host key.
587 *
588 * @param[in] endpt_name Exisitng endpoint name.
589 * @param[in] key_mov Name of the host key that will be moved.
590 * @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.
591 * @return 0 in success, -1 on error.
592 */
593int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after);
594
595/**
596 * @brief Modify endpoint SSH host key.
597 *
598 * @param[in] endpt_name Exisitng endpoint name.
599 * @param[in] name Name of an existing host key.
600 * @param[in] new_name New name of the host key \p name.
601 * @return 0 in success, -1 on error.
602 */
603int nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100604
Michal Vasko1a38c862016-01-15 15:50:07 +0100605/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100606 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100607 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100608 *
Michal Vaskoda514772016-02-01 11:32:01 +0100609 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100610 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
611 * @return 0 on success, -1 on error.
612 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100613int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100614
Michal Vasko1a38c862016-01-15 15:50:07 +0100615/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100616 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100617 *
Michal Vaskoda514772016-02-01 11:32:01 +0100618 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100619 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100620 * @return 0 on success, -1 on error.
621 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100622int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100623
Michal Vasko1a38c862016-01-15 15:50:07 +0100624/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100625 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100626 *
Michal Vaskoda514772016-02-01 11:32:01 +0100627 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100628 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
629 * @return 0 on success, -1 on error.
630 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100631int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100632
Radek Krejci6799a052017-05-19 14:23:23 +0200633/**@} Server SSH */
634
Radek Krejci53691be2016-02-22 13:58:37 +0100635#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100636
Radek Krejci53691be2016-02-22 13:58:37 +0100637#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100638
Michal Vasko1a38c862016-01-15 15:50:07 +0100639/**
Radek Krejci6799a052017-05-19 14:23:23 +0200640 * @defgroup server_tls Server TLS
641 * @ingroup server
642 *
643 * @brief Server-side settings for TLS connections.
644 * @{
645 */
646
647/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100648 * @brief Set the server TLS certificate. Only the name is set, the certificate itself
649 * wil be retrieved using a callback.
Michal Vaskoda514772016-02-01 11:32:01 +0100650 *
651 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100652 * @param[in] name Arbitrary certificate name.
Michal Vaskoda514772016-02-01 11:32:01 +0100653 * @return 0 on success, -1 on error.
654 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100655int nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name);
Michal Vaskoda514772016-02-01 11:32:01 +0100656
657/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100658 * @brief Set the callback for retrieving server certificate and matching private key.
Michal Vaskoda514772016-02-01 11:32:01 +0100659 *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100660 * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success,
661 * non-zero an error. On success exactly ONE of \p cert_path or \p cert_data and ONE of
662 * \p privkey_path and \p privkey_data is expected to be set. Those set will be freed.
663 * - \p cert_path expects a PEM file,
664 * - \p cert_data expects a base-64 encoded ASN.1 DER data,
665 * - \p privkey_path expects a PEM file,
666 * - \p privkey_data expects a base-64 encoded ANS.1 DER data,
667 * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key.
668 * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb.
669 * @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 +0100670 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100671void nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data,
672 char **privkey_path, char **privkey_data, int *privkey_data_rsa),
673 void *user_data, void (*free_user_data)(void *user_data));
Michal Vaskoda514772016-02-01 11:32:01 +0100674
675/**
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500676 * @brief Set the callback for retrieving server certificate chain
677 *
678 * @param[in] cert_chain_clb Callback that should return all the certificates of the chain. Zero return indicates success,
679 * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left
680 * NULL. Both will be (deeply) freed.
681 * - \p cert_paths expect an array of PEM files,
682 * - \p cert_path_count number of \p cert_paths array members,
683 * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
684 * - \p cert_data_count number of \p cert_data array members.
685 * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb.
686 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
687 */
688void nc_server_tls_set_server_cert_chain_clb(int (*cert_chain_clb)(const char *name, void *user_data, char ***cert_paths,
689 int *cert_path_count, char ***cert_data, int *cert_data_count),
690 void *user_data, void (*free_user_data)(void *user_data));
691
692/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100693 * @brief Add a trusted certificate list. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100694 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100695 *
Michal Vaskoda514772016-02-01 11:32:01 +0100696 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100697 * @param[in] name Arbitary name identifying this certificate list.
Michal Vasko1a38c862016-01-15 15:50:07 +0100698 * @return 0 on success, -1 on error.
699 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100700int nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100701
Michal Vasko1a38c862016-01-15 15:50:07 +0100702/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100703 * @brief Set the callback for retrieving trusted certificates.
704 *
705 * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success,
706 * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left
707 * NULL. Both will be (deeply) freed.
708 * - \p cert_paths expect an array of PEM files,
709 * - \p cert_path_count number of \p cert_paths array members,
710 * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
711 * - \p cert_data_count number of \p cert_data array members.
712 * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb.
713 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
714 */
715void nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths,
716 int *cert_path_count, char ***cert_data, int *cert_data_count),
717 void *user_data, void (*free_user_data)(void *user_data));
718
719/**
720 * @brief Remove a trusted certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100721 *
Michal Vaskoda514772016-02-01 11:32:01 +0100722 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100723 * @param[in] name Name of the certificate list to delete. NULL deletes all the lists.
724 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100725 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100726int nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100727
Michal Vasko1a38c862016-01-15 15:50:07 +0100728/**
729 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100730 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100731 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100732 *
Michal Vaskoda514772016-02-01 11:32:01 +0100733 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100734 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
735 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
736 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100737 * @return 0 on success, -1 on error.
738 */
Michal Vasko96830e32016-02-01 10:54:18 +0100739int 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 +0100740
Michal Vasko1a38c862016-01-15 15:50:07 +0100741/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100742 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100743 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100744 *
Michal Vaskoda514772016-02-01 11:32:01 +0100745 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100746 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
747 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
748 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100749 * @return 0 on success, -1 on error.
750 */
Michal Vasko96830e32016-02-01 10:54:18 +0100751int 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 +0100752
Michal Vasko1a38c862016-01-15 15:50:07 +0100753/**
Michal Vasko96830e32016-02-01 10:54:18 +0100754 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100755 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100756 *
757 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100758 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100759void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100760
Michal Vasko1a38c862016-01-15 15:50:07 +0100761/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100762 * @brief Add a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100763 *
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100764 * It is possible to add an entry step-by-step, specifying first only \p ip and in later calls
765 * \p fingerprint, \p map_type, and optionally \p name spearately.
Michal Vasko1a38c862016-01-15 15:50:07 +0100766 *
Michal Vaskoda514772016-02-01 11:32:01 +0100767 * @param[in] endpt_name Existing endpoint name.
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100768 * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id
769 * is modified.
770 * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset.
771 * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset.
772 * @param[in] name Specific username used only if \p map_type == NC_TLS_CTN_SPECIFED.
Michal Vasko1a38c862016-01-15 15:50:07 +0100773 * @return 0 on success, -1 on error.
774 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200775int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint,
776 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100777
Michal Vasko1a38c862016-01-15 15:50:07 +0100778/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100779 * @brief Remove a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100780 *
Michal Vaskoda514772016-02-01 11:32:01 +0100781 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100782 * @param[in] id Priority of the entry. -1 matches all the priorities.
783 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
784 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
785 * @param[in] name Specific username for the entry. NULL matches all the usernames.
786 * @return 0 on success, -1 on not finding any match.
787 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200788int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint,
789 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100790
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100791/**
792 * @brief Get a cert-to-name entry.
793 *
794 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
795 * it is filled and returned. If the value is set, it is used as a filter.
796 * Returns first matching entry.
797 *
798 * @param[in] endpt_name Existing endpoint name.
799 * @param[in,out] id Priority of the entry.
800 * @param[in,out] fingerprint Fingerprint fo the entry.
801 * @param[in,out] map_type Mapping type of the entry.
802 * @param[in,out] name Specific username for the entry.
803 * @return 0 on success, -1 on not finding any match.
804 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100805int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
806 char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100807
Michal Vasko7060bcf2016-11-28 14:48:11 +0100808/**
Michal Vasko709598e2016-11-28 14:48:32 +0100809 * @brief Get client certificate.
810 *
811 * @param[in] session Session to get the information from.
812 * @return Const session client certificate.
813 */
814const X509 *nc_session_get_client_cert(const struct nc_session *session);
815
816/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100817 * @brief Set TLS authentication additional verify callback.
818 *
819 * Server will always perform cert-to-name based on its configuration. Only after it passes
820 * and this callback is set, it is also called. It should return exactly what OpenSSL
821 * verify callback meaning 1 for success, 0 to deny the user.
822 *
823 * @param[in] verify_clb Additional user verify callback.
824 */
825void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
826
Radek Krejci6799a052017-05-19 14:23:23 +0200827/**@} Server TLS */
828
Radek Krejci53691be2016-02-22 13:58:37 +0100829#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100830
Michal Vaskof8352352016-05-24 09:11:36 +0200831/**
Radek Krejci6799a052017-05-19 14:23:23 +0200832 * @addtogroup server_session
833 * @{
834 */
835
836/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200837 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200838 *
839 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200840 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200841 */
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200842time_t nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200843
Michal Vasko3486a7c2017-03-03 13:28:07 +0100844/**
845 * @brief Set session notification subscription flag.
846 *
847 * It is used only to ignore timeouts, because they are
848 * ignored for sessions with active subscriptions.
849 *
850 * @param[in] session Session to modify.
851 * @param[in] notif_status 0 for no active subscriptions, non-zero for an active subscription.
852 */
853void nc_session_set_notif_status(struct nc_session *session, int notif_status);
854
855/**
856 * @brief Get session notification subscription flag.
857 *
858 * @param[in] session Session to get the information from.
859 * @return 0 for no active subscription, non-zero for an active subscription.
860 */
861int nc_session_get_notif_status(const struct nc_session *session);
862
Michal Vasko8f430592019-02-26 08:32:54 +0100863/**
864 * @brief Learn whether a session was created using Call Home or not.
865 * Works only for server sessions.
866 *
867 * @param[in] session Session to get the information from.
868 * @return 0 if a standard session, non-zero if a Call Home session.
869 */
870int nc_session_is_callhome(const struct nc_session *session);
871
Radek Krejci6799a052017-05-19 14:23:23 +0200872/**@} Server Session */
873
Michal Vaskoc09730e2019-01-17 10:07:26 +0100874#ifdef __cplusplus
875}
876#endif
877
Michal Vasko086311b2016-01-08 09:53:11 +0100878#endif /* NC_SESSION_SERVER_H_ */