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 | |
| 18 | #include <stdint.h> |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 19 | #include <libyang/libyang.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 20 | |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 21 | #ifdef NC_ENABLED_TLS |
| 22 | # include <openssl/x509.h> |
| 23 | #endif |
| 24 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 25 | #include "session.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 26 | #include "netconf.h" |
| 27 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 28 | /** |
| 29 | * @brief Prototype of callbacks that are called if some RPCs are received. |
| 30 | * |
| 31 | * If \p session termination reason is changed in the callback, one last reply |
| 32 | * is sent and then the session is considered invalid. |
| 33 | * |
| 34 | * @param[in] rpc Parsed client RPC request. |
| 35 | * @param[in] session Session the RPC arrived on. |
| 36 | * @return Server reply. If NULL, an operation-failed error will be sent to the client. |
| 37 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 38 | typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session); |
| 39 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 40 | /** |
Michal Vasko | 7b09624 | 2016-01-29 15:55:10 +0100 | [diff] [blame] | 41 | * @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] | 42 | * |
| 43 | * @param[in] session Session to modify. |
| 44 | * @param[in] reason Reason of termination. |
| 45 | */ |
| 46 | void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason); |
| 47 | |
| 48 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 49 | * @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] | 50 | * |
| 51 | * 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] | 52 | * all the strings, which is thread-safe. Reading models is considered thread-safe |
| 53 | * as models cannot be removed and are rarely modified (augments or deviations). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 54 | * |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 55 | * 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] | 56 | * server initialization with that particular context, they will be called (changes |
| 57 | * will take effect). However, there could be race conditions as the access to |
| 58 | * these callbacks is not thread-safe. |
| 59 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 60 | * Server capabilities are generated based on its content. Changing the context |
| 61 | * in ways that result in changed capabilities (adding models, changing features) |
| 62 | * is discouraged after sessions are established as it is not possible to change |
| 63 | * capabilities of a session. |
| 64 | * |
| 65 | * This context can safely be destroyed only after calling the last libnetconf2 |
| 66 | * function in an application. |
| 67 | * |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 68 | * Supported RPCs of models in the context are expected to have their callback |
| 69 | * in the corresponding RPC schema node set to a nc_rpc_clb function callback using nc_set_rpc_callback(). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 70 | * This callback is called by nc_ps_poll() if the particular RPC request is |
| 71 | * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format |
| 72 | * only) and ietf-netconf:close-session are set internally if left unset. |
| 73 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 74 | * @param[in] ctx Core NETCONF server context. |
| 75 | * @return 0 on success, -1 on error. |
| 76 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 77 | int nc_server_init(struct ly_ctx *ctx); |
| 78 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 79 | /** |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 80 | * @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and |
| 81 | * server resources. |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 82 | */ |
| 83 | void nc_server_destroy(void); |
| 84 | |
| 85 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 86 | * @brief Set the with-defaults capability extra parameters. |
| 87 | * |
| 88 | * For the capability to be actually advertised, the server context must also |
| 89 | * include the ietf-netconf-with-defaults model. |
| 90 | * |
| 91 | * Changing this option has the same ill effects as changing capabilities while |
| 92 | * sessions are already established. |
| 93 | * |
| 94 | * @param[in] basic_mode basic-mode with-defaults parameter. |
| 95 | * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults |
| 96 | * parameter. |
| 97 | * @return 0 on success, -1 on error. |
| 98 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 99 | int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported); |
| 100 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 101 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 102 | * @brief Get with-defaults capability extra parameters. |
| 103 | * |
| 104 | * At least one argument must be non-NULL. |
| 105 | * |
| 106 | * @param[in,out] basic_mode basic-mode parameter. |
| 107 | * @param[in,out] also_supported also-supported parameter. |
| 108 | */ |
| 109 | void nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported); |
| 110 | |
| 111 | /** |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 112 | * @brief Set capability of the server. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 113 | * |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 114 | * Capability can be used when some behavior or extension of the server is not defined |
| 115 | * as a YANG module. The provided value will be advertised in the server's \<hello\> |
| 116 | * messages. Note, that libnetconf only checks that the provided value is non-empty |
| 117 | * string. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 118 | * |
Michal Vasko | 50d2a5c | 2017-02-14 10:29:49 +0100 | [diff] [blame] | 119 | * @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] | 120 | */ |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 121 | int nc_server_set_capability(const char *value); |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 124 | * @brief Set server timeout for receiving a hello message. |
| 125 | * |
| 126 | * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting. |
| 127 | */ |
| 128 | void nc_server_set_hello_timeout(uint16_t hello_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 129 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 130 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 131 | * @brief get server timeout for receiving a hello message. |
| 132 | * |
| 133 | * @return Hello message timeout, 0 is infinite. |
| 134 | */ |
| 135 | uint16_t nc_server_get_hello_timeout(void); |
| 136 | |
| 137 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 138 | * @brief Set server timeout for dropping an idle session. |
| 139 | * |
| 140 | * @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] | 141 | * because of inactivity. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 142 | */ |
| 143 | void nc_server_set_idle_timeout(uint16_t idle_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 144 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 145 | /** |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 146 | * @brief Get server timeout for dropping an idle session. |
| 147 | * |
| 148 | * @return Idle session timeout, 0 for for never dropping |
| 149 | * a session because of inactivity. |
| 150 | */ |
| 151 | uint16_t nc_server_get_idle_timeout(void); |
| 152 | |
| 153 | /** |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 154 | * @brief Get all the server capabilities as will be sent to every client. |
| 155 | * |
| 156 | * A few capabilities (with-defaults, interleave) depend on the current |
| 157 | * server options. |
| 158 | * |
| 159 | * @param[in] ctx Context to read most capabilities from. |
| 160 | * @return Array of capabilities stored in the \p ctx dictionary, NULL on error. |
| 161 | */ |
| 162 | const char **nc_server_get_cpblts(struct ly_ctx *ctx); |
| 163 | |
| 164 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 165 | * @brief Accept a new session on a pre-established transport session. |
| 166 | * |
| 167 | * @param[in] fdin File descriptor to read (unencrypted) XML data from. |
| 168 | * @param[in] fdout File descriptor to write (unencrypted) XML data to. |
| 169 | * @param[in] username NETCONF username as provided by the transport protocol. |
| 170 | * @param[out] session New session on success. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 171 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 172 | * 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] | 173 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 174 | 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] | 175 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 176 | /** |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 177 | * @brief Set a global nc_rpc_clb that is called if the particular RPC request is |
| 178 | * received and the private field in the corresponding RPC schema node is NULL. |
| 179 | * |
| 180 | * @param[in] clb An user-defined nc_rpc_clb function callback, NULL to default. |
| 181 | */ |
| 182 | void nc_set_global_rpc_clb(nc_rpc_clb clb); |
| 183 | |
| 184 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 185 | * @brief Create an empty structure for polling sessions. |
| 186 | * |
| 187 | * @return Empty pollsession structure, NULL on error. |
| 188 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 189 | struct nc_pollsession *nc_ps_new(void); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 190 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 191 | /** |
| 192 | * @brief Free a pollsession structure. |
| 193 | * |
Michal Vasko | 01082bf | 2016-04-07 10:44:21 +0200 | [diff] [blame] | 194 | * !IMPORTANT! Make sure that \p ps is not accessible (is not used) |
| 195 | * by any thread before and after this call! |
| 196 | * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 197 | * @param[in] ps Pollsession structure to free. |
| 198 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 199 | void nc_ps_free(struct nc_pollsession *ps); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 200 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 201 | /** |
| 202 | * @brief Add a session to a pollsession structure. |
| 203 | * |
| 204 | * @param[in] ps Pollsession structure to modify. |
| 205 | * @param[in] session Session to add to \p ps. |
| 206 | * @return 0 on success, -1 on error. |
| 207 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 208 | 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] | 209 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 210 | /** |
| 211 | * @brief Remove a session from a pollsession structure. |
| 212 | * |
| 213 | * @param[in] ps Pollsession structure to modify. |
| 214 | * @param[in] session Session to remove from \p ps. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 215 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 216 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 217 | int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session); |
| 218 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 219 | /** |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 220 | * @brief Get a session from a pollsession structure matching the session ID. |
| 221 | * |
| 222 | * @param[in] ps Pollsession structure to read from. |
| 223 | * @param[in] sid Session ID of the session. |
| 224 | * @return Matching session or NULL on not found. |
| 225 | */ |
| 226 | struct nc_session *nc_ps_get_session_by_sid(const struct nc_pollsession *ps, uint32_t sid); |
| 227 | |
| 228 | /** |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 229 | * @brief Learn the number of sessions in a pollsession structure. |
| 230 | * |
Michal Vasko | f4462fd | 2017-02-15 14:29:05 +0100 | [diff] [blame] | 231 | * Does not lock \p ps structure for efficiency. |
| 232 | * |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 233 | * @param[in] ps Pollsession structure to check. |
Michal Vasko | c72d0e6 | 2016-07-26 11:36:11 +0200 | [diff] [blame] | 234 | * @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] | 235 | */ |
| 236 | uint16_t nc_ps_session_count(struct nc_pollsession *ps); |
| 237 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 238 | #define NC_PSPOLL_NOSESSIONS 0x0001 /**< No sessions to poll. */ |
| 239 | #define NC_PSPOLL_TIMEOUT 0x0002 /**< Timeout elapsed. */ |
| 240 | #define NC_PSPOLL_RPC 0x0004 /**< RPC was correctly parsed and processed. */ |
| 241 | #define NC_PSPOLL_BAD_RPC 0x0008 /**< RPC was received, but failed to be parsed. */ |
| 242 | #define NC_PSPOLL_REPLY_ERROR 0x0010 /**< Response to the RPC was a \<rpc-reply\> of type error. */ |
| 243 | #define NC_PSPOLL_SESSION_TERM 0x0020 /**< Some session was terminated. */ |
| 244 | #define NC_PSPOLL_SESSION_ERROR 0x0040 /**< Some session was terminated incorrectly (not by a \<close-session\> or \<kill-session\> RPC). */ |
| 245 | #define NC_PSPOLL_ERROR 0x0080 /**< Other fatal errors (they are printed). */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 246 | |
| 247 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 248 | # define NC_PSPOLL_SSH_MSG 0x0080 /**< SSH message received (and processed, if relevant, only with SSH support). */ |
| 249 | # define NC_PSPOLL_SSH_CHANNEL 0x0100 /**< New SSH channel opened on an existing session (only with SSH support). */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 250 | #endif |
| 251 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 252 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 253 | * @brief Poll sessions and process any received RPCs. |
| 254 | * |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 255 | * All the sessions must be running. Only one event on one session |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 256 | * is handled in one function call. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 257 | * |
| 258 | * @param[in] ps Pollsession structure to use. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 259 | * @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] | 260 | * infinite waiting. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 261 | * @param[in] session Session that was processed and that specific return bits concern. |
| 262 | * Can be NULL. |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 263 | * @return Bitfield of NC_PSPOLL_* macros. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 264 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 265 | 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] | 266 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 267 | /** |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 268 | * @brief Remove sessions from a pollsession structure and |
| 269 | * call nc_session_free() on them. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 270 | * |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 271 | * 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] | 272 | * |
| 273 | * @param[in] ps Pollsession structure to clear. |
Michal Vasko | cad3ac4 | 2016-03-01 09:06:01 +0100 | [diff] [blame] | 274 | * @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] | 275 | * @param[in] data_free Session user data destructor. |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 276 | */ |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 277 | 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] | 278 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 279 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 280 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 281 | /** |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 282 | * @brief Add a new endpoint. |
| 283 | * |
| 284 | * Before the endpoint can accept any connections, its address and port must |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 285 | * be set. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 286 | * |
| 287 | * @param[in] name Arbitrary unique endpoint name. |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 288 | * @param[in] ti Transport protocol to use. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 289 | * @return 0 on success, -1 on error. |
| 290 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 291 | int nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * @brief Stop listening on and remove an endpoint. |
| 295 | * |
| 296 | * @param[in] name Endpoint name. NULL matches all endpoints. |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 297 | * @param[in] ti Endpoint transport protocol. NULL matches any protocol. |
| 298 | * Redundant to set if \p name is set, endpoint names are |
| 299 | * unique disregarding their protocol. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 300 | * @return 0 on success, -1 on not finding any match. |
| 301 | */ |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 302 | int nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 303 | |
| 304 | /** |
Michal Vasko | e8e0770 | 2017-03-15 10:19:30 +0100 | [diff] [blame] | 305 | * @brief Get the number of currently configured listening endpoints. |
| 306 | * Note that an ednpoint without address and/or port will be included |
| 307 | * even though it is not, in fact, listening. |
| 308 | * |
| 309 | * @return Number of added listening endpoints. |
| 310 | */ |
| 311 | int nc_server_endpt_count(void); |
| 312 | |
| 313 | /** |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 314 | * @brief Change endpoint listening address. |
| 315 | * |
| 316 | * On error the previous listening socket (if any) is left untouched. |
| 317 | * |
| 318 | * @param[in] endpt_name Existing endpoint name. |
| 319 | * @param[in] address New listening address. |
| 320 | * @return 0 on success, -1 on error. |
| 321 | */ |
| 322 | int nc_server_endpt_set_address(const char *endpt_name, const char *address); |
| 323 | |
| 324 | /** |
| 325 | * @brief Change endpoint listening port. |
| 326 | * |
| 327 | * On error the previous listening socket (if any) is left untouched. |
| 328 | * |
| 329 | * @param[in] endpt_name Existing endpoint name. |
| 330 | * @param[in] port New listening port. |
| 331 | * @return 0 on success, -1 on error. |
| 332 | */ |
| 333 | 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] | 334 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 335 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 336 | * @brief Accept new sessions on all the listening endpoints. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 337 | * |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 338 | * Once a new (TCP/IP) conection is established a different (quite long) timeout |
| 339 | * is used for waiting for transport-related data, which means this call can block |
| 340 | * for much longer that \p timeout, but only with slow/faulty/malicious clients. |
| 341 | * |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 342 | * @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] | 343 | * non-blocking call, -1 for infinite waiting. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 344 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 345 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 346 | * 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] | 347 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 348 | NC_MSG_TYPE nc_accept(int timeout, struct nc_session **session); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 349 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 350 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 351 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 352 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 353 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 354 | /** |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 355 | * @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] | 356 | * 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] | 357 | * |
| 358 | * @param[in] orig_session Session that has a new SSH channel ready. |
| 359 | * @param[out] session New session. |
| 360 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 361 | * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors. |
| 362 | */ |
| 363 | NC_MSG_TYPE nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session); |
| 364 | |
| 365 | /** |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 366 | * @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] | 367 | * 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] | 368 | * 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] | 369 | * |
| 370 | * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call. |
| 371 | * @param[out] session New session. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 372 | * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message |
| 373 | * 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] | 374 | */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 375 | 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] | 376 | |
| 377 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 378 | * @brief Add an authorized client SSH public key. This public key can be used for |
| 379 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 380 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 381 | * @param[in] pubkey_base64 Authorized public key binary content encoded in base64. |
| 382 | * @param[in] type Authorized public key SSH type. |
| 383 | * @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] | 384 | * @return 0 on success, -1 on error. |
| 385 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 386 | 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] | 387 | |
| 388 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 389 | * @brief Add an authorized client SSH public key. This public key can be used for |
| 390 | * publickey authentication (for any SSH connection, even Call Home) afterwards. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 391 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 392 | * @param[in] pubkey_path Path to the public key. |
| 393 | * @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] | 394 | * @return 0 on success, -1 on error. |
| 395 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 396 | 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] | 397 | |
| 398 | /** |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 399 | * @brief Remove an authorized client SSH public key. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 400 | * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 401 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 402 | * @param[in] pubkey_base64 Authorized public key content. NULL matches any key. |
| 403 | * @param[in] type Authorized public key type. 0 matches all types. |
| 404 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 405 | * @return 0 on success, -1 on not finding any match. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 406 | */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 407 | int nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 408 | const char *username); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 409 | |
| 410 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 411 | * @brief Add endpoint SSH host keys the server will identify itself with. Only the name is set, the key itself |
| 412 | * wil be retrieved using a callback. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 413 | * |
| 414 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 415 | * @param[in] name Arbitrary name of the host key. |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 416 | * @param[in] idx Optional index where to add the key. -1 adds at the end. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 417 | * @return 0 on success, -1 on error. |
| 418 | */ |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 419 | int nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 420 | |
| 421 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 422 | * @brief Set the callback for retrieving host keys. Any RSA, DSA, and ECDSA keys can be added. However, |
| 423 | * a maximum of one key of each type will be used during SSH authentication, later keys replacing |
| 424 | * the earlier ones. |
| 425 | * |
| 426 | * @param[in] hostkey_clb Callback that should return the key itself. Zero return indicates success, non-zero |
| 427 | * an error. On success exactly ONE of \p privkey_path or \p privkey_data is expected |
| 428 | * to be set. The one set will be freed. |
| 429 | * - \p privkey_path expects a PEM file, |
| 430 | * - \p privkey_data expects a base-64 encoded ANS.1 DER data, |
| 431 | * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key. |
| 432 | * @param[in] user_data Optional arbitrary user data that will be passed to \p hostkey_clb. |
| 433 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 434 | */ |
| 435 | void nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path, |
| 436 | char **privkey_data, int *privkey_data_rsa), |
| 437 | void *user_data, void (*free_user_data)(void *user_data)); |
| 438 | |
| 439 | /** |
| 440 | * @brief Delete endpoint SSH host key. Their order is preserved. |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 441 | * |
| 442 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 443 | * @param[in] name Name of the host key. NULL matches all the keys, but if \p idx != -1 then this must be NULL. |
| 444 | * @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] | 445 | * @return 0 on success, -1 on error. |
| 446 | */ |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 447 | 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] | 448 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 449 | /** |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 450 | * @brief Move endpoint SSH host key. |
| 451 | * |
| 452 | * @param[in] endpt_name Exisitng endpoint name. |
| 453 | * @param[in] key_mov Name of the host key that will be moved. |
| 454 | * @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. |
| 455 | * @return 0 in success, -1 on error. |
| 456 | */ |
| 457 | int nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after); |
| 458 | |
| 459 | /** |
| 460 | * @brief Modify endpoint SSH host key. |
| 461 | * |
| 462 | * @param[in] endpt_name Exisitng endpoint name. |
| 463 | * @param[in] name Name of an existing host key. |
| 464 | * @param[in] new_name New name of the host key \p name. |
| 465 | * @return 0 in success, -1 on error. |
| 466 | */ |
| 467 | 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] | 468 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 469 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 470 | * @brief Set endpoint SSH banner the server will send to every client. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 471 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 472 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 473 | * @param[in] banner SSH banner. |
| 474 | * @return 0 on success, -1 on error. |
| 475 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 476 | int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 477 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 478 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 479 | * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 480 | * are supported by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 481 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 482 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 483 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 484 | * @return 0 on success, -1 on error. |
| 485 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 486 | 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] | 487 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 488 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 489 | * @brief Set endpoint SSH authentication attempts of every client. 3 by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 490 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 491 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 492 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 493 | * @return 0 on success, -1 on error. |
| 494 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 495 | 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] | 496 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 497 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 498 | * @brief Set endpoint SSH authentication timeout. 10 seconds by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 499 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 500 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 501 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 502 | * @return 0 on success, -1 on error. |
| 503 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 504 | 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] | 505 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 506 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 507 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 508 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 509 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 510 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 511 | * @brief Set the server TLS certificate. Only the name is set, the certificate itself |
| 512 | * wil be retrieved using a callback. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 513 | * |
| 514 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 515 | * @param[in] name Arbitrary certificate name. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 516 | * @return 0 on success, -1 on error. |
| 517 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 518 | 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] | 519 | |
| 520 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 521 | * @brief Set the callback for retrieving server certificate and matching private key. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 522 | * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 523 | * @param[in] cert_clb Callback that should return the certificate and the key itself. Zero return indicates success, |
| 524 | * non-zero an error. On success exactly ONE of \p cert_path or \p cert_data and ONE of |
| 525 | * \p privkey_path and \p privkey_data is expected to be set. Those set will be freed. |
| 526 | * - \p cert_path expects a PEM file, |
| 527 | * - \p cert_data expects a base-64 encoded ASN.1 DER data, |
| 528 | * - \p privkey_path expects a PEM file, |
| 529 | * - \p privkey_data expects a base-64 encoded ANS.1 DER data, |
| 530 | * - \p privkey_data_rsa flag whether \p privkey_data are the data of an RSA (1) or a DSA (0) key. |
| 531 | * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb. |
| 532 | * @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] | 533 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 534 | void nc_server_tls_set_server_cert_clb(int (*cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data, |
| 535 | char **privkey_path, char **privkey_data, int *privkey_data_rsa), |
| 536 | void *user_data, void (*free_user_data)(void *user_data)); |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 537 | |
| 538 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 539 | * @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] | 540 | * safely used together with nc_server_tls_endpt_set_trusted_ca_paths(). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 541 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 542 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 543 | * @param[in] name Arbitary name identifying this certificate list. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 544 | * @return 0 on success, -1 on error. |
| 545 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 546 | 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] | 547 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 548 | /** |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 549 | * @brief Set the callback for retrieving trusted certificates. |
| 550 | * |
| 551 | * @param[in] cert_list_clb Callback that should return all the certificates of a list. Zero return indicates success, |
| 552 | * non-zero an error. On success, \p cert_paths and \p cert_data are expected to be set or left |
| 553 | * NULL. Both will be (deeply) freed. |
| 554 | * - \p cert_paths expect an array of PEM files, |
| 555 | * - \p cert_path_count number of \p cert_paths array members, |
| 556 | * - \p cert_data expect an array of base-64 encoded ASN.1 DER cert data, |
| 557 | * - \p cert_data_count number of \p cert_data array members. |
| 558 | * @param[in] user_data Optional arbitrary user data that will be passed to \p cert_clb. |
| 559 | * @param[in] free_user_data Optional callback that will be called during cleanup to free any \p user_data. |
| 560 | */ |
| 561 | void nc_server_tls_set_trusted_cert_list_clb(int (*cert_list_clb)(const char *name, void *user_data, char ***cert_paths, |
| 562 | int *cert_path_count, char ***cert_data, int *cert_data_count), |
| 563 | void *user_data, void (*free_user_data)(void *user_data)); |
| 564 | |
| 565 | /** |
| 566 | * @brief Remove a trusted certificate. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 567 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 568 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 569 | * @param[in] name Name of the certificate list to delete. NULL deletes all the lists. |
| 570 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 571 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 572 | 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] | 573 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 574 | /** |
| 575 | * @brief Set trusted Certificate Authority certificate locations. There can only be |
Michal Vasko | 5d2ad08 | 2016-02-09 11:47:59 +0100 | [diff] [blame] | 576 | * 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] | 577 | * 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] | 578 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 579 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 580 | * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL. |
| 581 | * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility |
| 582 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 583 | * @return 0 on success, -1 on error. |
| 584 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 585 | 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] | 586 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 587 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 588 | * @brief Set Certificate Revocation List locations. There can only be one file |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 589 | * and one directory, they are replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 590 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 591 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 592 | * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL. |
| 593 | * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility |
| 594 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 595 | * @return 0 on success, -1 on error. |
| 596 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 597 | 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] | 598 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 599 | /** |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 600 | * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 601 | * not affected. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 602 | * |
| 603 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 604 | */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 605 | void nc_server_tls_endpt_clear_crls(const char *endpt_name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 606 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 607 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 608 | * @brief Add a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 609 | * |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 610 | * It is possible to add an entry step-by-step, specifying first only \p ip and in later calls |
| 611 | * \p fingerprint, \p map_type, and optionally \p name spearately. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 612 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 613 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 614 | * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id |
| 615 | * is modified. |
| 616 | * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset. |
| 617 | * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset. |
| 618 | * @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] | 619 | * @return 0 on success, -1 on error. |
| 620 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 621 | int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, |
| 622 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 623 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 624 | /** |
Michal Vasko | 58c22a2 | 2016-11-23 13:49:53 +0100 | [diff] [blame] | 625 | * @brief Remove a cert-to-name entry. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 626 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 627 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 628 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 629 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 630 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 631 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 632 | * @return 0 on success, -1 on not finding any match. |
| 633 | */ |
Michal Vasko | 3a889fd | 2016-09-30 12:16:37 +0200 | [diff] [blame] | 634 | int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, |
| 635 | NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 636 | |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 637 | /** |
| 638 | * @brief Get a cert-to-name entry. |
| 639 | * |
| 640 | * If a parameter is NULL, it is ignored. If its dereferenced value is NULL, |
| 641 | * it is filled and returned. If the value is set, it is used as a filter. |
| 642 | * Returns first matching entry. |
| 643 | * |
| 644 | * @param[in] endpt_name Existing endpoint name. |
| 645 | * @param[in,out] id Priority of the entry. |
| 646 | * @param[in,out] fingerprint Fingerprint fo the entry. |
| 647 | * @param[in,out] map_type Mapping type of the entry. |
| 648 | * @param[in,out] name Specific username for the entry. |
| 649 | * @return 0 on success, -1 on not finding any match. |
| 650 | */ |
Michal Vasko | f585ac7 | 2016-11-25 15:16:38 +0100 | [diff] [blame] | 651 | int nc_server_tls_endpt_get_ctn(const char *endpt_name, uint32_t *id, char **fingerprint, NC_TLS_CTN_MAPTYPE *map_type, |
| 652 | char **name); |
Michal Vasko | df5e6af | 2016-11-23 13:50:56 +0100 | [diff] [blame] | 653 | |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 654 | /** |
Michal Vasko | 709598e | 2016-11-28 14:48:32 +0100 | [diff] [blame] | 655 | * @brief Get client certificate. |
| 656 | * |
| 657 | * @param[in] session Session to get the information from. |
| 658 | * @return Const session client certificate. |
| 659 | */ |
| 660 | const X509 *nc_session_get_client_cert(const struct nc_session *session); |
| 661 | |
| 662 | /** |
Michal Vasko | 7060bcf | 2016-11-28 14:48:11 +0100 | [diff] [blame] | 663 | * @brief Set TLS authentication additional verify callback. |
| 664 | * |
| 665 | * Server will always perform cert-to-name based on its configuration. Only after it passes |
| 666 | * and this callback is set, it is also called. It should return exactly what OpenSSL |
| 667 | * verify callback meaning 1 for success, 0 to deny the user. |
| 668 | * |
| 669 | * @param[in] verify_clb Additional user verify callback. |
| 670 | */ |
| 671 | void nc_server_tls_set_verify_clb(int (*verify_clb)(const struct nc_session *session)); |
| 672 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 673 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 674 | |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 675 | /** |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 676 | * @brief Get session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 677 | * |
| 678 | * @param[in] session Session to get the information from. |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 679 | * @return Session start time. |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 680 | */ |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 681 | time_t nc_session_get_start_time(const struct nc_session *session); |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 682 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 683 | /** |
| 684 | * @brief Set session notification subscription flag. |
| 685 | * |
| 686 | * It is used only to ignore timeouts, because they are |
| 687 | * ignored for sessions with active subscriptions. |
| 688 | * |
| 689 | * @param[in] session Session to modify. |
| 690 | * @param[in] notif_status 0 for no active subscriptions, non-zero for an active subscription. |
| 691 | */ |
| 692 | void nc_session_set_notif_status(struct nc_session *session, int notif_status); |
| 693 | |
| 694 | /** |
| 695 | * @brief Get session notification subscription flag. |
| 696 | * |
| 697 | * @param[in] session Session to get the information from. |
| 698 | * @return 0 for no active subscription, non-zero for an active subscription. |
| 699 | */ |
| 700 | int nc_session_get_notif_status(const struct nc_session *session); |
| 701 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 702 | #endif /* NC_SESSION_SERVER_H_ */ |