blob: 106a1a159a1a70ee2726a978671fcc0870e5869a [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session server manipulation
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#ifndef NC_SESSION_SERVER_H_
16#define NC_SESSION_SERVER_H_
17
18#include <stdint.h>
Michal Vasko05ba9df2016-01-13 14:40:27 +010019#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020
Michal Vasko709598e2016-11-28 14:48:32 +010021#ifdef NC_ENABLED_TLS
22# include <openssl/x509.h>
23#endif
24
Michal Vasko086311b2016-01-08 09:53:11 +010025#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010026#include "netconf.h"
27
Michal Vasko1a38c862016-01-15 15:50:07 +010028/**
Radek Krejci6799a052017-05-19 14:23:23 +020029 * @defgroup server_session Server Session
30 * @ingroup server
31 *
32 * @brief Server-side NETCONF session manipulation.
33 * @{
34 */
35
36/**
Michal Vasko1a38c862016-01-15 15:50:07 +010037 * @brief Prototype of callbacks that are called if some RPCs are received.
38 *
39 * If \p session termination reason is changed in the callback, one last reply
40 * is sent and then the session is considered invalid.
41 *
Radek Krejci6799a052017-05-19 14:23:23 +020042 * The callback is set via nc_set_global_rpc_clb().
43 *
Michal Vasko1a38c862016-01-15 15:50:07 +010044 * @param[in] rpc Parsed client RPC request.
45 * @param[in] session Session the RPC arrived on.
46 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
47 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010048typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
49
Michal Vasko1a38c862016-01-15 15:50:07 +010050/**
Michal Vasko7b096242016-01-29 15:55:10 +010051 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010052 *
53 * @param[in] session Session to modify.
54 * @param[in] reason Reason of termination.
55 */
56void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
57
58/**
Michal Vasko142cfea2017-08-07 10:12:11 +020059 * @brief Set the session-id of the session responsible for this session's termination.
60 *
61 * @param[in] session Session to modify. Must have term_reason set to #NC_SESSION_TERM_KILLED.
62 * @param[in] sid SID of the killing session.
63 */
64void nc_session_set_killed_by(struct nc_session *session, uint32_t sid);
65
66/**
67 * @brief Set the status of a session.
68 *
69 * @param[in] session Session to modify.
70 * @param[in] status Status of the session.
71 */
72void nc_session_set_status(struct nc_session *session, NC_STATUS status);
73
74/**
Radek Krejci6799a052017-05-19 14:23:23 +020075 * @brief Set a global nc_rpc_clb that is called if the particular RPC request is
76 * received and the private field in the corresponding RPC schema node is NULL.
77 *
78 * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default.
79 */
80void nc_set_global_rpc_clb(nc_rpc_clb clb);
81
82/**@} Server Session */
83
84/**
85 * @addtogroup server
86 * @{
87 */
88
89/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +010090 * @brief Initialize libssh and/or libssl/libcrypto and the server using a libyang context.
Michal Vasko1a38c862016-01-15 15:50:07 +010091 *
92 * The context is not modified internally, only its dictionary is used for holding
Michal Vaskoa43589b2016-02-17 13:24:59 +010093 * all the strings, which is thread-safe. Reading models is considered thread-safe
94 * as models cannot be removed and are rarely modified (augments or deviations).
Michal Vasko1a38c862016-01-15 15:50:07 +010095 *
Michal Vasko3a889fd2016-09-30 12:16:37 +020096 * If the RPC callbacks on schema nodes (mentioned in @ref howtoserver) are modified after
Michal Vasko5494f512016-03-01 12:13:44 +010097 * server initialization with that particular context, they will be called (changes
98 * will take effect). However, there could be race conditions as the access to
99 * these callbacks is not thread-safe.
100 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100101 * Server capabilities are generated based on its content. Changing the context
102 * in ways that result in changed capabilities (adding models, changing features)
103 * is discouraged after sessions are established as it is not possible to change
104 * capabilities of a session.
105 *
106 * This context can safely be destroyed only after calling the last libnetconf2
107 * function in an application.
108 *
Michal Vasko3a889fd2016-09-30 12:16:37 +0200109 * Supported RPCs of models in the context are expected to have their callback
110 * 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 +0100111 * This callback is called by nc_ps_poll() if the particular RPC request is
112 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
113 * only) and ietf-netconf:close-session are set internally if left unset.
114 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100115 * @param[in] ctx Core NETCONF server context.
116 * @return 0 on success, -1 on error.
117 */
Michal Vasko086311b2016-01-08 09:53:11 +0100118int nc_server_init(struct ly_ctx *ctx);
119
Michal Vasko1a38c862016-01-15 15:50:07 +0100120/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100121 * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
122 * server resources.
Michal Vaskob48aa812016-01-18 14:13:09 +0100123 */
124void nc_server_destroy(void);
125
126/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100127 * @brief Set the with-defaults capability extra parameters.
128 *
129 * For the capability to be actually advertised, the server context must also
130 * include the ietf-netconf-with-defaults model.
131 *
132 * Changing this option has the same ill effects as changing capabilities while
133 * sessions are already established.
134 *
135 * @param[in] basic_mode basic-mode with-defaults parameter.
136 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
137 * parameter.
138 * @return 0 on success, -1 on error.
139 */
Michal Vasko086311b2016-01-08 09:53:11 +0100140int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
141
Michal Vasko1a38c862016-01-15 15:50:07 +0100142/**
Michal Vasko55f03972016-04-13 08:56:01 +0200143 * @brief Get with-defaults capability extra parameters.
144 *
145 * At least one argument must be non-NULL.
146 *
147 * @param[in,out] basic_mode basic-mode parameter.
148 * @param[in,out] also_supported also-supported parameter.
149 */
150void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
151
152/**
Radek Krejci658782b2016-12-04 22:04:55 +0100153 * @brief Set capability of the server.
Michal Vasko1a38c862016-01-15 15:50:07 +0100154 *
Radek Krejci658782b2016-12-04 22:04:55 +0100155 * Capability can be used when some behavior or extension of the server is not defined
156 * as a YANG module. The provided value will be advertised in the server's \<hello\>
157 * messages. Note, that libnetconf only checks that the provided value is non-empty
158 * string.
Michal Vasko1a38c862016-01-15 15:50:07 +0100159 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100160 * @param[in] value Capability string to be advertised in server's \<hello\> messages.
Michal Vasko1a38c862016-01-15 15:50:07 +0100161 */
Radek Krejci658782b2016-12-04 22:04:55 +0100162int nc_server_set_capability(const char *value);
Michal Vasko55f03972016-04-13 08:56:01 +0200163
164/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100165 * @brief Set server timeout for receiving a hello message.
166 *
167 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
168 */
169void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100170
Michal Vasko1a38c862016-01-15 15:50:07 +0100171/**
Michal Vasko55f03972016-04-13 08:56:01 +0200172 * @brief get server timeout for receiving a hello message.
173 *
174 * @return Hello message timeout, 0 is infinite.
175 */
176uint16_t nc_server_get_hello_timeout(void);
177
178/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100179 * @brief Set server timeout for dropping an idle session.
180 *
181 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100182 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100183 */
184void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100185
Michal Vasko1a38c862016-01-15 15:50:07 +0100186/**
Michal Vasko55f03972016-04-13 08:56:01 +0200187 * @brief Get server timeout for dropping an idle session.
188 *
189 * @return Idle session timeout, 0 for for never dropping
190 * a session because of inactivity.
191 */
192uint16_t nc_server_get_idle_timeout(void);
193
194/**
Radek Krejci24a18412018-05-16 15:09:10 +0200195 * @brief Get all the server capabilities including all the schemas.
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200196 *
197 * A few capabilities (with-defaults, interleave) depend on the current
198 * server options.
199 *
200 * @param[in] ctx Context to read most capabilities from.
201 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
202 */
203const char **nc_server_get_cpblts(struct ly_ctx *ctx);
204
Radek Krejci24a18412018-05-16 15:09:10 +0200205/**
206 * @brief Get the server capabilities including the schemas with the specified YANG version.
207 *
208 * A few capabilities (with-defaults, interleave) depend on the current
209 * server options.
210 *
211 * @param[in] ctx Context to read most capabilities from.
212 * @param[in] version YANG version of the schemas to be included in result, with
213 * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts().
214 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
215 */
216const char **nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version);
217
Radek Krejci6799a052017-05-19 14:23:23 +0200218/**@} Server */
219
220/**
221 * @addtogroup server_session
222 * @{
223 */
224
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200225/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100226 * @brief Accept a new session on a pre-established transport session.
227 *
228 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
229 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
230 * @param[in] username NETCONF username as provided by the transport protocol.
231 * @param[out] session New session on success.
Michal Vasko71090fc2016-05-24 16:37:28 +0200232 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
233 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100234 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200235NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100236
Michal Vasko1a38c862016-01-15 15:50:07 +0100237/**
238 * @brief Create an empty structure for polling sessions.
239 *
240 * @return Empty pollsession structure, NULL on error.
241 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100242struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100243
Michal Vasko1a38c862016-01-15 15:50:07 +0100244/**
245 * @brief Free a pollsession structure.
246 *
Michal Vasko01082bf2016-04-07 10:44:21 +0200247 * !IMPORTANT! Make sure that \p ps is not accessible (is not used)
248 * by any thread before and after this call!
249 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100250 * @param[in] ps Pollsession structure to free.
251 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100252void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100253
Michal Vasko1a38c862016-01-15 15:50:07 +0100254/**
255 * @brief Add a session to a pollsession structure.
256 *
257 * @param[in] ps Pollsession structure to modify.
258 * @param[in] session Session to add to \p ps.
259 * @return 0 on success, -1 on error.
260 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100261int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100262
Michal Vasko1a38c862016-01-15 15:50:07 +0100263/**
264 * @brief Remove a session from a pollsession structure.
265 *
266 * @param[in] ps Pollsession structure to modify.
267 * @param[in] session Session to remove from \p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100268 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100269 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100270int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
271
Michal Vasko1a38c862016-01-15 15:50:07 +0100272/**
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100273 * @brief Get a session from a pollsession structure matching the session ID.
274 *
275 * @param[in] ps Pollsession structure to read from.
Michal Vasko4871c9d2017-10-09 14:48:39 +0200276 * @param[in] idx Index of the session.
277 * @return Session on index, NULL if out-of-bounds.
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100278 */
Michal Vasko4871c9d2017-10-09 14:48:39 +0200279struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx);
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100280
281/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100282 * @brief Learn the number of sessions in a pollsession structure.
283 *
Michal Vaskof4462fd2017-02-15 14:29:05 +0100284 * Does not lock \p ps structure for efficiency.
285 *
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100286 * @param[in] ps Pollsession structure to check.
Michal Vaskoc72d0e62016-07-26 11:36:11 +0200287 * @return Number of sessions (even invalid ones) in \p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100288 */
289uint16_t nc_ps_session_count(struct nc_pollsession *ps);
290
Michal Vasko30a5d6b2017-02-15 14:29:39 +0100291#define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */
292#define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */
293#define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */
294#define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */
295#define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */
296#define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */
297#define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
298#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200299
300#ifdef NC_ENABLED_SSH
Michal Vaskoe645de32017-05-26 14:02:50 +0200301# define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */
302# 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 +0200303#endif
304
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100305/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100306 * @brief Poll sessions and process any received RPCs.
307 *
Michal Vaskoe4300a82017-05-24 10:35:42 +0200308 * Only one event on one session is handled in one function call. If this event
309 * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session
310 * should be removed from \p ps.
Michal Vasko1a38c862016-01-15 15:50:07 +0100311 *
312 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100313 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100314 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200315 * @param[in] session Session that was processed and that specific return bits concern.
316 * Can be NULL.
Michal Vaskoade892d2017-02-22 13:40:35 +0100317 * @return Bitfield of NC_PSPOLL_* macros.
Michal Vasko1a38c862016-01-15 15:50:07 +0100318 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200319int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100320
Michal Vasko11d142a2016-01-19 15:58:24 +0100321/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100322 * @brief Remove sessions from a pollsession structure and
323 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100324 *
Michal Vaskoade892d2017-02-22 13:40:35 +0100325 * 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 +0100326 *
327 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100328 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100329 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100330 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100331void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100332
Radek Krejci53691be2016-02-22 13:58:37 +0100333#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100334
Radek Krejci6799a052017-05-19 14:23:23 +0200335/**@} Server Session */
336
337/**
338 * @addtogroup server
339 * @{
340 */
341
Michal Vasko1a38c862016-01-15 15:50:07 +0100342/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200343 * @brief Add a new endpoint.
344 *
345 * Before the endpoint can accept any connections, its address and port must
Radek Krejci6799a052017-05-19 14:23:23 +0200346 * be set via nc_server_endpt_set_address() and nc_server_endpt_set_port().
Michal Vaskoe2713da2016-08-22 16:06:40 +0200347 *
348 * @param[in] name Arbitrary unique endpoint name.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200349 * @param[in] ti Transport protocol to use.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200350 * @return 0 on success, -1 on error.
351 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200352int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200353
354/**
355 * @brief Stop listening on and remove an endpoint.
356 *
357 * @param[in] name Endpoint name. NULL matches all endpoints.
Michal Vasko59050372016-11-22 14:33:55 +0100358 * @param[in] ti Endpoint transport protocol. NULL matches any protocol.
359 * Redundant to set if \p name is set, endpoint names are
360 * unique disregarding their protocol.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200361 * @return 0 on success, -1 on not finding any match.
362 */
Michal Vasko59050372016-11-22 14:33:55 +0100363int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200364
365/**
Michal Vaskoe8e07702017-03-15 10:19:30 +0100366 * @brief Get the number of currently configured listening endpoints.
367 * Note that an ednpoint without address and/or port will be included
368 * even though it is not, in fact, listening.
369 *
370 * @return Number of added listening endpoints.
371 */
372int nc_server_endpt_count(void);
373
374/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200375 * @brief Change endpoint listening address.
376 *
377 * On error the previous listening socket (if any) is left untouched.
378 *
379 * @param[in] endpt_name Existing endpoint name.
380 * @param[in] address New listening address.
381 * @return 0 on success, -1 on error.
382 */
383int nc_server_endpt_set_address(const char *endpt_name, const char *address);
384
385/**
386 * @brief Change endpoint listening port.
387 *
388 * On error the previous listening socket (if any) is left untouched.
389 *
390 * @param[in] endpt_name Existing endpoint name.
391 * @param[in] port New listening port.
392 * @return 0 on success, -1 on error.
393 */
394int nc_server_endpt_set_port(const char *endpt_name, uint16_t port);
Michal Vasko9e036d52016-01-08 10:49:26 +0100395
Radek Krejci6799a052017-05-19 14:23:23 +0200396/**@} Server */
397
398/**
399 * @addtogroup server_session
400 */
401
Michal Vasko1a38c862016-01-15 15:50:07 +0100402/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100403 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100404 *
Michal Vaskob70c8b82017-03-17 09:09:29 +0100405 * Once a new (TCP/IP) conection is established a different (quite long) timeout
406 * is used for waiting for transport-related data, which means this call can block
407 * for much longer that \p timeout, but only with slow/faulty/malicious clients.
408 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100409 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko71090fc2016-05-24 16:37:28 +0200410 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100411 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200412 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
413 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100414 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200415NC_MSG_TYPE nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100416
Radek Krejci53691be2016-02-22 13:58:37 +0100417#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100418
Radek Krejci53691be2016-02-22 13:58:37 +0100419#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100420
Michal Vasko1a38c862016-01-15 15:50:07 +0100421/**
Michal Vasko71090fc2016-05-24 16:37:28 +0200422 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF \p orig_session.
Michal Vaskoade892d2017-02-22 13:40:35 +0100423 * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on \p orig_session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200424 *
425 * @param[in] orig_session Session that has a new SSH channel ready.
426 * @param[out] session New session.
427 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
428 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
429 */
430NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
431
432/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100433 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vaskoade892d2017-02-22 13:40:35 +0100434 * 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 +0100435 * The new session is only returned in \p session, it is not added to \p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100436 *
437 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
438 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200439 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
440 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100441 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200442NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100443
Radek Krejci6799a052017-05-19 14:23:23 +0200444/**@} Server Session */
445
446/**
447 * @defgroup server_ssh Server SSH
448 * @ingroup server
449 *
450 * @brief Server-side settings for SSH connections.
451 * @{
452 */
453
Michal Vasko96164bf2016-01-21 15:41:58 +0100454/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100455 * @brief Add an authorized client SSH public key. This public key can be used for
456 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100457 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100458 * @param[in] pubkey_base64 Authorized public key binary content encoded in base64.
459 * @param[in] type Authorized public key SSH type.
460 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100461 * @return 0 on success, -1 on error.
462 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100463int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100464
465/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100466 * @brief Add an authorized client SSH public key. This public key can be used for
467 * publickey authentication (for any SSH connection, even Call Home) afterwards.
Michal Vaskoda514772016-02-01 11:32:01 +0100468 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100469 * @param[in] pubkey_path Path to the public key.
470 * @param[in] username Username that the client with the public key must use.
Michal Vaskoda514772016-02-01 11:32:01 +0100471 * @return 0 on success, -1 on error.
472 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100473int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username);
Michal Vaskoda514772016-02-01 11:32:01 +0100474
475/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100476 * @brief Remove an authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100477 *
Michal Vasko17dfda92016-12-01 14:06:16 +0100478 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
479 * @param[in] pubkey_base64 Authorized public key content. NULL matches any key.
480 * @param[in] type Authorized public key type. 0 matches all types.
481 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
482 * @return 0 on success, -1 on not finding any match.
Michal Vasko1a38c862016-01-15 15:50:07 +0100483 */
Michal Vasko17dfda92016-12-01 14:06:16 +0100484int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
485 const char *username);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200486
487/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100488 * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself
489 * wil be retrieved using a callback.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200490 *
491 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100492 * @param[in] name Arbitrary name of the host key.
Michal Vasko7d255882017-02-09 13:35:08 +0100493 * @param[in] idx Optional index where to add the key. -1 adds at the end.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200494 * @return 0 on success, -1 on error.
495 */
Michal Vasko7d255882017-02-09 13:35:08 +0100496int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200497
498/**
Michal Vaskoebba7602018-03-23 13:14:08 +0100499 * @brief Set the callback for SSH password authentication. If none is set, local system users are used.
500 *
501 * @param[in] passwd_auth_clb Callback that should authenticate the user. Username can be directly obtained from \p session.
502 * Zero return indicates success, non-zero an error.
503 * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb.
504 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
505 */
506void nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password,
507 void *user_data),
508 void *user_data, void (*free_user_data)(void *user_data));
509
510/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100511 * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However,
512 * a maximum of one key of each type will be used during SSH authentication, later keys replacing
513 * the earlier ones.
514 *
515 * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero
516 * an error. On success exactly ONE of \p privkey_path or \p privkey_data is expected
517 * to be set. The one set will be freed.
518 * - \p privkey_path expects a PEM file,
519 * - \p privkey_data expects a base-64 encoded ANS.1 DER data,
520 * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key.
521 * @param[in] user_data Optional arbitrary user data that will be passed to \p hostkey_clb.
522 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
523 */
524void nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
525 char **privkey_data, int *privkey_data_rsa),
526 void *user_data, void (*free_user_data)(void *user_data));
527
528/**
529 * @brief Delete endpoint SSH host key. Their order is preserved.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200530 *
531 * @param[in] endpt_name Existing endpoint name.
Michal Vasko7d255882017-02-09 13:35:08 +0100532 * @param[in] name Name of the host key. NULL matches all the keys, but if \p idx != -1 then this must be NULL.
533 * @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 +0200534 * @return 0 on success, -1 on error.
535 */
Michal Vasko7d255882017-02-09 13:35:08 +0100536int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx);
Michal Vasko086311b2016-01-08 09:53:11 +0100537
Michal Vasko1a38c862016-01-15 15:50:07 +0100538/**
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100539 * @brief Move endpoint SSH host key.
540 *
541 * @param[in] endpt_name Exisitng endpoint name.
542 * @param[in] key_mov Name of the host key that will be moved.
543 * @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.
544 * @return 0 in success, -1 on error.
545 */
546int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after);
547
548/**
549 * @brief Modify endpoint SSH host key.
550 *
551 * @param[in] endpt_name Exisitng endpoint name.
552 * @param[in] name Name of an existing host key.
553 * @param[in] new_name New name of the host key \p name.
554 * @return 0 in success, -1 on error.
555 */
556int nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name);
Michal Vasko086311b2016-01-08 09:53:11 +0100557
Michal Vasko1a38c862016-01-15 15:50:07 +0100558/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100559 * @brief Set endpoint SSH banner the server will send to every client.
Michal Vasko1a38c862016-01-15 15:50:07 +0100560 *
Michal Vaskoda514772016-02-01 11:32:01 +0100561 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100562 * @param[in] banner SSH banner.
563 * @return 0 on success, -1 on error.
564 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100565int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner);
Michal Vasko086311b2016-01-08 09:53:11 +0100566
Michal Vasko1a38c862016-01-15 15:50:07 +0100567/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100568 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100569 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100570 *
Michal Vaskoda514772016-02-01 11:32:01 +0100571 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100572 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
573 * @return 0 on success, -1 on error.
574 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100575int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100576
Michal Vasko1a38c862016-01-15 15:50:07 +0100577/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100578 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100579 *
Michal Vaskoda514772016-02-01 11:32:01 +0100580 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100581 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100582 * @return 0 on success, -1 on error.
583 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100584int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100585
Michal Vasko1a38c862016-01-15 15:50:07 +0100586/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100587 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100588 *
Michal Vaskoda514772016-02-01 11:32:01 +0100589 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100590 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
591 * @return 0 on success, -1 on error.
592 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100593int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100594
Radek Krejci6799a052017-05-19 14:23:23 +0200595/**@} Server SSH */
596
Radek Krejci53691be2016-02-22 13:58:37 +0100597#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100598
Radek Krejci53691be2016-02-22 13:58:37 +0100599#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100600
Michal Vasko1a38c862016-01-15 15:50:07 +0100601/**
Radek Krejci6799a052017-05-19 14:23:23 +0200602 * @defgroup server_tls Server TLS
603 * @ingroup server
604 *
605 * @brief Server-side settings for TLS connections.
606 * @{
607 */
608
609/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100610 * @brief Set the server TLS certificate. Only the name is set, the certificate itself
611 * wil be retrieved using a callback.
Michal Vaskoda514772016-02-01 11:32:01 +0100612 *
613 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100614 * @param[in] name Arbitrary certificate name.
Michal Vaskoda514772016-02-01 11:32:01 +0100615 * @return 0 on success, -1 on error.
616 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100617int nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name);
Michal Vaskoda514772016-02-01 11:32:01 +0100618
619/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100620 * @brief Set the callback for retrieving server certificate and matching private key.
Michal Vaskoda514772016-02-01 11:32:01 +0100621 *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100622 * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success,
623 * non-zero an error. On success exactly ONE of \p cert_path or \p cert_data and ONE of
624 * \p privkey_path and \p privkey_data is expected to be set. Those set will be freed.
625 * - \p cert_path expects a PEM file,
626 * - \p cert_data expects a base-64 encoded ASN.1 DER data,
627 * - \p privkey_path expects a PEM file,
628 * - \p privkey_data expects a base-64 encoded ANS.1 DER data,
629 * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key.
630 * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb.
631 * @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 +0100632 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100633void nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data,
634 char **privkey_path, char **privkey_data, int *privkey_data_rsa),
635 void *user_data, void (*free_user_data)(void *user_data));
Michal Vaskoda514772016-02-01 11:32:01 +0100636
637/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100638 * @brief Add a trusted certificate list. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100639 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100640 *
Michal Vaskoda514772016-02-01 11:32:01 +0100641 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100642 * @param[in] name Arbitary name identifying this certificate list.
Michal Vasko1a38c862016-01-15 15:50:07 +0100643 * @return 0 on success, -1 on error.
644 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100645int nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100646
Michal Vasko1a38c862016-01-15 15:50:07 +0100647/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100648 * @brief Set the callback for retrieving trusted certificates.
649 *
650 * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success,
651 * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left
652 * NULL. Both will be (deeply) freed.
653 * - \p cert_paths expect an array of PEM files,
654 * - \p cert_path_count number of \p cert_paths array members,
655 * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data,
656 * - \p cert_data_count number of \p cert_data array members.
657 * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb.
658 * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data.
659 */
660void nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths,
661 int *cert_path_count, char ***cert_data, int *cert_data_count),
662 void *user_data, void (*free_user_data)(void *user_data));
663
664/**
665 * @brief Remove a trusted certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100666 *
Michal Vaskoda514772016-02-01 11:32:01 +0100667 * @param[in] endpt_name Existing endpoint name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100668 * @param[in] name Name of the certificate list to delete. NULL deletes all the lists.
669 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100670 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100671int nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100672
Michal Vasko1a38c862016-01-15 15:50:07 +0100673/**
674 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100675 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100676 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100677 *
Michal Vaskoda514772016-02-01 11:32:01 +0100678 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100679 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
680 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
681 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100682 * @return 0 on success, -1 on error.
683 */
Michal Vasko96830e32016-02-01 10:54:18 +0100684int 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 +0100685
Michal Vasko1a38c862016-01-15 15:50:07 +0100686/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100687 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100688 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100689 *
Michal Vaskoda514772016-02-01 11:32:01 +0100690 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100691 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
692 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
693 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100694 * @return 0 on success, -1 on error.
695 */
Michal Vasko96830e32016-02-01 10:54:18 +0100696int 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 +0100697
Michal Vasko1a38c862016-01-15 15:50:07 +0100698/**
Michal Vasko96830e32016-02-01 10:54:18 +0100699 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100700 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100701 *
702 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100703 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100704void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100705
Michal Vasko1a38c862016-01-15 15:50:07 +0100706/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100707 * @brief Add a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100708 *
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100709 * It is possible to add an entry step-by-step, specifying first only \p ip and in later calls
710 * \p fingerprint, \p map_type, and optionally \p name spearately.
Michal Vasko1a38c862016-01-15 15:50:07 +0100711 *
Michal Vaskoda514772016-02-01 11:32:01 +0100712 * @param[in] endpt_name Existing endpoint name.
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100713 * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id
714 * is modified.
715 * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset.
716 * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset.
717 * @param[in] name Specific username used only if \p map_type == NC_TLS_CTN_SPECIFED.
Michal Vasko1a38c862016-01-15 15:50:07 +0100718 * @return 0 on success, -1 on error.
719 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200720int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint,
721 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100722
Michal Vasko1a38c862016-01-15 15:50:07 +0100723/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100724 * @brief Remove a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100725 *
Michal Vaskoda514772016-02-01 11:32:01 +0100726 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100727 * @param[in] id Priority of the entry. -1 matches all the priorities.
728 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
729 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
730 * @param[in] name Specific username for the entry. NULL matches all the usernames.
731 * @return 0 on success, -1 on not finding any match.
732 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200733int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint,
734 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100735
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100736/**
737 * @brief Get a cert-to-name entry.
738 *
739 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
740 * it is filled and returned. If the value is set, it is used as a filter.
741 * Returns first matching entry.
742 *
743 * @param[in] endpt_name Existing endpoint name.
744 * @param[in,out] id Priority of the entry.
745 * @param[in,out] fingerprint Fingerprint fo the entry.
746 * @param[in,out] map_type Mapping type of the entry.
747 * @param[in,out] name Specific username for the entry.
748 * @return 0 on success, -1 on not finding any match.
749 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100750int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
751 char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100752
Michal Vasko7060bcf2016-11-28 14:48:11 +0100753/**
Michal Vasko709598e2016-11-28 14:48:32 +0100754 * @brief Get client certificate.
755 *
756 * @param[in] session Session to get the information from.
757 * @return Const session client certificate.
758 */
759const X509 *nc_session_get_client_cert(const struct nc_session *session);
760
761/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100762 * @brief Set TLS authentication additional verify callback.
763 *
764 * Server will always perform cert-to-name based on its configuration. Only after it passes
765 * and this callback is set, it is also called. It should return exactly what OpenSSL
766 * verify callback meaning 1 for success, 0 to deny the user.
767 *
768 * @param[in] verify_clb Additional user verify callback.
769 */
770void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
771
Radek Krejci6799a052017-05-19 14:23:23 +0200772/**@} Server TLS */
773
Radek Krejci53691be2016-02-22 13:58:37 +0100774#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100775
Michal Vaskof8352352016-05-24 09:11:36 +0200776/**
Radek Krejci6799a052017-05-19 14:23:23 +0200777 * @addtogroup server_session
778 * @{
779 */
780
781/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200782 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200783 *
784 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200785 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200786 */
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200787time_t nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200788
Michal Vasko3486a7c2017-03-03 13:28:07 +0100789/**
790 * @brief Set session notification subscription flag.
791 *
792 * It is used only to ignore timeouts, because they are
793 * ignored for sessions with active subscriptions.
794 *
795 * @param[in] session Session to modify.
796 * @param[in] notif_status 0 for no active subscriptions, non-zero for an active subscription.
797 */
798void nc_session_set_notif_status(struct nc_session *session, int notif_status);
799
800/**
801 * @brief Get session notification subscription flag.
802 *
803 * @param[in] session Session to get the information from.
804 * @return 0 for no active subscription, non-zero for an active subscription.
805 */
806int nc_session_get_notif_status(const struct nc_session *session);
807
Radek Krejci6799a052017-05-19 14:23:23 +0200808/**@} Server Session */
809
Michal Vasko086311b2016-01-08 09:53:11 +0100810#endif /* NC_SESSION_SERVER_H_ */