blob: cb6791a6428cd1298b73fd2adc7456ce18a926bd [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
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 Krejcife0b3472015-10-12 13:43:42 +020026#include <stdint.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020027#include <pthread.h>
28
Michal Vasko4ef14932015-12-04 11:09:10 +010029#include <libyang/libyang.h>
30#include "libnetconf.h"
31#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "messages.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010033
Michal Vaskofb2fb762015-10-27 11:44:32 +010034#ifdef ENABLE_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010035
Radek Krejci206fcd62015-10-07 15:42:48 +020036# include <libssh/libssh.h>
37# include <libssh/callbacks.h>
Michal Vasko086311b2016-01-08 09:53:11 +010038# include <libssh/server.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010039
40/* seconds */
Michal Vaskoc111ac52015-12-08 14:36:36 +010041# define NC_SSH_TIMEOUT 10
42# define NC_SSH_AUTH_COUNT 3
Michal Vasko7b62fed2015-10-26 15:39:46 +010043
Michal Vasko086311b2016-01-08 09:53:11 +010044struct nc_ssh_client_opts {
Michal Vasko7b62fed2015-10-26 15:39:46 +010045 /* SSH authentication method preferences */
46 struct {
47 NC_SSH_AUTH_TYPE type;
48 short int value;
Michal Vaskoc111ac52015-12-08 14:36:36 +010049 } auth_pref[NC_SSH_AUTH_COUNT];
Michal Vasko7b62fed2015-10-26 15:39:46 +010050
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 Vasko086311b2016-01-08 09:53:11 +010060struct 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;
70};
71
72struct nc_bind {
73 char *address;
74 uint16_t port;
75 int sock;
76};
77
78struct nc_ssh_server_opts {
79 struct nc_bind *binds;
80 uint16_t bind_count;
81
82 ssh_bind sshbind;
83
84 struct {
85 char *path;
86 char *username;
87 } *authkeys;
88 uint16_t authkey_count;
89
90 int auth_methods;
91 uint16_t auth_attempts;
92 uint16_t auth_timeout;
93};
94
Michal Vasko4ef14932015-12-04 11:09:10 +010095#endif /* ENABLE_SSH */
Radek Krejci206fcd62015-10-07 15:42:48 +020096
97#ifdef ENABLE_TLS
Michal Vasko4ef14932015-12-04 11:09:10 +010098
Michal Vaskoc111ac52015-12-08 14:36:36 +010099# include <openssl/bio.h>
100# include <openssl/ssl.h>
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100101
Michal Vasko086311b2016-01-08 09:53:11 +0100102struct nc_tls_client_opts {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100103 SSL_CTX *tls_ctx;
104 X509_STORE *tls_store;
105};
106
Michal Vasko4ef14932015-12-04 11:09:10 +0100107#endif /* ENABLE_TLS */
Radek Krejci43390242015-10-08 15:34:04 +0200108
Radek Krejci206fcd62015-10-07 15:42:48 +0200109/**
Michal Vaskoc111ac52015-12-08 14:36:36 +0100110 * Sleep time in microseconds to wait between unsuccessful reading due to EAGAIN or EWOULDBLOCK.
Radek Krejci206fcd62015-10-07 15:42:48 +0200111 */
112#define NC_READ_SLEEP 100
113
114/**
Michal Vasko80cad7f2015-12-08 14:42:27 +0100115 * Number of sockets kept waiting to be accepted.
116 */
117#define NC_REVERSE_QUEUE 1
118
119/**
Radek Krejci695d4fa2015-10-22 13:23:54 +0200120 * @brief type of the session
Radek Krejci206fcd62015-10-07 15:42:48 +0200121 */
122typedef enum {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200123 NC_CLIENT, /**< client side */
124 NC_SERVER /**< server side */
Radek Krejci206fcd62015-10-07 15:42:48 +0200125} NC_SIDE;
126
127/**
128 * @brief Enumeration of the supported NETCONF protocol versions
129 */
130typedef enum {
131 NC_VERSION_10 = 0, /**< NETCONV 1.0 - RFC 4741, 4742 */
132 NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */
133} NC_VERSION;
134
135#define NC_VERSION_10_ENDTAG "]]>]]>"
136#define NC_VERSION_10_ENDTAG_LEN 6
137
138/**
Michal Vaskoad611702015-12-03 13:41:51 +0100139 * @brief Container to serialize PRC messages
Radek Krejci5686ff72015-10-09 13:33:56 +0200140 */
Michal Vaskoad611702015-12-03 13:41:51 +0100141struct nc_msg_cont {
142 struct lyxml_elem *msg;
143 struct nc_msg_cont *next;
Radek Krejci5686ff72015-10-09 13:33:56 +0200144};
145
146/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200147 * @brief NETCONF session structure
148 */
149struct nc_session {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200150 NC_STATUS status; /**< status of the session */
151 NC_SIDE side; /**< side of the session: client or server */
Radek Krejci5686ff72015-10-09 13:33:56 +0200152
153 /* NETCONF data */
Radek Krejci206fcd62015-10-07 15:42:48 +0200154 uint32_t id; /**< NETCONF session ID (session-id-type) */
155 NC_VERSION version; /**< NETCONF protocol version */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200156 pthread_t *notif; /**< running notifications thread - TODO server-side only? */
Radek Krejci5686ff72015-10-09 13:33:56 +0200157
158 /* Transport implementation */
Radek Krejci206fcd62015-10-07 15:42:48 +0200159 NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200160 pthread_mutex_t *ti_lock; /**< lock to access ti. Note that in case of libssh TI, it can be shared with other
Michal Vaskob7ea2432015-10-26 15:38:40 +0100161 NETCONF sessions on the same SSH session (but different SSH channel) */
Radek Krejci206fcd62015-10-07 15:42:48 +0200162 union {
163 struct {
164 int in; /**< input file descriptor */
165 int out; /**< output file descriptor */
Radek Krejci206fcd62015-10-07 15:42:48 +0200166 } fd; /**< NC_TI_FD transport implementation structure */
Michal Vaskofb2fb762015-10-27 11:44:32 +0100167#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +0200168 struct {
Radek Krejci206fcd62015-10-07 15:42:48 +0200169 ssh_channel channel;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200170 ssh_session session;
171 struct nc_session *next; /**< pointer to the next NETCONF session on the same
172 SSH session, but different SSH channel. If no such session exists, it is NULL.
173 otherwise there is a ring list of the NETCONF sessions */
Radek Krejci206fcd62015-10-07 15:42:48 +0200174 } libssh;
175#endif
176#ifdef ENABLE_TLS
177 SSL *tls;
178#endif
Radek Krejci5686ff72015-10-09 13:33:56 +0200179 } ti; /**< transport implementation data */
Radek Krejciac6d3472015-10-22 15:47:18 +0200180 const char *username;
181 const char *host;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100182 uint16_t port;
Radek Krejci5686ff72015-10-09 13:33:56 +0200183
184 /* other */
185 struct ly_ctx *ctx; /**< libyang context of the session */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200186 uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */
Michal Vasko086311b2016-01-08 09:53:11 +0100187#define NC_SESSION_SHAREDCTX 0x01
188#define NC_SESSION_SSH_AUTHENTICATED 0x02
189#define NC_SESSION_SSH_SUBSYS_NETCONF 0x04
Radek Krejci5686ff72015-10-09 13:33:56 +0200190
191 /* client side only data */
Radek Krejcife0b3472015-10-12 13:43:42 +0200192 uint64_t msgid;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200193 const char **cpblts; /**< list of server's capabilities on client side */
Michal Vaskoad611702015-12-03 13:41:51 +0100194 struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */
195 struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */
Michal Vasko086311b2016-01-08 09:53:11 +0100196
197 /* server side only data */
198 uint16_t auth_attempts;
Radek Krejci206fcd62015-10-07 15:42:48 +0200199};
200
Michal Vasko086311b2016-01-08 09:53:11 +0100201NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op);
202
203int session_ti_lock(struct nc_session *session, int32_t timeout);
204
205int session_ti_unlock(struct nc_session *session);
206
Radek Krejci206fcd62015-10-07 15:42:48 +0200207/**
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100208 * @brief Fill libyang context in \p session. Context models are based on the stored session
209 * capabilities. If the server does not support \<get-schema\>, the models are searched
210 * for in the directory set using nc_schema_searchpath().
211 *
212 * @param[in] session Session to create the context for.
213 * @return 0 on success, non-zero on failure.
214 */
Michal Vasko57eb9402015-12-08 14:38:12 +0100215int nc_ctx_check_and_fill(struct nc_session *session);
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100216
217/**
218 * @brief Create and connect a socket.
219 *
220 * @param[in] host Hostname to connect to.
221 * @param[in] port Port to connect on.
222 * @return Connected socket or -1 on error.
223 */
224int nc_connect_getsocket(const char *host, unsigned short port);
225
226/**
227 * @brief Perform NETCONF handshake on \p session.
228 *
229 * @param[in] session NETCONF session to use.
230 * @return 0 on success, non-zero on failure.
231 */
232int nc_handshake(struct nc_session *session);
233
234/**
Michal Vasko80cad7f2015-12-08 14:42:27 +0100235 * @brief Accept a new (Call Home) connection.
236 *
237 * @param[in] port Port to listen on.
238 * @param[in] timeout Timeout in msec.
239 * @param[out] server_port Port the new connection is connected on. Can be NULL.
240 * @param[out] server_host Host the new connection was initiated from. Can be NULL.
241 * @return Connected socket with the new connection, -1 on error.
242 */
243int nc_callhome_accept_connection(uint16_t port, int32_t timeout, uint16_t *server_port, char **server_host);
244
245/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200246 * Functions
247 * - io.c
248 */
249
250/**
251 * @brief Read message from the wire.
252 *
253 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
254 * libyang XML tree and the message type is detected from the top level element.
255 *
256 * @param[in] session NETCONF session from which the message is being read.
257 * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout,
258 * zero value causes to return immediately.
259 * @param[out] data XML tree built from the read data.
260 * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
Radek Krejci5686ff72015-10-09 13:33:56 +0200261 * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is
Radek Krejci206fcd62015-10-07 15:42:48 +0200262 * returned on error and #NC_MSG_NONE is never returned by this function.
263 */
264NC_MSG_TYPE nc_read_msg(struct nc_session* session, int timeout, struct lyxml_elem **data);
265
Radek Krejcife0b3472015-10-12 13:43:42 +0200266/**
267 * @brief Write message into wire.
268 *
269 * @param[in] session NETCONF session to which the message will be written.
270 * @param[in] type Type of the message to write. According to the type, the
271 * specific additional parameters are required or accepted:
272 * - #NC_MSG_RPC
273 * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
274 * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element.
275 * `message-id` attribute is added automatically and default namespace is set to
276 * #NC_NS_BASE. Optional parameter.
277 * - #NC_MSG_REPLY
278 * - `struct nc_rpc *rpc;` - RPC object to reply. Required parameter.
279 * - TODO: content
280 * - #NC_MSG_NOTIF
281 * - TODO: content
282 * @return 0 on success
283 */
284int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...);
285
Michal Vasko086311b2016-01-08 09:53:11 +0100286int nc_sock_listen(const char *address, uint32_t port);
287
288int nc_sock_accept(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port);
289
Radek Krejci206fcd62015-10-07 15:42:48 +0200290#endif /* NC_SESSION_PRIVATE_H_ */