blob: a7f36539487c00887d7845b56177068787770dc0 [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/**
29 * @brief Prototype of callbacks that are called if some RPCs are received.
30 *
31 * If \p session termination reason is changed in the callback, one last reply
32 * is sent and then the session is considered invalid.
33 *
34 * @param[in] rpc Parsed client RPC request.
35 * @param[in] session Session the RPC arrived on.
36 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
37 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010038typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
39
Michal Vasko1a38c862016-01-15 15:50:07 +010040/**
Michal Vasko7b096242016-01-29 15:55:10 +010041 * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks.
Michal Vasko1a38c862016-01-15 15:50:07 +010042 *
43 * @param[in] session Session to modify.
44 * @param[in] reason Reason of termination.
45 */
46void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
47
48/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +010049 * @brief Initialize libssh and/or libssl/libcrypto and the server using a libyang context.
Michal Vasko1a38c862016-01-15 15:50:07 +010050 *
51 * The context is not modified internally, only its dictionary is used for holding
Michal Vaskoa43589b2016-02-17 13:24:59 +010052 * all the strings, which is thread-safe. Reading models is considered thread-safe
53 * as models cannot be removed and are rarely modified (augments or deviations).
Michal Vasko1a38c862016-01-15 15:50:07 +010054 *
Michal Vasko3a889fd2016-09-30 12:16:37 +020055 * If the RPC callbacks on schema nodes (mentioned in @ref howtoserver) are modified after
Michal Vasko5494f512016-03-01 12:13:44 +010056 * server initialization with that particular context, they will be called (changes
57 * will take effect). However, there could be race conditions as the access to
58 * these callbacks is not thread-safe.
59 *
Michal Vasko1a38c862016-01-15 15:50:07 +010060 * Server capabilities are generated based on its content. Changing the context
61 * in ways that result in changed capabilities (adding models, changing features)
62 * is discouraged after sessions are established as it is not possible to change
63 * capabilities of a session.
64 *
65 * This context can safely be destroyed only after calling the last libnetconf2
66 * function in an application.
67 *
Michal Vasko3a889fd2016-09-30 12:16:37 +020068 * Supported RPCs of models in the context are expected to have their callback
69 * 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 +010070 * This callback is called by nc_ps_poll() if the particular RPC request is
71 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
72 * only) and ietf-netconf:close-session are set internally if left unset.
73 *
Michal Vasko1a38c862016-01-15 15:50:07 +010074 * @param[in] ctx Core NETCONF server context.
75 * @return 0 on success, -1 on error.
76 */
Michal Vasko086311b2016-01-08 09:53:11 +010077int nc_server_init(struct ly_ctx *ctx);
78
Michal Vasko1a38c862016-01-15 15:50:07 +010079/**
Michal Vaskoa7b8ca52016-03-01 12:09:29 +010080 * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
81 * server resources.
Michal Vaskob48aa812016-01-18 14:13:09 +010082 */
83void nc_server_destroy(void);
84
85/**
Michal Vasko1a38c862016-01-15 15:50:07 +010086 * @brief Set the with-defaults capability extra parameters.
87 *
88 * For the capability to be actually advertised, the server context must also
89 * include the ietf-netconf-with-defaults model.
90 *
91 * Changing this option has the same ill effects as changing capabilities while
92 * sessions are already established.
93 *
94 * @param[in] basic_mode basic-mode with-defaults parameter.
95 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
96 * parameter.
97 * @return 0 on success, -1 on error.
98 */
Michal Vasko086311b2016-01-08 09:53:11 +010099int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
100
Michal Vasko1a38c862016-01-15 15:50:07 +0100101/**
Michal Vasko55f03972016-04-13 08:56:01 +0200102 * @brief Get with-defaults capability extra parameters.
103 *
104 * At least one argument must be non-NULL.
105 *
106 * @param[in,out] basic_mode basic-mode parameter.
107 * @param[in,out] also_supported also-supported parameter.
108 */
109void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported);
110
111/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100112 * @brief Set the interleave capability.
113 *
114 * For the capability to be actually advertised, the server context must also
115 * include the nc-notifications model.
116 *
117 * Changing this option has the same ill effects as changing capabilities while
118 * sessions are already established.
119 *
120 * @param[in] interleave_support 1 to suport interleave, 0 to not.
121 */
122void nc_server_set_capab_interleave(int interleave_support);
Michal Vasko086311b2016-01-08 09:53:11 +0100123
Michal Vasko1a38c862016-01-15 15:50:07 +0100124/**
Michal Vasko55f03972016-04-13 08:56:01 +0200125 * @brief Get the interleave capability state.
126 *
127 * @return 1 for supported, 0 for not supported.
128 */
129int nc_server_get_capab_interleave(void);
130
131/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100132 * @brief Set server timeout for receiving a hello message.
133 *
134 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
135 */
136void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100137
Michal Vasko1a38c862016-01-15 15:50:07 +0100138/**
Michal Vasko55f03972016-04-13 08:56:01 +0200139 * @brief get server timeout for receiving a hello message.
140 *
141 * @return Hello message timeout, 0 is infinite.
142 */
143uint16_t nc_server_get_hello_timeout(void);
144
145/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100146 * @brief Set server timeout for dropping an idle session.
147 *
148 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
Michal Vaskof0537d82016-01-29 14:42:38 +0100149 * because of inactivity.
Michal Vasko1a38c862016-01-15 15:50:07 +0100150 */
151void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100152
Michal Vasko1a38c862016-01-15 15:50:07 +0100153/**
Michal Vasko55f03972016-04-13 08:56:01 +0200154 * @brief Get server timeout for dropping an idle session.
155 *
156 * @return Idle session timeout, 0 for for never dropping
157 * a session because of inactivity.
158 */
159uint16_t nc_server_get_idle_timeout(void);
160
161/**
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200162 * @brief Get all the server capabilities as will be sent to every client.
163 *
164 * A few capabilities (with-defaults, interleave) depend on the current
165 * server options.
166 *
167 * @param[in] ctx Context to read most capabilities from.
168 * @return Array of capabilities stored in the \p ctx dictionary, NULL on error.
169 */
170const char **nc_server_get_cpblts(struct ly_ctx *ctx);
171
172/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100173 * @brief Accept a new session on a pre-established transport session.
174 *
175 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
176 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
177 * @param[in] username NETCONF username as provided by the transport protocol.
178 * @param[out] session New session on success.
Michal Vasko71090fc2016-05-24 16:37:28 +0200179 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
180 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100181 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200182NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100183
Michal Vasko1a38c862016-01-15 15:50:07 +0100184/**
fanchanghu966f2de2016-07-21 02:28:57 -0400185 * @brief Set a global nc_rpc_clb that is called if the particular RPC request is
186 * received and the private field in the corresponding RPC schema node is NULL.
187 *
188 * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default.
189 */
190void nc_set_global_rpc_clb(nc_rpc_clb clb);
191
192/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100193 * @brief Create an empty structure for polling sessions.
194 *
195 * @return Empty pollsession structure, NULL on error.
196 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100197struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100198
Michal Vasko1a38c862016-01-15 15:50:07 +0100199/**
200 * @brief Free a pollsession structure.
201 *
Michal Vasko01082bf2016-04-07 10:44:21 +0200202 * !IMPORTANT! Make sure that \p ps is not accessible (is not used)
203 * by any thread before and after this call!
204 *
Michal Vasko1a38c862016-01-15 15:50:07 +0100205 * @param[in] ps Pollsession structure to free.
206 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100207void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100208
Michal Vasko1a38c862016-01-15 15:50:07 +0100209/**
210 * @brief Add a session to a pollsession structure.
211 *
212 * @param[in] ps Pollsession structure to modify.
213 * @param[in] session Session to add to \p ps.
214 * @return 0 on success, -1 on error.
215 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100216int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100217
Michal Vasko1a38c862016-01-15 15:50:07 +0100218/**
219 * @brief Remove a session from a pollsession structure.
220 *
221 * @param[in] ps Pollsession structure to modify.
222 * @param[in] session Session to remove from \p ps.
Michal Vaskof0537d82016-01-29 14:42:38 +0100223 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100224 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100225int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
226
Michal Vasko1a38c862016-01-15 15:50:07 +0100227/**
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100228 * @brief Learn the number of sessions in a pollsession structure.
229 *
230 * @param[in] ps Pollsession structure to check.
Michal Vaskoc72d0e62016-07-26 11:36:11 +0200231 * @return Number of sessions (even invalid ones) in \p ps, -1 on error.
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100232 */
233uint16_t nc_ps_session_count(struct nc_pollsession *ps);
234
Michal Vasko71090fc2016-05-24 16:37:28 +0200235#define NC_PSPOLL_TIMEOUT 0x0001 /**< Timeout elapsed. */
236#define NC_PSPOLL_RPC 0x0002 /**< RPC was correctly parsed and processed. */
237#define NC_PSPOLL_BAD_RPC 0x0004 /**< RPC was received, but failed to be parsed. */
238#define NC_PSPOLL_REPLY_ERROR 0x0008 /**< Response to the RPC was a \<rpc-reply\> of type error. */
239#define NC_PSPOLL_SESSION_TERM 0x0010 /**< Some session was terminated. */
Michal Vaskoc72d0e62016-07-26 11:36:11 +0200240#define NC_PSPOLL_SESSION_ERROR 0x0020 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */
Michal Vasko71090fc2016-05-24 16:37:28 +0200241#define NC_PSPOLL_PENDING 0x0040 /**< Unhandled pending events on other session. */
242#define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */
243
244#ifdef NC_ENABLED_SSH
245# define NC_PSPOLL_SSH_MSG 0x0100 /**< SSH message received (and processed, if relevant, only with SSH support). */
246# define NC_PSPOLL_SSH_CHANNEL 0x0200 /**< New SSH channel opened on an existing session (only with SSH support). */
247#endif
248
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100249/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100250 * @brief Poll sessions and process any received RPCs.
251 *
Michal Vasko71090fc2016-05-24 16:37:28 +0200252 * All the sessions must be running. Only one event on one session
Michal Vasko96164bf2016-01-21 15:41:58 +0100253 * is handled in one function call.
Michal Vasko1a38c862016-01-15 15:50:07 +0100254 *
255 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100256 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
Michal Vasko96830e32016-02-01 10:54:18 +0100257 * infinite waiting.
Michal Vasko71090fc2016-05-24 16:37:28 +0200258 * @param[in] session Session that was processed and that specific return bits concern.
259 * Can be NULL.
260 * @return Bitfield of NC_PSPOLL_* macros, almost any combination can be returned.
Michal Vasko1a38c862016-01-15 15:50:07 +0100261 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200262int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100263
Michal Vasko11d142a2016-01-19 15:58:24 +0100264/**
Michal Vaskocad3ac42016-03-01 09:06:01 +0100265 * @brief Remove sessions from a pollsession structure and
266 * call nc_session_free() on them.
Michal Vaskod09eae62016-02-01 10:32:52 +0100267 *
Michal Vaskocad3ac42016-03-01 09:06:01 +0100268 * Calling this function with \p all false makes sense if nc_ps_poll() returned 3.
Michal Vaskod09eae62016-02-01 10:32:52 +0100269 *
270 * @param[in] ps Pollsession structure to clear.
Michal Vaskocad3ac42016-03-01 09:06:01 +0100271 * @param[in] all Whether to free all sessions, or only the invalid ones.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100272 * @param[in] data_free Session user data destructor.
Michal Vaskod09eae62016-02-01 10:32:52 +0100273 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100274void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *));
Michal Vaskod09eae62016-02-01 10:32:52 +0100275
Radek Krejci53691be2016-02-22 13:58:37 +0100276#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100277
Michal Vasko1a38c862016-01-15 15:50:07 +0100278/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200279 * @brief Add a new endpoint.
280 *
281 * Before the endpoint can accept any connections, its address and port must
Michal Vasko2e6defd2016-10-07 15:48:15 +0200282 * be set.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200283 *
284 * @param[in] name Arbitrary unique endpoint name.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200285 * @param[in] ti Transport protocol to use.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200286 * @return 0 on success, -1 on error.
287 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200288int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200289
290/**
291 * @brief Stop listening on and remove an endpoint.
292 *
293 * @param[in] name Endpoint name. NULL matches all endpoints.
Michal Vasko59050372016-11-22 14:33:55 +0100294 * @param[in] ti Endpoint transport protocol. NULL matches any protocol.
295 * Redundant to set if \p name is set, endpoint names are
296 * unique disregarding their protocol.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200297 * @return 0 on success, -1 on not finding any match.
298 */
Michal Vasko59050372016-11-22 14:33:55 +0100299int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200300
301/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200302 * @brief Change endpoint listening address.
303 *
304 * On error the previous listening socket (if any) is left untouched.
305 *
306 * @param[in] endpt_name Existing endpoint name.
307 * @param[in] address New listening address.
308 * @return 0 on success, -1 on error.
309 */
310int nc_server_endpt_set_address(const char *endpt_name, const char *address);
311
312/**
313 * @brief Change endpoint listening port.
314 *
315 * On error the previous listening socket (if any) is left untouched.
316 *
317 * @param[in] endpt_name Existing endpoint name.
318 * @param[in] port New listening port.
319 * @return 0 on success, -1 on error.
320 */
321int nc_server_endpt_set_port(const char *endpt_name, uint16_t port);
322
323/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100324 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100325 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100326 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
Michal Vasko71090fc2016-05-24 16:37:28 +0200327 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100328 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200329 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
330 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko1a38c862016-01-15 15:50:07 +0100331 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200332NC_MSG_TYPE nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100333
Radek Krejci53691be2016-02-22 13:58:37 +0100334#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100335
Radek Krejci53691be2016-02-22 13:58:37 +0100336#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100337
Michal Vasko1a38c862016-01-15 15:50:07 +0100338/**
Michal Vasko71090fc2016-05-24 16:37:28 +0200339 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF \p orig_session.
340 * Call this function only when nc_ps_poll() returns NC_PSPOLL_SSH_CHANNEL on \p orig_session.
341 *
342 * @param[in] orig_session Session that has a new SSH channel ready.
343 * @param[out] session New session.
344 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
345 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
346 */
347NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session);
348
349/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100350 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
Michal Vasko71090fc2016-05-24 16:37:28 +0200351 * 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 +0100352 * The new session is only returned in \p session, it is not added to \p ps.
Michal Vasko96164bf2016-01-21 15:41:58 +0100353 *
354 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
355 * @param[out] session New session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200356 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
357 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vasko96164bf2016-01-21 15:41:58 +0100358 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200359NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
Michal Vasko96164bf2016-01-21 15:41:58 +0100360
361/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100362 * @brief Add an authorized client SSH public key. This public key can be used for
363 * publickey authentication (for any SSH connection, even Call Home) afterwards.
364 *
365 * @param[in] pubkey_base64 Authorized public key binary content encoded in base64.
366 * @param[in] type Authorized public key SSH type.
367 * @param[in] username Username that the client with the public key must use.
368 * @return 0 on success, -1 on error.
369 */
370int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username);
371
372/**
373 * @brief Add an authorized client SSH public key. This public key can be used for
374 * publickey authentication (for any SSH connection, even Call Home) afterwards.
375 *
376 * @param[in] pubkey_path Path to the public key.
377 * @param[in] username Username that the client with the public key must use.
378 * @return 0 on success, -1 on error.
379 */
380int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username);
381
382/**
383 * @brief Remove an authorized client SSH public key.
384 *
385 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
386 * @param[in] pubkey_base64 Authorized public key content. NULL matches any key.
387 * @param[in] type Authorized public key type. 0 matches all types.
388 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
389 * @return 0 on success, -1 on not finding any match.
390 */
391int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
392 const char *username);
393
394/**
Michal Vaskoe2713da2016-08-22 16:06:40 +0200395 * @brief Add endpoint SSH host keys the server will identify itself with. Any RSA, DSA, and
396 * ECDSA keys can be added. However, a maximum of one key of each type will be used
397 * during SSH authentication, later keys replacing the earlier ones.
Michal Vasko1a38c862016-01-15 15:50:07 +0100398 *
Michal Vaskoda514772016-02-01 11:32:01 +0100399 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100400 * @param[in] privkey_path Path to a private key.
401 * @return 0 on success, -1 on error.
402 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200403int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *privkey_path);
404
405/**
406 * @brief Delete endpoint SSH host keys. Their order is preserved.
407 *
408 * @param[in] endpt_name Existing endpoint name.
409 * @param[in] privkey_path Path to a private key. NULL matches all the keys.
410 * @return 0 on success, -1 on error.
411 */
412int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *privkey_path);
Michal Vasko086311b2016-01-08 09:53:11 +0100413
Michal Vasko1a38c862016-01-15 15:50:07 +0100414/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100415 * @brief Set endpoint SSH banner the server will send to every client.
Michal Vasko1a38c862016-01-15 15:50:07 +0100416 *
Michal Vaskoda514772016-02-01 11:32:01 +0100417 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100418 * @param[in] banner SSH banner.
419 * @return 0 on success, -1 on error.
420 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100421int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner);
Michal Vasko086311b2016-01-08 09:53:11 +0100422
Michal Vasko1a38c862016-01-15 15:50:07 +0100423/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100424 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100425 * are supported by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100426 *
Michal Vaskoda514772016-02-01 11:32:01 +0100427 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100428 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
429 * @return 0 on success, -1 on error.
430 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100431int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100432
Michal Vasko1a38c862016-01-15 15:50:07 +0100433/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100434 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100435 *
Michal Vaskoda514772016-02-01 11:32:01 +0100436 * @param[in] endpt_name Existing endpoint name.
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100437 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100438 * @return 0 on success, -1 on error.
439 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100440int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100441
Michal Vasko1a38c862016-01-15 15:50:07 +0100442/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100443 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100444 *
Michal Vaskoda514772016-02-01 11:32:01 +0100445 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100446 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
447 * @return 0 on success, -1 on error.
448 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100449int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100450
Radek Krejci53691be2016-02-22 13:58:37 +0100451#endif /* NC_ENABLED_SSH */
Michal Vasko086311b2016-01-08 09:53:11 +0100452
Radek Krejci53691be2016-02-22 13:58:37 +0100453#ifdef NC_ENABLED_TLS
Michal Vasko086311b2016-01-08 09:53:11 +0100454
Michal Vasko1a38c862016-01-15 15:50:07 +0100455/**
Michal Vaskoa8748792016-11-22 14:34:26 +0100456 * @brief Set the server TLS certificate. Alternative to nc_tls_server_set_cert_path().
Michal Vasko1a38c862016-01-15 15:50:07 +0100457 *
Michal Vaskoda514772016-02-01 11:32:01 +0100458 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoa8748792016-11-22 14:34:26 +0100459 * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. If NULL, the used certificate is cleared.
Michal Vasko1a38c862016-01-15 15:50:07 +0100460 * @return 0 on success, -1 on error.
461 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100462int nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert);
Michal Vasko086311b2016-01-08 09:53:11 +0100463
Michal Vasko1a38c862016-01-15 15:50:07 +0100464/**
Michal Vaskoa8748792016-11-22 14:34:26 +0100465 * @brief Set the server TLS certificate. Alternative to nc_tls_server_set_cert().
Michal Vasko1a38c862016-01-15 15:50:07 +0100466 *
Michal Vaskoda514772016-02-01 11:32:01 +0100467 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoa8748792016-11-22 14:34:26 +0100468 * @param[in] cert_path Path to a certificate file in PEM format. If NULL, the used certificate is cleared.
Michal Vasko1a38c862016-01-15 15:50:07 +0100469 * @return 0 on success, -1 on error.
470 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100471int nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100472
Michal Vasko1a38c862016-01-15 15:50:07 +0100473/**
Michal Vaskoa8748792016-11-22 14:34:26 +0100474 * @brief Set the server TLS private key matching the certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100475 *
Michal Vaskoda514772016-02-01 11:32:01 +0100476 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoa8748792016-11-22 14:34:26 +0100477 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. If NULL, the used key is cleared.
Michal Vasko1a38c862016-01-15 15:50:07 +0100478 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
479 * @return 0 on success, -1 on error.
480 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100481int nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100482
Michal Vasko1a38c862016-01-15 15:50:07 +0100483/**
Michal Vaskoa8748792016-11-22 14:34:26 +0100484 * @brief Set the server TLS private key matching the certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100485 *
Michal Vaskoda514772016-02-01 11:32:01 +0100486 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoa8748792016-11-22 14:34:26 +0100487 * @param[in] privkey_path Path to a private key file in PEM format. If NULL, the used key is cleared.
Michal Vasko1a38c862016-01-15 15:50:07 +0100488 * @return 0 on success, -1 on error.
489 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100490int nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100491
Michal Vasko1a38c862016-01-15 15:50:07 +0100492/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100493 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100494 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100495 *
Michal Vaskoda514772016-02-01 11:32:01 +0100496 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200497 * @param[in] cert_name Arbitary name identifying this certificate.
Michal Vaskof0537d82016-01-29 14:42:38 +0100498 * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding.
Michal Vasko1a38c862016-01-15 15:50:07 +0100499 * @return 0 on success, -1 on error.
500 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200501int nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert_name, const char *cert);
Michal Vasko0457bb42016-01-08 15:49:13 +0100502
Michal Vasko1a38c862016-01-15 15:50:07 +0100503/**
Michal Vasko5d2ad082016-02-09 11:47:59 +0100504 * @brief Add a trusted certificate. Can be both a CA or a client one. Can be
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100505 * safely used together with nc_server_tls_endpt_set_trusted_ca_paths().
Michal Vasko1a38c862016-01-15 15:50:07 +0100506 *
Michal Vaskoda514772016-02-01 11:32:01 +0100507 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200508 * @param[in] cert_name Arbitary name identifying this certificate.
Michal Vasko1a38c862016-01-15 15:50:07 +0100509 * @param[in] cert_path Path to a trusted certificate file in PEM format.
510 * @return 0 on success, -1 on error.
511 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200512int nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100513
Michal Vasko1a38c862016-01-15 15:50:07 +0100514/**
515 * @brief Set trusted Certificate Authority certificate locations. There can only be
Michal Vasko5d2ad082016-02-09 11:47:59 +0100516 * one file and one directory, they are replaced if already set. Can be safely
Michal Vaskofdfd9dd2016-02-29 10:18:46 +0100517 * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant.
Michal Vasko1a38c862016-01-15 15:50:07 +0100518 *
Michal Vaskoda514772016-02-01 11:32:01 +0100519 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100520 * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL.
521 * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility
522 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100523 * @return 0 on success, -1 on error.
524 */
Michal Vasko96830e32016-02-01 10:54:18 +0100525int 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 +0100526
Michal Vasko1a38c862016-01-15 15:50:07 +0100527/**
528 * @brief Destroy and clean all the set certificates and private keys. CRLs and
Michal Vaskof0537d82016-01-29 14:42:38 +0100529 * CTN entries are not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100530 *
531 * @param[in] endpt_name Existing endpoint name.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200532 * @param[in] cert_name Name of the certificate to delete. NULL deletes all the certificates.
533 * @return 0 on success, -1 on not found.
Michal Vasko1a38c862016-01-15 15:50:07 +0100534 */
Michal Vaskoe2713da2016-08-22 16:06:40 +0200535int nc_server_tls_endpt_del_trusted_cert(const char *endpt_name, const char *cert_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100536
Michal Vasko1a38c862016-01-15 15:50:07 +0100537/**
538 * @brief Set Certificate Revocation List locations. There can only be one file
Michal Vaskof0537d82016-01-29 14:42:38 +0100539 * and one directory, they are replaced if already set.
Michal Vasko1a38c862016-01-15 15:50:07 +0100540 *
Michal Vaskoda514772016-02-01 11:32:01 +0100541 * @param[in] endpt_name Existing endpoint name.
Michal Vasko96830e32016-02-01 10:54:18 +0100542 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
543 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
544 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100545 * @return 0 on success, -1 on error.
546 */
Michal Vasko96830e32016-02-01 10:54:18 +0100547int 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 +0100548
Michal Vasko1a38c862016-01-15 15:50:07 +0100549/**
Michal Vasko96830e32016-02-01 10:54:18 +0100550 * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are
Michal Vaskof0537d82016-01-29 14:42:38 +0100551 * not affected.
Michal Vaskoda514772016-02-01 11:32:01 +0100552 *
553 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100554 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100555void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100556
Michal Vasko1a38c862016-01-15 15:50:07 +0100557/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100558 * @brief Add a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100559 *
Michal Vaskoda514772016-02-01 11:32:01 +0100560 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100561 * @param[in] id Priority of the entry.
562 * @param[in] fingerprint Matching certificate fingerprint.
563 * @param[in] map_type Type of username-certificate mapping.
564 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
565 * @return 0 on success, -1 on error.
566 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200567int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint,
568 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100569
Michal Vasko1a38c862016-01-15 15:50:07 +0100570/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100571 * @brief Remove a cert-to-name entry.
Michal Vasko1a38c862016-01-15 15:50:07 +0100572 *
Michal Vaskoda514772016-02-01 11:32:01 +0100573 * @param[in] endpt_name Existing endpoint name.
Michal Vasko1a38c862016-01-15 15:50:07 +0100574 * @param[in] id Priority of the entry. -1 matches all the priorities.
575 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
576 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
577 * @param[in] name Specific username for the entry. NULL matches all the usernames.
578 * @return 0 on success, -1 on not finding any match.
579 */
Michal Vasko3a889fd2016-09-30 12:16:37 +0200580int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint,
581 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100582
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100583/**
584 * @brief Get a cert-to-name entry.
585 *
586 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
587 * it is filled and returned. If the value is set, it is used as a filter.
588 * Returns first matching entry.
589 *
590 * @param[in] endpt_name Existing endpoint name.
591 * @param[in,out] id Priority of the entry.
592 * @param[in,out] fingerprint Fingerprint fo the entry.
593 * @param[in,out] map_type Mapping type of the entry.
594 * @param[in,out] name Specific username for the entry.
595 * @return 0 on success, -1 on not finding any match.
596 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100597int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type,
598 char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100599
Michal Vasko7060bcf2016-11-28 14:48:11 +0100600/**
Michal Vasko709598e2016-11-28 14:48:32 +0100601 * @brief Get client certificate.
602 *
603 * @param[in] session Session to get the information from.
604 * @return Const session client certificate.
605 */
606const X509 *nc_session_get_client_cert(const struct nc_session *session);
607
608/**
Michal Vasko7060bcf2016-11-28 14:48:11 +0100609 * @brief Set TLS authentication additional verify callback.
610 *
611 * Server will always perform cert-to-name based on its configuration. Only after it passes
612 * and this callback is set, it is also called. It should return exactly what OpenSSL
613 * verify callback meaning 1 for success, 0 to deny the user.
614 *
615 * @param[in] verify_clb Additional user verify callback.
616 */
617void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session));
618
Radek Krejci53691be2016-02-22 13:58:37 +0100619#endif /* NC_ENABLED_TLS */
Michal Vasko086311b2016-01-08 09:53:11 +0100620
Michal Vaskof8352352016-05-24 09:11:36 +0200621/**
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200622 * @brief Get session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200623 *
624 * @param[in] session Session to get the information from.
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200625 * @return Session start time.
Michal Vaskof8352352016-05-24 09:11:36 +0200626 */
Michal Vaskoc45ebd32016-05-25 11:17:36 +0200627time_t nc_session_get_start_time(const struct nc_session *session);
Michal Vaskof8352352016-05-24 09:11:36 +0200628
Michal Vasko086311b2016-01-08 09:53:11 +0100629#endif /* NC_SESSION_SERVER_H_ */