Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NC_SESSION_SERVER_H_ |
| 16 | #define NC_SESSION_SERVER_H_ |
| 17 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 23 | #include <libyang/libyang.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 24 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 25 | #ifdef NC_ENABLED_TLS |
| 26 | # include <openssl/x509.h> |
| 27 | #endif |
| 28 | |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 29 | #ifdef NC_ENABLED_SSH |
| 30 | # include <libssh/libssh.h> |
| 31 | # include <libssh/callbacks.h> |
| 32 | # include <libssh/server.h> |
| 33 | #endif |
| 34 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | #include "session.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | #include "netconf.h" |
| 37 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 38 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 39 | * @defgroup server_session Server Session |
| 40 | * @ingroup server |
| 41 | * |
| 42 | * @brief Server-side NETCONF session manipulation. |
| 43 | * @{ |
| 44 | */ |
| 45 | |
| 46 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 47 | * @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 Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 52 | * The callback is set via nc_set_global_rpc_clb(). |
| 53 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 54 | * @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 Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 58 | typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session); |
| 59 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 60 | /** |
Michal Vasko | 7b09624 | 2016-01-29 15:55:10 +0100 | [diff] [blame] | 61 | * @brief Set the termination reason for a session. Use only in #nc_rpc_clb callbacks. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 62 | * |
| 63 | * @param[in] session Session to modify. |
| 64 | * @param[in] reason Reason of termination. |
| 65 | */ |
| 66 | void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason); |
| 67 | |
| 68 | /** |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 69 | * @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 | */ |
| 74 | void 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 | */ |
| 82 | void nc_session_set_status(struct nc_session *session, NC_STATUS status); |
| 83 | |
| 84 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 85 | * @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 | */ |
| 90 | void nc_set_global_rpc_clb(nc_rpc_clb clb); |
| 91 | |
| 92 | /**@} Server Session */ |
| 93 | |
| 94 | /** |
| 95 | * @addtogroup server |
| 96 | * @{ |
| 97 | */ |
| 98 | |
| 99 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 100 | * @brief Initialize libssh and/or libssl/libcrypto and the server using a libyang context. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 101 | * |
| 102 | * The context is not modified internally, only its dictionary is used for holding |
Michal Vasko | a43589b | 2016-02-17 13:24:59 +0100 | [diff] [blame] | 103 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 105 | * |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 106 | * If the RPC callbacks on schema nodes (mentioned in @ref howtoserver) are modified after |
Michal Vasko | 5494f51 | 2016-03-01 12:13:44 +0100 | [diff] [blame] | 107 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 111 | * 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 Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 119 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 121 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 125 | * @param[in] ctx Core NETCONF server context. |
| 126 | * @return 0 on success, -1 on error. |
| 127 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 128 | int nc_server_init(struct ly_ctx *ctx); |
| 129 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 130 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 131 | * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and |
| 132 | * server resources. |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 133 | */ |
| 134 | void nc_server_destroy(void); |
| 135 | |
| 136 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 137 | * @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 Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 150 | int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported); |
| 151 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 152 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 153 | * @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 | */ |
| 160 | void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported); |
| 161 | |
| 162 | /** |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 163 | * @brief Set capability of the server. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 164 | * |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 165 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 169 | * |
Michal Vasko | 50d2a5c | 2017-02-14 10:29:49 +0100 | [diff] [blame] | 170 | * @param[in] value Capability string to be advertised in server's \<hello\> messages. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 171 | */ |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 172 | int nc_server_set_capability(const char *value); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 175 | * @brief Set server timeout for receiving a hello message. |
| 176 | * |
| 177 | * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting. |
| 178 | */ |
| 179 | void nc_server_set_hello_timeout(uint16_t hello_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 180 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 181 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 182 | * @brief get server timeout for receiving a hello message. |
| 183 | * |
| 184 | * @return Hello message timeout, 0 is infinite. |
| 185 | */ |
| 186 | uint16_t nc_server_get_hello_timeout(void); |
| 187 | |
| 188 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 189 | * @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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 192 | * because of inactivity. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 193 | */ |
| 194 | void nc_server_set_idle_timeout(uint16_t idle_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 195 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 196 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 197 | * @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 | */ |
| 202 | uint16_t nc_server_get_idle_timeout(void); |
| 203 | |
| 204 | /** |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 205 | * @brief Get all the server capabilities including all the schemas. |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 206 | * |
| 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 | */ |
| 213 | const char **nc_server_get_cpblts(struct ly_ctx *ctx); |
| 214 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 215 | /** |
| 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 | */ |
| 226 | const char **nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version); |
| 227 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 228 | /**@} Server */ |
| 229 | |
| 230 | /** |
| 231 | * @addtogroup server_session |
| 232 | * @{ |
| 233 | */ |
| 234 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 235 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 236 | * @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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 242 | * @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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 244 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 245 | NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 246 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 247 | /** |
| 248 | * @brief Create an empty structure for polling sessions. |
| 249 | * |
| 250 | * @return Empty pollsession structure, NULL on error. |
| 251 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 252 | struct nc_pollsession *nc_ps_new(void); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 253 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 254 | /** |
| 255 | * @brief Free a pollsession structure. |
| 256 | * |
Michal Vasko | 01082bf | 2016-04-07 10:44:21 +0200 | [diff] [blame] | 257 | * !IMPORTANT! Make sure that \p ps is not accessible (is not used) |
| 258 | * by any thread before and after this call! |
| 259 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 260 | * @param[in] ps Pollsession structure to free. |
| 261 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 262 | void nc_ps_free(struct nc_pollsession *ps); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 263 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 264 | /** |
| 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 Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 271 | int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 272 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 273 | /** |
| 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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 278 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 279 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 280 | int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session); |
| 281 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 282 | /** |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 283 | * @brief Get a session from a pollsession structure matching the session ID. |
| 284 | * |
| 285 | * @param[in] ps Pollsession structure to read from. |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 286 | * @param[in] idx Index of the session. |
| 287 | * @return Session on index, NULL if out-of-bounds. |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 288 | */ |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 289 | struct nc_session *nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx); |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 290 | |
| 291 | /** |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 292 | * @brief Learn the number of sessions in a pollsession structure. |
| 293 | * |
Michal Vasko | f4462fd | 2017-02-15 14:29:05 +0100 | [diff] [blame] | 294 | * Does not lock \p ps structure for efficiency. |
| 295 | * |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 296 | * @param[in] ps Pollsession structure to check. |
Michal Vasko | c72d0e6 | 2016-07-26 11:36:11 +0200 | [diff] [blame] | 297 | * @return Number of sessions (even invalid ones) in \p ps, -1 on error. |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 298 | */ |
| 299 | uint16_t nc_ps_session_count(struct nc_pollsession *ps); |
| 300 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 301 | #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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 309 | |
| 310 | #ifdef NC_ENABLED_SSH |
Michal Vasko | e645de3 | 2017-05-26 14:02:50 +0200 | [diff] [blame] | 311 | # 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 Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 313 | #endif |
| 314 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 315 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 316 | * @brief Poll sessions and process any received RPCs. |
| 317 | * |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 318 | * 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 Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 321 | * |
| 322 | * @param[in] ps Pollsession structure to use. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 323 | * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 324 | * infinite waiting. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 325 | * @param[in] session Session that was processed and that specific return bits concern. |
| 326 | * Can be NULL. |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 327 | * @return Bitfield of NC_PSPOLL_* macros. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 328 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 329 | int nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 330 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 331 | /** |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 332 | * @brief Remove sessions from a pollsession structure and |
| 333 | * call nc_session_free() on them. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 334 | * |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 335 | * Calling this function with \p all false makes sense if nc_ps_poll() returned #NC_PSPOLL_SESSION_TERM. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 336 | * |
| 337 | * @param[in] ps Pollsession structure to clear. |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 338 | * @param[in] all Whether to free all sessions, or only the invalid ones. |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 339 | * @param[in] data_free Session user data destructor. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 340 | */ |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 341 | void nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *)); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 342 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 343 | /**@} Server Session */ |
| 344 | |
| 345 | /** |
| 346 | * @addtogroup server |
| 347 | * @{ |
| 348 | */ |
| 349 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 350 | /** |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 351 | * @brief Add a new endpoint. |
| 352 | * |
| 353 | * Before the endpoint can accept any connections, its address and port must |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 354 | * be set via nc_server_endpt_set_address() and nc_server_endpt_set_port(). |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 355 | * |
| 356 | * @param[in] name Arbitrary unique endpoint name. |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 357 | * @param[in] ti Transport protocol to use. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 358 | * @return 0 on success, -1 on error. |
| 359 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 360 | int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 361 | |
| 362 | /** |
| 363 | * @brief Stop listening on and remove an endpoint. |
| 364 | * |
| 365 | * @param[in] name Endpoint name. NULL matches all endpoints. |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 366 | * @param[in] ti Endpoint transport protocol. NULL matches any protocol. |
| 367 | * Redundant to set if \p name is set, endpoint names are |
| 368 | * unique disregarding their protocol. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 369 | * @return 0 on success, -1 on not finding any match. |
| 370 | */ |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 371 | int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 372 | |
| 373 | /** |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 374 | * @brief Get the number of currently configured listening endpoints. |
| 375 | * Note that an ednpoint without address and/or port will be included |
| 376 | * even though it is not, in fact, listening. |
| 377 | * |
| 378 | * @return Number of added listening endpoints. |
| 379 | */ |
| 380 | int nc_server_endpt_count(void); |
| 381 | |
| 382 | /** |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 383 | * @brief Check if an endpoint exists. |
| 384 | * |
| 385 | * @param[in] name Endpoint name. |
| 386 | * @return 0 if does not exists, non-zero otherwise. |
| 387 | */ |
| 388 | int nc_server_is_endpt(const char *name); |
| 389 | |
| 390 | /** |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 391 | * @brief Change endpoint listening address. |
| 392 | * |
| 393 | * On error the previous listening socket (if any) is left untouched. |
| 394 | * |
| 395 | * @param[in] endpt_name Existing endpoint name. |
| 396 | * @param[in] address New listening address. |
| 397 | * @return 0 on success, -1 on error. |
| 398 | */ |
| 399 | int nc_server_endpt_set_address(const char *endpt_name, const char *address); |
| 400 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 401 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 402 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 403 | /** |
| 404 | * @brief Change endpoint listening port. |
| 405 | * |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 406 | * This is only valid on SSH/TLS transport endpoint. |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 407 | * On error the previous listening socket (if any) is left untouched. |
| 408 | * |
| 409 | * @param[in] endpt_name Existing endpoint name. |
| 410 | * @param[in] port New listening port. |
| 411 | * @return 0 on success, -1 on error. |
| 412 | */ |
| 413 | int nc_server_endpt_set_port(const char *endpt_name, uint16_t port); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 414 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 415 | #endif |
| 416 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 417 | /** |
| 418 | * @brief Change endpoint permissions. |
| 419 | * |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 420 | * This is only valid on UNIX transport endpoint. |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 421 | * On error the previous listening socket (if any) is left untouched. |
| 422 | * |
| 423 | * @param[in] endpt_name Existing endpoint name. |
| 424 | * @param[in] mode New mode, -1 to use default. |
| 425 | * @param[in] uid New uid, -1 to use default. |
| 426 | * @param[in] gid New gid, -1 to use default. |
| 427 | * @return 0 on success, -1 on error. |
| 428 | */ |
| 429 | int nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid); |
| 430 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 431 | /** |
| 432 | * @brief Change endpoint keepalives state. Affects only new connections. |
| 433 | * |
| 434 | * @param[in] endpt_name Existing endpoint name. |
| 435 | * @param[in] enable Whether to enable or disable keepalives. |
| 436 | * @return 0 on success, -1 on error. |
| 437 | */ |
| 438 | int nc_server_endpt_enable_keepalives(const char *endpt_name, int enable); |
| 439 | |
| 440 | /** |
| 441 | * @brief Change endpoint keepalives parameters. Affects only new connections. |
| 442 | * |
| 443 | * @param[in] endpt_name Existing endpoint name. |
| 444 | * @param[in] idle_time Keepalive idle time in seconds, 1 by default, -1 to keep previous value. |
| 445 | * @param[in] max_probes Keepalive max probes sent, 10 by default, -1 to keep previous value. |
| 446 | * @param[in] probe_interval Keepalive probe interval in seconds, 5 by default, -1 to keep previous value. |
| 447 | * @return 0 on success, -1 on error. |
| 448 | */ |
| 449 | int nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval); |
| 450 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 451 | /**@} Server */ |
| 452 | |
| 453 | /** |
| 454 | * @addtogroup server_session |
| 455 | */ |
| 456 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 457 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 458 | * @brief Accept new sessions on all the listening endpoints. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 459 | * |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 460 | * Once a new (TCP/IP) conection is established a different (quite long) timeout |
| 461 | * is used for waiting for transport-related data, which means this call can block |
| 462 | * for much longer that \p timeout, but only with slow/faulty/malicious clients. |
| 463 | * |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 464 | * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 465 | * non-blocking call, -1 for infinite waiting. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 466 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 467 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 468 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 469 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 470 | NC_MSG_TYPE nc_accept(int timeout, struct nc_session **session); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 471 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 472 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 473 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 474 | /** |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 475 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF \p orig_session. |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 476 | * Call this function only when nc_ps_poll() returns #NC_PSPOLL_SSH_CHANNEL on \p orig_session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 477 | * |
| 478 | * @param[in] orig_session Session that has a new SSH channel ready. |
| 479 | * @param[out] session New session. |
| 480 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 481 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
| 482 | */ |
| 483 | NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session); |
| 484 | |
| 485 | /** |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 486 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 487 | * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns #NC_PSPOLL_SSH_CHANNEL. |
Michal Vasko | c0fde01 | 2016-03-01 09:07:23 +0100 | [diff] [blame] | 488 | * The new session is only returned in \p session, it is not added to \p ps. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 489 | * |
| 490 | * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call. |
| 491 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 492 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 493 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 494 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 495 | NC_MSG_TYPE nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 496 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 497 | /**@} Server Session */ |
| 498 | |
| 499 | /** |
| 500 | * @defgroup server_ssh Server SSH |
| 501 | * @ingroup server |
| 502 | * |
| 503 | * @brief Server-side settings for SSH connections. |
| 504 | * @{ |
| 505 | */ |
| 506 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 507 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 508 | * @brief Add an authorized client SSH public key. This public key can be used for |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 509 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 510 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 511 | * @param[in] pubkey_base64 Authorized public key binary content encoded in base64. |
| 512 | * @param[in] type Authorized public key SSH type. |
| 513 | * @param[in] username Username that the client with the public key must use. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 514 | * @return 0 on success, -1 on error. |
| 515 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 516 | int nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 517 | |
| 518 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 519 | * @brief Add an authorized client SSH public key. This public key can be used for |
| 520 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 521 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 522 | * @param[in] pubkey_path Path to the public key. |
| 523 | * @param[in] username Username that the client with the public key must use. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 524 | * @return 0 on success, -1 on error. |
| 525 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 526 | int nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 527 | |
| 528 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 529 | * @brief Remove an authorized client SSH public key. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 530 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 531 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 532 | * @param[in] pubkey_base64 Authorized public key content. NULL matches any key. |
| 533 | * @param[in] type Authorized public key type. 0 matches all types. |
| 534 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 535 | * @return 0 on success, -1 on not finding any match. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 536 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 537 | int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 538 | const char *username); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 539 | |
| 540 | /** |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 541 | * @brief Set the callback for SSH password authentication. If none is set, local system users are used. |
| 542 | * |
| 543 | * @param[in] passwd_auth_clb Callback that should authenticate the user. Username can be directly obtained from \p session. |
| 544 | * Zero return indicates success, non-zero an error. |
| 545 | * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb. |
| 546 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 547 | */ |
| 548 | void nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, |
| 549 | void *user_data), |
| 550 | void *user_data, void (*free_user_data)(void *user_data)); |
| 551 | |
| 552 | /** |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 553 | * @brief Set the callback for SSH interactive authentication. If none is set, local system users are used. |
| 554 | * |
| 555 | * @param[in] interactive_auth_clb Callback that should authenticate the user. |
| 556 | * Zero return indicates success, non-zero an error. |
| 557 | * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb. |
| 558 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 559 | */ |
David Sedlák | 9d21a66 | 2018-08-23 10:57:55 +0200 | [diff] [blame] | 560 | void nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, const ssh_message msg, |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 561 | void *user_data), |
| 562 | void *user_data, void (*free_user_data)(void *user_data)); |
| 563 | |
| 564 | /** |
| 565 | * @brief Set the callback for SSH public key authentication. If none is set, local system users are used. |
| 566 | * |
| 567 | * @param[in] pubkey_auth_clb Callback that should authenticate the user. |
| 568 | * Zero return indicates success, non-zero an error. |
| 569 | * @param[in] user_data Optional arbitrary user data that will be passed to \p passwd_auth_clb. |
| 570 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 571 | */ |
David Sedlák | 9d21a66 | 2018-08-23 10:57:55 +0200 | [diff] [blame] | 572 | void nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data), |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 573 | void *user_data, void (*free_user_data)(void *user_data)); |
| 574 | |
| 575 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 576 | * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However, |
| 577 | * a maximum of one key of each type will be used during SSH authentication, later keys replacing |
| 578 | * the earlier ones. |
| 579 | * |
| 580 | * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero |
| 581 | * an error. On success exactly ONE of \p privkey_path or \p privkey_data is expected |
| 582 | * to be set. The one set will be freed. |
| 583 | * - \p privkey_path expects a PEM file, |
| 584 | * - \p privkey_data expects a base-64 encoded ANS.1 DER data, |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 585 | * - \p privkey_type type of the key in \p privkey_data. Use ::NC_SSH_KEY_UNKNOWN for |
| 586 | * PKCS#8 key that includes the information about the key in its data. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 587 | * @param[in] user_data Optional arbitrary user data that will be passed to \p hostkey_clb. |
| 588 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 589 | */ |
| 590 | void nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 591 | char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, void (*free_user_data)(void *user_data)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 592 | |
| 593 | /** |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 594 | * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself |
| 595 | * wil be retrieved using a callback. |
| 596 | * |
| 597 | * @param[in] endpt_name Existing endpoint name. |
| 598 | * @param[in] name Arbitrary name of the host key. |
| 599 | * @param[in] idx Optional index where to add the key. -1 adds at the end. |
| 600 | * @return 0 on success, -1 on error. |
| 601 | */ |
| 602 | int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx); |
| 603 | |
| 604 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 605 | * @brief Delete endpoint SSH host key. Their order is preserved. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 606 | * |
| 607 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 608 | * @param[in] name Name of the host key. NULL matches all the keys, but if \p idx != -1 then this must be NULL. |
| 609 | * @param[in] idx Index of the hostkey. -1 matches all indices, but if \p name != NULL then this must be -1. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 610 | * @return 0 on success, -1 on error. |
| 611 | */ |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 612 | int nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 613 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 614 | /** |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 615 | * @brief Move endpoint SSH host key. |
| 616 | * |
| 617 | * @param[in] endpt_name Exisitng endpoint name. |
| 618 | * @param[in] key_mov Name of the host key that will be moved. |
| 619 | * @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. |
| 620 | * @return 0 in success, -1 on error. |
| 621 | */ |
| 622 | int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after); |
| 623 | |
| 624 | /** |
| 625 | * @brief Modify endpoint SSH host key. |
| 626 | * |
| 627 | * @param[in] endpt_name Exisitng endpoint name. |
| 628 | * @param[in] name Name of an existing host key. |
| 629 | * @param[in] new_name New name of the host key \p name. |
| 630 | * @return 0 in success, -1 on error. |
| 631 | */ |
| 632 | int nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 633 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 634 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 635 | * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 636 | * are supported by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 637 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 638 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 639 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 640 | * @return 0 on success, -1 on error. |
| 641 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 642 | int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 643 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 644 | /** |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 645 | * @brief Get endpoint accepted SSH authentication methods. |
| 646 | * |
| 647 | * @param[in] endpt_name Existing endpoint name. |
| 648 | * @return Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 649 | */ |
| 650 | int nc_server_ssh_endpt_get_auth_methods(const char *endpt_name); |
| 651 | |
| 652 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 653 | * @brief Set endpoint SSH authentication attempts of every client. 3 by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 654 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 655 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 656 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 657 | * @return 0 on success, -1 on error. |
| 658 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 659 | int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 660 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 661 | /** |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 662 | * @brief Set endpoint SSH authentication timeout. 30 seconds by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 663 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 664 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 665 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 666 | * @return 0 on success, -1 on error. |
| 667 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 668 | int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 669 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 670 | /**@} Server SSH */ |
| 671 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 672 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 673 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 674 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 675 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 676 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 677 | * @defgroup server_tls Server TLS |
| 678 | * @ingroup server |
| 679 | * |
| 680 | * @brief Server-side settings for TLS connections. |
| 681 | * @{ |
| 682 | */ |
| 683 | |
| 684 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 685 | * @brief Set the server TLS certificate. Only the name is set, the certificate itself |
| 686 | * wil be retrieved using a callback. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 687 | * |
| 688 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 689 | * @param[in] name Arbitrary certificate name. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 690 | * @return 0 on success, -1 on error. |
| 691 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 692 | int nc_server_tls_endpt_set_server_cert(const char *endpt_name, const char *name); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 693 | |
| 694 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 695 | * @brief Set the callback for retrieving server certificate and matching private key. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 696 | * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 697 | * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success, |
| 698 | * non-zero an error. On success exactly ONE of \p cert_path or \p cert_data and ONE of |
| 699 | * \p privkey_path and \p privkey_data is expected to be set. Those set will be freed. |
| 700 | * - \p cert_path expects a PEM file, |
| 701 | * - \p cert_data expects a base-64 encoded ASN.1 DER data, |
| 702 | * - \p privkey_path expects a PEM file, |
| 703 | * - \p privkey_data expects a base-64 encoded ANS.1 DER data, |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 704 | * - \p privkey_type type of the key in \p privkey_data. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 705 | * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb. |
| 706 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 707 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 708 | void nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 709 | char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, |
| 710 | void (*free_user_data)(void *user_data)); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 711 | |
| 712 | /** |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 713 | * @brief Set the callback for retrieving server certificate chain |
| 714 | * |
| 715 | * @param[in] cert_chain_clb Callback that should return all the certificates of the chain. Zero return indicates success, |
| 716 | * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left |
| 717 | * NULL. Both will be (deeply) freed. |
| 718 | * - \p cert_paths expect an array of PEM files, |
| 719 | * - \p cert_path_count number of \p cert_paths array members, |
| 720 | * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data, |
| 721 | * - \p cert_data_count number of \p cert_data array members. |
| 722 | * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb. |
| 723 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 724 | */ |
| 725 | void nc_server_tls_set_server_cert_chain_clb(int (*cert_chain_clb)(const char *name, void *user_data, char ***cert_paths, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 726 | int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data)); |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 727 | |
| 728 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 729 | * @brief Add a trusted certificate list. Can be both a CA or a client one. Can be |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 730 | * safely used together with nc_server_tls_endpt_set_trusted_ca_paths(). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 731 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 732 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 733 | * @param[in] name Arbitary name identifying this certificate list. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 734 | * @return 0 on success, -1 on error. |
| 735 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 736 | int nc_server_tls_endpt_add_trusted_cert_list(const char *endpt_name, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 737 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 738 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 739 | * @brief Set the callback for retrieving trusted certificates. |
| 740 | * |
| 741 | * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success, |
| 742 | * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left |
| 743 | * NULL. Both will be (deeply) freed. |
| 744 | * - \p cert_paths expect an array of PEM files, |
| 745 | * - \p cert_path_count number of \p cert_paths array members, |
| 746 | * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data, |
| 747 | * - \p cert_data_count number of \p cert_data array members. |
| 748 | * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb. |
| 749 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 750 | */ |
| 751 | void nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 752 | int *cert_path_count, char ***cert_data, int *cert_data_count), void *user_data, void (*free_user_data)(void *user_data)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 753 | |
| 754 | /** |
| 755 | * @brief Remove a trusted certificate. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 756 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 757 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 758 | * @param[in] name Name of the certificate list to delete. NULL deletes all the lists. |
| 759 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 760 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 761 | int nc_server_tls_endpt_del_trusted_cert_list(const char *endpt_name, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 762 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 763 | /** |
| 764 | * @brief Set trusted Certificate Authority certificate locations. There can only be |
Michal Vasko | 5d2ad08 | 2016-02-09 11:47:59 +0100 | [diff] [blame] | 765 | * one file and one directory, they are replaced if already set. Can be safely |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 766 | * used with nc_server_tls_endpt_add_trusted_cert() or its _path variant. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 767 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 768 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 769 | * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL. |
| 770 | * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility |
| 771 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 772 | * @return 0 on success, -1 on error. |
| 773 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 774 | int nc_server_tls_endpt_set_trusted_ca_paths(const char *endpt_name, const char *ca_file, const char *ca_dir); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 775 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 776 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 777 | * @brief Set Certificate Revocation List locations. There can only be one file |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 778 | * and one directory, they are replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 779 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 780 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 781 | * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL. |
| 782 | * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility |
| 783 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 784 | * @return 0 on success, -1 on error. |
| 785 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 786 | int nc_server_tls_endpt_set_crl_paths(const char *endpt_name, const char *crl_file, const char *crl_dir); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 787 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 788 | /** |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 789 | * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 790 | * not affected. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 791 | * |
| 792 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 793 | */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 794 | void nc_server_tls_endpt_clear_crls(const char *endpt_name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 795 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 796 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 797 | * @brief Add a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 798 | * |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 799 | * It is possible to add an entry step-by-step, specifying first only \p ip and in later calls |
| 800 | * \p fingerprint, \p map_type, and optionally \p name spearately. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 801 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 802 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 803 | * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id |
| 804 | * is modified. |
| 805 | * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset. |
| 806 | * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset. |
| 807 | * @param[in] name Specific username used only if \p map_type == NC_TLS_CTN_SPECIFED. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 808 | * @return 0 on success, -1 on error. |
| 809 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 810 | int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, |
| 811 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 812 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 813 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 814 | * @brief Remove a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 815 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 816 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 817 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 818 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 819 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 820 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 821 | * @return 0 on success, -1 on not finding any match. |
| 822 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 823 | int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, |
| 824 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 825 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 826 | /** |
| 827 | * @brief Get a cert-to-name entry. |
| 828 | * |
| 829 | * If a parameter is NULL, it is ignored. If its dereferenced value is NULL, |
| 830 | * it is filled and returned. If the value is set, it is used as a filter. |
| 831 | * Returns first matching entry. |
| 832 | * |
| 833 | * @param[in] endpt_name Existing endpoint name. |
| 834 | * @param[in,out] id Priority of the entry. |
| 835 | * @param[in,out] fingerprint Fingerprint fo the entry. |
| 836 | * @param[in,out] map_type Mapping type of the entry. |
| 837 | * @param[in,out] name Specific username for the entry. |
| 838 | * @return 0 on success, -1 on not finding any match. |
| 839 | */ |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 840 | int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, |
| 841 | char **name); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 842 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 843 | /** |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 844 | * @brief Get client certificate. |
| 845 | * |
| 846 | * @param[in] session Session to get the information from. |
| 847 | * @return Const session client certificate. |
| 848 | */ |
| 849 | const X509 *nc_session_get_client_cert(const struct nc_session *session); |
| 850 | |
| 851 | /** |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 852 | * @brief Set TLS authentication additional verify callback. |
| 853 | * |
| 854 | * Server will always perform cert-to-name based on its configuration. Only after it passes |
| 855 | * and this callback is set, it is also called. It should return exactly what OpenSSL |
| 856 | * verify callback meaning 1 for success, 0 to deny the user. |
| 857 | * |
| 858 | * @param[in] verify_clb Additional user verify callback. |
| 859 | */ |
| 860 | void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)); |
| 861 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 862 | /**@} Server TLS */ |
| 863 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 864 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 865 | |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 866 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 867 | * @addtogroup server_session |
| 868 | * @{ |
| 869 | */ |
| 870 | |
| 871 | /** |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 872 | * @brief Get session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 873 | * |
| 874 | * @param[in] session Session to get the information from. |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 875 | * @return Session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 876 | */ |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 877 | time_t nc_session_get_start_time(const struct nc_session *session); |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 878 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 879 | /** |
| 880 | * @brief Set session notification subscription flag. |
| 881 | * |
| 882 | * It is used only to ignore timeouts, because they are |
| 883 | * ignored for sessions with active subscriptions. |
| 884 | * |
| 885 | * @param[in] session Session to modify. |
| 886 | * @param[in] notif_status 0 for no active subscriptions, non-zero for an active subscription. |
| 887 | */ |
| 888 | void nc_session_set_notif_status(struct nc_session *session, int notif_status); |
| 889 | |
| 890 | /** |
| 891 | * @brief Get session notification subscription flag. |
| 892 | * |
| 893 | * @param[in] session Session to get the information from. |
| 894 | * @return 0 for no active subscription, non-zero for an active subscription. |
| 895 | */ |
| 896 | int nc_session_get_notif_status(const struct nc_session *session); |
| 897 | |
Michal Vasko | 8f43059 | 2019-02-26 08:32:54 +0100 | [diff] [blame] | 898 | /** |
| 899 | * @brief Learn whether a session was created using Call Home or not. |
| 900 | * Works only for server sessions. |
| 901 | * |
| 902 | * @param[in] session Session to get the information from. |
| 903 | * @return 0 if a standard session, non-zero if a Call Home session. |
| 904 | */ |
| 905 | int nc_session_is_callhome(const struct nc_session *session); |
| 906 | |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 907 | /**@} Server Session */ |
| 908 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 909 | #ifdef __cplusplus |
| 910 | } |
| 911 | #endif |
| 912 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 913 | #endif /* NC_SESSION_SERVER_H_ */ |