Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 5 | * @brief libnetconf2 session manipulation |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 7 | * @copyright |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 9 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 10 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 13 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 14 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef NC_SESSION_H_ |
| 18 | #define NC_SESSION_H_ |
| 19 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 24 | #include "netconf.h" |
| 25 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 26 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 27 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 28 | /** |
| 29 | * @brief Enumeration of NETCONF SSH authentication methods |
| 30 | */ |
Michal Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 31 | typedef enum { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 32 | 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 Vasko | 4ef1493 | 2015-12-04 11:09:10 +0100 | [diff] [blame] | 35 | } NC_SSH_AUTH_TYPE; |
| 36 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 37 | /** |
| 38 | * @brief Enumeration of host key checking and known_hosts entry adding modes |
| 39 | */ |
| 40 | typedef enum { |
| 41 | NC_SSH_KNOWNHOSTS_ASK = 0, /**< add a known_hosts entry, but with a prompt */ |
| 42 | NC_SSH_KNOWNHOSTS_STRICT, /**< do not add a known_hosts entry and the server's host key must be present in the configured known_hosts file */ |
| 43 | NC_SSH_KNOWNHOSTS_ACCEPT_NEW, /**< add a known_hosts entry without a prompt */ |
| 44 | NC_SSH_KNOWNHOSTS_ACCEPT, /**< add a known_hosts entry without a prompt and allow connections to servers which changed their host key */ |
| 45 | NC_SSH_KNOWNHOSTS_SKIP /**< do not add a known_hosts entry and skip all host key checks */ |
| 46 | } NC_SSH_KNOWNHOSTS_MODE; |
| 47 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 48 | /** |
| 49 | * @brief Enumeration of cert-to-name mapping types |
| 50 | */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 51 | typedef enum { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 52 | NC_TLS_CTN_UNKNOWN = 0, /**< unknown mapping */ |
| 53 | NC_TLS_CTN_SPECIFIED, /**< username explicitly specified */ |
| 54 | NC_TLS_CTN_SAN_RFC822_NAME, /**< email address as username */ |
| 55 | NC_TLS_CTN_SAN_DNS_NAME, /**< DNS name as username */ |
| 56 | NC_TLS_CTN_SAN_IP_ADDRESS, /**< IP address as username */ |
| 57 | NC_TLS_CTN_SAN_ANY, /**< any certificate Subject Alternative Name as username */ |
| 58 | NC_TLS_CTN_COMMON_NAME /**< common name as username */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 59 | } NC_TLS_CTN_MAPTYPE; |
| 60 | |
roman | 12644fe | 2023-06-08 11:06:42 +0200 | [diff] [blame] | 61 | /** |
| 62 | * @brief Enumeration of TLS versions. |
| 63 | */ |
| 64 | typedef enum { |
| 65 | NC_TLS_VERSION_10 = 1, /**< TLS1.0 */ |
| 66 | NC_TLS_VERSION_11 = 2, /**< TLS1.1 */ |
| 67 | NC_TLS_VERSION_12 = 4, /**< TLS1.2 */ |
| 68 | NC_TLS_VERSION_13 = 8 /**< TLS1.3 */ |
| 69 | } NC_TLS_VERSION; |
| 70 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 71 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 72 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 73 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 74 | * @brief Enumeration of possible session statuses |
| 75 | */ |
| 76 | typedef enum { |
Radek Krejci | 465308c | 2017-05-22 14:49:10 +0200 | [diff] [blame] | 77 | NC_STATUS_ERR = -1, /**< error return code for function getting the session status */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 78 | NC_STATUS_STARTING = 0, /**< session is not yet fully initiated */ |
| 79 | NC_STATUS_CLOSING, /**< session is being closed */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 80 | NC_STATUS_INVALID, /**< session is not running and is supposed to be closed (nc_session_free()) */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 81 | NC_STATUS_RUNNING /**< up and running */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 82 | } NC_STATUS; |
| 83 | |
| 84 | /** |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 85 | * @brief Enumeration of transport implementations (ways how libnetconf implements NETCONF transport protocol) |
| 86 | */ |
| 87 | typedef enum { |
| 88 | NC_TI_NONE = 0, /**< none - session is not connected yet */ |
| 89 | NC_TI_FD, /**< file descriptors - use standard input/output, transport protocol is implemented |
| 90 | outside the current application */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 91 | NC_TI_UNIX, /**< unix socket */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 92 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 93 | NC_TI_LIBSSH, /**< libssh - use libssh library, only for NETCONF over SSH transport */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 95 | NC_TI_OPENSSL /**< OpenSSL - use OpenSSL library, only for NETCONF over TLS transport */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 96 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 97 | } NC_TRANSPORT_IMPL; |
| 98 | |
| 99 | /** |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 100 | * @brief Enumeration of Call Home connection types. |
| 101 | */ |
| 102 | typedef enum { |
| 103 | NC_CH_CT_NOT_SET = 0, |
| 104 | NC_CH_PERSIST, |
| 105 | NC_CH_PERIOD |
| 106 | } NC_CH_CONN_TYPE; |
| 107 | |
| 108 | /** |
| 109 | * @brief Enumeration of Call Home client priority policy. |
| 110 | */ |
| 111 | typedef enum { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 112 | NC_CH_FIRST_LISTED = 0, // default |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 113 | NC_CH_LAST_CONNECTED, |
| 114 | NC_CH_RANDOM |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 115 | } NC_CH_START_WITH; |
| 116 | |
| 117 | /** |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 118 | * @brief NETCONF session object |
| 119 | */ |
| 120 | struct nc_session; |
| 121 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 122 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 123 | * @brief Get session status. |
| 124 | * |
| 125 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 126 | * @return Session status. |
| 127 | */ |
| 128 | NC_STATUS nc_session_get_status(const struct nc_session *session); |
| 129 | |
| 130 | /** |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 131 | * @brief Get session termination reason. |
| 132 | * |
| 133 | * @param[in] session Session to get the information from. |
| 134 | * @return Session termination reason enum value. |
| 135 | */ |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 136 | NC_SESSION_TERM_REASON nc_session_get_term_reason(const struct nc_session *session); |
| 137 | |
| 138 | /** |
| 139 | * @brief Get session killer session ID. |
| 140 | * |
| 141 | * @param[in] session Session to get the information from. |
| 142 | * @return Session killer ID. |
| 143 | */ |
| 144 | uint32_t nc_session_get_killed_by(const struct nc_session *session); |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 147 | * @brief Get session ID. |
| 148 | * |
| 149 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 150 | * @return Session ID. |
| 151 | */ |
| 152 | uint32_t nc_session_get_id(const struct nc_session *session); |
| 153 | |
| 154 | /** |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 155 | * @brief Get session NETCONF version. |
| 156 | * |
| 157 | * @param[in] session Session to get the information from. |
| 158 | * @return 0 for version 1.0, non-zero for version 1.1. |
| 159 | */ |
| 160 | int nc_session_get_version(const struct nc_session *session); |
| 161 | |
| 162 | /** |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 163 | * @brief Get session transport used. |
| 164 | * |
| 165 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 166 | * @return Session transport. |
| 167 | */ |
| 168 | NC_TRANSPORT_IMPL nc_session_get_ti(const struct nc_session *session); |
| 169 | |
| 170 | /** |
| 171 | * @brief Get session username. |
| 172 | * |
| 173 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 174 | * @return Session username. |
| 175 | */ |
| 176 | const char *nc_session_get_username(const struct nc_session *session); |
| 177 | |
| 178 | /** |
| 179 | * @brief Get session host. |
| 180 | * |
| 181 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 182 | * @return Session host. |
| 183 | */ |
| 184 | const char *nc_session_get_host(const struct nc_session *session); |
| 185 | |
| 186 | /** |
| 187 | * @brief Get session port. |
| 188 | * |
| 189 | * @param[in] session Session to get the information from. |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 190 | * @return Session port. |
| 191 | */ |
| 192 | uint16_t nc_session_get_port(const struct nc_session *session); |
| 193 | |
| 194 | /** |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 195 | * @brief Get session path (unix socket only). |
| 196 | * |
| 197 | * @param[in] session Session to get the information from. |
| 198 | * @return Session unix socket path. |
| 199 | */ |
| 200 | const char *nc_session_get_path(const struct nc_session *session); |
| 201 | |
| 202 | /** |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 203 | * @brief Get session context. |
| 204 | * |
| 205 | * @param[in] session Session to get the information from. |
| 206 | * @return Session context. |
| 207 | */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 208 | const struct ly_ctx *nc_session_get_ctx(const struct nc_session *session); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 209 | |
| 210 | /** |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 211 | * @brief Assign arbitrary data to a session. |
| 212 | * |
| 213 | * @param[in] session Session to modify. |
| 214 | * @param[in] data Data to be stored in the session. |
| 215 | */ |
| 216 | void nc_session_set_data(struct nc_session *session, void *data); |
| 217 | |
| 218 | /** |
| 219 | * @brief Get the data assigned to a session. |
| 220 | * |
| 221 | * @param[in] session Session to get the data from. |
| 222 | * @return Session-specific data. |
| 223 | */ |
| 224 | void *nc_session_get_data(const struct nc_session *session); |
| 225 | |
| 226 | /** |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 227 | * @brief Learn whether a session was created using Call Home or not. |
| 228 | * |
| 229 | * @param[in] session Session to get the information from. |
| 230 | * @return 0 if a standard session, non-zero if a Call Home session. |
| 231 | */ |
| 232 | int nc_session_is_callhome(const struct nc_session *session); |
| 233 | |
| 234 | /** |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 235 | * @brief Free the NETCONF session object. |
| 236 | * |
| 237 | * @param[in] session Object to free. |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 238 | * @param[in] data_free Session user data destructor. |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 239 | */ |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 240 | void nc_session_free(struct nc_session *session, void (*data_free)(void *)); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 241 | |
Michal Vasko | c09730e | 2019-01-17 10:07:26 +0100 | [diff] [blame] | 242 | #ifdef __cplusplus |
| 243 | } |
| 244 | #endif |
| 245 | |
Radek Krejci | 4339024 | 2015-10-08 15:34:04 +0200 | [diff] [blame] | 246 | #endif /* NC_SESSION_H_ */ |