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 | /** |
| 45 | * @brief Set the termination reason for a session. |
| 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 |
| 127 | * because of inactivity. |
| 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. |
| 170 | * @return 0 on success, 1 if not found, -1 on error. |
| 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 |
| 183 | * infinite waiting. |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 184 | * @return 0 on elapsed timeout, |
| 185 | * 1 if an RPC was processed, |
| 186 | * 2 if an RPC was processed and there are unhandled events on other sessions, |
| 187 | * 3 if a session from \p ps changed its status (was invalidated), |
| 188 | * -1 on error. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 189 | * |
| 190 | * Only with SSH support: |
| 191 | * 4 if an SSH message was processed, |
| 192 | * 5 if a new NETCONF SSH channel was created; call nc_ssh_ps_accept_channel() |
| 193 | * to establish a new NETCONF session. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 194 | */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 195 | int nc_ps_poll(struct nc_pollsession *ps, int timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 196 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 197 | /** |
| 198 | * @brief Lock server context. |
| 199 | * |
| 200 | * @param[in] timeout Timeout in milliseconds. 0 for non-blocking call, -1 for |
| 201 | * infinite waiting. |
| 202 | * @param[out] elapsed Elapsed milliseconds will be added to this variable. |
| 203 | * Can be NULL. |
| 204 | * @return 0 on elapsed timeout, 1 on success, -1 on error. |
| 205 | */ |
| 206 | int nc_ctx_lock(int timeout, int *elapsed); |
| 207 | |
| 208 | /** |
| 209 | * @brief Unlock server context. |
| 210 | * |
| 211 | * @return 0 on success, -1 on error. |
| 212 | */ |
| 213 | int nc_ctx_unlock(void); |
| 214 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 215 | #if defined(ENABLE_SSH) || defined(ENABLE_TLS) |
| 216 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 217 | /** |
| 218 | * @brief Add a new server bind and start listening on it. |
| 219 | * |
| 220 | * @param[in] address IP address to bind to. |
| 221 | * @param[in] port Port to bind to. |
| 222 | * @param[in] ti Expected transport protocol of incoming connections. |
| 223 | * @return 0 on success, -1 on error. |
| 224 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 225 | int nc_server_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti); |
| 226 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 227 | /** |
| 228 | * @brief Stop listening on and remove a server bind. |
| 229 | * |
| 230 | * @param[in] address IP address the bind was bound to. NULL matches all the addresses. |
| 231 | * @param[in] port Port the bind was bound to. NULL matches all the ports. |
| 232 | * @param[in] ti Expected transport. 0 matches all. |
| 233 | * @return 0 on success, -1 on not finding any match. |
| 234 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 235 | int nc_server_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti); |
| 236 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 237 | /** |
| 238 | * @brief Accept new sessions on all the listening binds. |
| 239 | * |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 240 | * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for |
| 241 | * non-blocking call, -1 for infinite waiting. |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 242 | * @param[out] session New session. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 243 | * @return 1 on success, 0 on timeout, -1 or error. |
| 244 | */ |
| 245 | int nc_accept(int timeout, struct nc_session **session); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 246 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 247 | int nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session); |
| 248 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 249 | #endif /* ENABLE_SSH || ENABLE_TLS */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 250 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 251 | #ifdef ENABLE_SSH |
| 252 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 253 | /** |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 254 | * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session |
| 255 | * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns 5. |
| 256 | * |
| 257 | * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call. |
| 258 | * @param[out] session New session. |
| 259 | * @return 1 on success, -1 on error. |
| 260 | */ |
| 261 | int nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session); |
| 262 | |
| 263 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 264 | * @brief Set SSH host keys the server will identify itself with. Each of RSA, DSA, and |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 265 | * 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] | 266 | * |
| 267 | * @param[in] privkey_path Path to a private key. |
| 268 | * @return 0 on success, -1 on error. |
| 269 | */ |
| 270 | int nc_ssh_server_set_hostkey(const char *privkey_path); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 271 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 272 | /** |
| 273 | * @brief Set SSH banner the server will send to every client. |
| 274 | * |
| 275 | * @param[in] banner SSH banner. |
| 276 | * @return 0 on success, -1 on error. |
| 277 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 278 | int nc_ssh_server_set_banner(const char *banner); |
| 279 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 280 | /** |
| 281 | * @brief Set accepted SSH authentication methods. All (publickey, password, interactive) |
| 282 | * are supported by default. |
| 283 | * |
| 284 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 285 | * @return 0 on success, -1 on error. |
| 286 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 287 | int nc_ssh_server_set_auth_methods(int auth_methods); |
| 288 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 289 | /** |
| 290 | * @brief Set SSH authentication attempts of every client. 3 by default. |
| 291 | * |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 292 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 293 | * @return 0 on success, -1 on error. |
| 294 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 295 | int nc_ssh_server_set_auth_attempts(uint16_t auth_attempts); |
| 296 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 297 | /** |
| 298 | * @brief Set SSH authentication timeout. 10 seconds by default. |
| 299 | * |
| 300 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 301 | * @return 0 on success, -1 on error. |
| 302 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 303 | int nc_ssh_server_set_auth_timeout(uint16_t auth_timeout); |
| 304 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 305 | /** |
| 306 | * @brief Add an authorized client SSH public key. This public key can be used for |
| 307 | * publickey authentication afterwards. |
| 308 | * |
| 309 | * @param[in] pubkey_path Path to the public key. |
| 310 | * @param[in] username Username that the client with the public key must use. |
| 311 | * @return 0 on success, -1 on error. |
| 312 | */ |
| 313 | int nc_ssh_server_add_authkey(const char *pubkey_path, const char *username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 314 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 315 | /** |
| 316 | * @brief Remove an authorized client SSH public key. |
| 317 | * |
| 318 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 319 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 320 | * @return 0 on success, -1 on not finding any match. |
| 321 | */ |
| 322 | int nc_ssh_server_del_authkey(const char *pubkey_path, const char *username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 323 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 324 | /* |
| 325 | * Call Home |
| 326 | */ |
| 327 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 328 | /** |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 329 | * @brief Set Call Home SSH host keys the server will identify itself with. Each of RSA, DSA, and |
| 330 | * ECDSA keys can be set. If the particular type was already set, it is replaced. |
| 331 | * |
| 332 | * @param[in] privkey_path Path to a private key. |
| 333 | * @return 0 on success, -1 on error. |
| 334 | */ |
| 335 | int nc_ssh_server_ch_set_hostkey(const char *privkey_path); |
| 336 | |
| 337 | /** |
| 338 | * @brief Set Call Home SSH banner the server will send to every client. |
| 339 | * |
| 340 | * @param[in] banner SSH banner. |
| 341 | * @return 0 on success, -1 on error. |
| 342 | */ |
| 343 | int nc_ssh_server_ch_set_banner(const char *banner); |
| 344 | |
| 345 | /** |
| 346 | * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive) |
| 347 | * are supported by default. |
| 348 | * |
| 349 | * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE. |
| 350 | * @return 0 on success, -1 on error. |
| 351 | */ |
| 352 | int nc_ssh_server_ch_set_auth_methods(int auth_methods); |
| 353 | |
| 354 | /** |
| 355 | * @brief Set Call Home SSH authentication attempts of every client. 3 by default. |
| 356 | * |
| 357 | * @param[in] auth_attempts Failed authentication attempts before a client is dropped. |
| 358 | * @return 0 on success, -1 on error. |
| 359 | */ |
| 360 | int nc_ssh_server_ch_set_auth_attempts(uint16_t auth_attempts); |
| 361 | |
| 362 | /** |
| 363 | * @brief Set Call Home SSH authentication timeout. 10 seconds by default. |
| 364 | * |
| 365 | * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped. |
| 366 | * @return 0 on success, -1 on error. |
| 367 | */ |
| 368 | int nc_ssh_server_ch_set_auth_timeout(uint16_t auth_timeout); |
| 369 | |
| 370 | /** |
| 371 | * @brief Add an authorized Call Home client SSH public key. This public key can be used for |
| 372 | * publickey authentication afterwards. |
| 373 | * |
| 374 | * @param[in] pubkey_path Path to the public key. |
| 375 | * @param[in] username Username that the client with the public key must use. |
| 376 | * @return 0 on success, -1 on error. |
| 377 | */ |
| 378 | int nc_ssh_server_ch_add_authkey(const char *pubkey_path, const char *username); |
| 379 | |
| 380 | /** |
| 381 | * @brief Remove an authorized Call Home client SSH public key. |
| 382 | * |
| 383 | * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys. |
| 384 | * @param[in] username Username for an authorized public key. NULL matches all the usernames. |
| 385 | * @return 0 on success, -1 on not finding any match. |
| 386 | */ |
| 387 | int nc_ssh_server_ch_del_authkey(const char *pubkey_path, const char *username); |
| 388 | |
| 389 | /** |
| 390 | * @brief Free all the various SSH server options (including Call Home ones). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 391 | */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 392 | void nc_ssh_server_free_opts(void); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 393 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 394 | #endif /* ENABLE_SSH */ |
| 395 | |
| 396 | #ifdef ENABLE_TLS |
| 397 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 398 | /** |
| 399 | * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert_path(). |
| 400 | * There can only be one certificate for each key type, it is replaced if already set. |
| 401 | * |
| 402 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 403 | * @return 0 on success, -1 on error. |
| 404 | */ |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 405 | int nc_tls_server_set_cert(const char *cert); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 406 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 407 | /** |
| 408 | * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert(). |
| 409 | * There can only be one certificate for each key type, it is replaced if already set. |
| 410 | * |
| 411 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 412 | * @return 0 on success, -1 on error. |
| 413 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 414 | int nc_tls_server_set_cert_path(const char *cert_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 415 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 416 | /** |
| 417 | * @brief Set server TLS private key matching the certificate. |
| 418 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 419 | * type, it is replaced if already set. |
| 420 | * |
| 421 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 422 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 423 | * @return 0 on success, -1 on error. |
| 424 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 425 | int nc_tls_server_set_key(const char *privkey, int is_rsa); |
| 426 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 427 | /** |
| 428 | * @brief Set server TLS private key matching the certificate. |
| 429 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 430 | * type, it is replaced if already set. |
| 431 | * |
| 432 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 433 | * @return 0 on success, -1 on error. |
| 434 | */ |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 435 | int nc_tls_server_set_key_path(const char *privkey_path); |
| 436 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 437 | /** |
| 438 | * @brief Add a trusted certificate. Can be both a CA or a client one. |
| 439 | * |
| 440 | * @param[in] cert Base64-enocded certificate in ASN.1DER encoding. |
| 441 | * @return 0 on success, -1 on error. |
| 442 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 443 | int nc_tls_server_add_trusted_cert(const char *cert); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 444 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 445 | /** |
| 446 | * @brief Add a trusted certificate. Can be both a CA or a client one. |
| 447 | * |
| 448 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 449 | * @return 0 on success, -1 on error. |
| 450 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 451 | int nc_tls_server_add_trusted_cert_path(const char *cert_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 452 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 453 | /** |
| 454 | * @brief Set trusted Certificate Authority certificate locations. There can only be |
| 455 | * one file and one directory, they are replaced if already set. |
| 456 | * |
| 457 | * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format. |
| 458 | * Can be NULL. |
| 459 | * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory |
| 460 | * (c_rehash utility can be used to create hashes) with PEM files. Can be NULL. |
| 461 | * @return 0 on success, -1 on error. |
| 462 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 463 | int nc_tls_server_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 464 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 465 | /** |
| 466 | * @brief Destroy and clean all the set certificates and private keys. CRLs and |
| 467 | * CTN entries are not affected. |
| 468 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 469 | void nc_tls_server_destroy_certs(void); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 470 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 471 | /** |
| 472 | * @brief Set Certificate Revocation List locations. There can only be one file |
| 473 | * and one directory, they are replaced if already set. |
| 474 | * |
| 475 | * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL. |
| 476 | * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility |
| 477 | * can be used to create hashes) with PEM files. Can be NULL. |
| 478 | * @return 0 on success, -1 on error. |
| 479 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 480 | int nc_tls_server_set_crl_locations(const char *crl_file_path, const char *crl_dir_path); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 481 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 482 | /** |
| 483 | * @brief Destroy and clean CRLs. Certificates, priavte keys, and CTN entries are |
| 484 | * not affected. |
| 485 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 486 | void nc_tls_server_destroy_crls(void); |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 487 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 488 | /** |
| 489 | * @brief Add a Cert-to-name entry. |
| 490 | * |
| 491 | * @param[in] id Priority of the entry. |
| 492 | * @param[in] fingerprint Matching certificate fingerprint. |
| 493 | * @param[in] map_type Type of username-certificate mapping. |
| 494 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 495 | * @return 0 on success, -1 on error. |
| 496 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 497 | int nc_tls_server_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 498 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 499 | /** |
| 500 | * @brief Remove a Cert-to-name entry. |
| 501 | * |
| 502 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 503 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 504 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 505 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 506 | * @return 0 on success, -1 on not finding any match. |
| 507 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 508 | int nc_tls_server_del_ctn(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] | 509 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 510 | /* |
| 511 | * Call Home |
| 512 | */ |
| 513 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 514 | /** |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame^] | 515 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path(). |
| 516 | * There can only be one certificate for each key type, it is replaced if already set. |
| 517 | * |
| 518 | * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding. |
| 519 | * @return 0 on success, -1 on error. |
| 520 | */ |
| 521 | int nc_tls_server_ch_set_cert(const char *cert); |
| 522 | |
| 523 | /** |
| 524 | * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert(). |
| 525 | * There can only be one certificate for each key type, it is replaced if already set. |
| 526 | * |
| 527 | * @param[in] cert_path Path to a certificate file in PEM format. |
| 528 | * @return 0 on success, -1 on error. |
| 529 | */ |
| 530 | int nc_tls_server_ch_set_cert_path(const char *cert_path); |
| 531 | |
| 532 | /** |
| 533 | * @brief Set server Call Home TLS private key matching the certificate. |
| 534 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 535 | * type, it is replaced if already set. |
| 536 | * |
| 537 | * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding. |
| 538 | * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key. |
| 539 | * @return 0 on success, -1 on error. |
| 540 | */ |
| 541 | int nc_tls_server_ch_set_key(const char *privkey, int is_rsa); |
| 542 | |
| 543 | /** |
| 544 | * @brief Set server Call Home TLS private key matching the certificate. |
| 545 | * Alternative to nc_tls_server_set_key_path(). There can only be one of every key |
| 546 | * type, it is replaced if already set. |
| 547 | * |
| 548 | * @param[in] privkey_path Path to a private key file in PEM format. |
| 549 | * @return 0 on success, -1 on error. |
| 550 | */ |
| 551 | int nc_tls_server_ch_set_key_path(const char *privkey_path); |
| 552 | |
| 553 | /** |
| 554 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 555 | * |
| 556 | * @param[in] cert Base64-enocded certificate in ASN.1DER encoding. |
| 557 | * @return 0 on success, -1 on error. |
| 558 | */ |
| 559 | int nc_tls_server_ch_add_trusted_cert(const char *cert); |
| 560 | |
| 561 | /** |
| 562 | * @brief Add a Call Home trusted certificate. Can be both a CA or a client one. |
| 563 | * |
| 564 | * @param[in] cert_path Path to a trusted certificate file in PEM format. |
| 565 | * @return 0 on success, -1 on error. |
| 566 | */ |
| 567 | int nc_tls_server_ch_add_trusted_cert_path(const char *cert_path); |
| 568 | |
| 569 | /** |
| 570 | * @brief Set trusted Call Home Certificate Authority certificate locations. There |
| 571 | * can only be one file and one directory, they are replaced if already set. |
| 572 | * |
| 573 | * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format. |
| 574 | * Can be NULL. |
| 575 | * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory |
| 576 | * (c_rehash utility can be used to create hashes) with PEM files. Can be NULL. |
| 577 | * @return 0 on success, -1 on error. |
| 578 | */ |
| 579 | int nc_tls_server_ch_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path); |
| 580 | |
| 581 | /** |
| 582 | * @brief Destroy and clean all the set Call Home certificates and private keys. |
| 583 | * CRLs and CTN entries are not affected. |
| 584 | */ |
| 585 | void nc_tls_server_ch_destroy_certs(void); |
| 586 | |
| 587 | /** |
| 588 | * @brief Set Call Home Certificate Revocation List locations. There can only be |
| 589 | * one file and one directory, they are replaced if already set. |
| 590 | * |
| 591 | * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL. |
| 592 | * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility |
| 593 | * can be used to create hashes) with PEM files. Can be NULL. |
| 594 | * @return 0 on success, -1 on error. |
| 595 | */ |
| 596 | int nc_tls_server_ch_set_crl_locations(const char *crl_file_path, const char *crl_dir_path); |
| 597 | |
| 598 | /** |
| 599 | * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys, |
| 600 | * and CTN entries are not affected. |
| 601 | */ |
| 602 | void nc_tls_server_ch_destroy_crls(void); |
| 603 | |
| 604 | /** |
| 605 | * @brief Add a Call Home Cert-to-name entry. |
| 606 | * |
| 607 | * @param[in] id Priority of the entry. |
| 608 | * @param[in] fingerprint Matching certificate fingerprint. |
| 609 | * @param[in] map_type Type of username-certificate mapping. |
| 610 | * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise. |
| 611 | * @return 0 on success, -1 on error. |
| 612 | */ |
| 613 | int nc_tls_server_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 614 | |
| 615 | /** |
| 616 | * @brief Remove a Call Home Cert-to-name entry. |
| 617 | * |
| 618 | * @param[in] id Priority of the entry. -1 matches all the priorities. |
| 619 | * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints. |
| 620 | * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types. |
| 621 | * @param[in] name Specific username for the entry. NULL matches all the usernames. |
| 622 | * @return 0 on success, -1 on not finding any match. |
| 623 | */ |
| 624 | int nc_tls_server_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name); |
| 625 | |
| 626 | /** |
| 627 | * @brief Free all the various TLS server options (including Call Home ones). |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 628 | */ |
Michal Vasko | 0457bb4 | 2016-01-08 15:49:13 +0100 | [diff] [blame] | 629 | void nc_tls_server_free_opts(void); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 630 | |
| 631 | #endif /* ENABLE_TLS */ |
| 632 | |
| 633 | #endif /* NC_SESSION_SERVER_H_ */ |