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