Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file session_p.h |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 session 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_PRIVATE_H_ |
| 24 | #define NC_SESSION_PRIVATE_H_ |
| 25 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 27 | #include <pthread.h> |
| 28 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 29 | #include <libyang/libyang.h> |
| 30 | #include "libnetconf.h" |
| 31 | #include "session.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 32 | #include "messages.h" |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 33 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 34 | #ifdef ENABLE_SSH |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 35 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 36 | # include <libssh/libssh.h> |
| 37 | # include <libssh/callbacks.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 38 | # include <libssh/server.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 39 | |
| 40 | /* seconds */ |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 41 | # define NC_SSH_TIMEOUT 10 |
| 42 | # define NC_SSH_AUTH_COUNT 3 |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 43 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 44 | struct nc_ssh_client_opts { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 45 | /* SSH authentication method preferences */ |
| 46 | struct { |
| 47 | NC_SSH_AUTH_TYPE type; |
| 48 | short int value; |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 49 | } auth_pref[NC_SSH_AUTH_COUNT]; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 50 | |
| 51 | /* SSH key pairs */ |
| 52 | struct { |
| 53 | char *pubkey_path; |
| 54 | char *privkey_path; |
| 55 | int privkey_crypt; |
| 56 | } *keys; |
| 57 | int key_count; |
| 58 | }; |
| 59 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 60 | struct nc_server_opts { |
| 61 | struct ly_ctx *ctx; |
| 62 | |
| 63 | NC_WD_MODE wd_basic_mode; |
| 64 | int wd_also_supported; |
| 65 | int interleave_capab; |
| 66 | |
| 67 | uint16_t hello_timeout; |
| 68 | uint16_t idle_timeout; |
| 69 | uint16_t max_sessions; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 70 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 71 | struct nc_bind { |
| 72 | char *address; |
| 73 | uint16_t port; |
| 74 | int sock; |
| 75 | NC_TRANSPORT_IMPL ti; |
| 76 | } *binds; |
| 77 | uint16_t bind_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct nc_ssh_server_opts { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 81 | ssh_bind sshbind; |
| 82 | |
| 83 | struct { |
| 84 | char *path; |
| 85 | char *username; |
| 86 | } *authkeys; |
| 87 | uint16_t authkey_count; |
| 88 | |
| 89 | int auth_methods; |
| 90 | uint16_t auth_attempts; |
| 91 | uint16_t auth_timeout; |
| 92 | }; |
| 93 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 94 | #endif /* ENABLE_SSH */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 95 | |
| 96 | #ifdef ENABLE_TLS |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 97 | |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 98 | # include <openssl/bio.h> |
| 99 | # include <openssl/ssl.h> |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 100 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 101 | struct nc_tls_client_opts { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 102 | SSL_CTX *tls_ctx; |
| 103 | X509_STORE *tls_store; |
| 104 | }; |
| 105 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 106 | #endif /* ENABLE_TLS */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 107 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 108 | /** |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 109 | * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK. |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 110 | */ |
| 111 | #define NC_READ_SLEEP 100 |
| 112 | |
| 113 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 114 | * Number of sockets kept waiting to be accepted. |
| 115 | */ |
| 116 | #define NC_REVERSE_QUEUE 1 |
| 117 | |
| 118 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 119 | * @brief type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 120 | */ |
| 121 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 122 | NC_CLIENT, /**< client side */ |
| 123 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 124 | } NC_SIDE; |
| 125 | |
| 126 | /** |
| 127 | * @brief Enumeration of the supported NETCONF protocol versions |
| 128 | */ |
| 129 | typedef enum { |
| 130 | NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */ |
| 131 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 132 | } NC_VERSION; |
| 133 | |
| 134 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 135 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 136 | |
| 137 | /** |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 138 | * @brief Container to serialize PRC messages |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 139 | */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 140 | struct nc_msg_cont { |
| 141 | struct lyxml_elem *msg; |
| 142 | struct nc_msg_cont *next; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 146 | * @brief NETCONF session structure |
| 147 | */ |
| 148 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 149 | NC_STATUS status; /**< status of the session */ |
| 150 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 151 | |
| 152 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 153 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 154 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 155 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 156 | |
| 157 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 158 | NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 159 | pthread_mutex_t *ti_lock; /**< lock to access ti. Note that in case of libssh TI, it can be shared with other |
Michal Vasko | b7ea243 | 2015-10-26 15:38:40 +0100 | [diff] [blame] | 160 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 161 | union { |
| 162 | struct { |
| 163 | int in; /**< input file descriptor */ |
| 164 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 165 | } fd; /**< NC_TI_FD transport implementation structure */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 166 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 167 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 168 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 169 | ssh_session session; |
| 170 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 171 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 172 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 173 | } libssh; |
| 174 | #endif |
| 175 | #ifdef ENABLE_TLS |
| 176 | SSL *tls; |
| 177 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 178 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 179 | const char *username; |
| 180 | const char *host; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 181 | uint16_t port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 182 | |
| 183 | /* other */ |
| 184 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 185 | uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 186 | #define NC_SESSION_SHAREDCTX 0x01 |
| 187 | #define NC_SESSION_SSH_AUTHENTICATED 0x02 |
| 188 | #define NC_SESSION_SSH_SUBSYS_NETCONF 0x04 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 189 | |
| 190 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 191 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 192 | const char **cpblts; /**< list of server's capabilities on client side */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 193 | struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 194 | struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 195 | |
| 196 | /* server side only data */ |
| 197 | uint16_t auth_attempts; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame^] | 200 | struct nc_pollsession { |
| 201 | struct { |
| 202 | int fd; |
| 203 | short events; |
| 204 | short revents; |
| 205 | struct nc_session *session; |
| 206 | } *sessions; |
| 207 | uint16_t session_count; |
| 208 | }; |
| 209 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 210 | NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op); |
| 211 | |
| 212 | int session_ti_lock(struct nc_session *session, int32_t timeout); |
| 213 | |
| 214 | int session_ti_unlock(struct nc_session *session); |
| 215 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 216 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 217 | * @brief Fill libyang context in \p session. Context models are based on the stored session |
| 218 | * capabilities. If the server does not support \<get-schema\>, the models are searched |
| 219 | * for in the directory set using nc_schema_searchpath(). |
| 220 | * |
| 221 | * @param[in] session Session to create the context for. |
| 222 | * @return 0 on success, non-zero on failure. |
| 223 | */ |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 224 | int nc_ctx_check_and_fill(struct nc_session *session); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * @brief Create and connect a socket. |
| 228 | * |
| 229 | * @param[in] host Hostname to connect to. |
| 230 | * @param[in] port Port to connect on. |
| 231 | * @return Connected socket or -1 on error. |
| 232 | */ |
| 233 | int nc_connect_getsocket(const char *host, unsigned short port); |
| 234 | |
| 235 | /** |
| 236 | * @brief Perform NETCONF handshake on \p session. |
| 237 | * |
| 238 | * @param[in] session NETCONF session to use. |
| 239 | * @return 0 on success, non-zero on failure. |
| 240 | */ |
| 241 | int nc_handshake(struct nc_session *session); |
| 242 | |
| 243 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 244 | * @brief Accept a new (Call Home) connection. |
| 245 | * |
| 246 | * @param[in] port Port to listen on. |
| 247 | * @param[in] timeout Timeout in msec. |
| 248 | * @param[out] server_port Port the new connection is connected on. Can be NULL. |
| 249 | * @param[out] server_host Host the new connection was initiated from. Can be NULL. |
| 250 | * @return Connected socket with the new connection, -1 on error. |
| 251 | */ |
| 252 | int nc_callhome_accept_connection(uint16_t port, int32_t timeout, uint16_t *server_port, char **server_host); |
| 253 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame^] | 254 | int nc_sock_listen(const char *address, uint32_t port); |
| 255 | |
| 256 | int nc_sock_accept(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port); |
| 257 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 258 | #ifdef ENABLE_SSH |
| 259 | |
| 260 | int nc_accept_ssh_session(struct nc_session *session, int sock, int timeout); |
| 261 | |
| 262 | #endif |
| 263 | |
| 264 | #ifdef ENABLE_TLS |
| 265 | |
| 266 | int nc_accept_tls_session(struct nc_session *session, int sock, int timeout); |
| 267 | |
| 268 | #endif |
| 269 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 270 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 271 | * Functions |
| 272 | * - io.c |
| 273 | */ |
| 274 | |
| 275 | /** |
| 276 | * @brief Read message from the wire. |
| 277 | * |
| 278 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 279 | * libyang XML tree and the message type is detected from the top level element. |
| 280 | * |
| 281 | * @param[in] session NETCONF session from which the message is being read. |
| 282 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 283 | * zero value causes to return immediately. |
| 284 | * @param[out] data XML tree built from the read data. |
| 285 | * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 286 | * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 287 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 288 | */ |
| 289 | NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 290 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 291 | /** |
| 292 | * @brief Write message into wire. |
| 293 | * |
| 294 | * @param[in] session NETCONF session to which the message will be written. |
| 295 | * @param[in] type Type of the message to write. According to the type, the |
| 296 | * specific additional parameters are required or accepted: |
| 297 | * - #NC_MSG_RPC |
| 298 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 299 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
| 300 | * `message-id` attribute is added automatically and default namespace is set to |
| 301 | * #NC_NS_BASE. Optional parameter. |
| 302 | * - #NC_MSG_REPLY |
| 303 | * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter. |
| 304 | * - TODO: content |
| 305 | * - #NC_MSG_NOTIF |
| 306 | * - TODO: content |
| 307 | * @return 0 on success |
| 308 | */ |
| 309 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 310 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 311 | #endif /* NC_SESSION_PRIVATE_H_ */ |