blob: 8e95a04de86ec1b1e9b719a0cab84f81357c48a3 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file session_p.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief libnetconf2 session manipulation
Radek Krejci206fcd62015-10-07 15:42:48 +02006 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01007 * @copyright
Michal Vasko3341d1d2023-03-24 11:33:09 +01008 * Copyright (c) 2017 - 2023 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02009 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010010 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010013 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010014 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020015 */
16
17#ifndef NC_SESSION_PRIVATE_H_
18#define NC_SESSION_PRIVATE_H_
19
Michal Vasko3341d1d2023-03-24 11:33:09 +010020#define _GNU_SOURCE
21
Radek Krejci206fcd62015-10-07 15:42:48 +020022#include <pthread.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <stdint.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020024#include <sys/stat.h>
Michal Vasko69a3ff62018-11-09 09:31:48 +010025
Michal Vasko4ef14932015-12-04 11:09:10 +010026#include <libyang/libyang.h>
Michal Vasko7bcb48e2016-01-15 10:28:54 +010027
Michal Vasko5bd4a3f2021-06-17 16:40:10 +020028#include "compat.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010029#include "libnetconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include "messages_client.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010031#include "netconf.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010032#include "session.h"
Michal Vaskoffb35e92022-10-20 09:07:25 +020033#include "session_client.h"
Michal Vasko4ef14932015-12-04 11:09:10 +010034
Radek Krejci53691be2016-02-22 13:58:37 +010035#ifdef NC_ENABLED_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010036
Michal Vaskoc446a382021-06-18 08:54:05 +020037# include <libssh/callbacks.h>
38# include <libssh/libssh.h>
39# include <libssh/server.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010040
41/* seconds */
Michal Vaskoc446a382021-06-18 08:54:05 +020042# define NC_SSH_TIMEOUT 10
Michal Vasko5c2f7952016-01-22 13:16:31 +010043/* number of all supported authentication methods */
Michal Vaskoc446a382021-06-18 08:54:05 +020044# define NC_SSH_AUTH_COUNT 3
Michal Vasko7b62fed2015-10-26 15:39:46 +010045
romanc1d2b092023-02-02 08:58:27 +010046/**
47 * Enumeration of diff operation types.
48 */
49typedef enum {
50 NC_OP_NONE,
51 NC_OP_CREATE,
52 NC_OP_DELETE,
53 NC_OP_REPLACE
54} NC_OPERATION;
55
56/**
57 * Enumeration of key or certificate store type.
58 */
59typedef enum {
60 NC_STORE_LOCAL, /**< key/certificate is stored locally in the ietf-netconf-server YANG data */
61 NC_STORE_KEYSTORE, /**< key/certificate is stored externally in a keystore module YANG data */
62 NC_STORE_TRUSTSTORE /**< key/certificate is stored externally in a truststore module YANG data */
63} NC_STORE_TYPE;
64
65/**
66 * Enumeration of SSH public key representation types.
67 */
68typedef enum {
69 NC_SSH_PUBKEY_SSH2, /**< begins with BEGIN SSH2 PUBLICKEY, see RFC 4716 */
70 NC_SSH_PUBKEY_X509 /**< begins with BEGIN PUBLICKEY, see RFC 5280 sec. 4.1.2.7 */
71} NC_SSH_PUBKEY_TYPE;
72
Michal Vasko7227f352016-02-01 12:11:40 +010073/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +010074struct nc_client_ssh_opts {
Michal Vasko7b62fed2015-10-26 15:39:46 +010075 /* SSH authentication method preferences */
76 struct {
77 NC_SSH_AUTH_TYPE type;
Michal Vasko3031aae2016-01-27 16:07:18 +010078 int16_t value;
Michal Vaskoc111ac52015-12-08 14:36:36 +010079 } auth_pref[NC_SSH_AUTH_COUNT];
Michal Vasko7b62fed2015-10-26 15:39:46 +010080
81 /* SSH key pairs */
82 struct {
83 char *pubkey_path;
84 char *privkey_path;
Michal Vasko3031aae2016-01-27 16:07:18 +010085 int8_t privkey_crypt;
Michal Vasko7b62fed2015-10-26 15:39:46 +010086 } *keys;
Michal Vasko3031aae2016-01-27 16:07:18 +010087 uint16_t key_count;
88
Michal Vasko30e2c872016-02-18 10:03:21 +010089 /* SSH authentication callbacks */
Radek Krejci90a84a22017-05-25 13:53:00 +020090 int (*auth_hostkey_check)(const char *, ssh_session, void *);
91 char *(*auth_password)(const char *, const char *, void *);
92 char *(*auth_interactive)(const char *, const char *, const char *, int, void *);
93 char *(*auth_privkey_passphrase)(const char *, void *);
94
95 /* private data for the callbacks */
96 void *auth_hostkey_check_priv;
97 void *auth_password_priv;
98 void *auth_interactive_priv;
99 void *auth_privkey_passphrase_priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100100
Michal Vasko3031aae2016-01-27 16:07:18 +0100101 char *username;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100102};
103
romanc1d2b092023-02-02 08:58:27 +0100104struct nc_certificate {
105 char *name;
106 char *cert_data;
107};
108
109struct nc_keystore {
roman45cec4e2023-02-17 10:21:39 +0100110 struct nc_ks_asym_key {
111 char *name;
112 NC_SSH_PUBKEY_TYPE pubkey_type;
113 char *pub_base64;
114 NC_SSH_KEY_TYPE privkey_type;
115 char *priv_base64;
116 struct {
117 char *name;
118 char *cert_base64;
119 } *certs;
120 uint16_t cert_count;
121 } *asym_keys;
122 uint16_t asym_key_count;
romanc1d2b092023-02-02 08:58:27 +0100123
roman45cec4e2023-02-17 10:21:39 +0100124 struct nc_ks_sym_key {
125 char *name;
126 char *base64;
127 } *sym_keys;
128 uint16_t sym_key_count;
romanc1d2b092023-02-02 08:58:27 +0100129};
130
romand745f752023-03-24 15:53:12 +0100131struct nc_auth_state {
132 int auth_method_count;
133 int auth_success_count;
134};
135
romanc1d2b092023-02-02 08:58:27 +0100136struct nc_client_auth {
137 char *username;
138
139 NC_STORE_TYPE ks_type;
140 union {
141 struct {
142 struct nc_client_auth_pubkey {
143 char *name;
romanc1d2b092023-02-02 08:58:27 +0100144 NC_SSH_PUBKEY_TYPE pubkey_type;
roman45cec4e2023-02-17 10:21:39 +0100145 char *pub_base64;
romanc1d2b092023-02-02 08:58:27 +0100146 } *pubkeys;
147 uint16_t pubkey_count;
148 };
149 char *ts_reference;
150 };
151
152 char *password;
153 char *pam_config_name;
154 char *pam_config_dir;
155 int supports_none;
156};
157
158struct nc_hostkey {
159 char *name;
160
161 NC_STORE_TYPE ks_type;
162 union {
163 struct {
164 NC_SSH_PUBKEY_TYPE pubkey_type;
165 char *pub_base64;
166 NC_SSH_KEY_TYPE privkey_type;
167 char *priv_base64;
168 };
roman45cec4e2023-02-17 10:21:39 +0100169 struct nc_ks_asym_key *ks_ref;
romanc1d2b092023-02-02 08:58:27 +0100170 };
171};
172
Michal Vasko7227f352016-02-01 12:11:40 +0100173/* ACCESS locked, separate locks */
Michal Vasko3031aae2016-01-27 16:07:18 +0100174struct nc_server_ssh_opts {
romanc1d2b092023-02-02 08:58:27 +0100175 struct nc_hostkey *hostkeys; /* everything in ks */
176 uint16_t hostkey_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100177
romanc1d2b092023-02-02 08:58:27 +0100178 struct nc_client_auth *auth_clients;
179 uint16_t client_count;
180
181 char *hostkey_algs;
182 char *encryption_algs;
183 char *kex_algs;
184 char *mac_algs;
185
Michal Vasko086311b2016-01-08 09:53:11 +0100186 uint16_t auth_attempts;
187 uint16_t auth_timeout;
188};
189
Radek Krejci53691be2016-02-22 13:58:37 +0100190#endif /* NC_ENABLED_SSH */
Radek Krejci206fcd62015-10-07 15:42:48 +0200191
Radek Krejci53691be2016-02-22 13:58:37 +0100192#ifdef NC_ENABLED_TLS
Michal Vasko4ef14932015-12-04 11:09:10 +0100193
Michal Vaskoc446a382021-06-18 08:54:05 +0200194# include <openssl/bio.h>
195# include <openssl/ssl.h>
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100196
Michal Vasko7227f352016-02-01 12:11:40 +0100197/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +0100198struct nc_client_tls_opts {
199 char *cert_path;
200 char *key_path;
201 char *ca_file;
202 char *ca_dir;
203 int8_t tls_ctx_change;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100204 SSL_CTX *tls_ctx;
Michal Vasko3031aae2016-01-27 16:07:18 +0100205
206 char *crl_file;
207 char *crl_dir;
208 int8_t crl_store_change;
209 X509_STORE *crl_store;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100210};
211
Michal Vasko7227f352016-02-01 12:11:40 +0100212/* ACCESS locked, separate locks */
Michal Vasko3031aae2016-01-27 16:07:18 +0100213struct nc_server_tls_opts {
Michal Vasko93224072021-11-09 12:14:28 +0100214 char *server_cert;
215 char **trusted_cert_lists;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100216 uint16_t trusted_cert_list_count;
Michal Vasko93224072021-11-09 12:14:28 +0100217 char *trusted_ca_file;
218 char *trusted_ca_dir;
Michal Vasko0457bb42016-01-08 15:49:13 +0100219 X509_STORE *crl_store;
220
Michal Vasko5e3f3392016-01-20 11:13:01 +0100221 struct nc_ctn {
Michal Vasko0457bb42016-01-08 15:49:13 +0100222 uint32_t id;
Michal Vasko93224072021-11-09 12:14:28 +0100223 char *fingerprint;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100224 NC_TLS_CTN_MAPTYPE map_type;
Michal Vasko93224072021-11-09 12:14:28 +0100225 char *name;
Michal Vasko5e3f3392016-01-20 11:13:01 +0100226 struct nc_ctn *next;
Michal Vasko0457bb42016-01-08 15:49:13 +0100227 } *ctn;
Michal Vasko0457bb42016-01-08 15:49:13 +0100228};
229
Radek Krejci53691be2016-02-22 13:58:37 +0100230#endif /* NC_ENABLED_TLS */
Radek Krejci43390242015-10-08 15:34:04 +0200231
Michal Vasko7227f352016-02-01 12:11:40 +0100232/* ACCESS unlocked */
Michal Vasko5f352c52019-07-10 16:12:06 +0200233struct nc_keepalives {
234 int enabled;
235 uint16_t idle_time;
236 uint16_t max_probes;
237 uint16_t probe_interval;
238};
239
240/* ACCESS unlocked */
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200241struct nc_server_unix_opts {
roman83683fb2023-02-24 09:15:23 +0100242 char *address;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200243 mode_t mode;
244 uid_t uid;
245 gid_t gid;
246};
247
romanc1d2b092023-02-02 08:58:27 +0100248struct nc_bind {
249 char *address;
250 uint16_t port;
251 int sock;
252 int pollin;
253};
254
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200255/* ACCESS unlocked */
Michal Vasko3031aae2016-01-27 16:07:18 +0100256struct nc_client_opts {
257 char *schema_searchpath;
Michal Vaskoa272e092023-07-10 14:34:03 +0200258 int auto_context_fill_disabled;
Radek Krejcifd5b6682017-06-13 15:52:53 +0200259 ly_module_imp_clb schema_clb;
260 void *schema_clb_data;
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200261 struct nc_keepalives ka;
Michal Vasko3031aae2016-01-27 16:07:18 +0100262
romanc1d2b092023-02-02 08:58:27 +0100263 struct nc_bind *ch_binds;
Michal Vaskob163b022022-09-07 11:55:46 +0200264
Michal Vasko9d4cca52022-09-07 11:19:57 +0200265 struct {
266 NC_TRANSPORT_IMPL ti;
267 char *hostname;
268 } *ch_binds_aux;
Michal Vasko3031aae2016-01-27 16:07:18 +0100269 uint16_t ch_bind_count;
270};
271
Radek Krejci62aa0642017-05-25 16:33:49 +0200272/* ACCESS unlocked */
273struct nc_client_context {
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200274 unsigned int refcount;
Radek Krejci62aa0642017-05-25 16:33:49 +0200275 struct nc_client_opts opts;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200276
Radek Krejci62aa0642017-05-25 16:33:49 +0200277#ifdef NC_ENABLED_SSH
278 struct nc_client_ssh_opts ssh_opts;
279 struct nc_client_ssh_opts ssh_ch_opts;
280#endif /* NC_ENABLED_SSH */
281#ifdef NC_ENABLED_TLS
282 struct nc_client_tls_opts tls_opts;
283 struct nc_client_tls_opts tls_ch_opts;
284#endif /* NC_ENABLED_TLS */
285};
286
Michal Vasko0457bb42016-01-08 15:49:13 +0100287struct nc_server_opts {
Michal Vasko7227f352016-02-01 12:11:40 +0100288 /* ACCESS unlocked */
romanc1d2b092023-02-02 08:58:27 +0100289 ATOMIC_T wd_basic_mode;
290 ATOMIC_T wd_also_supported;
Michal Vasko1440a742021-03-31 11:11:03 +0200291 uint32_t capabilities_count;
Michal Vasko93224072021-11-09 12:14:28 +0100292 char **capabilities;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200293
Michal Vasko1440a742021-03-31 11:11:03 +0200294 char *(*content_id_clb)(void *user_data);
295 void *content_id_data;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200296
Michal Vasko1440a742021-03-31 11:11:03 +0200297 void (*content_id_data_free)(void *data);
Michal Vasko0457bb42016-01-08 15:49:13 +0100298
Michal Vasko7227f352016-02-01 12:11:40 +0100299 /* ACCESS unlocked */
romanc1d2b092023-02-02 08:58:27 +0100300 ATOMIC_T hello_timeout;
301 ATOMIC_T idle_timeout;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200302
Michal Vaskoebba7602018-03-23 13:14:08 +0100303#ifdef NC_ENABLED_SSH
304 int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data);
305 void *passwd_auth_data;
306 void (*passwd_auth_data_free)(void *data);
bhart3bc2f582018-06-05 12:40:32 -0500307
308 int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data);
309 void *pubkey_auth_data;
310 void (*pubkey_auth_data_free)(void *data);
311
Michal Vasko1c2d2652023-10-17 08:53:36 +0200312 int (*interactive_auth_sess_clb)(const struct nc_session *session, ssh_session ssh_sess, ssh_message msg, void *user_data);
313 void *interactive_auth_sess_data;
314 void (*interactive_auth_sess_data_free)(void *data);
315
bhart1bb7cdb2018-07-02 15:03:30 -0500316 int (*interactive_auth_clb)(const struct nc_session *session, ssh_message msg, void *user_data);
bhart3bc2f582018-06-05 12:40:32 -0500317 void *interactive_auth_data;
318 void (*interactive_auth_data_free)(void *data);
Michal Vaskoebba7602018-03-23 13:14:08 +0100319#endif
Michal Vasko7060bcf2016-11-28 14:48:11 +0100320#ifdef NC_ENABLED_TLS
321 int (*user_verify_clb)(const struct nc_session *session);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100322
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200323 int (*server_cert_clb)(const char *name, void *user_data, char **cert_path, char **cert_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200324 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100325 void *server_cert_data;
326 void (*server_cert_data_free)(void *data);
327
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500328 int (*server_cert_chain_clb)(const char *name, void *user_data, char ***cert_paths, int *cert_path_count,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200329 char ***cert_data, int *cert_data_count);
Andrew Langefeld440b6c72018-08-27 16:26:20 -0500330 void *server_cert_chain_data;
331 void (*server_cert_chain_data_free)(void *data);
332
Michal Vasko4c1fb492017-01-30 14:31:07 +0100333 int (*trusted_cert_list_clb)(const char *name, void *user_data, char ***cert_paths, int *cert_path_count,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200334 char ***cert_data, int *cert_data_count);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100335 void *trusted_cert_list_data;
336 void (*trusted_cert_list_data_free)(void *data);
Michal Vasko7060bcf2016-11-28 14:48:11 +0100337#endif
Michal Vasko0457bb42016-01-08 15:49:13 +0100338
romanc1d2b092023-02-02 08:58:27 +0100339 pthread_rwlock_t config_lock;
roman45cec4e2023-02-17 10:21:39 +0100340 struct nc_keystore keystore; /**< store for keys/certificates */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100341
Michal Vasko3031aae2016-01-27 16:07:18 +0100342 struct nc_bind *binds;
343 struct nc_endpt {
Michal Vasko93224072021-11-09 12:14:28 +0100344 char *name;
romanc1d2b092023-02-02 08:58:27 +0100345 int changed;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200346 NC_TRANSPORT_IMPL ti;
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200347 struct nc_keepalives ka;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200348
Michal Vasko2e6defd2016-10-07 15:48:15 +0200349 union {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200350#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +0200351 struct nc_server_ssh_opts *ssh;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200352#endif
Michal Vasko6b151062016-08-22 16:20:47 +0200353#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +0200354 struct nc_server_tls_opts *tls;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200355#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200356 struct nc_server_unix_opts *unixsock;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200357 } opts;
Michal Vasko3031aae2016-01-27 16:07:18 +0100358 } *endpts;
359 uint16_t endpt_count;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200360
361 /* ACCESS locked, add/remove CH clients - WRITE lock ch_client_lock
362 * modify CH clients - READ lock ch_client_lock + ch_client_lock */
363 struct nc_ch_client {
Michal Vasko93224072021-11-09 12:14:28 +0100364 char *name;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200365 struct nc_ch_endpt {
Michal Vasko93224072021-11-09 12:14:28 +0100366 char *name;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200367 NC_TRANSPORT_IMPL ti;
Michal Vasko93224072021-11-09 12:14:28 +0100368 char *address;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200369 uint16_t port;
Frank Rimpler9f838b02018-07-25 06:44:03 +0000370 int sock_pending;
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200371 struct nc_keepalives ka;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200372
Michal Vaskoadf30f02019-06-24 09:34:47 +0200373 union {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200374#ifdef NC_ENABLED_SSH
Michal Vaskoadf30f02019-06-24 09:34:47 +0200375 struct nc_server_ssh_opts *ssh;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200376#endif
377#ifdef NC_ENABLED_TLS
Michal Vaskoadf30f02019-06-24 09:34:47 +0200378 struct nc_server_tls_opts *tls;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200379#endif
Michal Vaskoadf30f02019-06-24 09:34:47 +0200380 } opts;
381 } *ch_endpts;
382 uint16_t ch_endpt_count;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200383 NC_CH_CONN_TYPE conn_type;
Michal Vasko6ce8e822022-08-02 15:09:17 +0200384
Michal Vasko2e6defd2016-10-07 15:48:15 +0200385 union {
386 struct {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200387 uint16_t period;
388 time_t anchor_time;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200389 uint16_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200390 } period;
391 } conn;
392 NC_CH_START_WITH start_with;
393 uint8_t max_attempts;
Andrew Langefeld6ed922d2018-09-12 14:08:32 -0500394 uint32_t id;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200395 pthread_mutex_t lock;
396 } *ch_clients;
397 uint16_t ch_client_count;
398 pthread_rwlock_t ch_client_lock;
Michal Vaskob48aa812016-01-18 14:13:09 +0100399
Andrew Langefeld6ed922d2018-09-12 14:08:32 -0500400 /* Atomic IDs */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200401 ATOMIC_T new_session_id;
402 ATOMIC_T new_client_id;
Michal Vasko0457bb42016-01-08 15:49:13 +0100403};
404
Radek Krejci206fcd62015-10-07 15:42:48 +0200405/**
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200406 * Sleep time in usec to wait between nc_recv_notif() calls.
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100407 */
408#define NC_CLIENT_NOTIF_THREAD_SLEEP 10000
409
410/**
Michal Vasko0190bc32016-03-02 15:47:49 +0100411 * Timeout in msec for transport-related data to arrive (ssh_handle_key_exchange(), SSL_accept(), SSL_connect()).
Michal Vasko36c7be82017-02-22 13:37:59 +0100412 * It can be quite a lot on slow machines (waiting for TLS cert-to-name resolution, ...).
Michal Vasko0190bc32016-03-02 15:47:49 +0100413 */
Michal Vasko23edb632017-02-23 15:15:52 +0100414#define NC_TRANSPORT_TIMEOUT 10000
Michal Vasko36c7be82017-02-22 13:37:59 +0100415
416/**
417 * Timeout in msec for acquiring a lock of a session (used with a condition, so higher numbers could be required
418 * only in case of extreme concurrency).
419 */
420#define NC_SESSION_LOCK_TIMEOUT 500
Michal Vasko0190bc32016-03-02 15:47:49 +0100421
422/**
Michal Vaskof471fa02017-02-15 10:48:12 +0100423 * Timeout in msec for acquiring a lock of a session that is supposed to be freed.
424 */
Michal Vasko36c7be82017-02-22 13:37:59 +0100425#define NC_SESSION_FREE_LOCK_TIMEOUT 1000
Michal Vaskof471fa02017-02-15 10:48:12 +0100426
427/**
Michal Vasko2b768092018-02-12 16:37:12 +0100428 * Timeout in msec for a thread to wait for its turn to work with a pollsession structure.
Michal Vasko2b768092018-02-12 16:37:12 +0100429 */
Michal Vasko98a78032019-10-03 15:01:11 +0200430#define NC_PS_QUEUE_TIMEOUT 5000
Michal Vaskof471fa02017-02-15 10:48:12 +0100431
432/**
Michal Vasko1a22db42017-03-17 09:09:15 +0100433 * Time slept in msec if no endpoint was created for a running Call Home client.
Michal Vaskoade892d2017-02-22 13:40:35 +0100434 */
435#define NC_CH_NO_ENDPT_WAIT 1000
436
437/**
Michal Vasko056f53c2022-10-21 13:38:15 +0200438 * Timeout in msec for a Call Home socket to establish its connection.
439 */
440#define NC_CH_CONNECT_TIMEOUT 500
441
442/**
Michal Vasko80cad7f2015-12-08 14:42:27 +0100443 * Number of sockets kept waiting to be accepted.
444 */
Michal Vasko0a3f3752016-10-13 14:58:38 +0200445#define NC_REVERSE_QUEUE 5
Michal Vasko80cad7f2015-12-08 14:42:27 +0100446
447/**
Michal Vasko5c2f7952016-01-22 13:16:31 +0100448 * @brief Type of the session
Radek Krejci206fcd62015-10-07 15:42:48 +0200449 */
450typedef enum {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200451 NC_CLIENT, /**< client side */
452 NC_SERVER /**< server side */
Radek Krejci206fcd62015-10-07 15:42:48 +0200453} NC_SIDE;
454
455/**
456 * @brief Enumeration of the supported NETCONF protocol versions
457 */
458typedef enum {
Michal Vasko5c2f7952016-01-22 13:16:31 +0100459 NC_VERSION_10 = 0, /**< NETCONF 1.0 - RFC 4741, 4742 */
Radek Krejci206fcd62015-10-07 15:42:48 +0200460 NC_VERSION_11 = 1 /**< NETCONF 1.1 - RFC 6241, 6242 */
461} NC_VERSION;
462
463#define NC_VERSION_10_ENDTAG "]]>]]>"
464#define NC_VERSION_10_ENDTAG_LEN 6
465
466/**
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200467 * @brief Container to serialize RPC messages
Radek Krejci5686ff72015-10-09 13:33:56 +0200468 */
Michal Vaskoad611702015-12-03 13:41:51 +0100469struct nc_msg_cont {
Michal Vasko77367452021-02-16 16:32:18 +0100470 struct ly_in *msg;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200471 NC_MSG_TYPE type; /**< can be either NC_MSG_REPLY or NC_MSG_NOTIF */
Michal Vaskoad611702015-12-03 13:41:51 +0100472 struct nc_msg_cont *next;
Radek Krejci5686ff72015-10-09 13:33:56 +0200473};
474
475/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200476 * @brief NETCONF session structure
477 */
478struct nc_session {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200479 NC_STATUS status; /**< status of the session */
Michal Vasko428087d2016-01-14 16:04:28 +0100480 NC_SESSION_TERM_REASON term_reason; /**< reason of termination, if status is NC_STATUS_INVALID */
Michal Vasko142cfea2017-08-07 10:12:11 +0200481 uint32_t killed_by; /**< session responsible for termination, if term_reason is NC_SESSION_TERM_KILLED */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200482 NC_SIDE side; /**< side of the session: client or server */
Radek Krejci5686ff72015-10-09 13:33:56 +0200483
484 /* NETCONF data */
Radek Krejci206fcd62015-10-07 15:42:48 +0200485 uint32_t id; /**< NETCONF session ID (session-id-type) */
486 NC_VERSION version; /**< NETCONF protocol version */
Radek Krejci5686ff72015-10-09 13:33:56 +0200487
488 /* Transport implementation */
Radek Krejci206fcd62015-10-07 15:42:48 +0200489 NC_TRANSPORT_IMPL ti_type; /**< transport implementation type to select items from ti union */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200490 pthread_mutex_t *io_lock; /**< input/output lock, note that in case of libssh TI, it will be shared with
491 other NETCONF sessions on the same SSH session (but different SSH channel) */
Michal Vasko131120a2018-05-29 15:44:02 +0200492
Radek Krejci206fcd62015-10-07 15:42:48 +0200493 union {
494 struct {
495 int in; /**< input file descriptor */
496 int out; /**< output file descriptor */
Radek Krejci206fcd62015-10-07 15:42:48 +0200497 } fd; /**< NC_TI_FD transport implementation structure */
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200498 struct {
499 int sock; /**< socket file descriptor */
500 } unixsock; /**< NC_TI_UNIX transport implementation structure */
Radek Krejci53691be2016-02-22 13:58:37 +0100501#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +0200502 struct {
Radek Krejci206fcd62015-10-07 15:42:48 +0200503 ssh_channel channel;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200504 ssh_session session;
505 struct nc_session *next; /**< pointer to the next NETCONF session on the same
506 SSH session, but different SSH channel. If no such session exists, it is NULL.
507 otherwise there is a ring list of the NETCONF sessions */
Radek Krejci206fcd62015-10-07 15:42:48 +0200508 } libssh;
509#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100510#ifdef NC_ENABLED_TLS
Radek Krejci206fcd62015-10-07 15:42:48 +0200511 SSL *tls;
512#endif
Radek Krejci5686ff72015-10-09 13:33:56 +0200513 } ti; /**< transport implementation data */
Michal Vasko93224072021-11-09 12:14:28 +0100514 char *username;
515 char *host;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100516 uint16_t port;
Michal Vasko93224072021-11-09 12:14:28 +0100517 char *path; /**< socket path in case of unix socket */
Radek Krejci5686ff72015-10-09 13:33:56 +0200518
519 /* other */
520 struct ly_ctx *ctx; /**< libyang context of the session */
Michal Vasko2cc4c682016-03-01 09:16:48 +0100521 void *data; /**< arbitrary user data */
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200522 uint8_t flags; /**< various flags of the session */
Michal Vasko086311b2016-01-08 09:53:11 +0100523#define NC_SESSION_SHAREDCTX 0x01
Michal Vaskofeccb312022-03-24 15:24:59 +0100524#define NC_SESSION_CALLHOME 0x02 /**< session is Call Home and ch_lock is initialized */
525#define NC_SESSION_CH_THREAD 0x04 /**< protected by ch_lock */
Radek Krejci5686ff72015-10-09 13:33:56 +0200526
Michal Vasko2e6defd2016-10-07 15:48:15 +0200527 union {
528 struct {
Radek Krejcif637ba82016-10-18 14:18:14 +0200529 /* client side only data */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200530 uint64_t msgid;
Michal Vasko2cb24b42017-05-23 14:59:11 +0200531 char **cpblts; /**< list of server's capabilities on client side */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200532 pthread_mutex_t msgs_lock; /**< lock for the msgs buffer */
533 struct nc_msg_cont *msgs; /**< queue for messages received of different type than expected */
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200534 ATOMIC_T ntf_thread_count; /**< number of running notification threads */
535 ATOMIC_T ntf_thread_running; /**< flag whether there are notification threads for this session running or not */
Michal Vasko78939072022-12-12 07:43:18 +0100536 struct lyd_node *ext_data; /**< LY ext data used in the context callback */
Michal Vaskoeee99412016-11-21 10:19:43 +0100537
538 /* client flags */
539 /* some server modules failed to load so the data from them will be ignored - not use strict flag for parsing */
Michal Vaskofeccb312022-03-24 15:24:59 +0100540# define NC_SESSION_CLIENT_NOT_STRICT 0x08
Michal Vasko2e6defd2016-10-07 15:48:15 +0200541 } client;
542 struct {
Radek Krejcif637ba82016-10-18 14:18:14 +0200543 /* server side only data */
Michal Vasko9f6275e2017-10-05 13:50:05 +0200544 time_t session_start; /**< real time the session was created */
545 time_t last_rpc; /**< monotonic time (seconds) the last RPC was received on this session */
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200546
547 pthread_mutex_t ntf_status_lock; /**< lock for ntf_status */
548 uint32_t ntf_status; /**< flag (count) whether the session is subscribed to notifications */
Michal Vasko131120a2018-05-29 15:44:02 +0200549
Michal Vaskoacf98472021-02-04 15:33:57 +0100550 pthread_mutex_t rpc_lock; /**< lock indicating RPC processing, this lock is always locked before io_lock!! */
551 pthread_cond_t rpc_cond; /**< RPC condition (tied with rpc_lock and rpc_inuse) */
552 int rpc_inuse; /**< variable indicating whether there is RPC being processed or not (tied with
Michal Vasko131120a2018-05-29 15:44:02 +0200553 rpc_cond and rpc_lock) */
554
Michal Vaskoacf98472021-02-04 15:33:57 +0100555 pthread_mutex_t ch_lock; /**< Call Home thread lock */
556 pthread_cond_t ch_cond; /**< Call Home thread condition */
Michal Vasko086311b2016-01-08 09:53:11 +0100557
Michal Vaskoeee99412016-11-21 10:19:43 +0100558 /* server flags */
Radek Krejci53691be2016-02-22 13:58:37 +0100559#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +0200560 /* SSH session authenticated */
Michal Vaskofeccb312022-03-24 15:24:59 +0100561# define NC_SESSION_SSH_AUTHENTICATED 0x10
Michal Vasko2e6defd2016-10-07 15:48:15 +0200562 /* netconf subsystem requested */
Michal Vaskofeccb312022-03-24 15:24:59 +0100563# define NC_SESSION_SSH_SUBSYS_NETCONF 0x20
Michal Vasko2e6defd2016-10-07 15:48:15 +0200564 uint16_t ssh_auth_attempts; /**< number of failed SSH authentication attempts */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100565#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100566#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +0200567 X509 *client_cert; /**< TLS client certificate if used for authentication */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100568#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +0200569 } server;
570 } opts;
Radek Krejci206fcd62015-10-07 15:42:48 +0200571};
572
Michal Vaskoe4300a82017-05-24 10:35:42 +0200573enum nc_ps_session_state {
574 NC_PS_STATE_NONE = 0, /**< session is not being worked with */
575 NC_PS_STATE_BUSY, /**< session is being polled or communicated on (and locked) */
576 NC_PS_STATE_INVALID /**< session is invalid and was already returned by another poll */
577};
578
fanchanghu3d4e7212017-08-09 09:42:30 +0800579struct nc_ps_session {
580 struct nc_session *session;
581 enum nc_ps_session_state state;
582};
583
Michal Vasko48a63ed2016-03-01 09:48:21 +0100584/* ACCESS locked */
Michal Vaskofb89d772016-01-08 12:25:35 +0100585struct nc_pollsession {
fanchanghu3d4e7212017-08-09 09:42:30 +0800586 struct nc_ps_session **sessions;
Michal Vaskofb89d772016-01-08 12:25:35 +0100587 uint16_t session_count;
Michal Vasko99f251b2017-01-11 11:31:46 +0100588 uint16_t last_event_session;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200589
590 pthread_cond_t cond;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100591 pthread_mutex_t lock;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200592 uint8_t queue[NC_PS_QUEUE_SIZE]; /**< round buffer, queue is empty when queue_len == 0 */
593 uint8_t queue_begin; /**< queue starts on queue[queue_begin] */
Michal Vasko74c345f2018-02-07 10:37:11 +0100594 uint8_t queue_len; /**< queue ends on queue[(queue_begin + queue_len - 1) % NC_PS_QUEUE_SIZE] */
Michal Vaskofb89d772016-01-08 12:25:35 +0100595};
596
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100597struct nc_ntf_thread_arg {
598 struct nc_session *session;
Michal Vaskoffb35e92022-10-20 09:07:25 +0200599 nc_notif_dispatch_clb notif_clb;
600 void *user_data;
Michal Vasko743ff9f2022-10-20 10:03:13 +0200601
Michal Vaskoffb35e92022-10-20 09:07:25 +0200602 void (*free_data)(void *);
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100603};
604
roman41a11e42022-06-22 09:27:08 +0200605#ifdef NC_ENABLED_SSH
606
607/**
608 * @brief PAM callback arguments.
609 */
610struct nc_pam_thread_arg {
611 ssh_message msg; /**< libssh message */
612 struct nc_session *session; /**< NETCONF session */
romanc1d2b092023-02-02 08:58:27 +0100613 struct nc_server_ssh_opts *opts; /**< SSH server opts */
roman41a11e42022-06-22 09:27:08 +0200614};
615
616#endif
617
Michal Vasko4eb3c312016-03-01 14:09:37 +0100618void *nc_realloc(void *ptr, size_t size);
619
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +0200620struct passwd *nc_getpwuid(uid_t uid, struct passwd *pwd_buf, char **buf, size_t *buf_size);
621
Michal Vasko131120a2018-05-29 15:44:02 +0200622NC_MSG_TYPE nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op);
Michal Vasko086311b2016-01-08 09:53:11 +0100623
roman6ece9c52022-06-22 09:29:17 +0200624/**
Michal Vaskod8a74192023-02-06 15:51:50 +0100625 * @brief Get current clock (uses COMPAT_CLOCK_ID) time with an offset.
roman6ece9c52022-06-22 09:29:17 +0200626 *
Michal Vaskod8a74192023-02-06 15:51:50 +0100627 * @param[out] ts Current time offset by @p add_ms.
628 * @param[in] add_ms Number of milliseconds to add.
roman6ece9c52022-06-22 09:29:17 +0200629 */
Michal Vaskod8a74192023-02-06 15:51:50 +0100630void nc_timeouttime_get(struct timespec *ts, uint32_t add_ms);
Michal Vaskoe852c8b2017-10-05 10:02:20 +0200631
roman6ece9c52022-06-22 09:29:17 +0200632/**
Michal Vaskod8a74192023-02-06 15:51:50 +0100633 * @brief Get time difference based on the current time (uses COMPAT_CLOCK_ID).
roman6ece9c52022-06-22 09:29:17 +0200634 *
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200635 * @param[in] ts Timespec structure holding real time from which the current time is going to be subtracted.
roman6ece9c52022-06-22 09:29:17 +0200636 * @return Time difference in milliseconds.
637 */
Michal Vaskod8a74192023-02-06 15:51:50 +0100638int32_t nc_timeouttime_cur_diff(const struct timespec *ts);
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200639
640/**
Michal Vaskod8a74192023-02-06 15:51:50 +0100641 * @brief Get current CLOCK_REALTIME time.
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200642 *
Michal Vaskod8a74192023-02-06 15:51:50 +0100643 * @param[out] ts Current real time.
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200644 */
Michal Vaskod8a74192023-02-06 15:51:50 +0100645void nc_realtime_get(struct timespec *ts);
Michal Vasko99f251b2017-01-11 11:31:46 +0100646
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200647const char *nc_keytype2str(NC_SSH_KEY_TYPE type);
Michal Vaskoddce1212019-05-24 09:58:49 +0200648
romanc1d2b092023-02-02 08:58:27 +0100649int nc_sock_configure_keepalive(int sock, struct nc_keepalives *ka);
Michal Vaskobe52dc22018-10-17 09:28:17 +0200650
Michal Vasko131120a2018-05-29 15:44:02 +0200651struct nc_session *nc_new_session(NC_SIDE side, int shared_ti);
Michal Vaskoade892d2017-02-22 13:40:35 +0100652
Michal Vasko131120a2018-05-29 15:44:02 +0200653int nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100654
Michal Vasko131120a2018-05-29 15:44:02 +0200655int nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func);
656
Michal Vasko01130bd2021-08-26 11:47:38 +0200657/**
658 * @brief Lock IO lock on a session.
659 *
660 * @param[in] session Session to lock.
661 * @param[in] timeout Timeout in msec to use.
662 * @param[in] func Caller function for logging.
663 * @return 1 on success;
664 * @return 0 on timeout;
665 * @return -1 on error.
666 */
Michal Vasko131120a2018-05-29 15:44:02 +0200667int nc_session_io_lock(struct nc_session *session, int timeout, const char *func);
668
Michal Vasko01130bd2021-08-26 11:47:38 +0200669/**
670 * @brief Unlock IO lock on a session.
671 *
672 * @param[in] session Session to unlock.
673 * @param[in] func Caller function for logging.
674 * @return 1 on success;
675 * @return -1 on error.
676 */
Michal Vasko131120a2018-05-29 15:44:02 +0200677int nc_session_io_unlock(struct nc_session *session, const char *func);
Michal Vasko086311b2016-01-08 09:53:11 +0100678
Michal Vasko01130bd2021-08-26 11:47:38 +0200679/**
680 * @brief Lock MSGS lock on a session.
681 *
682 * @param[in] session Session to lock.
683 * @param[in,out] timeout Timeout in msec to use. If positive and on successful lock, is updated based on what was elapsed.
684 * @param[in] func Caller function for logging.
685 * @return 1 on success;
686 * @return 0 on timeout;
687 * @return -1 on error.
688 */
689int nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func);
690
691/**
692 * @brief Unlock MSGS lock on a session.
693 *
694 * @param[in] session Session to unlock.
695 * @param[in] func Caller function for logging.
696 * @return 1 on success;
697 * @return -1 on error.
698 */
699int nc_session_client_msgs_unlock(struct nc_session *session, const char *func);
700
Michal Vasko227f8ff2016-07-26 14:08:59 +0200701int nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func);
Michal Vaskof04a52a2016-04-07 10:52:10 +0200702
Michal Vasko227f8ff2016-07-26 14:08:59 +0200703int nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func);
Michal Vaskof04a52a2016-04-07 10:52:10 +0200704
Michal Vasko78939072022-12-12 07:43:18 +0100705int nc_client_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx);
706
Radek Krejci206fcd62015-10-07 15:42:48 +0200707/**
Michal Vaskoc446a382021-06-18 08:54:05 +0200708 * @brief Fill libyang context in @p session. Context models are based on the stored session
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100709 * capabilities. If the server does not support \<get-schema\>, the models are searched
Radek Krejcic9a6d252016-03-04 14:50:34 +0100710 * for in the directory set using nc_client_schema_searchpath().
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100711 *
712 * @param[in] session Session to create the context for.
Michal Vaskoef578332016-01-25 13:20:09 +0100713 * @return 0 on success, 1 on some missing schemas, -1 on error.
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100714 */
Michal Vasko57eb9402015-12-08 14:38:12 +0100715int nc_ctx_check_and_fill(struct nc_session *session);
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100716
717/**
Michal Vaskoc446a382021-06-18 08:54:05 +0200718 * @brief Perform NETCONF handshake on @p session.
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100719 *
720 * @param[in] session NETCONF session to use.
Michal Vasko71090fc2016-05-24 16:37:28 +0200721 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message parsing fail
722 * (server-side only), NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other error.
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100723 */
Michal Vasko131120a2018-05-29 15:44:02 +0200724NC_MSG_TYPE nc_handshake_io(struct nc_session *session);
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100725
726/**
Michal Vaskof05562c2016-01-20 12:06:43 +0100727 * @brief Create a socket connection.
728 *
729 * @param[in] host Hostname to connect to.
730 * @param[in] port Port to connect on.
Michal Vasko056f53c2022-10-21 13:38:15 +0200731 * @param[in] timeout_ms Timeout in ms for blocking the connect + select call (-1 for infinite).
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200732 * @param[in] ka Keepalives parameters.
Michal Vasko056f53c2022-10-21 13:38:15 +0200733 * @param[in,out] sock_pending Previous pending socket. If set, equal to -1, and the connection is still in progress
734 * after @p timeout, it is set to the pending socket but -1 is returned. If NULL, the socket is closed on timeout.
Michal Vasko66032bc2019-01-22 15:03:12 +0100735 * @param[out] ip_host Optional parameter with string IP address of the connected host.
Michal Vaskof05562c2016-01-20 12:06:43 +0100736 * @return Connected socket or -1 on error.
737 */
Michal Vasko056f53c2022-10-21 13:38:15 +0200738int nc_sock_connect(const char *host, uint16_t port, int timeout_ms, struct nc_keepalives *ka, int *sock_pending,
739 char **ip_host);
Michal Vaskof05562c2016-01-20 12:06:43 +0100740
741/**
742 * @brief Accept a new socket connection.
Michal Vasko80cad7f2015-12-08 14:42:27 +0100743 *
Michal Vaskoc61c4492016-01-25 11:13:34 +0100744 * @param[in] sock Listening socket.
Michal Vaskof05562c2016-01-20 12:06:43 +0100745 * @param[in] timeout Timeout in milliseconds.
746 * @param[out] peer_host Host the new connection was initiated from. Can be NULL.
747 * @param[out] peer_port Port the new connection is connected on. Can be NULL.
Michal Vasko80cad7f2015-12-08 14:42:27 +0100748 * @return Connected socket with the new connection, -1 on error.
749 */
Michal Vaskoc61c4492016-01-25 11:13:34 +0100750int nc_sock_accept(int sock, int timeout, char **peer_host, uint16_t *peer_port);
Michal Vasko80cad7f2015-12-08 14:42:27 +0100751
Michal Vasko1a38c862016-01-15 15:50:07 +0100752/**
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200753 * @brief Create a listening socket (AF_INET or AF_INET6).
Michal Vasko1a38c862016-01-15 15:50:07 +0100754 *
755 * @param[in] address IP address to listen on.
756 * @param[in] port Port to listen on.
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200757 * @param[in] ka Keepalives parameters.
Michal Vasko1a38c862016-01-15 15:50:07 +0100758 * @return Listening socket, -1 on error.
759 */
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200760int nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200761
762/**
763 * @brief Create a listening socket (AF_UNIX).
764 *
roman83683fb2023-02-24 09:15:23 +0100765 * @param[in] opts The server options (unix permissions and address of the socket).
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200766 * @return Listening socket, -1 on error.
767 */
roman83683fb2023-02-24 09:15:23 +0100768int nc_sock_listen_unix(const struct nc_server_unix_opts *opts);
Michal Vaskofb89d772016-01-08 12:25:35 +0100769
Michal Vasko1a38c862016-01-15 15:50:07 +0100770/**
771 * @brief Accept a new connection on a listening socket.
772 *
773 * @param[in] binds Structure with the listening sockets.
Michal Vaskoc446a382021-06-18 08:54:05 +0200774 * @param[in] bind_count Number of @p binds.
Michal Vasko1a38c862016-01-15 15:50:07 +0100775 * @param[in] timeout Timeout for accepting.
Michal Vasko1a38c862016-01-15 15:50:07 +0100776 * @param[out] host Host of the remote peer. Can be NULL.
777 * @param[out] port Port of the new connection. Can be NULL.
Michal Vasko3031aae2016-01-27 16:07:18 +0100778 * @param[out] idx Index of the bind that was accepted. Can be NULL.
Michal Vasko1a38c862016-01-15 15:50:07 +0100779 * @return Accepted socket of the new connection, -1 on error.
780 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100781int nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx);
782
783/**
Michal Vaskoda514772016-02-01 11:32:01 +0100784 * @brief Lock endpoint structures for reading and the specific endpoint.
785 *
786 * @param[in] name Name of the endpoint.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200787 * @param[in] ti Expected transport.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200788 * @param[out] idx Index of the endpoint. Optional.
Michal Vaskoda514772016-02-01 11:32:01 +0100789 * @return Endpoint structure.
790 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100791struct nc_endpt *nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100792
Michal Vaskoda514772016-02-01 11:32:01 +0100793/**
Michal Vaskoadf30f02019-06-24 09:34:47 +0200794 * @brief Lock CH client structures for reading and lock the specific client.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200795 *
796 * @param[in] name Name of the CH client.
Michal Vaskoadf30f02019-06-24 09:34:47 +0200797 * @param[in] endpt_name Endpoint of the CH client.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200798 * @param[in] ti Expected transport.
Michal Vaskoadf30f02019-06-24 09:34:47 +0200799 * @param[out] client_p Pointer to the CH client.
800 * @return CH endpoint structure.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200801 */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200802struct nc_ch_endpt *nc_server_ch_client_lock(const char *name, const char *endpt_name, NC_TRANSPORT_IMPL ti,
803 struct nc_ch_client **client_p);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200804
805/**
806 * @brief Unlock CH client strcutures and the specific client.
807 *
808 * @param[in] endpt Locked CH client structure.
809 */
810void nc_server_ch_client_unlock(struct nc_ch_client *client);
811
812/**
Michal Vaskoda514772016-02-01 11:32:01 +0100813 * @brief Add a client Call Home bind, listen on it.
814 *
815 * @param[in] address Address to bind to.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200816 * @param[in] port Port to bind to.
Michal Vasko9d4cca52022-09-07 11:19:57 +0200817 * @param[in] hostname Expected server hostname, may be NULL.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200818 * @param[in] ti Transport to use.
Michal Vaskoda514772016-02-01 11:32:01 +0100819 * @return 0 on success, -1 on error.
820 */
Michal Vasko9d4cca52022-09-07 11:19:57 +0200821int nc_client_ch_add_bind_listen(const char *address, uint16_t port, const char *hostname, NC_TRANSPORT_IMPL ti);
Michal Vasko3031aae2016-01-27 16:07:18 +0100822
Michal Vaskoda514772016-02-01 11:32:01 +0100823/**
824 * @brief Remove a client Call Home bind, stop listening on it.
825 *
826 * @param[in] address Address of the bind. NULL matches any address.
827 * @param[in] port Port of the bind. 0 matches all ports.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200828 * @param[in] ti Transport of the bind. 0 matches all transports.
Michal Vaskoda514772016-02-01 11:32:01 +0100829 * @return 0 on success, -1 on no matches found.
830 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100831int nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti);
832
Michal Vaskoda514772016-02-01 11:32:01 +0100833/**
834 * @brief Connect to a listening NETCONF client using Call Home.
835 *
836 * @param[in] host Hostname to connect to.
837 * @param[in] port Port to connect to.
838 * @param[in] ti Transport fo the connection.
Michal Vaskoda514772016-02-01 11:32:01 +0100839 * @param[out] session New Call Home session.
Michal Vasko71090fc2016-05-24 16:37:28 +0200840 * @return NC_MSG_HELLO on success, NC_MSG_BAD_HELLO on client \<hello\> message
841 * parsing fail, NC_MSG_WOULDBLOCK on timeout, NC_MSG_ERROR on other errors.
Michal Vaskoda514772016-02-01 11:32:01 +0100842 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200843NC_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 +0100844
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100845void nc_init(void);
846
847void nc_destroy(void);
848
Radek Krejci53691be2016-02-22 13:58:37 +0100849#ifdef NC_ENABLED_SSH
Michal Vasko9e036d52016-01-08 10:49:26 +0100850
Michal Vaskoda514772016-02-01 11:32:01 +0100851/**
852 * @brief Accept a server Call Home connection on a socket.
853 *
854 * @param[in] sock Socket with a new connection.
855 * @param[in] host Hostname of the server.
856 * @param[in] port Port of the server.
857 * @param[in] ctx Context for the session. Can be NULL.
Michal Vasko0190bc32016-03-02 15:47:49 +0100858 * @param[in] timeout Transport operations timeout in msec.
Michal Vaskoda514772016-02-01 11:32:01 +0100859 * @return New session, NULL on error.
860 */
Michal Vasko0190bc32016-03-02 15:47:49 +0100861struct 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 +0100862
Michal Vasko1a38c862016-01-15 15:50:07 +0100863/**
864 * @brief Establish SSH transport on a socket.
865 *
866 * @param[in] session Session structure of the new connection.
867 * @param[in] sock Socket of the new connection.
Michal Vasko0190bc32016-03-02 15:47:49 +0100868 * @param[in] timeout Transport operations timeout in msec (not SSH authentication one).
Michal Vasko1a38c862016-01-15 15:50:07 +0100869 * @return 1 on success, 0 on timeout, -1 on error.
870 */
romanc1d2b092023-02-02 08:58:27 +0100871int nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout);
Michal Vasko9e036d52016-01-08 10:49:26 +0100872
Michal Vasko96164bf2016-01-21 15:41:58 +0100873/**
romanc1d2b092023-02-02 08:58:27 +0100874 * @brief Process a SSH message.
Michal Vasko96164bf2016-01-21 15:41:58 +0100875 *
romanc1d2b092023-02-02 08:58:27 +0100876 * @param[in] session Session structure of the connection.
877 * @param[in] opts Endpoint SSH options on which the session was created.
Michal Vasko96164bf2016-01-21 15:41:58 +0100878 * @param[in] msg SSH message itself.
romand745f752023-03-24 15:53:12 +0100879 * @param[in] state State of the authentication.
Michal Vasko96164bf2016-01-21 15:41:58 +0100880 * @return 0 if the message was handled, 1 if it is left up to libssh.
881 */
romand745f752023-03-24 15:53:12 +0100882int nc_session_ssh_msg(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg, struct nc_auth_state *state);
Michal Vasko96164bf2016-01-21 15:41:58 +0100883
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100884void nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100885
Michal Vaskob7558c52016-02-26 15:04:19 +0100886void nc_client_ssh_destroy_opts(void);
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400887void _nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts);
Michal Vaskob7558c52016-02-26 15:04:19 +0100888
Radek Krejci53691be2016-02-22 13:58:37 +0100889#endif /* NC_ENABLED_SSH */
Michal Vasko9e036d52016-01-08 10:49:26 +0100890
Radek Krejci53691be2016-02-22 13:58:37 +0100891#ifdef NC_ENABLED_TLS
Michal Vasko9e036d52016-01-08 10:49:26 +0100892
Michal Vasko9d4cca52022-09-07 11:19:57 +0200893struct nc_session *nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx,
894 int timeout, const char *peername);
Michal Vasko3031aae2016-01-27 16:07:18 +0100895
Michal Vasko1a38c862016-01-15 15:50:07 +0100896/**
897 * @brief Establish TLS transport on a socket.
898 *
899 * @param[in] session Session structure of the new connection.
900 * @param[in] sock Socket of the new connection.
Michal Vasko0190bc32016-03-02 15:47:49 +0100901 * @param[in] timeout Transport operations timeout in msec.
Michal Vasko1a38c862016-01-15 15:50:07 +0100902 * @return 1 on success, 0 on timeout, -1 on error.
903 */
Michal Vasko0190bc32016-03-02 15:47:49 +0100904int nc_accept_tls_session(struct nc_session *session, int sock, int timeout);
Michal Vasko9e036d52016-01-08 10:49:26 +0100905
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100906void nc_server_tls_clear_opts(struct nc_server_tls_opts *opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100907
Michal Vaskob7558c52016-02-26 15:04:19 +0100908void nc_client_tls_destroy_opts(void);
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400909void _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts);
Michal Vaskob7558c52016-02-26 15:04:19 +0100910
Radek Krejci53691be2016-02-22 13:58:37 +0100911#endif /* NC_ENABLED_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100912
Michal Vasko80cad7f2015-12-08 14:42:27 +0100913/**
Radek Krejci206fcd62015-10-07 15:42:48 +0200914 * Functions
915 * - io.c
916 */
917
918/**
919 * @brief Read message from the wire.
920 *
921 * Accepts hello, rpc, rpc-reply and notification. Received string is transformed into
922 * libyang XML tree and the message type is detected from the top level element.
923 *
924 * @param[in] session NETCONF session from which the message is being read.
Michal Vasko131120a2018-05-29 15:44:02 +0200925 * @param[in] io_timeout Timeout in milliseconds. Negative value means infinite timeout,
Radek Krejci206fcd62015-10-07 15:42:48 +0200926 * zero value causes to return immediately.
Michal Vasko77367452021-02-16 16:32:18 +0100927 * @param[out] msg Input handled with the NETCONF message (application layer data).
928 * @return 1 on success.
929 * @return 0 on timeout.
930 * @return -1 on error.
931 * @return -2 on malformed message error.
Radek Krejci206fcd62015-10-07 15:42:48 +0200932 */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200933int nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct ly_in **msg);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100934
935/**
Michal Vasko77367452021-02-16 16:32:18 +0100936 * @brief Read a message from the wire.
Michal Vasko05ba9df2016-01-13 14:40:27 +0100937 *
938 * @param[in] session NETCONF session from which the message is being read.
Michal Vasko131120a2018-05-29 15:44:02 +0200939 * @param[in] io_timeout Timeout in milliseconds. Negative value means infinite timeout,
940 * zero value causes to return immediately.
Michal Vasko77367452021-02-16 16:32:18 +0100941 * @param[out] msg Input handled with the NETCONF message (application layer data).
Michal Vaskoc446a382021-06-18 08:54:05 +0200942 * @param[in] passing_io_lock True if @p session IO lock is already held. This function always unlocks
Michal Vasko131120a2018-05-29 15:44:02 +0200943 * it before returning!
Michal Vasko77367452021-02-16 16:32:18 +0100944 * @return 1 on success.
945 * @return 0 on timeout.
946 * @return -1 on error.
947 * @return -2 on malformed message error.
Michal Vasko05ba9df2016-01-13 14:40:27 +0100948 */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200949int nc_read_msg_io(struct nc_session *session, int io_timeout, struct ly_in **msg, int passing_io_lock);
Radek Krejci206fcd62015-10-07 15:42:48 +0200950
Radek Krejcife0b3472015-10-12 13:43:42 +0200951/**
952 * @brief Write message into wire.
953 *
954 * @param[in] session NETCONF session to which the message will be written.
Michal Vasko131120a2018-05-29 15:44:02 +0200955 * @param[in] io_timeout Timeout in milliseconds. Negative value means infinite timeout,
956 * zero value causes to return immediately.
Radek Krejci127f8952016-10-12 14:57:16 +0200957 * @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 +0200958 * specific additional parameters are required or accepted:
959 * - #NC_MSG_RPC
960 * - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
Michal Vasko77367452021-02-16 16:32:18 +0100961 * - `const char *attrs;` - additional attributes to be added into the \<rpc/\> element. Required parameter.
Radek Krejcife0b3472015-10-12 13:43:42 +0200962 * - #NC_MSG_REPLY
Michal Vasko77367452021-02-16 16:32:18 +0100963 * - `struct lyd_node_opaq *rpc_envp;` - parsed envelopes of the RPC to reply to. Required parameter.
Michal Vasko05ba9df2016-01-13 14:40:27 +0100964 * - `struct nc_server_reply *reply;` - RPC reply. Required parameter.
Radek Krejcife0b3472015-10-12 13:43:42 +0200965 * - #NC_MSG_NOTIF
Michal Vasko131120a2018-05-29 15:44:02 +0200966 * - `struct nc_server_notif *notif;` - notification object. Required parameter.
967 * - #NC_MSG_HELLO
968 * - `const char **capabs;` - capabilities array ended with NULL. Required parameter.
969 * - `uint32_t *sid;` - session ID to be included in the hello message. Optional parameter.
970 *
971 * @return Type of the written message. #NC_MSG_WOULDBLOCK is returned if timeout is positive
972 * (or zero) value and IO lock could not be acquired in that time. #NC_MSG_ERROR is
973 * returned on error and #NC_MSG_NONE is never returned by this function.
Radek Krejcife0b3472015-10-12 13:43:42 +0200974 */
Michal Vasko131120a2018-05-29 15:44:02 +0200975NC_MSG_TYPE nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...);
Radek Krejcife0b3472015-10-12 13:43:42 +0200976
Michal Vasko1a38c862016-01-15 15:50:07 +0100977/**
978 * @brief Check whether a session is still connected (on transport layer).
979 *
980 * @param[in] session Session to check.
981 * @return 1 if connected, 0 if not.
982 */
Michal Vasko428087d2016-01-14 16:04:28 +0100983int nc_session_is_connected(struct nc_session *session);
984
Radek Krejci206fcd62015-10-07 15:42:48 +0200985#endif /* NC_SESSION_PRIVATE_H_ */