Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file session.c |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 - general session functions |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Radek Krejci | 952eb86 | 2016-01-08 14:22:55 +0100 | [diff] [blame] | 16 | #include <stdlib.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 17 | #include <string.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 18 | #include <pthread.h> |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 19 | #include <time.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 20 | #include <libyang/libyang.h> |
| 21 | |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 22 | #include "session.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 23 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 24 | #include "session_server.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 25 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 26 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 27 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 28 | # include <libssh/libssh.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 30 | #endif /* NC_ENABLED_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 31 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 32 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 33 | |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 34 | # include <openssl/engine.h> |
| 35 | # include <openssl/conf.h> |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 36 | # include <openssl/err.h> |
| 37 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 38 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 39 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | /* in seconds */ |
| 41 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 42 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 43 | /* in milliseconds */ |
| 44 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 45 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 46 | extern struct nc_server_opts server_opts; |
| 47 | |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 48 | int |
| 49 | nc_gettimespec(struct timespec *ts) |
| 50 | { |
| 51 | #ifdef CLOCK_MONOTONIC |
| 52 | return clock_gettime(CLOCK_MONOTONIC, ts); |
| 53 | #elif defined CLOCK_REALTIME |
| 54 | return clock_gettime(CLOCK_REALTIME, ts); |
| 55 | #else |
| 56 | int rc; |
| 57 | struct timeval tv; |
| 58 | |
| 59 | rc = gettimeofday(&tv, NULL); |
| 60 | if (!rc) { |
| 61 | ts->tv_sec = (time_t)tv.tv_sec; |
| 62 | ts->tv_nsec = 1000L * (long)tv.tv_usec; |
| 63 | } |
| 64 | return rc; |
| 65 | #endif |
| 66 | } |
| 67 | |
Radek Krejci | 2847292 | 2016-07-15 11:51:16 +0200 | [diff] [blame^] | 68 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 69 | int |
| 70 | pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) |
| 71 | { |
| 72 | int rc; |
| 73 | struct timespec cur, dur; |
| 74 | |
| 75 | /* Try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 76 | while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) { |
| 77 | nc_gettimespec(&cur); |
| 78 | |
| 79 | if ((cur.tv_sec > abstime->tv_sec) || ((cur.tv_sec == abstime->tv_sec) && (cur.tv_nsec >= abstime->tv_nsec))) { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | dur.tv_sec = abstime->tv_sec - cur.tv_sec; |
| 84 | dur.tv_nsec = abstime->tv_nsec - cur.tv_nsec; |
| 85 | if (dur.tv_nsec < 0) { |
| 86 | dur.tv_sec--; |
| 87 | dur.tv_nsec += 1000000000; |
| 88 | } |
| 89 | |
| 90 | if ((dur.tv_sec != 0) || (dur.tv_nsec > 5000000)) { |
| 91 | dur.tv_sec = 0; |
| 92 | dur.tv_nsec = 5000000; |
| 93 | } |
| 94 | |
| 95 | nanosleep(&dur, NULL); |
| 96 | } |
| 97 | |
| 98 | return rc; |
| 99 | } |
| 100 | #endif |
| 101 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 102 | /* |
| 103 | * @return 1 - success |
| 104 | * 0 - timeout |
| 105 | * -1 - error |
| 106 | */ |
| 107 | int |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 108 | nc_timedlock(pthread_mutex_t *lock, int timeout) |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 109 | { |
| 110 | int ret; |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 111 | struct timespec ts_timeout; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 112 | |
| 113 | if (timeout > 0) { |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 114 | nc_gettimespec(&ts_timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 115 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 116 | ts_timeout.tv_sec += timeout / 1000; |
| 117 | ts_timeout.tv_nsec += (timeout % 1000) * 1000000; |
| 118 | |
| 119 | ret = pthread_mutex_timedlock(lock, &ts_timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 120 | } else if (!timeout) { |
| 121 | ret = pthread_mutex_trylock(lock); |
| 122 | } else { /* timeout == -1 */ |
| 123 | ret = pthread_mutex_lock(lock); |
| 124 | } |
| 125 | |
| 126 | if (ret == ETIMEDOUT) { |
| 127 | /* timeout */ |
| 128 | return 0; |
| 129 | } else if (ret) { |
| 130 | /* error */ |
Radek Krejci | da8f58d | 2016-03-03 13:10:21 +0100 | [diff] [blame] | 131 | ERR("Mutex lock failed (%s).", strerror(ret)); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | /* ok */ |
| 136 | return 1; |
| 137 | } |
| 138 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 139 | API NC_STATUS |
| 140 | nc_session_get_status(const struct nc_session *session) |
| 141 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 142 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 143 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 147 | return session->status; |
| 148 | } |
| 149 | |
| 150 | API uint32_t |
| 151 | nc_session_get_id(const struct nc_session *session) |
| 152 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 153 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 154 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 158 | return session->id; |
| 159 | } |
| 160 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 161 | API int |
| 162 | nc_session_get_version(const struct nc_session *session) |
| 163 | { |
| 164 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 165 | ERRARG("session"); |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | return (session->version == NC_VERSION_10 ? 0 : 1); |
| 170 | } |
| 171 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 172 | API NC_TRANSPORT_IMPL |
| 173 | nc_session_get_ti(const struct nc_session *session) |
| 174 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 175 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 176 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 180 | return session->ti_type; |
| 181 | } |
| 182 | |
| 183 | API const char * |
| 184 | nc_session_get_username(const struct nc_session *session) |
| 185 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 186 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 187 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 191 | return session->username; |
| 192 | } |
| 193 | |
| 194 | API const char * |
| 195 | nc_session_get_host(const struct nc_session *session) |
| 196 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 197 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 198 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 202 | return session->host; |
| 203 | } |
| 204 | |
| 205 | API uint16_t |
| 206 | nc_session_get_port(const struct nc_session *session) |
| 207 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 208 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 209 | ERRARG("session"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 213 | return session->port; |
| 214 | } |
| 215 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 216 | API struct ly_ctx * |
| 217 | nc_session_get_ctx(const struct nc_session *session) |
| 218 | { |
| 219 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 220 | ERRARG("session"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | return session->ctx; |
| 225 | } |
| 226 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 227 | API void |
| 228 | nc_session_set_data(struct nc_session *session, void *data) |
| 229 | { |
| 230 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 231 | ERRARG("session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | session->data = data; |
| 236 | } |
| 237 | |
| 238 | API void * |
| 239 | nc_session_get_data(const struct nc_session *session) |
| 240 | { |
| 241 | if (!session) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 242 | ERRARG("session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | return session->data; |
| 247 | } |
| 248 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 249 | NC_MSG_TYPE |
| 250 | nc_send_msg(struct nc_session *session, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 251 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 252 | int r; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 253 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 254 | if (session->ctx != op->schema->module->ctx) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 255 | ERR("Session %u: RPC \"%s\" was created in different context than that of the session.", |
| 256 | session->id, op->schema->name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 257 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 260 | r = nc_write_msg(session, NC_MSG_RPC, op, NULL); |
| 261 | |
| 262 | if (r) { |
| 263 | return NC_MSG_ERROR; |
| 264 | } |
| 265 | |
| 266 | return NC_MSG_RPC; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 269 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 270 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 271 | { |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 272 | int r, i, locked; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 273 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 274 | int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */ |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 275 | pthread_t tid; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 276 | struct nc_session *siter; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 277 | struct nc_msg_cont *contiter; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 278 | struct lyxml_elem *rpl, *child; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 279 | struct lyd_node *close_rpc; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 280 | const struct lys_module *ietfnc; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 281 | void *p; |
| 282 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 283 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 287 | /* stop notifications loop if any */ |
| 288 | if (session->ntf_tid) { |
| 289 | tid = *session->ntf_tid; |
| 290 | free((pthread_t *)session->ntf_tid); |
| 291 | session->ntf_tid = NULL; |
| 292 | /* the thread now knows it should quit */ |
| 293 | |
| 294 | pthread_join(tid, NULL); |
| 295 | } |
| 296 | |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 297 | if (session->ti_lock) { |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 298 | r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 299 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 300 | return; |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 301 | } else if (!r) { |
| 302 | /* we failed to lock it, too bad */ |
| 303 | locked = 0; |
| 304 | } else { |
| 305 | locked = 1; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 306 | } |
Michal Vasko | 5ecbf8c | 2016-03-29 16:05:22 +0200 | [diff] [blame] | 307 | } else { |
| 308 | ERRINT; |
| 309 | return; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 312 | if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 313 | /* cleanup message queues */ |
| 314 | /* notifications */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 315 | for (contiter = session->notifs; contiter; ) { |
| 316 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 317 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 318 | p = contiter; |
| 319 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 320 | free(p); |
| 321 | } |
| 322 | |
| 323 | /* rpc replies */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 324 | for (contiter = session->replies; contiter; ) { |
| 325 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 326 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 327 | p = contiter; |
| 328 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 329 | free(p); |
| 330 | } |
| 331 | |
| 332 | /* send closing info to the other side */ |
| 333 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 334 | if (!ietfnc) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 335 | WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 336 | } else { |
| 337 | close_rpc = lyd_new(NULL, ietfnc, "close-session"); |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 338 | nc_send_msg(session, close_rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 339 | lyd_free(close_rpc); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 340 | switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) { |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 341 | case NC_MSG_REPLY: |
| 342 | LY_TREE_FOR(rpl->child, child) { |
| 343 | if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) { |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | if (!child) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 348 | WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 349 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 350 | lyxml_free(session->ctx, rpl); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 351 | break; |
| 352 | case NC_MSG_WOULDBLOCK: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 353 | WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 354 | break; |
| 355 | case NC_MSG_ERROR: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 356 | ERR("Session %u: failed to receive a reply to <close-session>.", session->id); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | /* cannot happen */ |
| 360 | break; |
| 361 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* list of server's capabilities */ |
| 365 | if (session->cpblts) { |
| 366 | for (i = 0; session->cpblts[i]; i++) { |
| 367 | lydict_remove(session->ctx, session->cpblts[i]); |
| 368 | } |
| 369 | free(session->cpblts); |
| 370 | } |
| 371 | } |
| 372 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 373 | if (session->data && data_free) { |
| 374 | data_free(session->data); |
| 375 | } |
| 376 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 377 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 378 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 379 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 380 | |
| 381 | /* transport implementation cleanup */ |
| 382 | switch (session->ti_type) { |
| 383 | case NC_TI_FD: |
| 384 | /* nothing needed - file descriptors were provided by caller, |
| 385 | * so it is up to the caller to close them correctly |
| 386 | * TODO use callbacks |
| 387 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 388 | /* just to avoid compiler warning */ |
| 389 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 390 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 391 | break; |
| 392 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 393 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 394 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 395 | if (connected) { |
| 396 | ssh_channel_free(session->ti.libssh.channel); |
| 397 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 398 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 399 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 400 | * it. |
| 401 | */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 402 | multisession = 0; |
| 403 | if (session->ti.libssh.next) { |
| 404 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 405 | if (siter->status != NC_STATUS_STARTING) { |
| 406 | multisession = 1; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (!multisession) { |
| 413 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 414 | if (session->ti.libssh.next) { |
| 415 | do { |
| 416 | siter = session->ti.libssh.next; |
| 417 | session->ti.libssh.next = siter->ti.libssh.next; |
| 418 | |
| 419 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 420 | lydict_remove(session->ctx, session->username); |
| 421 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 422 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 423 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | free(siter); |
| 427 | } while (session->ti.libssh.next != session); |
| 428 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 429 | if (connected) { |
| 430 | ssh_disconnect(session->ti.libssh.session); |
| 431 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 432 | ssh_free(session->ti.libssh.session); |
| 433 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 434 | /* remove the session from the list */ |
| 435 | for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next); |
Michal Vasko | aec4f21 | 2015-10-26 15:37:45 +0100 | [diff] [blame] | 436 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 437 | /* there will be only one session */ |
| 438 | siter->ti.libssh.next = NULL; |
| 439 | } else { |
| 440 | /* there are still multiple sessions, keep the ring list */ |
| 441 | siter->ti.libssh.next = session->ti.libssh.next; |
| 442 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 443 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 444 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 445 | for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) { |
| 446 | if (siter->ti.libssh.next == session) { |
| 447 | ERRINT; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 452 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 453 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 454 | } |
| 455 | break; |
| 456 | #endif |
| 457 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 458 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 459 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 460 | if (connected) { |
| 461 | SSL_shutdown(session->ti.tls); |
| 462 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 463 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 464 | |
| 465 | X509_free(session->tls_cert); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 466 | break; |
| 467 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 468 | case NC_TI_NONE: |
| 469 | ERRINT; |
| 470 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 471 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 472 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 473 | lydict_remove(session->ctx, session->username); |
| 474 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 475 | |
| 476 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 477 | if (session->ti_lock) { |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 478 | if (locked) { |
| 479 | pthread_mutex_unlock(session->ti_lock); |
| 480 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 481 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 482 | pthread_mutex_destroy(session->ti_lock); |
| 483 | free(session->ti_lock); |
| 484 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 488 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | free(session); |
| 492 | } |
| 493 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 494 | static void |
| 495 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 496 | { |
| 497 | if (*count == *size) { |
| 498 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 499 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 500 | if (!(*cpblts)) { |
| 501 | ERRMEM; |
| 502 | return; |
| 503 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (capab) { |
| 507 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 508 | } else { |
| 509 | (*cpblts)[*count] = NULL; |
| 510 | } |
| 511 | ++(*count); |
| 512 | } |
| 513 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 514 | API const char ** |
| 515 | nc_server_get_cpblts(struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 516 | { |
| 517 | struct lyd_node *child, *child2, *yanglib; |
| 518 | struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL; |
| 519 | const char **cpblts; |
| 520 | const struct lys_module *mod; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 521 | int size = 10, count, feat_count = 0, i, str_len; |
Michal Vasko | c1119d8 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 522 | #define NC_CPBLT_BUF_LEN 512 |
| 523 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 524 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 525 | if (!ctx) { |
| 526 | ERRARG("ctx"); |
| 527 | return NULL; |
| 528 | } |
| 529 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 530 | yanglib = ly_ctx_info(ctx); |
| 531 | if (!yanglib) { |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 532 | ERR("Failed to get ietf-yang-library data from the context."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 537 | if (!cpblts) { |
| 538 | ERRMEM; |
| 539 | return NULL; |
| 540 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 541 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 542 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 543 | count = 2; |
| 544 | |
| 545 | /* capabilities */ |
| 546 | |
| 547 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 548 | if (mod) { |
| 549 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 550 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 551 | } |
| 552 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 553 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 554 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 555 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 559 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 560 | } |
| 561 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 562 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 563 | } |
| 564 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 565 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 566 | } |
| 567 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 568 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 569 | } |
| 570 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 571 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 576 | if (mod) { |
| 577 | if (!server_opts.wd_basic_mode) { |
| 578 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 579 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 580 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 581 | switch (server_opts.wd_basic_mode) { |
| 582 | case NC_WD_ALL: |
| 583 | strcat(str, "?basic-mode=report-all"); |
| 584 | break; |
| 585 | case NC_WD_TRIM: |
| 586 | strcat(str, "?basic-mode=trim"); |
| 587 | break; |
| 588 | case NC_WD_EXPLICIT: |
| 589 | strcat(str, "?basic-mode=explicit"); |
| 590 | break; |
| 591 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 592 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (server_opts.wd_also_supported) { |
Michal Vasko | c1119d8 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 597 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 598 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 599 | strcat(str, "report-all,"); |
| 600 | } |
| 601 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 602 | strcat(str, "report-all-tagged,"); |
| 603 | } |
| 604 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 605 | strcat(str, "trim,"); |
| 606 | } |
| 607 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 608 | strcat(str, "explicit,"); |
| 609 | } |
| 610 | str[strlen(str) - 1] = '\0'; |
| 611 | |
| 612 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 617 | mod = ly_ctx_get_module(ctx, "nc-notifications", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 618 | if (mod) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 619 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 620 | if (server_opts.interleave_capab) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 621 | add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | /* models */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 626 | LY_TREE_FOR(yanglib->child, child) { |
| 627 | if (!strcmp(child->schema->name, "module")) { |
| 628 | LY_TREE_FOR(child->child, child2) { |
| 629 | if (!strcmp(child2->schema->name, "namespace")) { |
| 630 | ns = (struct lyd_node_leaf_list *)child2; |
| 631 | } else if (!strcmp(child2->schema->name, "name")) { |
| 632 | name = (struct lyd_node_leaf_list *)child2; |
| 633 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 634 | rev = (struct lyd_node_leaf_list *)child2; |
| 635 | } else if (!strcmp(child2->schema->name, "feature")) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 636 | features = nc_realloc(features, ++feat_count * sizeof *features); |
| 637 | if (!features) { |
| 638 | ERRMEM; |
| 639 | free(cpblts); |
| 640 | return NULL; |
| 641 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 642 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 647 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 648 | continue; |
| 649 | } |
| 650 | |
Radek Krejci | df7ba52 | 2016-03-01 16:05:25 +0100 | [diff] [blame] | 651 | str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str, |
Michal Vasko | c1119d8 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 652 | rev->value_str[0] ? "&revision=" : "", rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 653 | if (feat_count) { |
Michal Vasko | c1119d8 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 654 | strcat(str, "&features="); |
| 655 | str_len += 10; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 656 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | c1119d8 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 657 | if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 658 | ERRINT; |
| 659 | break; |
| 660 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 661 | if (i) { |
| 662 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 663 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 664 | } |
| 665 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 666 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 671 | |
| 672 | ns = NULL; |
| 673 | name = NULL; |
| 674 | rev = NULL; |
| 675 | free(features); |
| 676 | features = NULL; |
| 677 | feat_count = 0; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | lyd_free(yanglib); |
| 682 | |
| 683 | /* ending NULL capability */ |
| 684 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 685 | |
| 686 | return cpblts; |
| 687 | } |
| 688 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 689 | static int |
| 690 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 691 | { |
| 692 | struct lyxml_elem *cpblt; |
| 693 | int ver = -1; |
| 694 | int i = 0; |
| 695 | |
| 696 | if (list) { |
| 697 | /* get the storage for server's capabilities */ |
| 698 | LY_TREE_FOR(xml->child, cpblt) { |
| 699 | i++; |
| 700 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 701 | /* last item remains NULL */ |
| 702 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 703 | if (!*list) { |
| 704 | ERRMEM; |
| 705 | return -1; |
| 706 | } |
| 707 | i = 0; |
| 708 | } |
| 709 | |
| 710 | LY_TREE_FOR(xml->child, cpblt) { |
| 711 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 712 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 713 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 714 | return -1; |
| 715 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | /* detect NETCONF version */ |
| 720 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 721 | ver = 0; |
| 722 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 723 | ver = 1; |
| 724 | } |
| 725 | |
| 726 | /* store capabilities */ |
| 727 | if (list) { |
| 728 | (*list)[i] = cpblt->content; |
| 729 | cpblt->content = NULL; |
| 730 | i++; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 735 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | return ver; |
| 739 | } |
| 740 | |
| 741 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 742 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 743 | { |
| 744 | int r, i; |
| 745 | const char **cpblts; |
| 746 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 747 | /* client side hello - send only NETCONF base capabilities */ |
| 748 | cpblts = malloc(3 * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 749 | if (!cpblts) { |
| 750 | ERRMEM; |
| 751 | return NC_MSG_ERROR; |
| 752 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 753 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 754 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 755 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 756 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 757 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 758 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 759 | for (i = 0; cpblts[i]; ++i) { |
| 760 | lydict_remove(session->ctx, cpblts[i]); |
| 761 | } |
| 762 | free(cpblts); |
| 763 | |
| 764 | if (r) { |
| 765 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 766 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 767 | |
| 768 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 772 | nc_send_server_hello(struct nc_session *session) |
| 773 | { |
| 774 | int r, i; |
| 775 | const char **cpblts; |
| 776 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 777 | cpblts = nc_server_get_cpblts(session->ctx); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 778 | |
| 779 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 780 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 781 | for (i = 0; cpblts[i]; ++i) { |
| 782 | lydict_remove(session->ctx, cpblts[i]); |
| 783 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 784 | free(cpblts); |
| 785 | |
| 786 | if (r) { |
| 787 | return NC_MSG_ERROR; |
| 788 | } |
| 789 | |
| 790 | return NC_MSG_HELLO; |
| 791 | } |
| 792 | |
| 793 | static NC_MSG_TYPE |
| 794 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 795 | { |
| 796 | struct lyxml_elem *xml = NULL, *node; |
| 797 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 798 | int ver = -1; |
| 799 | char *str; |
| 800 | long long int id; |
| 801 | int flag = 0; |
| 802 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 803 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 804 | |
| 805 | switch(msgtype) { |
| 806 | case NC_MSG_HELLO: |
| 807 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 808 | LY_TREE_FOR(xml->child, node) { |
| 809 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 810 | continue; |
| 811 | } else if (!strcmp(node->name, "session-id")) { |
| 812 | if (!node->content || !strlen(node->content)) { |
| 813 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 814 | goto error; |
| 815 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 816 | str = NULL; |
| 817 | id = strtoll(node->content, &str, 10); |
| 818 | if (*str || id < 1 || id > UINT32_MAX) { |
| 819 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 820 | goto error; |
| 821 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 822 | session->id = (uint32_t)id; |
| 823 | continue; |
| 824 | } else if (strcmp(node->name, "capabilities")) { |
| 825 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 826 | goto error; |
| 827 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 828 | |
| 829 | if (flag) { |
| 830 | /* multiple capabilities elements */ |
| 831 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 832 | goto error; |
| 833 | } |
| 834 | flag = 1; |
| 835 | |
| 836 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 837 | goto error; |
| 838 | } |
| 839 | session->version = ver; |
| 840 | } |
| 841 | |
| 842 | if (!session->id) { |
| 843 | ERR("Missing <session-id> in server's <hello>."); |
| 844 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 845 | } |
| 846 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 847 | case NC_MSG_WOULDBLOCK: |
| 848 | ERR("Server's <hello> timeout elapsed."); |
| 849 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 850 | case NC_MSG_ERROR: |
| 851 | /* nothing special, just pass it out */ |
| 852 | break; |
| 853 | default: |
| 854 | ERR("Unexpected message received instead of <hello>."); |
| 855 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 856 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 860 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 861 | |
| 862 | return msgtype; |
| 863 | |
| 864 | error: |
| 865 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 866 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 867 | |
| 868 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 869 | } |
| 870 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 871 | static NC_MSG_TYPE |
| 872 | nc_recv_server_hello(struct nc_session *session) |
| 873 | { |
| 874 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 875 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 876 | int ver = -1; |
| 877 | int flag = 0; |
| 878 | |
Michal Vasko | adb850e | 2016-01-20 14:06:32 +0100 | [diff] [blame] | 879 | msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 880 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 881 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 882 | case NC_MSG_HELLO: |
| 883 | /* get know NETCONF version */ |
| 884 | LY_TREE_FOR(xml->child, node) { |
| 885 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 886 | continue; |
| 887 | } else if (strcmp(node->name, "capabilities")) { |
| 888 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 889 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 890 | goto cleanup; |
| 891 | } |
| 892 | |
| 893 | if (flag) { |
| 894 | /* multiple capabilities elements */ |
| 895 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 896 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 897 | goto cleanup; |
| 898 | } |
| 899 | flag = 1; |
| 900 | |
| 901 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 902 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 903 | goto cleanup; |
| 904 | } |
| 905 | session->version = ver; |
| 906 | } |
| 907 | break; |
| 908 | case NC_MSG_ERROR: |
| 909 | /* nothing special, just pass it out */ |
| 910 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 911 | case NC_MSG_WOULDBLOCK: |
| 912 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 913 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 914 | default: |
| 915 | ERR("Unexpected message received instead of <hello>."); |
| 916 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 917 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 921 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 922 | |
| 923 | return msgtype; |
| 924 | } |
| 925 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 926 | NC_MSG_TYPE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 927 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 928 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 929 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 930 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 931 | if (session->side == NC_CLIENT) { |
| 932 | type = nc_send_client_hello(session); |
| 933 | } else { |
| 934 | type = nc_send_server_hello(session); |
| 935 | } |
| 936 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 937 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 938 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 941 | if (session->side == NC_CLIENT) { |
| 942 | type = nc_recv_client_hello(session); |
| 943 | } else { |
| 944 | type = nc_recv_server_hello(session); |
| 945 | } |
| 946 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 947 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 948 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 949 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 950 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 951 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 952 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 953 | nc_ssh_init(void) |
| 954 | { |
| 955 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 956 | ssh_init(); |
| 957 | ssh_set_log_level(verbose_level); |
| 958 | } |
| 959 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 960 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 961 | nc_ssh_destroy(void) |
| 962 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 963 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 964 | ENGINE_cleanup(); |
| 965 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 966 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 967 | ssh_finalize(); |
| 968 | } |
| 969 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 970 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 971 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 972 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 973 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 974 | struct CRYPTO_dynlock_value { |
| 975 | pthread_mutex_t lock; |
| 976 | }; |
| 977 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 978 | static struct CRYPTO_dynlock_value * |
| 979 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 980 | { |
| 981 | struct CRYPTO_dynlock_value *value; |
| 982 | |
| 983 | value = malloc(sizeof *value); |
| 984 | if (!value) { |
| 985 | ERRMEM; |
| 986 | return NULL; |
| 987 | } |
| 988 | pthread_mutex_init(&value->lock, NULL); |
| 989 | |
| 990 | return value; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 995 | { |
| 996 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 997 | * I found ignored this fact, what do I know... */ |
| 998 | if (mode & CRYPTO_LOCK) { |
| 999 | pthread_mutex_lock(&l->lock); |
| 1000 | } else { |
| 1001 | pthread_mutex_unlock(&l->lock); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1007 | { |
| 1008 | pthread_mutex_destroy(&l->lock); |
| 1009 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1012 | #endif /* NC_ENABLED_TLS */ |
| 1013 | |
| 1014 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1015 | |
| 1016 | static pthread_mutex_t *tls_locks; |
| 1017 | |
| 1018 | static void |
| 1019 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1020 | { |
| 1021 | if (mode & CRYPTO_LOCK) { |
| 1022 | pthread_mutex_lock(tls_locks + n); |
| 1023 | } else { |
| 1024 | pthread_mutex_unlock(tls_locks + n); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static void |
| 1029 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1030 | { |
| 1031 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1032 | } |
| 1033 | |
| 1034 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1035 | nc_tls_init(void) |
| 1036 | { |
| 1037 | int i; |
| 1038 | |
| 1039 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1040 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1041 | SSL_library_init(); |
| 1042 | |
| 1043 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1044 | if (!tls_locks) { |
| 1045 | ERRMEM; |
| 1046 | return; |
| 1047 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1048 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1049 | pthread_mutex_init(tls_locks + i, NULL); |
| 1050 | } |
| 1051 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1052 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1053 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1054 | |
| 1055 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1056 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1057 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1060 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1061 | nc_tls_destroy(void) |
| 1062 | { |
| 1063 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1064 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1065 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1066 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1067 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1068 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1069 | ERR_free_strings(); |
| 1070 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1071 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1072 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1073 | CRYPTO_set_locking_callback(NULL); |
| 1074 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1075 | pthread_mutex_destroy(tls_locks + i); |
| 1076 | } |
| 1077 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1078 | |
| 1079 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1080 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1081 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1084 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1085 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1086 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1087 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1088 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1089 | nc_ssh_tls_init(void) |
| 1090 | { |
| 1091 | SSL_load_error_strings(); |
| 1092 | ERR_load_BIO_strings(); |
| 1093 | SSL_library_init(); |
| 1094 | |
| 1095 | nc_ssh_init(); |
| 1096 | |
| 1097 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1098 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1099 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
| 1100 | } |
| 1101 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1102 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1103 | nc_ssh_tls_destroy(void) |
| 1104 | { |
| 1105 | ERR_free_strings(); |
| 1106 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 1107 | |
| 1108 | nc_ssh_destroy(); |
| 1109 | |
| 1110 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1111 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1112 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 1113 | } |
| 1114 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1115 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1116 | |
| 1117 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1118 | |
| 1119 | API void |
| 1120 | nc_thread_destroy(void) |
| 1121 | { |
| 1122 | CRYPTO_THREADID crypto_tid; |
| 1123 | |
| 1124 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1125 | //CRYPTO_cleanup_all_ex_data(); |
| 1126 | |
| 1127 | CRYPTO_THREADID_current(&crypto_tid); |
| 1128 | ERR_remove_thread_state(&crypto_tid); |
| 1129 | } |
| 1130 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1131 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1132 | |
| 1133 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1134 | nc_init(void) |
| 1135 | { |
| 1136 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1137 | nc_ssh_tls_init(); |
| 1138 | #elif defined(NC_ENABLED_SSH) |
| 1139 | nc_ssh_init(); |
| 1140 | #elif defined(NC_ENABLED_TLS) |
| 1141 | nc_tls_init(); |
| 1142 | #endif |
| 1143 | } |
| 1144 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1145 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1146 | nc_destroy(void) |
| 1147 | { |
| 1148 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1149 | nc_ssh_tls_destroy(); |
| 1150 | #elif defined(NC_ENABLED_SSH) |
| 1151 | nc_ssh_destroy(); |
| 1152 | #elif defined(NC_ENABLED_TLS) |
| 1153 | nc_tls_destroy(); |
| 1154 | #endif |
| 1155 | } |