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 | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 29 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 30 | # include <libssh/libssh.h> |
| 31 | # include <libssh/callbacks.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 32 | |
| 33 | /* seconds */ |
| 34 | # define SSH_TIMEOUT 10 |
| 35 | # define SSH_AUTH_COUNT 3 |
| 36 | |
| 37 | typedef enum { |
| 38 | NC_SSH_AUTH_PUBLIC_KEYS = 0x01, |
| 39 | NC_SSH_AUTH_PASSWORD = 0x02, |
| 40 | NC_SSH_AUTH_INTERACTIVE = 0x04 |
| 41 | } NC_SSH_AUTH_TYPE; |
| 42 | |
| 43 | struct nc_ssh_auth_opts { |
| 44 | /* SSH authentication method preferences */ |
| 45 | struct { |
| 46 | NC_SSH_AUTH_TYPE type; |
| 47 | short int value; |
| 48 | } auth_pref[SSH_AUTH_COUNT]; |
| 49 | |
| 50 | /* SSH key pairs */ |
| 51 | struct { |
| 52 | char *pubkey_path; |
| 53 | char *privkey_path; |
| 54 | int privkey_crypt; |
| 55 | } *keys; |
| 56 | int key_count; |
| 57 | }; |
| 58 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | #ifdef ENABLE_TLS |
| 62 | # include <openssl/bio.h> |
| 63 | # include <openssl/ssl.h> |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 64 | |
| 65 | struct nc_tls_auth_opts { |
| 66 | SSL_CTX *tls_ctx; |
| 67 | X509_STORE *tls_store; |
| 68 | }; |
| 69 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | #include <libyang/libyang.h> |
| 73 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 74 | #include "libnetconf.h" |
| 75 | #include "session.h" |
| 76 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK |
| 79 | */ |
| 80 | #define NC_READ_SLEEP 100 |
| 81 | |
| 82 | /** |
| 83 | * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol) |
| 84 | */ |
| 85 | typedef enum { |
| 86 | NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented |
| 87 | outside the current application (only for NETCONF over SSH transport) */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 88 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 89 | NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */ |
| 90 | #endif |
| 91 | #ifdef ENABLE_TLS |
Michal Vasko | b7ea243 | 2015-10-26 15:38:40 +0100 | [diff] [blame] | 92 | NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 93 | #endif |
| 94 | } NC_TRANSPORT_IMPL; |
| 95 | |
| 96 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 97 | * @brief type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 98 | */ |
| 99 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 100 | NC_CLIENT, /**< client side */ |
| 101 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 102 | } NC_SIDE; |
| 103 | |
| 104 | /** |
| 105 | * @brief Enumeration of the supported NETCONF protocol versions |
| 106 | */ |
| 107 | typedef enum { |
| 108 | NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */ |
| 109 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 110 | } NC_VERSION; |
| 111 | |
| 112 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 113 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 114 | |
| 115 | /** |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 116 | * @brief Container to serialize RPC reply objects |
| 117 | */ |
| 118 | struct nc_reply_cont { |
| 119 | struct nc_reply *msg; |
| 120 | struct nc_reply_cont *next; |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * @brief Container to serialize Notification objects |
| 125 | */ |
| 126 | struct nc_notif_cont { |
| 127 | struct nc_notif *msg; |
| 128 | struct nc_notif_cont *next; |
| 129 | }; |
| 130 | |
| 131 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 132 | * @brief NETCONF session structure |
| 133 | */ |
| 134 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 135 | NC_STATUS status; /**< status of the session */ |
| 136 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 137 | |
| 138 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 139 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 140 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 141 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 142 | |
| 143 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 144 | 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] | 145 | 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] | 146 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 147 | union { |
| 148 | struct { |
| 149 | int in; /**< input file descriptor */ |
| 150 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 151 | } fd; /**< NC_TI_FD transport implementation structure */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 152 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 153 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 154 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 155 | ssh_session session; |
| 156 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 157 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 158 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 159 | } libssh; |
| 160 | #endif |
| 161 | #ifdef ENABLE_TLS |
| 162 | SSL *tls; |
| 163 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 164 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 165 | const char *username; |
| 166 | const char *host; |
| 167 | unsigned short port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 168 | |
| 169 | /* other */ |
| 170 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 171 | uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */ |
| 172 | #define NC_SESSION_SHAREDCTX 0x1 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 173 | |
| 174 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 175 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 176 | const char **cpblts; /**< list of server's capabilities on client side */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 177 | struct nc_reply_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 178 | struct nc_notif_cont *notifs; /**< queue for notifications received instead of RPC reply */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 182 | * @brief Fill libyang context in \p session. Context models are based on the stored session |
| 183 | * capabilities. If the server does not support \<get-schema\>, the models are searched |
| 184 | * for in the directory set using nc_schema_searchpath(). |
| 185 | * |
| 186 | * @param[in] session Session to create the context for. |
| 187 | * @return 0 on success, non-zero on failure. |
| 188 | */ |
| 189 | int nc_ctx_fill(struct nc_session *session); |
| 190 | |
| 191 | /** |
| 192 | * @brief Check whether the libyang context in \p session is suitable for NETCONF use |
| 193 | * meaning whether the ietf-netconf model is loaded. |
| 194 | * |
| 195 | * @param[in] session Session with the capabilities to be supported if loading ietf-netconf |
| 196 | * explicitly. |
| 197 | * @return 0 on success, non-zero on failure. |
| 198 | */ |
| 199 | int nc_ctx_check(struct nc_session *session); |
| 200 | |
| 201 | /** |
| 202 | * @brief Create and connect a socket. |
| 203 | * |
| 204 | * @param[in] host Hostname to connect to. |
| 205 | * @param[in] port Port to connect on. |
| 206 | * @return Connected socket or -1 on error. |
| 207 | */ |
| 208 | int nc_connect_getsocket(const char *host, unsigned short port); |
| 209 | |
| 210 | /** |
| 211 | * @brief Perform NETCONF handshake on \p session. |
| 212 | * |
| 213 | * @param[in] session NETCONF session to use. |
| 214 | * @return 0 on success, non-zero on failure. |
| 215 | */ |
| 216 | int nc_handshake(struct nc_session *session); |
| 217 | |
| 218 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 219 | * Functions |
| 220 | * - io.c |
| 221 | */ |
| 222 | |
| 223 | /** |
| 224 | * @brief Read message from the wire. |
| 225 | * |
| 226 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 227 | * libyang XML tree and the message type is detected from the top level element. |
| 228 | * |
| 229 | * @param[in] session NETCONF session from which the message is being read. |
| 230 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 231 | * zero value causes to return immediately. |
| 232 | * @param[out] data XML tree built from the read data. |
| 233 | * @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] | 234 | * (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] | 235 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 236 | */ |
| 237 | NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 238 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 239 | /** |
| 240 | * @brief Write message into wire. |
| 241 | * |
| 242 | * @param[in] session NETCONF session to which the message will be written. |
| 243 | * @param[in] type Type of the message to write. According to the type, the |
| 244 | * specific additional parameters are required or accepted: |
| 245 | * - #NC_MSG_RPC |
| 246 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 247 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
| 248 | * `message-id` attribute is added automatically and default namespace is set to |
| 249 | * #NC_NS_BASE. Optional parameter. |
| 250 | * - #NC_MSG_REPLY |
| 251 | * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter. |
| 252 | * - TODO: content |
| 253 | * - #NC_MSG_NOTIF |
| 254 | * - TODO: content |
| 255 | * @return 0 on success |
| 256 | */ |
| 257 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 258 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 259 | #endif /* NC_SESSION_PRIVATE_H_ */ |