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