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