Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 2 | * @file session_server.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 session server manipulation |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #ifndef NC_SESSION_SERVER_H_ |
| 17 | #define NC_SESSION_SERVER_H_ |
| 18 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 23 | #include <libyang/libyang.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Michal Vasko | cca9ba6 | 2021-11-25 13:26:17 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 26 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 27 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 28 | # include <openssl/x509.h> |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 29 | #endif |
| 30 | |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 31 | #ifdef NC_ENABLED_SSH |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 32 | # include <libssh/callbacks.h> |
| 33 | # include <libssh/libssh.h> |
| 34 | # include <libssh/server.h> |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 35 | #endif |
| 36 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 37 | #include "netconf.h" |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 38 | #include "session.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 40 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 41 | * @defgroup server_session Server Session |
| 42 | * @ingroup server |
| 43 | * |
| 44 | * @brief Server-side NETCONF session manipulation. |
| 45 | * @{ |
| 46 | */ |
| 47 | |
| 48 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 49 | * @brief Prototype of callbacks that are called if some RPCs are received. |
| 50 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 51 | * If @p session termination reason is changed in the callback, one last reply |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 52 | * is sent and then the session is considered invalid. |
| 53 | * |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 54 | * The callback is set via nc_set_global_rpc_clb(). |
| 55 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 56 | * @param[in] rpc Parsed client RPC request. |
| 57 | * @param[in] session Session the RPC arrived on. |
| 58 | * @return Server reply. If NULL, an operation-failed error will be sent to the client. |
| 59 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 60 | typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session); |
| 61 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 62 | /** |
Michal Vasko | 7b09624 | 2016-01-29 15:55:10 +0100 | [diff] [blame] | 63 | * @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] | 64 | * |
| 65 | * @param[in] session Session to modify. |
| 66 | * @param[in] reason Reason of termination. |
| 67 | */ |
| 68 | void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason); |
| 69 | |
| 70 | /** |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 71 | * @brief Set the session-id of the session responsible for this session's termination. |
| 72 | * |
| 73 | * @param[in] session Session to modify. Must have term_reason set to #NC_SESSION_TERM_KILLED. |
| 74 | * @param[in] sid SID of the killing session. |
| 75 | */ |
| 76 | void nc_session_set_killed_by(struct nc_session *session, uint32_t sid); |
| 77 | |
| 78 | /** |
| 79 | * @brief Set the status of a session. |
| 80 | * |
| 81 | * @param[in] session Session to modify. |
| 82 | * @param[in] status Status of the session. |
| 83 | */ |
| 84 | void nc_session_set_status(struct nc_session *session, NC_STATUS status); |
| 85 | |
| 86 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 87 | * @brief Set a global nc_rpc_clb that is called if the particular RPC request is |
| 88 | * received and the private field in the corresponding RPC schema node is NULL. |
| 89 | * |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 90 | * If this callback is set, the default callbacks for "get-schema" and "close-session" are not used. |
| 91 | * |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 92 | * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default. |
| 93 | */ |
| 94 | void nc_set_global_rpc_clb(nc_rpc_clb clb); |
| 95 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 96 | /** |
| 97 | * @brief Default RPC callback used for "ietf-netconf-monitoring:get-schema" RPC if no other specific |
| 98 | * or global callback is set. |
| 99 | * |
| 100 | * @param[in] rpc Received RPC. |
| 101 | * @param[in] session NC session @p rpc was received on. |
| 102 | * @return Server reply. |
| 103 | */ |
| 104 | struct nc_server_reply *nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session); |
| 105 | |
| 106 | /** |
| 107 | * @brief Default RPC callback used for "ietf-netconf:close-session" RPC if no other specific |
| 108 | * or global callback is set. |
| 109 | * |
| 110 | * @param[in] rpc Received RPC. |
| 111 | * @param[in] session NC session @p rpc was received on. |
| 112 | * @return Server reply. |
| 113 | */ |
| 114 | struct nc_server_reply *nc_clb_default_close_session(struct lyd_node *rpc, struct nc_session *session); |
| 115 | |
Michal Vasko | 4e6d324 | 2021-05-26 09:13:24 +0200 | [diff] [blame] | 116 | /** @} Server Session */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * @addtogroup server |
| 120 | * @{ |
| 121 | */ |
| 122 | |
| 123 | /** |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 124 | * @brief Initialize libssh and/or libssl/libcrypto and the server. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 125 | * |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 126 | * Must be called before other nc_server* functions. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 127 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 128 | * @return 0 on success, -1 on error. |
| 129 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 130 | int nc_server_init(void); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 131 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 132 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 133 | * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and |
| 134 | * server resources. |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 135 | */ |
| 136 | void nc_server_destroy(void); |
| 137 | |
| 138 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 139 | * @brief Set the with-defaults capability extra parameters. |
| 140 | * |
| 141 | * For the capability to be actually advertised, the server context must also |
| 142 | * include the ietf-netconf-with-defaults model. |
| 143 | * |
| 144 | * Changing this option has the same ill effects as changing capabilities while |
| 145 | * sessions are already established. |
| 146 | * |
| 147 | * @param[in] basic_mode basic-mode with-defaults parameter. |
| 148 | * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults |
| 149 | * parameter. |
| 150 | * @return 0 on success, -1 on error. |
| 151 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 152 | int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported); |
| 153 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 154 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 155 | * @brief Get with-defaults capability extra parameters. |
| 156 | * |
| 157 | * At least one argument must be non-NULL. |
| 158 | * |
| 159 | * @param[in,out] basic_mode basic-mode parameter. |
| 160 | * @param[in,out] also_supported also-supported parameter. |
| 161 | */ |
| 162 | void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported); |
| 163 | |
| 164 | /** |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 165 | * @brief Set capability of the server. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 166 | * |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 167 | * Capability can be used when some behavior or extension of the server is not defined |
| 168 | * as a YANG module. The provided value will be advertised in the server's \<hello\> |
| 169 | * messages. Note, that libnetconf only checks that the provided value is non-empty |
| 170 | * string. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 171 | * |
Michal Vasko | 50d2a5c | 2017-02-14 10:29:49 +0100 | [diff] [blame] | 172 | * @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] | 173 | */ |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 174 | int nc_server_set_capability(const char *value); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 175 | |
| 176 | /** |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 177 | * @brief Set the callback for getting yang-library capability identifier. If none is set, libyang context change count is used. |
| 178 | * |
| 179 | * @param[in] content_id_clb Callback that should return the yang-library content identifier. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 180 | * @param[in] user_data Optional arbitrary user data that will be passed to @p content_id_clb. |
| 181 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 182 | */ |
| 183 | void nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data, |
| 184 | void (*free_user_data)(void *user_data)); |
| 185 | |
| 186 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 187 | * @brief Set server timeout for receiving a hello message. |
| 188 | * |
| 189 | * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting. |
| 190 | */ |
| 191 | void nc_server_set_hello_timeout(uint16_t hello_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 192 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 193 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 194 | * @brief get server timeout for receiving a hello message. |
| 195 | * |
| 196 | * @return Hello message timeout, 0 is infinite. |
| 197 | */ |
| 198 | uint16_t nc_server_get_hello_timeout(void); |
| 199 | |
| 200 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 201 | * @brief Set server timeout for dropping an idle session. |
| 202 | * |
| 203 | * @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] | 204 | * because of inactivity. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 205 | */ |
| 206 | void nc_server_set_idle_timeout(uint16_t idle_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 207 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 208 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 209 | * @brief Get server timeout for dropping an idle session. |
| 210 | * |
| 211 | * @return Idle session timeout, 0 for for never dropping |
| 212 | * a session because of inactivity. |
| 213 | */ |
| 214 | uint16_t nc_server_get_idle_timeout(void); |
| 215 | |
| 216 | /** |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 217 | * @brief Get all the server capabilities including all the schemas. |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 218 | * |
| 219 | * A few capabilities (with-defaults, interleave) depend on the current |
| 220 | * server options. |
| 221 | * |
| 222 | * @param[in] ctx Context to read most capabilities from. |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 223 | * @return Array of capabilities, NULL on error. |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 224 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 225 | char **nc_server_get_cpblts(const struct ly_ctx *ctx); |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 226 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 227 | /** |
| 228 | * @brief Get the server capabilities including the schemas with the specified YANG version. |
| 229 | * |
| 230 | * A few capabilities (with-defaults, interleave) depend on the current |
| 231 | * server options. |
| 232 | * |
| 233 | * @param[in] ctx Context to read most capabilities from. |
| 234 | * @param[in] version YANG version of the schemas to be included in result, with |
| 235 | * LYS_VERSION_UNDEF the result is the same as from nc_server_get_cpblts(). |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 236 | * @return Array of capabilities, NULL on error. |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 237 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 238 | char **nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 240 | /** @} Server */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * @addtogroup server_session |
| 244 | * @{ |
| 245 | */ |
| 246 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 247 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 248 | * @brief Accept a new session on a pre-established transport session. |
| 249 | * |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 250 | * For detailed description, look at ::nc_accept(). |
| 251 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 252 | * @param[in] fdin File descriptor to read (unencrypted) XML data from. |
| 253 | * @param[in] fdout File descriptor to write (unencrypted) XML data to. |
| 254 | * @param[in] username NETCONF username as provided by the transport protocol. |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 255 | * @param[in] ctx Context for the session to use. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 256 | * @param[out] session New session on success. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 257 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 258 | * 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] | 259 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 260 | NC_MSG_TYPE nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx, |
| 261 | struct nc_session **session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 262 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 263 | /** |
| 264 | * @brief Create an empty structure for polling sessions. |
| 265 | * |
| 266 | * @return Empty pollsession structure, NULL on error. |
| 267 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 268 | struct nc_pollsession *nc_ps_new(void); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 269 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 270 | /** |
| 271 | * @brief Free a pollsession structure. |
| 272 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 273 | * !IMPORTANT! Make sure that @p ps is not accessible (is not used) |
Michal Vasko | 01082bf | 2016-04-07 10:44:21 +0200 | [diff] [blame] | 274 | * by any thread before and after this call! |
| 275 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 276 | * @param[in] ps Pollsession structure to free. |
| 277 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 278 | void nc_ps_free(struct nc_pollsession *ps); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 279 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 280 | /** |
| 281 | * @brief Add a session to a pollsession structure. |
| 282 | * |
| 283 | * @param[in] ps Pollsession structure to modify. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 284 | * @param[in] session Session to add to @p ps. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 285 | * @return 0 on success, -1 on error. |
| 286 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 287 | 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] | 288 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 289 | /** |
| 290 | * @brief Remove a session from a pollsession structure. |
| 291 | * |
| 292 | * @param[in] ps Pollsession structure to modify. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 293 | * @param[in] session Session to remove from @p ps. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 294 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 295 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 296 | int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session); |
| 297 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 298 | /** |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 299 | * @brief Get a session from a pollsession structure matching the session ID. |
| 300 | * |
| 301 | * @param[in] ps Pollsession structure to read from. |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 302 | * @param[in] idx Index of the session. |
| 303 | * @return Session on index, NULL if out-of-bounds. |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 304 | */ |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 305 | 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] | 306 | |
| 307 | /** |
Michal Vasko | 3ec3b11 | 2022-07-21 12:32:33 +0200 | [diff] [blame] | 308 | * @brief Callback for finding a session in a pollsession structure. |
| 309 | * |
| 310 | * @param[in] session Considered NETCONF session. |
| 311 | * @param[in] cb_data User data. |
| 312 | * @return 0 if the session does not match. |
| 313 | * @return non-zero if the session matches and should be returned. |
| 314 | */ |
| 315 | typedef int (*nc_ps_session_match_cb)(struct nc_session *session, void *cb_data); |
| 316 | |
| 317 | /** |
| 318 | * @brief Find a session in a pollsession structure using a matching callback. |
| 319 | * |
| 320 | * @param[in] ps Pollsession structure to read from. |
| 321 | * @param[in] match_cb Matching callback to use. |
| 322 | * @param[in] cb_data User data passed to @p cb. |
| 323 | * @return Found session, NULL if none matched. |
| 324 | */ |
| 325 | struct nc_session *nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data); |
| 326 | |
| 327 | /** |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 328 | * @brief Learn the number of sessions in a pollsession structure. |
| 329 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 330 | * Does not lock @p ps structure for efficiency. |
Michal Vasko | f4462fd | 2017-02-15 14:29:05 +0100 | [diff] [blame] | 331 | * |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 332 | * @param[in] ps Pollsession structure to check. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 333 | * @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] | 334 | */ |
| 335 | uint16_t nc_ps_session_count(struct nc_pollsession *ps); |
| 336 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 337 | #define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */ |
| 338 | #define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */ |
| 339 | #define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */ |
| 340 | #define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */ |
| 341 | #define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */ |
| 342 | #define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */ |
| 343 | #define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */ |
| 344 | #define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 345 | |
| 346 | #ifdef NC_ENABLED_SSH |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 347 | # define NC_PSPOLL_SSH_MSG 0x00100 /**< SSH message received (and processed, if relevant, only with SSH support). */ |
| 348 | # 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] | 349 | #endif |
| 350 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 351 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 352 | * @brief Poll sessions and process any received RPCs. |
| 353 | * |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 354 | * Only one event on one session is handled in one function call. If this event |
| 355 | * is a session termination (#NC_PSPOLL_SESSION_TERM returned), the session |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 356 | * should be removed from @p ps. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 357 | * |
| 358 | * @param[in] ps Pollsession structure to use. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 359 | * @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] | 360 | * infinite waiting. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 361 | * @param[in] session Session that was processed and that specific return bits concern. |
| 362 | * Can be NULL. |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 363 | * @return Bitfield of NC_PSPOLL_* macros. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 364 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 365 | 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] | 366 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 367 | /** |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 368 | * @brief Remove sessions from a pollsession structure and |
| 369 | * call nc_session_free() on them. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 370 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 371 | * 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] | 372 | * |
| 373 | * @param[in] ps Pollsession structure to clear. |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 374 | * @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] | 375 | * @param[in] data_free Session user data destructor. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 376 | */ |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 377 | 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] | 378 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 379 | /** @} Server Session */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * @addtogroup server |
| 383 | * @{ |
| 384 | */ |
| 385 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 386 | /** |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 387 | * @brief Add a new endpoint. |
| 388 | * |
| 389 | * Before the endpoint can accept any connections, its address and port must |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 390 | * 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] | 391 | * |
| 392 | * @param[in] name Arbitrary unique endpoint name. |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 393 | * @param[in] ti Transport protocol to use. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 394 | * @return 0 on success, -1 on error. |
| 395 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 396 | int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 397 | |
| 398 | /** |
| 399 | * @brief Stop listening on and remove an endpoint. |
| 400 | * |
| 401 | * @param[in] name Endpoint name. NULL matches all endpoints. |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 402 | * @param[in] ti Endpoint transport protocol. NULL matches any protocol. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 403 | * Redundant to set if @p name is set, endpoint names are |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 404 | * unique disregarding their protocol. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 405 | * @return 0 on success, -1 on not finding any match. |
| 406 | */ |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 407 | int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 408 | |
| 409 | /** |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 410 | * @brief Get the number of currently configured listening endpoints. |
| 411 | * Note that an ednpoint without address and/or port will be included |
| 412 | * even though it is not, in fact, listening. |
| 413 | * |
| 414 | * @return Number of added listening endpoints. |
| 415 | */ |
| 416 | int nc_server_endpt_count(void); |
| 417 | |
| 418 | /** |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 419 | * @brief Check if an endpoint exists. |
| 420 | * |
| 421 | * @param[in] name Endpoint name. |
| 422 | * @return 0 if does not exists, non-zero otherwise. |
| 423 | */ |
| 424 | int nc_server_is_endpt(const char *name); |
| 425 | |
| 426 | /** |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 427 | * @brief Change endpoint listening address. |
| 428 | * |
| 429 | * On error the previous listening socket (if any) is left untouched. |
| 430 | * |
| 431 | * @param[in] endpt_name Existing endpoint name. |
| 432 | * @param[in] address New listening address. |
| 433 | * @return 0 on success, -1 on error. |
| 434 | */ |
| 435 | int nc_server_endpt_set_address(const char *endpt_name, const char *address); |
| 436 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 437 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 438 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 439 | /** |
| 440 | * @brief Change endpoint listening port. |
| 441 | * |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 442 | * This is only valid on SSH/TLS transport endpoint. |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 443 | * On error the previous listening socket (if any) is left untouched. |
| 444 | * |
| 445 | * @param[in] endpt_name Existing endpoint name. |
| 446 | * @param[in] port New listening port. |
| 447 | * @return 0 on success, -1 on error. |
| 448 | */ |
| 449 | 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] | 450 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 451 | #endif |
| 452 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 453 | /** |
| 454 | * @brief Change endpoint permissions. |
| 455 | * |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 456 | * This is only valid on UNIX transport endpoint. |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 457 | * On error the previous listening socket (if any) is left untouched. |
| 458 | * |
| 459 | * @param[in] endpt_name Existing endpoint name. |
| 460 | * @param[in] mode New mode, -1 to use default. |
| 461 | * @param[in] uid New uid, -1 to use default. |
| 462 | * @param[in] gid New gid, -1 to use default. |
| 463 | * @return 0 on success, -1 on error. |
| 464 | */ |
| 465 | int nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid); |
| 466 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 467 | /** |
| 468 | * @brief Change endpoint keepalives state. Affects only new connections. |
| 469 | * |
| 470 | * @param[in] endpt_name Existing endpoint name. |
| 471 | * @param[in] enable Whether to enable or disable keepalives. |
| 472 | * @return 0 on success, -1 on error. |
| 473 | */ |
| 474 | int nc_server_endpt_enable_keepalives(const char *endpt_name, int enable); |
| 475 | |
| 476 | /** |
| 477 | * @brief Change endpoint keepalives parameters. Affects only new connections. |
| 478 | * |
| 479 | * @param[in] endpt_name Existing endpoint name. |
| 480 | * @param[in] idle_time Keepalive idle time in seconds, 1 by default, -1 to keep previous value. |
| 481 | * @param[in] max_probes Keepalive max probes sent, 10 by default, -1 to keep previous value. |
| 482 | * @param[in] probe_interval Keepalive probe interval in seconds, 5 by default, -1 to keep previous value. |
| 483 | * @return 0 on success, -1 on error. |
| 484 | */ |
| 485 | int nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval); |
| 486 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 487 | /** @} Server */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * @addtogroup server_session |
Michal Vasko | 4e6d324 | 2021-05-26 09:13:24 +0200 | [diff] [blame] | 491 | * @{ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 492 | */ |
| 493 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 494 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 495 | * @brief Accept new sessions on all the listening endpoints. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 496 | * |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 497 | * Once a new (TCP/IP) conection is established a different (quite long) timeout |
| 498 | * is used for waiting for transport-related data, which means this call can block |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 499 | * for much longer that @p timeout, but only with slow/faulty/malicious clients. |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 500 | * |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 501 | * Server capabilities are generated based on the content of @p ctx. The context must |
| 502 | * not be destroyed before the accepted NETCONF session is freed. |
| 503 | * |
| 504 | * Supported RPCs of models in the context are expected to have their callback |
| 505 | * in the corresponding RPC schema node set to a nc_rpc_clb function callback using ::nc_set_rpc_callback(). |
| 506 | * This callback is called by ::nc_ps_poll() if the particular RPC request is |
| 507 | * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format |
| 508 | * only) and ietf-netconf:close-session are set internally if left unset. |
| 509 | * |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 510 | * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 511 | * non-blocking call, -1 for infinite waiting. |
| 512 | * @param[in] ctx Context for the session to use. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 513 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 514 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 515 | * 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] | 516 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 517 | NC_MSG_TYPE nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 518 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 519 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 520 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 521 | /** |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 522 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF @p orig_session. |
| 523 | * 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] | 524 | * |
| 525 | * @param[in] orig_session Session that has a new SSH channel ready. |
| 526 | * @param[out] session New session. |
| 527 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 528 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
| 529 | */ |
| 530 | NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session); |
| 531 | |
| 532 | /** |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 533 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 534 | * that was polled in @p ps. Call this function only when nc_ps_poll() on @p ps returns #NC_PSPOLL_SSH_CHANNEL. |
| 535 | * 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] | 536 | * |
| 537 | * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call. |
| 538 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 539 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 540 | * 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] | 541 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 542 | 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] | 543 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 544 | /** @} Server Session */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 545 | |
| 546 | /** |
| 547 | * @defgroup server_ssh Server SSH |
| 548 | * @ingroup server |
| 549 | * |
| 550 | * @brief Server-side settings for SSH connections. |
| 551 | * @{ |
| 552 | */ |
| 553 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 554 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 555 | * @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] | 556 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 557 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 558 | * @param[in] pubkey_base64 Authorized public key binary content encoded in base64. |
| 559 | * @param[in] type Authorized public key SSH type. |
| 560 | * @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] | 561 | * @return 0 on success, -1 on error. |
| 562 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 563 | 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] | 564 | |
| 565 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 566 | * @brief Add an authorized client SSH public key. This public key can be used for |
| 567 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 568 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 569 | * @param[in] pubkey_path Path to the public key. |
| 570 | * @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] | 571 | * @return 0 on success, -1 on error. |
| 572 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 573 | 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] | 574 | |
| 575 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 576 | * @brief Remove an authorized client SSH public key. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 577 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 578 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 579 | * @param[in] pubkey_base64 Authorized public key content. NULL matches any key. |
| 580 | * @param[in] type Authorized public key type. 0 matches all types. |
| 581 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 582 | * @return 0 on success, -1 on not finding any match. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 583 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 584 | int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 585 | const char *username); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 586 | |
| 587 | /** |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 588 | * @brief Set the callback for SSH password authentication. If none is set, local system users are used. |
| 589 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 590 | * @param[in] passwd_auth_clb Callback that should authenticate the user. Username can be directly obtained from @p session. |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 591 | * Zero return indicates success, non-zero an error. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 592 | * @param[in] user_data Optional arbitrary user data that will be passed to @p passwd_auth_clb. |
| 593 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 594 | */ |
| 595 | void nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 596 | void *user_data), void *user_data, void (*free_user_data)(void *user_data)); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 597 | |
| 598 | /** |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame^] | 599 | * @brief Set the callback for SSH interactive authentication. If none is set, local PAM-based authentication is used. |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 600 | * |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame^] | 601 | * @param[in] interactive_auth_sess_clb Callback that should authenticate the user. |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 602 | * Zero return indicates success, non-zero an error. |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame^] | 603 | * @param[in] user_data Optional arbitrary user data that will be passed to @p interactive_auth_sess_clb. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 604 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 605 | */ |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame^] | 606 | void nc_server_ssh_set_interactive_auth_sess_clb(int (*interactive_auth_sess_clb)(const struct nc_session *session, |
| 607 | ssh_session ssh_sess, ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data)); |
| 608 | |
| 609 | /** |
| 610 | * @brief Deprecated, use ::nc_server_ssh_set_interactive_auth_sess_clb() instead. |
| 611 | */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 612 | void nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, |
| 613 | const ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data)); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 614 | |
| 615 | /** |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 616 | * @brief Set the name and a path to a PAM configuration file. |
| 617 | * |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame^] | 618 | * @p conf_name has to be set via this function prior to using PAM keyboard-interactive authentication method. |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 619 | * |
| 620 | * @param[in] conf_name Name of the configuration file. |
| 621 | * @param[in] conf_dir Optional. The absolute path to the directory in which the configuration file |
| 622 | * with the name @p conf_name is located. A newer version (>= 1.4) of PAM library is required to be |
| 623 | * able to specify the path. If NULL is passed, |
| 624 | * then the PAM's system directories will be searched (usually /etc/pam.d/). |
| 625 | * @return 0 on success, -1 on error. |
| 626 | */ |
| 627 | int nc_server_ssh_set_pam_conf_path(const char *conf_name, const char *conf_dir); |
| 628 | |
| 629 | /** |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 630 | * @brief Set the callback for SSH public key authentication. If none is set, local system users are used. |
| 631 | * |
| 632 | * @param[in] pubkey_auth_clb Callback that should authenticate the user. |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 633 | * Zero return indicates success, non-zero an error. |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 634 | * @param[in] user_data Optional arbitrary user data that will be passed to @p pubkey_auth_clb. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 635 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 636 | */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 637 | void nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 638 | void *user_data), void *user_data, void (*free_user_data)(void *user_data)); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 639 | |
| 640 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 641 | * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However, |
| 642 | * a maximum of one key of each type will be used during SSH authentication, later keys replacing |
| 643 | * the earlier ones. |
| 644 | * |
| 645 | * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 646 | * an error. On success exactly ONE of @p privkey_path or @p privkey_data is expected |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 647 | * to be set. The one set will be freed. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 648 | * - @p privkey_path expects a PEM file, |
| 649 | * - @p privkey_data expects a base-64 encoded ANS.1 DER data, |
| 650 | * - @p privkey_type type of the key in @p privkey_data. Use ::NC_SSH_KEY_UNKNOWN for |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 651 | * PKCS#8 key that includes the information about the key in its data. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 652 | * @param[in] user_data Optional arbitrary user data that will be passed to @p hostkey_clb. |
| 653 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 654 | */ |
| 655 | 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] | 656 | 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] | 657 | |
| 658 | /** |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 659 | * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself |
| 660 | * wil be retrieved using a callback. |
| 661 | * |
| 662 | * @param[in] endpt_name Existing endpoint name. |
| 663 | * @param[in] name Arbitrary name of the host key. |
| 664 | * @param[in] idx Optional index where to add the key. -1 adds at the end. |
| 665 | * @return 0 on success, -1 on error. |
| 666 | */ |
| 667 | int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx); |
| 668 | |
| 669 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 670 | * @brief Delete endpoint SSH host key. Their order is preserved. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 671 | * |
| 672 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 673 | * @param[in] name Name of the host key. NULL matches all the keys, but if @p idx != -1 then this must be NULL. |
| 674 | * @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] | 675 | * @return 0 on success, -1 on error. |
| 676 | */ |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 677 | 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] | 678 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 679 | /** |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 680 | * @brief Move endpoint SSH host key. |
| 681 | * |
| 682 | * @param[in] endpt_name Exisitng endpoint name. |
| 683 | * @param[in] key_mov Name of the host key that will be moved. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 684 | * @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. |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 685 | * @return 0 on success, -1 on error. |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 686 | */ |
| 687 | int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after); |
| 688 | |
| 689 | /** |
| 690 | * @brief Modify endpoint SSH host key. |
| 691 | * |
| 692 | * @param[in] endpt_name Exisitng endpoint name. |
| 693 | * @param[in] name Name of an existing host key. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 694 | * @param[in] new_name New name of the host key @p name. |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 695 | * @return 0 on success, -1 on error. |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 696 | */ |
| 697 | 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] | 698 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 699 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 700 | * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 701 | * are supported by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 702 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 703 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 704 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 705 | * @return 0 on success, -1 on error. |
| 706 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 707 | 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] | 708 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 709 | /** |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 710 | * @brief Get endpoint accepted SSH authentication methods. |
| 711 | * |
| 712 | * @param[in] endpt_name Existing endpoint name. |
| 713 | * @return Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 714 | */ |
| 715 | int nc_server_ssh_endpt_get_auth_methods(const char *endpt_name); |
| 716 | |
| 717 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 718 | * @brief Set endpoint SSH authentication attempts of every client. 3 by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 719 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 720 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 721 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 722 | * @return 0 on success, -1 on error. |
| 723 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 724 | 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] | 725 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 726 | /** |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 727 | * @brief Set endpoint SSH authentication timeout. 30 seconds by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 728 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 729 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 730 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 731 | * @return 0 on success, -1 on error. |
| 732 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 733 | 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] | 734 | |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 735 | /** @} Server SSH */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 736 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 737 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 738 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 739 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 740 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 741 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 742 | * @defgroup server_tls Server TLS |
| 743 | * @ingroup server |
| 744 | * |
| 745 | * @brief Server-side settings for TLS connections. |
| 746 | * @{ |
| 747 | */ |
| 748 | |
| 749 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 750 | * @brief Set the server TLS certificate. Only the name is set, the certificate itself |
| 751 | * wil be retrieved using a callback. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 752 | * |
| 753 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 754 | * @param[in] name Arbitrary certificate name. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 755 | * @return 0 on success, -1 on error. |
| 756 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 757 | 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] | 758 | |
| 759 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 760 | * @brief Set the callback for retrieving server certificate and matching private key. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 761 | * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 762 | * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success, |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 763 | * non-zero an error. On success exactly ONE of @p cert_path or @p cert_data and ONE of |
| 764 | * @p privkey_path and @p privkey_data is expected to be set. Those set will be freed. |
| 765 | * - @p cert_path expects a PEM file, |
| 766 | * - @p cert_data expects a base-64 encoded ASN.1 DER data, |
| 767 | * - @p privkey_path expects a PEM file, |
| 768 | * - @p privkey_data expects a base-64 encoded ANS.1 DER data, |
| 769 | * - @p privkey_type type of the key in @p privkey_data. |
| 770 | * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb. |
| 771 | * @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] | 772 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 773 | 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] | 774 | char **privkey_path, char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, |
| 775 | void (*free_user_data)(void *user_data)); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 776 | |
| 777 | /** |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 778 | * @brief Set the callback for retrieving server certificate chain |
| 779 | * |
| 780 | * @param[in] cert_chain_clb Callback that should return all the certificates of the chain. Zero return indicates success, |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 781 | * non-zero an error. On success, @p cert_paths and @p cert_data are expected to be set or left |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 782 | * NULL. Both will be (deeply) freed. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 783 | * - @p cert_paths expect an array of PEM files, |
| 784 | * - @p cert_path_count number of @p cert_paths array members, |
| 785 | * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data, |
| 786 | * - @p cert_data_count number of @p cert_data array members. |
| 787 | * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb. |
| 788 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
Andrew Langefeld | 440b6c7 | 2018-08-27 16:26:20 -0500 | [diff] [blame] | 789 | */ |
| 790 | 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] | 791 | 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] | 792 | |
| 793 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 794 | * @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] | 795 | * safely used together with nc_server_tls_endpt_set_trusted_ca_paths(). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 796 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 797 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 798 | * @param[in] name Arbitary name identifying this certificate list. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 799 | * @return 0 on success, -1 on error. |
| 800 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 801 | 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] | 802 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 803 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 804 | * @brief Set the callback for retrieving trusted certificates. |
| 805 | * |
| 806 | * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success, |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 807 | * non-zero an error. On success, @p cert_paths and @p cert_data are expected to be set or left |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 808 | * NULL. Both will be (deeply) freed. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 809 | * - @p cert_paths expect an array of PEM files, |
| 810 | * - @p cert_path_count number of @p cert_paths array members, |
| 811 | * - @p cert_data expect an array of base-64 encoded ASN.1 DER cert data, |
| 812 | * - @p cert_data_count number of @p cert_data array members. |
| 813 | * @param[in] user_data Optional arbitrary user data that will be passed to @p cert_clb. |
| 814 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any @p user_data. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 815 | */ |
| 816 | 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] | 817 | 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] | 818 | |
| 819 | /** |
| 820 | * @brief Remove a trusted certificate. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 821 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 822 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 823 | * @param[in] name Name of the certificate list to delete. NULL deletes all the lists. |
| 824 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 825 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 826 | 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] | 827 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 828 | /** |
| 829 | * @brief Set trusted Certificate Authority certificate locations. There can only be |
Michal Vasko | 5d2ad08 | 2016-02-09 11:47:59 +0100 | [diff] [blame] | 830 | * 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] | 831 | * 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] | 832 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 833 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 834 | * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL. |
| 835 | * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility |
| 836 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 837 | * @return 0 on success, -1 on error. |
| 838 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 839 | 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] | 840 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 841 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 842 | * @brief Set Certificate Revocation List locations. There can only be one file |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 843 | * and one directory, they are replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 844 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 845 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 846 | * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL. |
| 847 | * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility |
| 848 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 849 | * @return 0 on success, -1 on error. |
| 850 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 851 | 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] | 852 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 853 | /** |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 854 | * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 855 | * not affected. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 856 | * |
| 857 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 858 | */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 859 | void nc_server_tls_endpt_clear_crls(const char *endpt_name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 860 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 861 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 862 | * @brief Add a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 863 | * |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 864 | * It is possible to add an entry step-by-step, specifying first only @p ip and in later calls |
| 865 | * @p fingerprint, @p map_type, and optionally @p name spearately. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 866 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 867 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 868 | * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id |
| 869 | * is modified. |
| 870 | * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset. |
| 871 | * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset. |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 872 | * @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] | 873 | * @return 0 on success, -1 on error. |
| 874 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 875 | int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 876 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 877 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 878 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 879 | * @brief Remove a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 880 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 881 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 882 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 883 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 884 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 885 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 886 | * @return 0 on success, -1 on not finding any match. |
| 887 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 888 | int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 889 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 890 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 891 | /** |
| 892 | * @brief Get a cert-to-name entry. |
| 893 | * |
| 894 | * If a parameter is NULL, it is ignored. If its dereferenced value is NULL, |
| 895 | * it is filled and returned. If the value is set, it is used as a filter. |
| 896 | * Returns first matching entry. |
| 897 | * |
| 898 | * @param[in] endpt_name Existing endpoint name. |
| 899 | * @param[in,out] id Priority of the entry. |
| 900 | * @param[in,out] fingerprint Fingerprint fo the entry. |
| 901 | * @param[in,out] map_type Mapping type of the entry. |
| 902 | * @param[in,out] name Specific username for the entry. |
| 903 | * @return 0 on success, -1 on not finding any match. |
| 904 | */ |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 905 | int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 906 | char **name); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 907 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 908 | /** |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 909 | * @brief Get client certificate. |
| 910 | * |
| 911 | * @param[in] session Session to get the information from. |
| 912 | * @return Const session client certificate. |
| 913 | */ |
| 914 | const X509 *nc_session_get_client_cert(const struct nc_session *session); |
| 915 | |
| 916 | /** |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 917 | * @brief Set TLS authentication additional verify callback. |
| 918 | * |
| 919 | * Server will always perform cert-to-name based on its configuration. Only after it passes |
| 920 | * and this callback is set, it is also called. It should return exactly what OpenSSL |
| 921 | * verify callback meaning 1 for success, 0 to deny the user. |
| 922 | * |
| 923 | * @param[in] verify_clb Additional user verify callback. |
| 924 | */ |
| 925 | void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)); |
| 926 | |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 927 | /** @} Server TLS */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 928 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 929 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 930 | |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 931 | /** |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 932 | * @addtogroup server_session |
| 933 | * @{ |
| 934 | */ |
| 935 | |
| 936 | /** |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 937 | * @brief Get session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 938 | * |
| 939 | * @param[in] session Session to get the information from. |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 940 | * @return Session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 941 | */ |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 942 | time_t nc_session_get_start_time(const struct nc_session *session); |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 943 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 944 | /** |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 945 | * @brief Increase session notification subscription flag count. |
| 946 | * Supports multiple subscriptions on one session. |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 947 | * |
| 948 | * It is used only to ignore timeouts, because they are |
| 949 | * ignored for sessions with active subscriptions. |
| 950 | * |
| 951 | * @param[in] session Session to modify. |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 952 | */ |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 953 | void nc_session_inc_notif_status(struct nc_session *session); |
| 954 | |
| 955 | /** |
| 956 | * @brief Decrease session notification subscription flag count. |
| 957 | * Supports multiple subscriptions on one session. |
| 958 | * |
| 959 | * @param[in] session Session to modify. |
| 960 | */ |
| 961 | void nc_session_dec_notif_status(struct nc_session *session); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 962 | |
| 963 | /** |
| 964 | * @brief Get session notification subscription flag. |
| 965 | * |
| 966 | * @param[in] session Session to get the information from. |
| 967 | * @return 0 for no active subscription, non-zero for an active subscription. |
| 968 | */ |
| 969 | int nc_session_get_notif_status(const struct nc_session *session); |
| 970 | |
Michal Vasko | c446a38 | 2021-06-18 08:54:05 +0200 | [diff] [blame] | 971 | /** @} Server Session */ |
Radek Krejci | 6799a05 | 2017-05-19 14:23:23 +0200 | [diff] [blame] | 972 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 973 | #ifdef __cplusplus |
| 974 | } |
| 975 | #endif |
| 976 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 977 | #endif /* NC_SESSION_SERVER_H_ */ |