blob: a533477f67e47b9d1b9c816e1246cc1bfbb92523 [file] [log] [blame]
Radek Krejci43390242015-10-08 15:34:04 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskodc96bb92023-03-28 08:52:48 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko95ea9ff2021-11-09 12:29:14 +01005 * @brief libnetconf2 session manipulation
Radek Krejci43390242015-10-08 15:34:04 +02006 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01007 * @copyright
Michal Vaskodc96bb92023-03-28 08:52:48 +02008 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Radek Krejci43390242015-10-08 15:34:04 +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 Krejci43390242015-10-08 15:34:04 +020015 */
16
17#ifndef NC_SESSION_H_
18#define NC_SESSION_H_
19
Michal Vaskoc09730e2019-01-17 10:07:26 +010020#ifdef __cplusplus
21extern "C" {
22#endif
23
Radek Krejci6e36bfb2016-12-01 21:40:16 +010024#include "netconf.h"
25
Radek Krejci53691be2016-02-22 13:58:37 +010026#ifdef NC_ENABLED_SSH
Michal Vasko4ef14932015-12-04 11:09:10 +010027
Michal Vaskof0537d82016-01-29 14:42:38 +010028/**
29 * @brief Enumeration of NETCONF SSH authentication methods
30 */
Michal Vasko4ef14932015-12-04 11:09:10 +010031typedef enum {
Michal Vaskof0537d82016-01-29 14:42:38 +010032 NC_SSH_AUTH_PUBLICKEY = 0x01, /**< publickey SSH authentication */
33 NC_SSH_AUTH_PASSWORD = 0x02, /**< password SSH authentication */
34 NC_SSH_AUTH_INTERACTIVE = 0x04 /**< interactive SSH authentication */
Michal Vasko4ef14932015-12-04 11:09:10 +010035} NC_SSH_AUTH_TYPE;
36
Radek Krejci53691be2016-02-22 13:58:37 +010037#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020038
Radek Krejci53691be2016-02-22 13:58:37 +010039#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +010040
Michal Vaskof0537d82016-01-29 14:42:38 +010041/**
42 * @brief Enumeration of cert-to-name mapping types
43 */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044typedef enum {
Michal Vaskof0537d82016-01-29 14:42:38 +010045 NC_TLS_CTN_UNKNOWN = 0, /**< unknown mapping */
46 NC_TLS_CTN_SPECIFIED, /**< username explicitly specified */
47 NC_TLS_CTN_SAN_RFC822_NAME, /**< email address as username */
48 NC_TLS_CTN_SAN_DNS_NAME, /**< DNS name as username */
49 NC_TLS_CTN_SAN_IP_ADDRESS, /**< IP address as username */
50 NC_TLS_CTN_SAN_ANY, /**< any certificate Subject Alternative Name as username */
51 NC_TLS_CTN_COMMON_NAME /**< common name as username */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010052} NC_TLS_CTN_MAPTYPE;
53
Radek Krejci53691be2016-02-22 13:58:37 +010054#endif /* NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010055
Radek Krejci43390242015-10-08 15:34:04 +020056/**
Radek Krejci695d4fa2015-10-22 13:23:54 +020057 * @brief Enumeration of possible session statuses
58 */
59typedef enum {
Radek Krejci465308c2017-05-22 14:49:10 +020060 NC_STATUS_ERR = -1, /**< error return code for function getting the session status */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010061 NC_STATUS_STARTING = 0, /**< session is not yet fully initiated */
62 NC_STATUS_CLOSING, /**< session is being closed */
Michal Vasko428087d2016-01-14 16:04:28 +010063 NC_STATUS_INVALID, /**< session is not running and is supposed to be closed (nc_session_free()) */
Michal Vasko38a7c6c2015-12-04 12:29:20 +010064 NC_STATUS_RUNNING /**< up and running */
Radek Krejci695d4fa2015-10-22 13:23:54 +020065} NC_STATUS;
66
67/**
Michal Vasko38a7c6c2015-12-04 12:29:20 +010068 * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol)
69 */
70typedef enum {
71 NC_TI_NONE = 0, /**< none - session is not connected yet */
72 NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented
73 outside the current application */
Olivier Matzac7fa2f2018-10-11 10:02:04 +020074 NC_TI_UNIX, /**< unix socket */
Radek Krejci53691be2016-02-22 13:58:37 +010075#ifdef NC_ENABLED_SSH
Michal Vasko38a7c6c2015-12-04 12:29:20 +010076 NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */
77#endif
Radek Krejci53691be2016-02-22 13:58:37 +010078#ifdef NC_ENABLED_TLS
Michal Vasko38a7c6c2015-12-04 12:29:20 +010079 NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */
80#endif
81} NC_TRANSPORT_IMPL;
82
83/**
Michal Vasko2e6defd2016-10-07 15:48:15 +020084 * @brief Enumeration of Call Home connection types.
85 */
86typedef enum {
87 NC_CH_CT_NOT_SET = 0,
88 NC_CH_PERSIST,
89 NC_CH_PERIOD
90} NC_CH_CONN_TYPE;
91
92/**
93 * @brief Enumeration of Call Home client priority policy.
94 */
95typedef enum {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020096 NC_CH_FIRST_LISTED = 0, // default
Michal Vaskoe49a15f2019-05-27 14:18:36 +020097 NC_CH_LAST_CONNECTED,
98 NC_CH_RANDOM
Michal Vasko2e6defd2016-10-07 15:48:15 +020099} NC_CH_START_WITH;
100
101/**
Michal Vasko17dfda92016-12-01 14:06:16 +0100102 * @brief Enumeration of SSH key types.
103 */
104typedef enum {
105 NC_SSH_KEY_UNKNOWN = 0,
106 NC_SSH_KEY_DSA,
107 NC_SSH_KEY_RSA,
108 NC_SSH_KEY_ECDSA
109} NC_SSH_KEY_TYPE;
110
111/**
Radek Krejci43390242015-10-08 15:34:04 +0200112 * @brief NETCONF session object
113 */
114struct nc_session;
115
Radek Krejci695d4fa2015-10-22 13:23:54 +0200116/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100117 * @brief Get session status.
118 *
119 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100120 * @return Session status.
121 */
122NC_STATUS nc_session_get_status(const struct nc_session *session);
123
124/**
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100125 * @brief Get session termination reason.
126 *
127 * @param[in] session Session to get the information from.
128 * @return Session termination reason enum value.
129 */
Michal Vasko142cfea2017-08-07 10:12:11 +0200130NC_SESSION_TERM_REASON nc_session_get_term_reason(const struct nc_session *session);
131
132/**
133 * @brief Get session killer session ID.
134 *
135 * @param[in] session Session to get the information from.
136 * @return Session killer ID.
137 */
138uint32_t nc_session_get_killed_by(const struct nc_session *session);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100139
140/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100141 * @brief Get session ID.
142 *
143 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100144 * @return Session ID.
145 */
146uint32_t nc_session_get_id(const struct nc_session *session);
147
148/**
Michal Vasko174fe8e2016-02-17 15:38:09 +0100149 * @brief Get session NETCONF version.
150 *
151 * @param[in] session Session to get the information from.
152 * @return 0 for version 1.0, non-zero for version 1.1.
153 */
154int nc_session_get_version(const struct nc_session *session);
155
156/**
Michal Vasko8dadf782016-01-15 10:29:36 +0100157 * @brief Get session transport used.
158 *
159 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100160 * @return Session transport.
161 */
162NC_TRANSPORT_IMPL nc_session_get_ti(const struct nc_session *session);
163
164/**
165 * @brief Get session username.
166 *
167 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100168 * @return Session username.
169 */
170const char *nc_session_get_username(const struct nc_session *session);
171
172/**
173 * @brief Get session host.
174 *
175 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100176 * @return Session host.
177 */
178const char *nc_session_get_host(const struct nc_session *session);
179
180/**
181 * @brief Get session port.
182 *
183 * @param[in] session Session to get the information from.
Michal Vasko8dadf782016-01-15 10:29:36 +0100184 * @return Session port.
185 */
186uint16_t nc_session_get_port(const struct nc_session *session);
187
188/**
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200189 * @brief Get session path (unix socket only).
190 *
191 * @param[in] session Session to get the information from.
192 * @return Session unix socket path.
193 */
194const char *nc_session_get_path(const struct nc_session *session);
195
196/**
Michal Vasko9a25e932016-02-01 10:36:42 +0100197 * @brief Get session context.
198 *
199 * @param[in] session Session to get the information from.
200 * @return Session context.
201 */
Michal Vasko93224072021-11-09 12:14:28 +0100202const struct ly_ctx *nc_session_get_ctx(const struct nc_session *session);
Michal Vasko9a25e932016-02-01 10:36:42 +0100203
204/**
Michal Vasko2cc4c682016-03-01 09:16:48 +0100205 * @brief Assign arbitrary data to a session.
206 *
207 * @param[in] session Session to modify.
208 * @param[in] data Data to be stored in the session.
209 */
210void nc_session_set_data(struct nc_session *session, void *data);
211
212/**
213 * @brief Get the data assigned to a session.
214 *
215 * @param[in] session Session to get the data from.
216 * @return Session-specific data.
217 */
218void *nc_session_get_data(const struct nc_session *session);
219
220/**
Michal Vaskodc96bb92023-03-28 08:52:48 +0200221 * @brief Learn whether a session was created using Call Home or not.
222 *
223 * @param[in] session Session to get the information from.
224 * @return 0 if a standard session, non-zero if a Call Home session.
225 */
226int nc_session_is_callhome(const struct nc_session *session);
227
228/**
Radek Krejci695d4fa2015-10-22 13:23:54 +0200229 * @brief Free the NETCONF session object.
230 *
231 * @param[in] session Object to free.
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100232 * @param[in] data_free Session user data destructor.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200233 */
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100234void nc_session_free(struct nc_session *session, void (*data_free)(void *));
Radek Krejci695d4fa2015-10-22 13:23:54 +0200235
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200236#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Radek Krejci62aa0642017-05-25 16:33:49 +0200237
238/**
239 * @brief Free all the dynamically allocated thread-specific libssl/libcrypto
240 * resources.
241 *
242 * This function should be called only if init (nc_client_init(), respectively nc_server_init()) was called.
243 * Call it in every thread your application creates just before the thread exits. In the last thread
244 * (usually the main one) call nc_client_destroy(), respectively nc_server_destroy().
245 */
246void nc_thread_destroy(void);
247
248#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
249
Michal Vaskoc09730e2019-01-17 10:07:26 +0100250#ifdef __cplusplus
251}
252#endif
253
Radek Krejci43390242015-10-08 15:34:04 +0200254#endif /* NC_SESSION_H_ */