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" |
| 32 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 33 | #ifdef ENABLE_SSH |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 34 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 35 | # include <libssh/libssh.h> |
| 36 | # include <libssh/callbacks.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 37 | |
| 38 | /* seconds */ |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 39 | # define NC_SSH_TIMEOUT 10 |
| 40 | # define NC_SSH_AUTH_COUNT 3 |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 41 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 42 | struct nc_ssh_auth_opts { |
| 43 | /* SSH authentication method preferences */ |
| 44 | struct { |
| 45 | NC_SSH_AUTH_TYPE type; |
| 46 | short int value; |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 47 | } auth_pref[NC_SSH_AUTH_COUNT]; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 48 | |
| 49 | /* SSH key pairs */ |
| 50 | struct { |
| 51 | char *pubkey_path; |
| 52 | char *privkey_path; |
| 53 | int privkey_crypt; |
| 54 | } *keys; |
| 55 | int key_count; |
| 56 | }; |
| 57 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 58 | #endif /* ENABLE_SSH */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 59 | |
| 60 | #ifdef ENABLE_TLS |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 61 | |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 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 | |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 70 | #endif /* ENABLE_TLS */ |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 71 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 72 | /** |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 73 | * 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] | 74 | */ |
| 75 | #define NC_READ_SLEEP 100 |
| 76 | |
| 77 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame^] | 78 | * Number of sockets kept waiting to be accepted. |
| 79 | */ |
| 80 | #define NC_REVERSE_QUEUE 1 |
| 81 | |
| 82 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 83 | * @brief type of the session |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 84 | */ |
| 85 | typedef enum { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 86 | NC_CLIENT, /**< client side */ |
| 87 | NC_SERVER /**< server side */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 88 | } NC_SIDE; |
| 89 | |
| 90 | /** |
| 91 | * @brief Enumeration of the supported NETCONF protocol versions |
| 92 | */ |
| 93 | typedef enum { |
| 94 | NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */ |
| 95 | NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */ |
| 96 | } NC_VERSION; |
| 97 | |
| 98 | #define NC_VERSION_10_ENDTAG "]]>]]>" |
| 99 | #define NC_VERSION_10_ENDTAG_LEN 6 |
| 100 | |
| 101 | /** |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 102 | * @brief Container to serialize PRC messages |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 103 | */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 104 | struct nc_msg_cont { |
| 105 | struct lyxml_elem *msg; |
| 106 | struct nc_msg_cont *next; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 110 | * @brief NETCONF session structure |
| 111 | */ |
| 112 | struct nc_session { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 113 | NC_STATUS status; /**< status of the session */ |
| 114 | NC_SIDE side; /**< side of the session: client or server */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 115 | |
| 116 | /* NETCONF data */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 117 | uint32_t id; /**< NETCONF session ID (session-id-type) */ |
| 118 | NC_VERSION version; /**< NETCONF protocol version */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 119 | pthread_t *notif; /**< running notifications thread - TODO server-side only? */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 120 | |
| 121 | /* Transport implementation */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 122 | 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] | 123 | 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] | 124 | NETCONF sessions on the same SSH session (but different SSH channel) */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 125 | union { |
| 126 | struct { |
| 127 | int in; /**< input file descriptor */ |
| 128 | int out; /**< output file descriptor */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 129 | } fd; /**< NC_TI_FD transport implementation structure */ |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 130 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 131 | struct { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 132 | ssh_channel channel; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 133 | ssh_session session; |
| 134 | struct nc_session *next; /**< pointer to the next NETCONF session on the same |
| 135 | SSH session, but different SSH channel. If no such session exists, it is NULL. |
| 136 | otherwise there is a ring list of the NETCONF sessions */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 137 | } libssh; |
| 138 | #endif |
| 139 | #ifdef ENABLE_TLS |
| 140 | SSL *tls; |
| 141 | #endif |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 142 | } ti; /**< transport implementation data */ |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 143 | const char *username; |
| 144 | const char *host; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 145 | uint16_t port; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 146 | |
| 147 | /* other */ |
| 148 | struct ly_ctx *ctx; /**< libyang context of the session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 149 | uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */ |
| 150 | #define NC_SESSION_SHAREDCTX 0x1 |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 151 | |
| 152 | /* client side only data */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 153 | uint64_t msgid; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 154 | const char **cpblts; /**< list of server's capabilities on client side */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 155 | struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */ |
| 156 | struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 160 | * @brief Fill libyang context in \p session. Context models are based on the stored session |
| 161 | * capabilities. If the server does not support \<get-schema\>, the models are searched |
| 162 | * for in the directory set using nc_schema_searchpath(). |
| 163 | * |
| 164 | * @param[in] session Session to create the context for. |
| 165 | * @return 0 on success, non-zero on failure. |
| 166 | */ |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 167 | int nc_ctx_check_and_fill(struct nc_session *session); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * @brief Create and connect a socket. |
| 171 | * |
| 172 | * @param[in] host Hostname to connect to. |
| 173 | * @param[in] port Port to connect on. |
| 174 | * @return Connected socket or -1 on error. |
| 175 | */ |
| 176 | int nc_connect_getsocket(const char *host, unsigned short port); |
| 177 | |
| 178 | /** |
| 179 | * @brief Perform NETCONF handshake on \p session. |
| 180 | * |
| 181 | * @param[in] session NETCONF session to use. |
| 182 | * @return 0 on success, non-zero on failure. |
| 183 | */ |
| 184 | int nc_handshake(struct nc_session *session); |
| 185 | |
| 186 | /** |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame^] | 187 | * @brief Accept a new (Call Home) connection. |
| 188 | * |
| 189 | * @param[in] port Port to listen on. |
| 190 | * @param[in] timeout Timeout in msec. |
| 191 | * @param[out] server_port Port the new connection is connected on. Can be NULL. |
| 192 | * @param[out] server_host Host the new connection was initiated from. Can be NULL. |
| 193 | * @return Connected socket with the new connection, -1 on error. |
| 194 | */ |
| 195 | int nc_callhome_accept_connection(uint16_t port, int32_t timeout, uint16_t *server_port, char **server_host); |
| 196 | |
| 197 | /** |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 198 | * Functions |
| 199 | * - io.c |
| 200 | */ |
| 201 | |
| 202 | /** |
| 203 | * @brief Read message from the wire. |
| 204 | * |
| 205 | * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into |
| 206 | * libyang XML tree and the message type is detected from the top level element. |
| 207 | * |
| 208 | * @param[in] session NETCONF session from which the message is being read. |
| 209 | * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout, |
| 210 | * zero value causes to return immediately. |
| 211 | * @param[out] data XML tree built from the read data. |
| 212 | * @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] | 213 | * (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] | 214 | * returned on error and #NC_MSG_NONE is never returned by this function. |
| 215 | */ |
| 216 | NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data); |
| 217 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 218 | /** |
| 219 | * @brief Write message into wire. |
| 220 | * |
| 221 | * @param[in] session NETCONF session to which the message will be written. |
| 222 | * @param[in] type Type of the message to write. According to the type, the |
| 223 | * specific additional parameters are required or accepted: |
| 224 | * - #NC_MSG_RPC |
| 225 | * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter. |
| 226 | * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. |
| 227 | * `message-id` attribute is added automatically and default namespace is set to |
| 228 | * #NC_NS_BASE. Optional parameter. |
| 229 | * - #NC_MSG_REPLY |
| 230 | * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter. |
| 231 | * - TODO: content |
| 232 | * - #NC_MSG_NOTIF |
| 233 | * - TODO: content |
| 234 | * @return 0 on success |
| 235 | */ |
| 236 | int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...); |
| 237 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 238 | #endif /* NC_SESSION_PRIVATE_H_ */ |