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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef NC_SESSION_SERVER_H_ |
| 24 | #define NC_SESSION_SERVER_H_ |
| 25 | |
| 26 | #include <stdint.h> |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 27 | #include <libyang/libyang.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 28 | |
| 29 | #include "session.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 30 | #include "netconf.h" |
| 31 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 32 | /** |
| 33 | * @brief Prototype of callbacks that are called if some RPCs are received. |
| 34 | * |
| 35 | * If \p session termination reason is changed in the callback, one last reply |
| 36 | * is sent and then the session is considered invalid. |
| 37 | * |
| 38 | * @param[in] rpc Parsed client RPC request. |
| 39 | * @param[in] session Session the RPC arrived on. |
| 40 | * @return Server reply. If NULL, an operation-failed error will be sent to the client. |
| 41 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 42 | typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session); |
| 43 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 44 | /** |
Michal Vasko | 7b09624 | 2016-01-29 15:55:10 +0100 | [diff] [blame] | 45 | * @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] | 46 | * |
| 47 | * @param[in] session Session to modify. |
| 48 | * @param[in] reason Reason of termination. |
| 49 | */ |
| 50 | void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason); |
| 51 | |
| 52 | /** |
| 53 | * @brief Initialize the server using a libyang context. |
| 54 | * |
| 55 | * The context is not modified internally, only its dictionary is used for holding |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 56 | * all the strings. When the dictionary is being written to or removed from, |
| 57 | * libnetconf2 always holds ctx lock using nc_ctx_lock(). Reading models is considered |
| 58 | * thread-safe as models cannot be removed and are rarely modified. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 59 | * |
| 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 | * |
| 68 | * Supported RPCs of models in the context are expected to have the private field |
| 69 | * in the corresponding RPC schema node set to a nc_rpc_clb function callback. |
| 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 | * |
| 74 | * Access to the context in libnetconf2 functions is not managed in any way, |
| 75 | * the application is responsible for handling it in a thread-safe manner. |
| 76 | * |
| 77 | * @param[in] ctx Core NETCONF server context. |
| 78 | * @return 0 on success, -1 on error. |
| 79 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 80 | int nc_server_init(struct ly_ctx *ctx); |
| 81 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 82 | /** |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 83 | * @brief Destroy any dynamically allocated server resources. |
| 84 | */ |
| 85 | void nc_server_destroy(void); |
| 86 | |
| 87 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 88 | * @brief Set the with-defaults capability extra parameters. |
| 89 | * |
| 90 | * For the capability to be actually advertised, the server context must also |
| 91 | * include the ietf-netconf-with-defaults model. |
| 92 | * |
| 93 | * Changing this option has the same ill effects as changing capabilities while |
| 94 | * sessions are already established. |
| 95 | * |
| 96 | * @param[in] basic_mode basic-mode with-defaults parameter. |
| 97 | * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults |
| 98 | * parameter. |
| 99 | * @return 0 on success, -1 on error. |
| 100 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 101 | int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported); |
| 102 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 103 | /** |
| 104 | * @brief Set the interleave capability. |
| 105 | * |
| 106 | * For the capability to be actually advertised, the server context must also |
| 107 | * include the nc-notifications model. |
| 108 | * |
| 109 | * Changing this option has the same ill effects as changing capabilities while |
| 110 | * sessions are already established. |
| 111 | * |
| 112 | * @param[in] interleave_support 1 to suport interleave, 0 to not. |
| 113 | */ |
| 114 | void nc_server_set_capab_interleave(int interleave_support); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 115 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 116 | /** |
| 117 | * @brief Set server timeout for receiving a hello message. |
| 118 | * |
| 119 | * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting. |
| 120 | */ |
| 121 | void nc_server_set_hello_timeout(uint16_t hello_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 122 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 123 | /** |
| 124 | * @brief Set server timeout for dropping an idle session. |
| 125 | * |
| 126 | * @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] | 127 | * because of inactivity. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 128 | */ |
| 129 | void nc_server_set_idle_timeout(uint16_t idle_timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 130 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 131 | /** |
| 132 | * @brief Accept a new session on a pre-established transport session. |
| 133 | * |
| 134 | * @param[in] fdin File descriptor to read (unencrypted) XML data from. |
| 135 | * @param[in] fdout File descriptor to write (unencrypted) XML data to. |
| 136 | * @param[in] username NETCONF username as provided by the transport protocol. |
| 137 | * @param[out] session New session on success. |
| 138 | * @return 0 on success, -1 on error. |
| 139 | */ |
| 140 | int 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] | 141 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 142 | /** |
| 143 | * @brief Create an empty structure for polling sessions. |
| 144 | * |
| 145 | * @return Empty pollsession structure, NULL on error. |
| 146 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 147 | struct nc_pollsession *nc_ps_new(void); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 148 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 149 | /** |
| 150 | * @brief Free a pollsession structure. |
| 151 | * |
| 152 | * @param[in] ps Pollsession structure to free. |
| 153 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 154 | void nc_ps_free(struct nc_pollsession *ps); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 155 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 156 | /** |
| 157 | * @brief Add a session to a pollsession structure. |
| 158 | * |
| 159 | * @param[in] ps Pollsession structure to modify. |
| 160 | * @param[in] session Session to add to \p ps. |
| 161 | * @return 0 on success, -1 on error. |
| 162 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 163 | 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] | 164 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 165 | /** |
| 166 | * @brief Remove a session from a pollsession structure. |
| 167 | * |
| 168 | * @param[in] ps Pollsession structure to modify. |
| 169 | * @param[in] session Session to remove from \p ps. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 170 | * @return 0 on success, -1 on not found. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 171 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 172 | int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session); |
| 173 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 174 | /** |
| 175 | * @brief Poll sessions and process any received RPCs. |
| 176 | * |
| 177 | * All the sessions must be running. If a session fails causing it to change its |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 178 | * status, it can be learnt from the return value. Only one event on one session |
| 179 | * is handled in one function call. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 180 | * |
| 181 | * @param[in] ps Pollsession structure to use. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 182 | * @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] | 183 | * infinite waiting. |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 184 | * @return 0 on elapsed timeout, |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame^] | 185 | * 1 if an RPC was processed (even if it was not known - it failed to be |
| 186 | * parsed into session ctx), |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 187 | * 2 if an RPC was processed and there are unhandled events on other sessions, |
| 188 | * 3 if a session from \p ps changed its status (was invalidated), |
| 189 | * -1 on error. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 190 | * |
| 191 | * Only with SSH support: |
| 192 | * 4 if an SSH message was processed, |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 193 | * 5 if a new NETCONF SSH channel was created; call #nc_ssh_ps_accept_channel() |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 194 | * to establish a new NETCONF session. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 195 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 196 | int nc_ps_poll(struct nc_pollsession *ps, int timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 197 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 198 | /** |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 199 | * @brief Remove invalid sessions from a pollsession structure and |
| 200 | * call #nc_session_free() on them. |
| 201 | * |
| 202 | * Calling this function makes sense if #nc_ps_poll() returned 3. |
| 203 | * |
| 204 | * @param[in] ps Pollsession structure to clear. |
| 205 | */ |
| 206 | void nc_ps_clear(struct nc_pollsession *ps); |
| 207 | |
| 208 | /** |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 209 | * @brief Lock server context. |
| 210 | * |
| 211 | * @param[in] timeout Timeout in milliseconds. 0 for non-blocking call, -1 for |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 212 | * infinite waiting. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 213 | * @param[out] elapsed Elapsed milliseconds will be added to this variable. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 214 | * Can be NULL. |
| 215 | * @return 1 on success, 0 on elapsed timeout, -1 on error. |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 216 | */ |
| 217 | int nc_ctx_lock(int timeout, int *elapsed); |
| 218 | |
| 219 | /** |
| 220 | * @brief Unlock server context. |
| 221 | * |
| 222 | * @return 0 on success, -1 on error. |
| 223 | */ |
| 224 | int nc_ctx_unlock(void); |
| 225 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 226 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
| 227 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 228 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 229 | * @brief Accept new sessions on all the listening endpoints. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 230 | * |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 231 | * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for |
| 232 | * non-blocking call, -1 for infinite waiting. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 233 | * @param[out] session New session. |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 234 | * @return 1 on success, 0 on timeout, -1 on error. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 235 | */ |
| 236 | int nc_accept(int timeout, struct nc_session **session); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 237 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 238 | #endif /* ENABLE_SSH || ENABLE_TLS */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 239 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 240 | #ifdef ENABLE_SSH |
| 241 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 242 | /** |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 243 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 244 | * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns 5. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 245 | * |
| 246 | * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call. |
| 247 | * @param[out] session New session. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 248 | * @return 0 on success, -1 on error. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 249 | */ |
| 250 | int nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session); |
| 251 | |
| 252 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 253 | * @brief Add a new SSH endpoint and start listening on it. |
| 254 | * |
| 255 | * @param[in] name Arbitrary unique endpoint name. There can be a TLS endpoint with |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 256 | * the same name. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 257 | * @param[in] address IP address to listen on. |
| 258 | * @param[in] port Port to listen on. |
| 259 | * @return 0 on success, -1 on error. |
| 260 | */ |
| 261 | int nc_server_ssh_add_endpt_listen(const char *name, const char *address, uint16_t port); |
| 262 | |
| 263 | /** |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 264 | * @brief Change SSH endpoint listening address. |
| 265 | * |
| 266 | * On error the previous listening socket is left untouched. |
| 267 | * |
| 268 | * @param[in] endpt_name Existing endpoint name. |
| 269 | * @param[in] address New listening address. |
| 270 | * @return 0 on success, -1 on error. |
| 271 | */ |
| 272 | int nc_server_ssh_endpt_set_address(const char *endpt_name, const char *address); |
| 273 | |
| 274 | /** |
| 275 | * @brief Change SSH endpoint listening port. |
| 276 | * |
| 277 | * On error the previous listening socket is left untouched. |
| 278 | * |
| 279 | * @param[in] endpt_name Existing endpoint name. |
| 280 | * @param[in] port New listening port. |
| 281 | * @return 0 on success, -1 on error. |
| 282 | */ |
| 283 | int nc_server_ssh_endpt_set_port(const char *endpt_name, uint16_t port); |
| 284 | |
| 285 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 286 | * @brief Stop listening on and remove an SSH endpoint. |
| 287 | * |
| 288 | * @param[in] name Endpoint name. NULL matches all (SSH) endpoints. |
| 289 | * @return 0 on success, -1 on not finding any match. |
| 290 | */ |
| 291 | int nc_server_ssh_del_endpt(const char *name); |
| 292 | |
| 293 | /** |
| 294 | * @brief Set endpoint SSH host keys the server will identify itself with. Each of RSA, DSA, and |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 295 | * ECDSA keys can be set. If the particular type was already set, it is replaced. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 296 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 297 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 298 | * @param[in] privkey_path Path to a private key. |
| 299 | * @return 0 on success, -1 on error. |
| 300 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 301 | int nc_server_ssh_endpt_set_hostkey(const char *endpt_name, const char *privkey_path); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 302 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 303 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 304 | * @brief Set endpoint SSH banner the server will send to every client. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 305 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 306 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 307 | * @param[in] banner SSH banner. |
| 308 | * @return 0 on success, -1 on error. |
| 309 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 310 | 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] | 311 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 312 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 313 | * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive) |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 314 | * are supported by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 315 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 316 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 317 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 318 | * @return 0 on success, -1 on error. |
| 319 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 320 | 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] | 321 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 322 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 323 | * @brief Set endpoint SSH authentication attempts of every client. 3 by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 324 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 325 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 326 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 327 | * @return 0 on success, -1 on error. |
| 328 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 329 | 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] | 330 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 331 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 332 | * @brief Set endpoint SSH authentication timeout. 10 seconds by default. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 333 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 334 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 335 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 336 | * @return 0 on success, -1 on error. |
| 337 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 338 | 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] | 339 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 340 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 341 | * @brief Add an endpoint authorized client SSH public key. This public key can be used for |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 342 | * publickey authentication afterwards. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 343 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 344 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 345 | * @param[in] pubkey_path Path to the public key. |
| 346 | * @param[in] username Username that the client with the public key must use. |
| 347 | * @return 0 on success, -1 on error. |
| 348 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 349 | int nc_server_ssh_endpt_add_authkey(const char *endpt_name, const char *pubkey_path, const char *username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 350 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 351 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 352 | * @brief Remove an endpoint authorized client SSH public key. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 353 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 354 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 355 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 356 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 357 | * @return 0 on success, -1 on not finding any match. |
| 358 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 359 | int nc_server_ssh_endpt_del_authkey(const char *endpt_name, const char *pubkey_path, const char *username); |
| 360 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 361 | #endif /* ENABLE_SSH */ |
| 362 | |
| 363 | #ifdef ENABLE_TLS |
| 364 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 365 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 366 | * @brief Add a new TLS endpoint and start listening on it. |
| 367 | * |
| 368 | * @param[in] name Arbitrary unique endpoint name. There can be an SSH endpoint with |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 369 | * the same name. |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 370 | * @param[in] address IP address to listen on. |
| 371 | * @param[in] port Port to listen on. |
| 372 | * @return 0 on success, -1 on error. |
| 373 | */ |
| 374 | int nc_server_tls_add_endpt_listen(const char *name, const char *address, uint16_t port); |
| 375 | |
| 376 | /** |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 377 | * @brief Change TLS endpoint listening address. |
| 378 | * |
| 379 | * On error the previous listening socket is left untouched. |
| 380 | * |
| 381 | * @param[in] endpt_name Existing endpoint name. |
| 382 | * @param[in] address New listening address. |
| 383 | * @return 0 on success, -1 on error. |
| 384 | */ |
| 385 | int nc_server_tls_endpt_set_address(const char *endpt_name, const char *address); |
| 386 | |
| 387 | /** |
| 388 | * @brief Change TLS endpoint listening port. |
| 389 | * |
| 390 | * On error the previous listening socket is left untouched. |
| 391 | * |
| 392 | * @param[in] endpt_name Existing endpoint name. |
| 393 | * @param[in] port New listening port. |
| 394 | * @return 0 on success, -1 on error. |
| 395 | */ |
| 396 | int nc_server_tls_endpt_set_port(const char *endpt_name, uint16_t port); |
| 397 | |
| 398 | /** |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 399 | * @brief Stop listening on and remove a TLS endpoint. |
| 400 | * |
| 401 | * @param[in] name Endpoint name. NULL matches all (TLS) endpoints. |
| 402 | * @return 0 on success, -1 on not finding any match. |
| 403 | */ |
| 404 | int nc_server_tls_del_endpt(const char *name); |
| 405 | |
| 406 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 407 | * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert_path(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 408 | * There can only be one certificate for each key type, it is replaced if |
| 409 | * already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 410 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 411 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 412 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 413 | * @return 0 on success, -1 on error. |
| 414 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 415 | int nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 416 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 417 | /** |
| 418 | * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert(). |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 419 | * There can only be one certificate for each key type, it is replaced if |
| 420 | * already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 421 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 422 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 423 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 424 | * @return 0 on success, -1 on error. |
| 425 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 426 | int nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 427 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 428 | /** |
| 429 | * @brief Set server TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 430 | * Alternative to nc_tls_server_set_key_path(). There can only be one of |
| 431 | * every key type, it is replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 432 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 433 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 434 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 435 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 436 | * @return 0 on success, -1 on error. |
| 437 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 438 | int nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 439 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 440 | /** |
| 441 | * @brief Set server TLS private key matching the certificate. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 442 | * Alternative to nc_tls_server_set_key_path(). There can only be one of |
| 443 | * every key type, it is replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 444 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 445 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 446 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 447 | * @return 0 on success, -1 on error. |
| 448 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 449 | int nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 450 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 451 | /** |
| 452 | * @brief Add a trusted certificate. Can be both a CA or a client one. |
| 453 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 454 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 455 | * @param[in] cert Base64-enocded certificate in ASN.1 DER encoding. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 456 | * @return 0 on success, -1 on error. |
| 457 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 458 | int nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 459 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 460 | /** |
| 461 | * @brief Add a trusted certificate. Can be both a CA or a client one. |
| 462 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 463 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 464 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 465 | * @return 0 on success, -1 on error. |
| 466 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 467 | int nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 468 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 469 | /** |
| 470 | * @brief Set trusted Certificate Authority certificate locations. There can only be |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 471 | * one file and one directory, they are replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 472 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 473 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 474 | * @param[in] ca_file Path to a trusted CA cert store file in PEM format. Can be NULL. |
| 475 | * @param[in] ca_dir Path to a trusted CA cert store hashed directory (c_rehash utility |
| 476 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 477 | * @return 0 on success, -1 on error. |
| 478 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 479 | 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] | 480 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 481 | /** |
| 482 | * @brief Destroy and clean all the set certificates and private keys. CRLs and |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 483 | * CTN entries are not affected. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 484 | * |
| 485 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 486 | */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 487 | void nc_server_tls_endpt_clear_certs(const char *endpt_name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 488 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 489 | /** |
| 490 | * @brief Set Certificate Revocation List locations. There can only be one file |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 491 | * and one directory, they are replaced if already set. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 492 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 493 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 494 | * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL. |
| 495 | * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility |
| 496 | * can be used to create hashes) with PEM files. Can be NULL. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 497 | * @return 0 on success, -1 on error. |
| 498 | */ |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 499 | 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] | 500 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 501 | /** |
Michal Vasko | 96830e3 | 2016-02-01 10:54:18 +0100 | [diff] [blame] | 502 | * @brief Destroy and clean CRLs. Certificates, private keys, and CTN entries are |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 503 | * not affected. |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 504 | * |
| 505 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 506 | */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 507 | void nc_server_tls_endpt_clear_crls(const char *endpt_name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 508 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 509 | /** |
| 510 | * @brief Add a Cert-to-name entry. |
| 511 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 512 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 513 | * @param[in] id Priority of the entry. |
| 514 | * @param[in] fingerprint Matching certificate fingerprint. |
| 515 | * @param[in] map_type Type of username-certificate mapping. |
| 516 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 517 | * @return 0 on success, -1 on error. |
| 518 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 519 | int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 520 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 521 | /** |
| 522 | * @brief Remove a Cert-to-name entry. |
| 523 | * |
Michal Vasko | da51477 | 2016-02-01 11:32:01 +0100 | [diff] [blame] | 524 | * @param[in] endpt_name Existing endpoint name. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 525 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 526 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 527 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 528 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 529 | * @return 0 on success, -1 on not finding any match. |
| 530 | */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 531 | int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 532 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 533 | #endif /* ENABLE_TLS */ |
| 534 | |
| 535 | #endif /* NC_SESSION_SERVER_H_ */ |