blob: 333eb0900ba6dd897569ccb6a38ef48ab24a0720 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session_p.h
3 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob48aa812016-01-18 14:13:09 +01004 * \author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci206fcd62015-10-07 15:42:48 +02005 * \brief libnetconf2 session manipulation
6 *
Michal Vasko4c1fb492017-01-30 14:31:07 +01007 * Copyright (c) 2017 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020014 */
15
16#ifndef NC_SESSION_PRIVATE_H_
17#define NC_SESSION_PRIVATE_H_
18
Radek Krejcife0b3472015-10-12 13:43:42 +020019#include <stdint.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020020#include <pthread.h>
21
Michal Vasko4ef14932015-12-04 11:09:10 +010022#include <libyang/libyang.h>
Michal Vasko7bcb48e2016-01-15 10:28:54 +010023
Michal Vasko4ef14932015-12-04 11:09:10 +010024#include "libnetconf.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010025#include "netconf.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010026#include "session.h"
Michal Vaskoa8ad4482016-01-28 14:25:54 +010027#include "messages_client.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010028
Radek Krejci53691be2016-02-22 13:58:37 +010029#ifdef NC_ENABLED_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010030
Radek Krejci206fcd62015-10-07 15:42:48 +020031# include <libssh/libssh.h>
32# include <libssh/callbacks.h>
Michal Vasko086311b2016-01-08 09:53:11 +010033# include <libssh/server.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010034
35/* seconds */
Michal Vaskoc111ac52015-12-08 14:36:36 +010036# define NC_SSH_TIMEOUT 10
Michal Vasko5c2f7952016-01-22 13:16:31 +010037/* number of all supported authentication methods */
Michal Vaskoc111ac52015-12-08 14:36:36 +010038# define NC_SSH_AUTH_COUNT 3
Michal Vasko7b62fed2015-10-26 15:39:46 +010039
Michal Vasko7227f352016-02-01 12:11:40 +010040/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +010041struct nc_client_ssh_opts {
Michal Vasko7b62fed2015-10-26 15:39:46 +010042 /* SSH authentication method preferences */
43 struct {
44 NC_SSH_AUTH_TYPE type;
Michal Vasko3031aae2016-01-27 16:07:18 +010045 int16_t value;
Michal Vaskoc111ac52015-12-08 14:36:36 +010046 } auth_pref[NC_SSH_AUTH_COUNT];
Michal Vasko7b62fed2015-10-26 15:39:46 +010047
48 /* SSH key pairs */
49 struct {
50 char *pubkey_path;
51 char *privkey_path;
Michal Vasko3031aae2016-01-27 16:07:18 +010052 int8_t privkey_crypt;
Michal Vasko7b62fed2015-10-26 15:39:46 +010053 } *keys;
Michal Vasko3031aae2016-01-27 16:07:18 +010054 uint16_t key_count;
55
Michal Vasko30e2c872016-02-18 10:03:21 +010056 /* SSH authentication callbacks */
Radek Krejci90a84a22017-05-25 13:53:00 +020057 int (*auth_hostkey_check)(const char *, ssh_session, void *);
58 char *(*auth_password)(const char *, const char *, void *);
59 char *(*auth_interactive)(const char *, const char *, const char *, int, void *);
60 char *(*auth_privkey_passphrase)(const char *, void *);
61
62 /* private data for the callbacks */
63 void *auth_hostkey_check_priv;
64 void *auth_password_priv;
65 void *auth_interactive_priv;
66 void *auth_privkey_passphrase_priv;
Michal Vasko30e2c872016-02-18 10:03:21 +010067
Michal Vasko3031aae2016-01-27 16:07:18 +010068 char *username;
Michal Vasko7b62fed2015-10-26 15:39:46 +010069};
70
Michal Vasko7227f352016-02-01 12:11:40 +010071/* ACCESS locked, separate locks */
Michal Vasko3031aae2016-01-27 16:07:18 +010072struct nc_server_ssh_opts {
Michal Vaskoe2713da2016-08-22 16:06:40 +020073 /* SSH bind options */
74 const char **hostkeys;
75 uint8_t hostkey_count;
76 const char *banner;
Michal Vasko086311b2016-01-08 09:53:11 +010077
Michal Vasko086311b2016-01-08 09:53:11 +010078 int auth_methods;
79 uint16_t auth_attempts;
80 uint16_t auth_timeout;
81};
82
Radek Krejci53691be2016-02-22 13:58:37 +010083#endif /* NC_ENABLED_SSH */
Radek Krejci206fcd62015-10-07 15:42:48 +020084
Radek Krejci53691be2016-02-22 13:58:37 +010085#ifdef NC_ENABLED_TLS
Michal Vasko4ef14932015-12-04 11:09:10 +010086
Michal Vaskoc111ac52015-12-08 14:36:36 +010087# include <openssl/bio.h>
88# include <openssl/ssl.h>
Michal Vasko11d4cdb2015-10-29 11:42:52 +010089
Michal Vasko7227f352016-02-01 12:11:40 +010090/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +010091struct nc_client_tls_opts {
92 char *cert_path;
93 char *key_path;
94 char *ca_file;
95 char *ca_dir;
96 int8_t tls_ctx_change;
Michal Vasko11d4cdb2015-10-29 11:42:52 +010097 SSL_CTX *tls_ctx;
Michal Vasko3031aae2016-01-27 16:07:18 +010098
99 char *crl_file;
100 char *crl_dir;
101 int8_t crl_store_change;
102 X509_STORE *crl_store;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100103};
104
Michal Vasko7227f352016-02-01 12:11:40 +0100105/* ACCESS locked, separate locks */
Michal Vasko3031aae2016-01-27 16:07:18 +0100106struct nc_server_tls_opts {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100107 const char *server_cert;
108 const char **trusted_cert_lists;
109 uint16_t trusted_cert_list_count;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200110 const char *trusted_ca_file;
111 const char *trusted_ca_dir;
Michal Vasko0457bb42016-01-08 15:49:13 +0100112 X509_STORE *crl_store;
113
Michal Vasko5e3f3392016-01-20 11:13:01 +0100114 struct nc_ctn {
Michal Vasko0457bb42016-01-08 15:49:13 +0100115 uint32_t id;
116 const char *fingerprint;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100117 NC_TLS_CTN_MAPTYPE map_type;
Michal Vasko0457bb42016-01-08 15:49:13 +0100118 const char *name;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100119 struct nc_ctn *next;
Michal Vasko0457bb42016-01-08 15:49:13 +0100120 } *ctn;
Michal Vasko0457bb42016-01-08 15:49:13 +0100121};
122
Radek Krejci53691be2016-02-22 13:58:37 +0100123#endif /* NC_ENABLED_TLS */
Radek Krejci43390242015-10-08 15:34:04 +0200124
Michal Vasko7227f352016-02-01 12:11:40 +0100125/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +0100126struct nc_client_opts {
127 char *schema_searchpath;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200128 ly_module_imp_clb schema_clb;
129 void *schema_clb_data;
Michal Vasko3031aae2016-01-27 16:07:18 +0100130
131 struct nc_bind {
132 const char *address;
133 uint16_t port;
134 int sock;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200135 int pollin;
Michal Vasko3031aae2016-01-27 16:07:18 +0100136 } *ch_binds;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200137 NC_TRANSPORT_IMPL *ch_bind_ti;
Michal Vasko3031aae2016-01-27 16:07:18 +0100138 uint16_t ch_bind_count;
139};
140
Radek Krejci62aa0642017-05-25 16:33:49 +0200141/* ACCESS unlocked */
142struct nc_client_context {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200143 unsigned int refcount;
Radek Krejci62aa0642017-05-25 16:33:49 +0200144 struct nc_client_opts opts;
145#ifdef NC_ENABLED_SSH
146 struct nc_client_ssh_opts ssh_opts;
147 struct nc_client_ssh_opts ssh_ch_opts;
148#endif /* NC_ENABLED_SSH */
149#ifdef NC_ENABLED_TLS
150 struct nc_client_tls_opts tls_opts;
151 struct nc_client_tls_opts tls_ch_opts;
152#endif /* NC_ENABLED_TLS */
153};
154
Michal Vasko0457bb42016-01-08 15:49:13 +0100155struct nc_server_opts {
Michal Vaskoa43589b2016-02-17 13:24:59 +0100156 /* ACCESS unlocked (dictionary locked internally in libyang) */
Michal Vasko0457bb42016-01-08 15:49:13 +0100157 struct ly_ctx *ctx;
158
Michal Vasko7227f352016-02-01 12:11:40 +0100159 /* ACCESS unlocked */
Michal Vasko0457bb42016-01-08 15:49:13 +0100160 NC_WD_MODE wd_basic_mode;
161 int wd_also_supported;
Radek Krejci658782b2016-12-04 22:04:55 +0100162 unsigned int capabilities_count;
163 const char **capabilities;
Michal Vasko0457bb42016-01-08 15:49:13 +0100164
Michal Vasko7227f352016-02-01 12:11:40 +0100165 /* ACCESS unlocked */
Michal Vasko0457bb42016-01-08 15:49:13 +0100166 uint16_t hello_timeout;
167 uint16_t idle_timeout;
Michal Vasko7060bcf2016-11-28 14:48:11 +0100168#ifdef NC_ENABLED_TLS
169 int (*user_verify_clb)(const struct nc_session *session);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100170
171 int (*server_cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data,char **privkey_path,
172 char **privkey_data, int *privkey_data_rsa);
173 void *server_cert_data;
174 void (*server_cert_data_free)(void *data);
175
176 int (*trusted_cert_list_clb)(const char *name, void *user_data, char ***cert_paths, int *cert_path_count,
177 char ***cert_data, int *cert_data_count);
178 void *trusted_cert_list_data;
179 void (*trusted_cert_list_data_free)(void *data);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100180#endif
Michal Vasko0457bb42016-01-08 15:49:13 +0100181
Michal Vasko17dfda92016-12-01 14:06:16 +0100182#ifdef NC_ENABLED_SSH
183 /* ACCESS locked with authkey_lock */
184 struct {
185 const char *path;
186 const char *base64;
187 NC_SSH_KEY_TYPE type;
188 const char *username;
189 } *authkeys;
190 uint16_t authkey_count;
191 pthread_mutex_t authkey_lock;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100192
193 int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path, char **privkey_data, int *privkey_data_rsa);
194 void *hostkey_data;
195 void (*hostkey_data_free)(void *data);
Michal Vasko17dfda92016-12-01 14:06:16 +0100196#endif
197
Michal Vaskoade892d2017-02-22 13:40:35 +0100198 /* ACCESS locked, add/remove endpts/binds - bind_lock + WRITE endpt_lock (strict order!)
199 * modify endpts - WRITE endpt_lock
200 * access endpts - READ endpt_lock
201 * modify/poll binds - bind_lock */
Michal Vasko3031aae2016-01-27 16:07:18 +0100202 struct nc_bind *binds;
Michal Vaskoade892d2017-02-22 13:40:35 +0100203 pthread_mutex_t bind_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +0100204 struct nc_endpt {
205 const char *name;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200206 NC_TRANSPORT_IMPL ti;
207 union {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200208#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +0200209 struct nc_server_ssh_opts *ssh;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200210#endif
Michal Vasko6b151062016-08-22 16:20:47 +0200211#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +0200212 struct nc_server_tls_opts *tls;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200213#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +0200214 } opts;
Michal Vasko3031aae2016-01-27 16:07:18 +0100215 } *endpts;
216 uint16_t endpt_count;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200217 pthread_rwlock_t endpt_lock;
218
219 /* ACCESS locked, add/remove CH clients - WRITE lock ch_client_lock
220 * modify CH clients - READ lock ch_client_lock + ch_client_lock */
221 struct nc_ch_client {
222 const char *name;
223 NC_TRANSPORT_IMPL ti;
224 struct nc_ch_endpt {
225 const char *name;
226 const char *address;
227 uint16_t port;
228 } *ch_endpts;
229 uint16_t ch_endpt_count;
230 union {
231#ifdef NC_ENABLED_SSH
232 struct nc_server_ssh_opts *ssh;
233#endif
234#ifdef NC_ENABLED_TLS
235 struct nc_server_tls_opts *tls;
236#endif
237 } opts;
238 NC_CH_CONN_TYPE conn_type;
239 union {
240 struct {
241 uint32_t idle_timeout;
242 uint16_t ka_max_wait;
243 uint8_t ka_max_attempts;
244 } persist;
245 struct {
246 uint16_t idle_timeout;
247 uint16_t reconnect_timeout;
248 } period;
249 } conn;
250 NC_CH_START_WITH start_with;
251 uint8_t max_attempts;
252 pthread_mutex_t lock;
253 } *ch_clients;
254 uint16_t ch_client_count;
255 pthread_rwlock_t ch_client_lock;
Michal Vaskob48aa812016-01-18 14:13:09 +0100256
Michal Vasko7227f352016-02-01 12:11:40 +0100257 /* ACCESS locked with sid_lock */
Michal Vaskob48aa812016-01-18 14:13:09 +0100258 uint32_t new_session_id;
259 pthread_spinlock_t sid_lock;
Michal Vasko0457bb42016-01-08 15:49:13 +0100260};
261
Radek Krejci206fcd62015-10-07 15:42:48 +0200262/**
Michal Vasko0190bc32016-03-02 15:47:49 +0100263 * Sleep time in msec to wait between nc_recv_notif() calls.
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100264 */
265#define NC_CLIENT_NOTIF_THREAD_SLEEP 10000
266
267/**
Michal Vasko0190bc32016-03-02 15:47:49 +0100268 * Timeout in msec for transport-related data to arrive (ssh_handle_key_exchange(), SSL_accept(), SSL_connect()).
Michal Vasko36c7be82017-02-22 13:37:59 +0100269 * It can be quite a lot on slow machines (waiting for TLS cert-to-name resolution, ...).
Michal Vasko0190bc32016-03-02 15:47:49 +0100270 */
Michal Vasko23edb632017-02-23 15:15:52 +0100271#define NC_TRANSPORT_TIMEOUT 10000
Michal Vasko36c7be82017-02-22 13:37:59 +0100272
273/**
274 * Timeout in msec for acquiring a lock of a session (used with a condition, so higher numbers could be required
275 * only in case of extreme concurrency).
276 */
277#define NC_SESSION_LOCK_TIMEOUT 500
Michal Vasko0190bc32016-03-02 15:47:49 +0100278
279/**
Michal Vaskof471fa02017-02-15 10:48:12 +0100280 * Timeout in msec for acquiring a lock of a session that is supposed to be freed.
281 */
Michal Vasko36c7be82017-02-22 13:37:59 +0100282#define NC_SESSION_FREE_LOCK_TIMEOUT 1000
Michal Vaskof471fa02017-02-15 10:48:12 +0100283
284/**
285 * Timeout in msec for acquiring a lock of a pollsession structure.
286 */
287#define NC_PS_LOCK_TIMEOUT 500
288
289/**
Michal Vasko1a22db42017-03-17 09:09:15 +0100290 * Time slept in msec if no endpoint was created for a running Call Home client.
Michal Vaskoade892d2017-02-22 13:40:35 +0100291 */
292#define NC_CH_NO_ENDPT_WAIT 1000
293
294/**
Michal Vasko1a22db42017-03-17 09:09:15 +0100295 * Time slept in msec after a failed Call Home endpoint session creation.
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 */
297#define NC_CH_ENDPT_FAIL_WAIT 1000
298
299/**
Michal Vasko80cad7f2015-12-08 14:42:27 +0100300 * Number of sockets kept waiting to be accepted.
301 */
Michal Vasko0a3f3752016-10-13 14:58:38 +0200302#define NC_REVERSE_QUEUE 5
Michal Vasko80cad7f2015-12-08 14:42:27 +0100303
304/**
Michal Vasko5c2f7952016-01-22 13:16:31 +0100305 * @brief Type of the session
Radek Krejci206fcd62015-10-07 15:42:48 +0200306 */
307typedef enum {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200308 NC_CLIENT, /**< client side */
309 NC_SERVER /**< server side */
Radek Krejci206fcd62015-10-07 15:42:48 +0200310} NC_SIDE;
311
312/**
313 * @brief Enumeration of the supported NETCONF protocol versions
314 */
315typedef enum {
Michal Vasko5c2f7952016-01-22 13:16:31 +0100316 NC_VERSION_10 = 0, /**< NETCONF 1.0 - RFC 4741, 4742 */
Radek Krejci206fcd62015-10-07 15:42:48 +0200317 NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */
318} NC_VERSION;
319
320#define NC_VERSION_10_ENDTAG "]]>]]>"
321#define NC_VERSION_10_ENDTAG_LEN 6
322
323/**
Michal Vaskoad611702015-12-03 13:41:51 +0100324 * @brief Container to serialize PRC messages
Radek Krejci5686ff72015-10-09 13:33:56 +0200325 */
Michal Vaskoad611702015-12-03 13:41:51 +0100326struct nc_msg_cont {
327 struct lyxml_elem *msg;
328 struct nc_msg_cont *next;
Radek Krejci5686ff72015-10-09 13:33:56 +0200329};
330
331/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200332 * @brief NETCONF session structure
333 */
334struct nc_session {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200335 NC_STATUS status; /**< status of the session */
Michal Vasko428087d2016-01-14 16:04:28 +0100336 NC_SESSION_TERM_REASON term_reason; /**< reason of termination, if status is NC_STATUS_INVALID */
Michal Vasko142cfea2017-08-07 10:12:11 +0200337 uint32_t killed_by; /**< session responsible for termination, if term_reason is NC_SESSION_TERM_KILLED */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200338 NC_SIDE side; /**< side of the session: client or server */
Radek Krejci5686ff72015-10-09 13:33:56 +0200339
340 /* NETCONF data */
Radek Krejci206fcd62015-10-07 15:42:48 +0200341 uint32_t id; /**< NETCONF session ID (session-id-type) */
342 NC_VERSION version; /**< NETCONF protocol version */
Radek Krejci5686ff72015-10-09 13:33:56 +0200343
344 /* Transport implementation */
Radek Krejci206fcd62015-10-07 15:42:48 +0200345 NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200346 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 +0100347 NETCONF sessions on the same SSH session (but different SSH channel) */
Michal Vaskoade892d2017-02-22 13:40:35 +0100348 pthread_cond_t *ti_cond; /**< ti_inuse condition */
349 volatile int *ti_inuse; /**< variable indicating whether TI is being communicated on or not, protected by
350 ti_cond and ti_lock */
Radek Krejci206fcd62015-10-07 15:42:48 +0200351 union {
352 struct {
353 int in; /**< input file descriptor */
354 int out; /**< output file descriptor */
Radek Krejci206fcd62015-10-07 15:42:48 +0200355 } fd; /**< NC_TI_FD transport implementation structure */
Radek Krejci53691be2016-02-22 13:58:37 +0100356#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +0200357 struct {
Radek Krejci206fcd62015-10-07 15:42:48 +0200358 ssh_channel channel;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200359 ssh_session session;
360 struct nc_session *next; /**< pointer to the next NETCONF session on the same
361 SSH session, but different SSH channel. If no such session exists, it is NULL.
362 otherwise there is a ring list of the NETCONF sessions */
Radek Krejci206fcd62015-10-07 15:42:48 +0200363 } libssh;
364#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100365#ifdef NC_ENABLED_TLS
Radek Krejci206fcd62015-10-07 15:42:48 +0200366 SSL *tls;
367#endif
Radek Krejci5686ff72015-10-09 13:33:56 +0200368 } ti; /**< transport implementation data */
Radek Krejciac6d3472015-10-22 15:47:18 +0200369 const char *username;
370 const char *host;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100371 uint16_t port;
Radek Krejci5686ff72015-10-09 13:33:56 +0200372
373 /* other */
374 struct ly_ctx *ctx; /**< libyang context of the session */
Michal Vasko2cc4c682016-03-01 09:16:48 +0100375 void *data; /**< arbitrary user data */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200376 uint8_t flags; /**< various flags of the session - TODO combine with status and/or side */
Michal Vasko086311b2016-01-08 09:53:11 +0100377#define NC_SESSION_SHAREDCTX 0x01
Michal Vaskob05053d2016-01-22 16:12:06 +0100378#define NC_SESSION_CALLHOME 0x02
Radek Krejci5686ff72015-10-09 13:33:56 +0200379
Michal Vasko2e6defd2016-10-07 15:48:15 +0200380 union {
381 struct {
Radek Krejcif637ba82016-10-18 14:18:14 +0200382 /* client side only data */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200383 uint64_t msgid;
Michal Vasko2cb24b42017-05-23 14:59:11 +0200384 char **cpblts; /**< list of server's capabilities on client side */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200385 struct nc_msg_cont *replies; /**< queue for RPC replies received instead of notifications */
386 struct nc_msg_cont *notifs; /**< queue for notifications received instead of RPC reply */
Radek Krejcif637ba82016-10-18 14:18:14 +0200387 volatile pthread_t *ntf_tid; /**< running notifications receiving thread */
Michal Vaskoeee99412016-11-21 10:19:43 +0100388
389 /* client flags */
390 /* some server modules failed to load so the data from them will be ignored - not use strict flag for parsing */
391# define NC_SESSION_CLIENT_NOT_STRICT 0x40
Michal Vasko2e6defd2016-10-07 15:48:15 +0200392 } client;
393 struct {
Radek Krejcif637ba82016-10-18 14:18:14 +0200394 /* server side only data */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200395 time_t session_start; /**< time the session was created */
396 time_t last_rpc; /**< time the last RPC was received on this session */
Michal Vasko3486a7c2017-03-03 13:28:07 +0100397 int ntf_status; /**< flag whether the session is subscribed to any stream */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200398 pthread_mutex_t *ch_lock; /**< Call Home thread lock */
399 pthread_cond_t *ch_cond; /**< Call Home thread condition */
Michal Vasko086311b2016-01-08 09:53:11 +0100400
Michal Vaskoeee99412016-11-21 10:19:43 +0100401 /* server flags */
Radek Krejci53691be2016-02-22 13:58:37 +0100402#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +0200403 /* SSH session authenticated */
404# define NC_SESSION_SSH_AUTHENTICATED 0x04
405 /* netconf subsystem requested */
406# define NC_SESSION_SSH_SUBSYS_NETCONF 0x08
407 /* new SSH message arrived */
408# define NC_SESSION_SSH_NEW_MSG 0x10
409 /* this session is passed to nc_sshcb_msg() */
410# define NC_SESSION_SSH_MSG_CB 0x20
Michal Vasko06e22432016-01-15 10:30:06 +0100411
Michal Vasko2e6defd2016-10-07 15:48:15 +0200412 uint16_t ssh_auth_attempts; /**< number of failed SSH authentication attempts */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100413#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100414#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +0200415 X509 *client_cert; /**< TLS client certificate if used for authentication */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100416#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +0200417 } server;
418 } opts;
Radek Krejci206fcd62015-10-07 15:42:48 +0200419};
420
Michal Vaskoe4300a82017-05-24 10:35:42 +0200421enum nc_ps_session_state {
422 NC_PS_STATE_NONE = 0, /**< session is not being worked with */
423 NC_PS_STATE_BUSY, /**< session is being polled or communicated on (and locked) */
424 NC_PS_STATE_INVALID /**< session is invalid and was already returned by another poll */
425};
426
Michal Vasko48a63ed2016-03-01 09:48:21 +0100427/* ACCESS locked */
Michal Vaskofb89d772016-01-08 12:25:35 +0100428struct nc_pollsession {
Michal Vaskoe4300a82017-05-24 10:35:42 +0200429 struct {
430 struct nc_session *session;
431 enum nc_ps_session_state state;
432 } *sessions;
Michal Vaskofb89d772016-01-08 12:25:35 +0100433 uint16_t session_count;
Michal Vasko99f251b2017-01-11 11:31:46 +0100434 uint16_t last_event_session;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200435
436 pthread_cond_t cond;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100437 pthread_mutex_t lock;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200438 uint8_t queue[NC_PS_QUEUE_SIZE]; /**< round buffer, queue is empty when queue_len == 0 */
439 uint8_t queue_begin; /**< queue starts on queue[queue_begin] */
440 uint8_t queue_len; /**< queue ends on queue[queue_begin + queue_len - 1] */
Michal Vaskofb89d772016-01-08 12:25:35 +0100441};
442
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100443struct nc_ntf_thread_arg {
444 struct nc_session *session;
445 void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif);
446};
447
Michal Vasko4eb3c312016-03-01 14:09:37 +0100448void *nc_realloc(void *ptr, size_t size);
449
Michal Vasko086311b2016-01-08 09:53:11 +0100450NC_MSG_TYPE nc_send_msg(struct nc_session *session, struct lyd_node *op);
451
Radek Krejci28472922016-07-15 11:51:16 +0200452#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
453int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
454#endif
455
Radek Krejci7ac16052016-07-15 11:48:18 +0200456int nc_gettimespec(struct timespec *ts);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200457
Radek Krejcida0afb22017-05-26 16:25:59 +0200458int32_t nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2);
Michal Vasko36c7be82017-02-22 13:37:59 +0100459
460void nc_addtimespec(struct timespec *ts, uint32_t msec);
Michal Vasko99f251b2017-01-11 11:31:46 +0100461
Michal Vaskoade892d2017-02-22 13:40:35 +0100462struct nc_session *nc_new_session(int not_allocate_ti);
463
464int nc_session_lock(struct nc_session *session, int timeout, const char *func);
465
466int nc_session_unlock(struct nc_session *session, int timeout, const char *func);
Michal Vasko086311b2016-01-08 09:53:11 +0100467
Michal Vasko227f8ff2016-07-26 14:08:59 +0200468int nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func);
Michal Vaskof04a52a2016-04-07 10:52:10 +0200469
Michal Vasko227f8ff2016-07-26 14:08:59 +0200470int nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func);
Michal Vaskof04a52a2016-04-07 10:52:10 +0200471
Radek Krejci206fcd62015-10-07 15:42:48 +0200472/**
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100473 * @brief Fill libyang context in \p session. Context models are based on the stored session
474 * capabilities. If the server does not support \<get-schema\>, the models are searched
Radek Krejcic9a6d252016-03-04 14:50:34 +0100475 * for in the directory set using nc_client_schema_searchpath().
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100476 *
477 * @param[in] session Session to create the context for.
Michal Vaskoef578332016-01-25 13:20:09 +0100478 * @return 0 on success, 1 on some missing schemas, -1 on error.
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100479 */
Michal Vasko57eb9402015-12-08 14:38:12 +0100480int nc_ctx_check_and_fill(struct nc_session *session);
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100481
482/**
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100483 * @brief Perform NETCONF handshake on \p session.
484 *
485 * @param[in] session NETCONF session to use.
Michal Vasko71090fc2016-05-24 16:37:28 +0200486 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message parsing fail
487 * (server-side only), NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other error.
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100488 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200489NC_MSG_TYPE nc_handshake(struct nc_session *session);
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100490
491/**
Michal Vaskof05562c2016-01-20 12:06:43 +0100492 * @brief Create a socket connection.
493 *
494 * @param[in] host Hostname to connect to.
495 * @param[in] port Port to connect on.
496 * @return Connected socket or -1 on error.
497 */
498int nc_sock_connect(const char *host, uint16_t port);
499
500/**
501 * @brief Accept a new socket connection.
Michal Vasko80cad7f2015-12-08 14:42:27 +0100502 *
Michal Vaskoc61c4492016-01-25 11:13:34 +0100503 * @param[in] sock Listening socket.
Michal Vaskof05562c2016-01-20 12:06:43 +0100504 * @param[in] timeout Timeout in milliseconds.
505 * @param[out] peer_host Host the new connection was initiated from. Can be NULL.
506 * @param[out] peer_port Port the new connection is connected on. Can be NULL.
Michal Vasko80cad7f2015-12-08 14:42:27 +0100507 * @return Connected socket with the new connection, -1 on error.
508 */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100509int nc_sock_accept(int sock, int timeout, char **peer_host, uint16_t *peer_port);
Michal Vasko80cad7f2015-12-08 14:42:27 +0100510
Michal Vasko1a38c862016-01-15 15:50:07 +0100511/**
512 * @brief Create a listening socket.
513 *
514 * @param[in] address IP address to listen on.
515 * @param[in] port Port to listen on.
516 * @return Listening socket, -1 on error.
517 */
Michal Vaskof05562c2016-01-20 12:06:43 +0100518int nc_sock_listen(const char *address, uint16_t port);
Michal Vaskofb89d772016-01-08 12:25:35 +0100519
Michal Vasko1a38c862016-01-15 15:50:07 +0100520/**
521 * @brief Accept a new connection on a listening socket.
522 *
523 * @param[in] binds Structure with the listening sockets.
524 * @param[in] bind_count Number of \p binds.
525 * @param[in] timeout Timeout for accepting.
Michal Vasko1a38c862016-01-15 15:50:07 +0100526 * @param[out] host Host of the remote peer. Can be NULL.
527 * @param[out] port Port of the new connection. Can be NULL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100528 * @param[out] idx Index of the bind that was accepted. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100529 * @return Accepted socket of the new connection, -1 on error.
530 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100531int nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx);
532
533/**
Michal Vaskoda514772016-02-01 11:32:01 +0100534 * @brief Lock endpoint structures for reading and the specific endpoint.
535 *
536 * @param[in] name Name of the endpoint.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200537 * @param[in] ti Expected transport.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200538 * @param[out] idx Index of the endpoint. Optional.
Michal Vaskoda514772016-02-01 11:32:01 +0100539 * @return Endpoint structure.
540 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100541struct nc_endpt *nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100542
Michal Vaskoda514772016-02-01 11:32:01 +0100543/**
Michal Vasko2e6defd2016-10-07 15:48:15 +0200544 * @brief Lock CH client structures for reading and the specific client.
545 *
546 * @param[in] name Name of the CH client.
547 * @param[in] ti Expected transport.
548 * @param[out] idx Index of the client. Optional.
549 * @return CH client structure.
550 */
551struct nc_ch_client *nc_server_ch_client_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx);
552
553/**
554 * @brief Unlock CH client strcutures and the specific client.
555 *
556 * @param[in] endpt Locked CH client structure.
557 */
558void nc_server_ch_client_unlock(struct nc_ch_client *client);
559
560/**
Michal Vaskoda514772016-02-01 11:32:01 +0100561 * @brief Add a client Call Home bind, listen on it.
562 *
563 * @param[in] address Address to bind to.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200564 * @param[in] port Port to bind to.
565 * @param[in] ti Transport to use.
Michal Vaskoda514772016-02-01 11:32:01 +0100566 * @return 0 on success, -1 on error.
567 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100568int nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti);
569
Michal Vaskoda514772016-02-01 11:32:01 +0100570/**
571 * @brief Remove a client Call Home bind, stop listening on it.
572 *
573 * @param[in] address Address of the bind. NULL matches any address.
574 * @param[in] port Port of the bind. 0 matches all ports.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200575 * @param[in] ti Transport of the bind. 0 matches all transports.
Michal Vaskoda514772016-02-01 11:32:01 +0100576 * @return 0 on success, -1 on no matches found.
577 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100578int nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti);
579
Michal Vaskoda514772016-02-01 11:32:01 +0100580/**
581 * @brief Connect to a listening NETCONF client using Call Home.
582 *
583 * @param[in] host Hostname to connect to.
584 * @param[in] port Port to connect to.
585 * @param[in] ti Transport fo the connection.
Michal Vaskoda514772016-02-01 11:32:01 +0100586 * @param[out] session New Call Home session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200587 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
588 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vaskoda514772016-02-01 11:32:01 +0100589 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200590NC_MSG_TYPE nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100591
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100592void nc_init(void);
593
594void nc_destroy(void);
595
Radek Krejci53691be2016-02-22 13:58:37 +0100596#ifdef NC_ENABLED_SSH
Michal Vasko9e036d52016-01-08 10:49:26 +0100597
Michal Vaskoda514772016-02-01 11:32:01 +0100598/**
599 * @brief Accept a server Call Home connection on a socket.
600 *
601 * @param[in] sock Socket with a new connection.
602 * @param[in] host Hostname of the server.
603 * @param[in] port Port of the server.
604 * @param[in] ctx Context for the session. Can be NULL.
Michal Vasko0190bc32016-03-02 15:47:49 +0100605 * @param[in] timeout Transport operations timeout in msec.
Michal Vaskoda514772016-02-01 11:32:01 +0100606 * @return New session, NULL on error.
607 */
Michal Vasko0190bc32016-03-02 15:47:49 +0100608struct nc_session *nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout);
Michal Vasko3031aae2016-01-27 16:07:18 +0100609
Michal Vasko1a38c862016-01-15 15:50:07 +0100610/**
611 * @brief Establish SSH transport on a socket.
612 *
613 * @param[in] session Session structure of the new connection.
614 * @param[in] sock Socket of the new connection.
Michal Vasko0190bc32016-03-02 15:47:49 +0100615 * @param[in] timeout Transport operations timeout in msec (not SSH authentication one).
Michal Vasko1a38c862016-01-15 15:50:07 +0100616 * @return 1 on success, 0 on timeout, -1 on error.
617 */
Michal Vasko0190bc32016-03-02 15:47:49 +0100618int nc_accept_ssh_session(struct nc_session *session, int sock, int timeout);
Michal Vasko9e036d52016-01-08 10:49:26 +0100619
Michal Vasko96164bf2016-01-21 15:41:58 +0100620/**
621 * @brief Callback called when a new SSH message is received.
622 *
623 * @param[in] sshsession SSH session the message arrived on.
624 * @param[in] msg SSH message itself.
625 * @param[in] data NETCONF session running on \p sshsession.
626 * @return 0 if the message was handled, 1 if it is left up to libssh.
627 */
628int nc_sshcb_msg(ssh_session sshsession, ssh_message msg, void *data);
629
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100630void nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100631
Michal Vaskob7558c52016-02-26 15:04:19 +0100632void nc_client_ssh_destroy_opts(void);
633
Radek Krejci53691be2016-02-22 13:58:37 +0100634#endif /* NC_ENABLED_SSH */
Michal Vasko9e036d52016-01-08 10:49:26 +0100635
Radek Krejci53691be2016-02-22 13:58:37 +0100636#ifdef NC_ENABLED_TLS
Michal Vasko9e036d52016-01-08 10:49:26 +0100637
Michal Vasko0190bc32016-03-02 15:47:49 +0100638struct nc_session *nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout);
Michal Vasko3031aae2016-01-27 16:07:18 +0100639
Michal Vasko1a38c862016-01-15 15:50:07 +0100640/**
641 * @brief Establish TLS transport on a socket.
642 *
643 * @param[in] session Session structure of the new connection.
644 * @param[in] sock Socket of the new connection.
Michal Vasko0190bc32016-03-02 15:47:49 +0100645 * @param[in] timeout Transport operations timeout in msec.
Michal Vasko1a38c862016-01-15 15:50:07 +0100646 * @return 1 on success, 0 on timeout, -1 on error.
647 */
Michal Vasko0190bc32016-03-02 15:47:49 +0100648int nc_accept_tls_session(struct nc_session *session, int sock, int timeout);
Michal Vasko9e036d52016-01-08 10:49:26 +0100649
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100650void nc_server_tls_clear_opts(struct nc_server_tls_opts *opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100651
Michal Vaskob7558c52016-02-26 15:04:19 +0100652void nc_client_tls_destroy_opts(void);
653
Radek Krejci53691be2016-02-22 13:58:37 +0100654#endif /* NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100655
Michal Vasko80cad7f2015-12-08 14:42:27 +0100656/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200657 * Functions
658 * - io.c
659 */
660
661/**
662 * @brief Read message from the wire.
663 *
664 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
665 * libyang XML tree and the message type is detected from the top level element.
666 *
667 * @param[in] session NETCONF session from which the message is being read.
668 * @param[in] timeout Timeout in milliseconds. Negative value means infinite timeout,
669 * zero value causes to return immediately.
670 * @param[out] data XML tree built from the read data.
671 * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
Radek Krejci5686ff72015-10-09 13:33:56 +0200672 * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is
Radek Krejci206fcd62015-10-07 15:42:48 +0200673 * returned on error and #NC_MSG_NONE is never returned by this function.
674 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100675NC_MSG_TYPE nc_read_msg_poll(struct nc_session* session, int timeout, struct lyxml_elem **data);
676
677/**
678 * @brief Read message from the wire.
679 *
680 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
681 * libyang XML tree and the message type is detected from the top level element.
682 *
683 * @param[in] session NETCONF session from which the message is being read.
684 * @param[out] data XML tree built from the read data.
685 * @return Type of the read message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
686 * (or zero) value and it passed out without any data on the wire. #NC_MSG_ERROR is
687 * returned on error and #NC_MSG_NONE is never returned by this function.
688 */
689NC_MSG_TYPE nc_read_msg(struct nc_session* session, struct lyxml_elem **data);
Radek Krejci206fcd62015-10-07 15:42:48 +0200690
Radek Krejcife0b3472015-10-12 13:43:42 +0200691/**
692 * @brief Write message into wire.
693 *
694 * @param[in] session NETCONF session to which the message will be written.
Radek Krejci127f8952016-10-12 14:57:16 +0200695 * @param[in] type The type of the message to write, specified as #NC_MSG_TYPE value. According to the type, the
Radek Krejcife0b3472015-10-12 13:43:42 +0200696 * specific additional parameters are required or accepted:
697 * - #NC_MSG_RPC
698 * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
699 * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element.
Michal Vasko05ba9df2016-01-13 14:40:27 +0100700 * Required parameter.
701 * `message-id` attribute is added automatically and default namespace is set to #NC_NS_BASE.
702 * Optional parameter.
Radek Krejcife0b3472015-10-12 13:43:42 +0200703 * - #NC_MSG_REPLY
Michal Vasko05ba9df2016-01-13 14:40:27 +0100704 * - `struct lyxml_node *rpc_elem;` - root of the RPC object to reply to. Required parameter.
705 * - `struct nc_server_reply *reply;` - RPC reply. Required parameter.
Radek Krejcife0b3472015-10-12 13:43:42 +0200706 * - #NC_MSG_NOTIF
707 * - TODO: content
708 * @return 0 on success
709 */
Radek Krejci127f8952016-10-12 14:57:16 +0200710int nc_write_msg(struct nc_session *session, int type, ...);
Radek Krejcife0b3472015-10-12 13:43:42 +0200711
Michal Vasko1a38c862016-01-15 15:50:07 +0100712/**
713 * @brief Check whether a session is still connected (on transport layer).
714 *
715 * @param[in] session Session to check.
716 * @return 1 if connected, 0 if not.
717 */
Michal Vasko428087d2016-01-14 16:04:28 +0100718int nc_session_is_connected(struct nc_session *session);
719
Radek Krejci206fcd62015-10-07 15:42:48 +0200720#endif /* NC_SESSION_PRIVATE_H_ */