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 */ |
| 291 | if (session->ntf_tid) { |
| 292 | tid = *session->ntf_tid; |
| 293 | free((pthread_t *)session->ntf_tid); |
| 294 | session->ntf_tid = NULL; |
| 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 | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 318 | for (contiter = session->notifs; contiter; ) { |
| 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 | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 327 | for (contiter = session->replies; contiter; ) { |
| 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 */ |
| 368 | if (session->cpblts) { |
| 369 | for (i = 0; session->cpblts[i]; i++) { |
| 370 | lydict_remove(session->ctx, session->cpblts[i]); |
| 371 | } |
| 372 | free(session->cpblts); |
| 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 | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 380 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 381 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 382 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 383 | |
| 384 | /* transport implementation cleanup */ |
| 385 | switch (session->ti_type) { |
| 386 | case NC_TI_FD: |
| 387 | /* nothing needed - file descriptors were provided by caller, |
| 388 | * so it is up to the caller to close them correctly |
| 389 | * TODO use callbacks |
| 390 | */ |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 391 | /* just to avoid compiler warning */ |
| 392 | (void)connected; |
Michal Vasko | 4589bbe | 2016-01-29 09:41:30 +0100 | [diff] [blame] | 393 | (void)siter; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 394 | break; |
| 395 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 396 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 397 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 398 | if (connected) { |
| 399 | ssh_channel_free(session->ti.libssh.channel); |
| 400 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 401 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 402 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 403 | * it. |
| 404 | */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 405 | multisession = 0; |
| 406 | if (session->ti.libssh.next) { |
| 407 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 408 | if (siter->status != NC_STATUS_STARTING) { |
| 409 | multisession = 1; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (!multisession) { |
| 416 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 417 | if (session->ti.libssh.next) { |
| 418 | do { |
| 419 | siter = session->ti.libssh.next; |
| 420 | session->ti.libssh.next = siter->ti.libssh.next; |
| 421 | |
| 422 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 423 | lydict_remove(session->ctx, session->username); |
| 424 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 425 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 426 | ly_ctx_destroy(session->ctx, NULL); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | free(siter); |
| 430 | } while (session->ti.libssh.next != session); |
| 431 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 432 | if (connected) { |
| 433 | ssh_disconnect(session->ti.libssh.session); |
| 434 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 435 | ssh_free(session->ti.libssh.session); |
| 436 | } else { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 437 | /* remove the session from the list */ |
| 438 | 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] | 439 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 440 | /* there will be only one session */ |
| 441 | siter->ti.libssh.next = NULL; |
| 442 | } else { |
| 443 | /* there are still multiple sessions, keep the ring list */ |
| 444 | siter->ti.libssh.next = session->ti.libssh.next; |
| 445 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 446 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 447 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 448 | for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) { |
| 449 | if (siter->ti.libssh.next == session) { |
| 450 | ERRINT; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 455 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 456 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 457 | } |
| 458 | break; |
| 459 | #endif |
| 460 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 461 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 462 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 463 | if (connected) { |
| 464 | SSL_shutdown(session->ti.tls); |
| 465 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 466 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 467 | |
| 468 | X509_free(session->tls_cert); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 469 | break; |
| 470 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 471 | case NC_TI_NONE: |
| 472 | ERRINT; |
| 473 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 474 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 475 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 476 | lydict_remove(session->ctx, session->username); |
| 477 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 478 | |
| 479 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 480 | if (session->ti_lock) { |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 481 | if (locked) { |
| 482 | pthread_mutex_unlock(session->ti_lock); |
| 483 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 484 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 485 | pthread_mutex_destroy(session->ti_lock); |
| 486 | free(session->ti_lock); |
| 487 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Radek Krejci | 4ba285b | 2016-02-04 17:34:06 +0100 | [diff] [blame] | 491 | ly_ctx_destroy(session->ctx, NULL); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | free(session); |
| 495 | } |
| 496 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 497 | static void |
| 498 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 499 | { |
| 500 | if (*count == *size) { |
| 501 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 502 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 503 | if (!(*cpblts)) { |
| 504 | ERRMEM; |
| 505 | return; |
| 506 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (capab) { |
| 510 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 511 | } else { |
| 512 | (*cpblts)[*count] = NULL; |
| 513 | } |
| 514 | ++(*count); |
| 515 | } |
| 516 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 517 | API const char ** |
| 518 | nc_server_get_cpblts(struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 519 | { |
| 520 | struct lyd_node *child, *child2, *yanglib; |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 521 | 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] | 522 | const char **cpblts; |
| 523 | const struct lys_module *mod; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 524 | 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] | 525 | #define NC_CPBLT_BUF_LEN 512 |
| 526 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 527 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 528 | if (!ctx) { |
| 529 | ERRARG("ctx"); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 533 | yanglib = ly_ctx_info(ctx); |
| 534 | if (!yanglib) { |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 535 | ERR("Failed to get ietf-yang-library data from the context."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 540 | if (!cpblts) { |
| 541 | ERRMEM; |
| 542 | return NULL; |
| 543 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 544 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 545 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 546 | count = 2; |
| 547 | |
| 548 | /* capabilities */ |
| 549 | |
| 550 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 551 | if (mod) { |
| 552 | if (lys_features_state(mod, "writable-running") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 553 | 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] | 554 | } |
| 555 | if (lys_features_state(mod, "candidate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 556 | 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] | 557 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 558 | 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] | 559 | } |
| 560 | } |
| 561 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 562 | 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] | 563 | } |
| 564 | if (lys_features_state(mod, "validate") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 565 | 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] | 566 | } |
| 567 | if (lys_features_state(mod, "startup") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 568 | 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] | 569 | } |
| 570 | if (lys_features_state(mod, "url") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 571 | 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] | 572 | } |
| 573 | if (lys_features_state(mod, "xpath") == 1) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 574 | 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] | 575 | } |
| 576 | } |
| 577 | |
| 578 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 579 | if (mod) { |
| 580 | if (!server_opts.wd_basic_mode) { |
| 581 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 582 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 583 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 584 | switch (server_opts.wd_basic_mode) { |
| 585 | case NC_WD_ALL: |
| 586 | strcat(str, "?basic-mode=report-all"); |
| 587 | break; |
| 588 | case NC_WD_TRIM: |
| 589 | strcat(str, "?basic-mode=trim"); |
| 590 | break; |
| 591 | case NC_WD_EXPLICIT: |
| 592 | strcat(str, "?basic-mode=explicit"); |
| 593 | break; |
| 594 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 595 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | |
| 599 | if (server_opts.wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 600 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 601 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 602 | strcat(str, "report-all,"); |
| 603 | } |
| 604 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 605 | strcat(str, "report-all-tagged,"); |
| 606 | } |
| 607 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 608 | strcat(str, "trim,"); |
| 609 | } |
| 610 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 611 | strcat(str, "explicit,"); |
| 612 | } |
| 613 | str[strlen(str) - 1] = '\0'; |
| 614 | |
| 615 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 620 | mod = ly_ctx_get_module(ctx, "nc-notifications", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 621 | if (mod) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 622 | 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] | 623 | if (server_opts.interleave_capab) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 624 | 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] | 625 | } |
| 626 | } |
| 627 | |
| 628 | /* models */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 629 | LY_TREE_FOR(yanglib->child, child) { |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 630 | if (!module_set_id) { |
| 631 | if (strcmp(child->prev->schema->name, "module-set-id")) { |
| 632 | ERRINT; |
| 633 | return NULL; |
| 634 | } |
| 635 | module_set_id = (struct lyd_node_leaf_list *)child->prev; |
| 636 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 637 | if (!strcmp(child->schema->name, "module")) { |
| 638 | LY_TREE_FOR(child->child, child2) { |
| 639 | if (!strcmp(child2->schema->name, "namespace")) { |
| 640 | ns = (struct lyd_node_leaf_list *)child2; |
| 641 | } else if (!strcmp(child2->schema->name, "name")) { |
| 642 | name = (struct lyd_node_leaf_list *)child2; |
| 643 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 644 | rev = (struct lyd_node_leaf_list *)child2; |
| 645 | } else if (!strcmp(child2->schema->name, "feature")) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 646 | features = nc_realloc(features, ++feat_count * sizeof *features); |
| 647 | if (!features) { |
| 648 | ERRMEM; |
| 649 | free(cpblts); |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 650 | free(deviations); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 653 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 654 | } else if (!strcmp(child2->schema->name, "deviation")) { |
| 655 | deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations); |
| 656 | if (!deviations) { |
| 657 | ERRMEM; |
| 658 | free(cpblts); |
| 659 | free(features); |
| 660 | return NULL; |
| 661 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 666 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 667 | continue; |
| 668 | } |
| 669 | |
Radek Krejci | df7ba52 | 2016-03-01 16:05:25 +0100 | [diff] [blame] | 670 | 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] | 671 | rev->value_str[0] ? "&revision=" : "", rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 672 | if (feat_count) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 673 | strcat(str, "&features="); |
| 674 | str_len += 10; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 675 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 676 | 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] | 677 | ERRINT; |
| 678 | break; |
| 679 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 680 | if (i) { |
| 681 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 682 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 683 | } |
| 684 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 685 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 686 | } |
| 687 | } |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 688 | if (dev_count) { |
| 689 | strcat(str, "&deviations="); |
| 690 | str_len += 12; |
| 691 | for (i = 0; i < dev_count; ++i) { |
| 692 | if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) { |
| 693 | ERRINT; |
| 694 | break; |
| 695 | } |
| 696 | if (i) { |
| 697 | strcat(str, ","); |
| 698 | ++str_len; |
| 699 | } |
| 700 | strcat(str, deviations[i]->value_str); |
| 701 | str_len += strlen(deviations[i]->value_str); |
| 702 | } |
| 703 | } |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 704 | if (!strcmp(name->value_str, "ietf-yang-library")) { |
| 705 | str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str); |
| 706 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 707 | |
| 708 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 709 | |
| 710 | ns = NULL; |
| 711 | name = NULL; |
| 712 | rev = NULL; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 713 | if (features || feat_count) { |
| 714 | free(features); |
| 715 | features = NULL; |
| 716 | feat_count = 0; |
| 717 | } |
| 718 | if (deviations || dev_count) { |
| 719 | free(deviations); |
| 720 | deviations = NULL; |
| 721 | dev_count = 0; |
| 722 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | lyd_free(yanglib); |
| 727 | |
| 728 | /* ending NULL capability */ |
| 729 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 730 | |
| 731 | return cpblts; |
| 732 | } |
| 733 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 734 | static int |
| 735 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 736 | { |
| 737 | struct lyxml_elem *cpblt; |
| 738 | int ver = -1; |
| 739 | int i = 0; |
| 740 | |
| 741 | if (list) { |
| 742 | /* get the storage for server's capabilities */ |
| 743 | LY_TREE_FOR(xml->child, cpblt) { |
| 744 | i++; |
| 745 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 746 | /* last item remains NULL */ |
| 747 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 748 | if (!*list) { |
| 749 | ERRMEM; |
| 750 | return -1; |
| 751 | } |
| 752 | i = 0; |
| 753 | } |
| 754 | |
| 755 | LY_TREE_FOR(xml->child, cpblt) { |
| 756 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 757 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 758 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 759 | return -1; |
| 760 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 761 | continue; |
| 762 | } |
| 763 | |
| 764 | /* detect NETCONF version */ |
| 765 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 766 | ver = 0; |
| 767 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 768 | ver = 1; |
| 769 | } |
| 770 | |
| 771 | /* store capabilities */ |
| 772 | if (list) { |
| 773 | (*list)[i] = cpblt->content; |
| 774 | cpblt->content = NULL; |
| 775 | i++; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 780 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | return ver; |
| 784 | } |
| 785 | |
| 786 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 787 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 788 | { |
| 789 | int r, i; |
| 790 | const char **cpblts; |
| 791 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 792 | /* client side hello - send only NETCONF base capabilities */ |
| 793 | cpblts = malloc(3 * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 794 | if (!cpblts) { |
| 795 | ERRMEM; |
| 796 | return NC_MSG_ERROR; |
| 797 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 798 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 799 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 800 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 801 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 802 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 803 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 804 | for (i = 0; cpblts[i]; ++i) { |
| 805 | lydict_remove(session->ctx, cpblts[i]); |
| 806 | } |
| 807 | free(cpblts); |
| 808 | |
| 809 | if (r) { |
| 810 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 811 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 812 | |
| 813 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 817 | nc_send_server_hello(struct nc_session *session) |
| 818 | { |
| 819 | int r, i; |
| 820 | const char **cpblts; |
| 821 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 822 | cpblts = nc_server_get_cpblts(session->ctx); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 823 | |
| 824 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 825 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 826 | for (i = 0; cpblts[i]; ++i) { |
| 827 | lydict_remove(session->ctx, cpblts[i]); |
| 828 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 829 | free(cpblts); |
| 830 | |
| 831 | if (r) { |
| 832 | return NC_MSG_ERROR; |
| 833 | } |
| 834 | |
| 835 | return NC_MSG_HELLO; |
| 836 | } |
| 837 | |
| 838 | static NC_MSG_TYPE |
| 839 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 840 | { |
| 841 | struct lyxml_elem *xml = NULL, *node; |
| 842 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 843 | int ver = -1; |
| 844 | char *str; |
| 845 | long long int id; |
| 846 | int flag = 0; |
| 847 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 848 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 849 | |
| 850 | switch(msgtype) { |
| 851 | case NC_MSG_HELLO: |
| 852 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 853 | LY_TREE_FOR(xml->child, node) { |
| 854 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 855 | continue; |
| 856 | } else if (!strcmp(node->name, "session-id")) { |
| 857 | if (!node->content || !strlen(node->content)) { |
| 858 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 859 | goto error; |
| 860 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 861 | str = NULL; |
| 862 | id = strtoll(node->content, &str, 10); |
| 863 | if (*str || id < 1 || id > UINT32_MAX) { |
| 864 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 865 | goto error; |
| 866 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 867 | session->id = (uint32_t)id; |
| 868 | continue; |
| 869 | } else if (strcmp(node->name, "capabilities")) { |
| 870 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 871 | goto error; |
| 872 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 873 | |
| 874 | if (flag) { |
| 875 | /* multiple capabilities elements */ |
| 876 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 877 | goto error; |
| 878 | } |
| 879 | flag = 1; |
| 880 | |
| 881 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 882 | goto error; |
| 883 | } |
| 884 | session->version = ver; |
| 885 | } |
| 886 | |
| 887 | if (!session->id) { |
| 888 | ERR("Missing <session-id> in server's <hello>."); |
| 889 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 890 | } |
| 891 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 892 | case NC_MSG_WOULDBLOCK: |
| 893 | ERR("Server's <hello> timeout elapsed."); |
| 894 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 895 | case NC_MSG_ERROR: |
| 896 | /* nothing special, just pass it out */ |
| 897 | break; |
| 898 | default: |
| 899 | ERR("Unexpected message received instead of <hello>."); |
| 900 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 901 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 905 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 906 | |
| 907 | return msgtype; |
| 908 | |
| 909 | error: |
| 910 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 911 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 912 | |
| 913 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 916 | static NC_MSG_TYPE |
| 917 | nc_recv_server_hello(struct nc_session *session) |
| 918 | { |
| 919 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 920 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 921 | int ver = -1; |
| 922 | int flag = 0; |
| 923 | |
Michal Vasko | adb850e | 2016-01-20 14:06:32 +0100 | [diff] [blame] | 924 | 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] | 925 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 926 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 927 | case NC_MSG_HELLO: |
| 928 | /* get know NETCONF version */ |
| 929 | LY_TREE_FOR(xml->child, node) { |
| 930 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 931 | continue; |
| 932 | } else if (strcmp(node->name, "capabilities")) { |
| 933 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 934 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 935 | goto cleanup; |
| 936 | } |
| 937 | |
| 938 | if (flag) { |
| 939 | /* multiple capabilities elements */ |
| 940 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 941 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 942 | goto cleanup; |
| 943 | } |
| 944 | flag = 1; |
| 945 | |
| 946 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 947 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 948 | goto cleanup; |
| 949 | } |
| 950 | session->version = ver; |
| 951 | } |
| 952 | break; |
| 953 | case NC_MSG_ERROR: |
| 954 | /* nothing special, just pass it out */ |
| 955 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 956 | case NC_MSG_WOULDBLOCK: |
| 957 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 958 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 959 | default: |
| 960 | ERR("Unexpected message received instead of <hello>."); |
| 961 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 962 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 966 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 967 | |
| 968 | return msgtype; |
| 969 | } |
| 970 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 971 | NC_MSG_TYPE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 972 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 973 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 974 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 975 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 976 | if (session->side == NC_CLIENT) { |
| 977 | type = nc_send_client_hello(session); |
| 978 | } else { |
| 979 | type = nc_send_server_hello(session); |
| 980 | } |
| 981 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 982 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 983 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 984 | } |
| 985 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 986 | if (session->side == NC_CLIENT) { |
| 987 | type = nc_recv_client_hello(session); |
| 988 | } else { |
| 989 | type = nc_recv_server_hello(session); |
| 990 | } |
| 991 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 992 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 993 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 994 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 995 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 996 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 997 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | nc_ssh_init(void) |
| 999 | { |
| 1000 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1001 | ssh_init(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1004 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1005 | nc_ssh_destroy(void) |
| 1006 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1007 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1008 | ENGINE_cleanup(); |
| 1009 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1010 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1011 | ssh_finalize(); |
| 1012 | } |
| 1013 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1014 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1015 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1016 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1017 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1018 | struct CRYPTO_dynlock_value { |
| 1019 | pthread_mutex_t lock; |
| 1020 | }; |
| 1021 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1022 | static struct CRYPTO_dynlock_value * |
| 1023 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1024 | { |
| 1025 | struct CRYPTO_dynlock_value *value; |
| 1026 | |
| 1027 | value = malloc(sizeof *value); |
| 1028 | if (!value) { |
| 1029 | ERRMEM; |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | pthread_mutex_init(&value->lock, NULL); |
| 1033 | |
| 1034 | return value; |
| 1035 | } |
| 1036 | |
| 1037 | static void |
| 1038 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1039 | { |
| 1040 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1041 | * I found ignored this fact, what do I know... */ |
| 1042 | if (mode & CRYPTO_LOCK) { |
| 1043 | pthread_mutex_lock(&l->lock); |
| 1044 | } else { |
| 1045 | pthread_mutex_unlock(&l->lock); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | static void |
| 1050 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1051 | { |
| 1052 | pthread_mutex_destroy(&l->lock); |
| 1053 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1056 | #endif /* NC_ENABLED_TLS */ |
| 1057 | |
| 1058 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1059 | |
| 1060 | static pthread_mutex_t *tls_locks; |
| 1061 | |
| 1062 | static void |
| 1063 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1064 | { |
| 1065 | if (mode & CRYPTO_LOCK) { |
| 1066 | pthread_mutex_lock(tls_locks + n); |
| 1067 | } else { |
| 1068 | pthread_mutex_unlock(tls_locks + n); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | static void |
| 1073 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1074 | { |
| 1075 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1076 | } |
| 1077 | |
| 1078 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1079 | nc_tls_init(void) |
| 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1084 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1085 | SSL_library_init(); |
| 1086 | |
| 1087 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1088 | if (!tls_locks) { |
| 1089 | ERRMEM; |
| 1090 | return; |
| 1091 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1092 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1093 | pthread_mutex_init(tls_locks + i, NULL); |
| 1094 | } |
| 1095 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1096 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1097 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1098 | |
| 1099 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1100 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1101 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1104 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1105 | nc_tls_destroy(void) |
| 1106 | { |
| 1107 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1108 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1109 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1110 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1111 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1112 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1113 | ERR_free_strings(); |
| 1114 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1115 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1116 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1117 | CRYPTO_set_locking_callback(NULL); |
| 1118 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1119 | pthread_mutex_destroy(tls_locks + i); |
| 1120 | } |
| 1121 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1122 | |
| 1123 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1124 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1125 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1128 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1129 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1130 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1131 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1132 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1133 | nc_ssh_tls_init(void) |
| 1134 | { |
| 1135 | SSL_load_error_strings(); |
| 1136 | ERR_load_BIO_strings(); |
| 1137 | SSL_library_init(); |
| 1138 | |
| 1139 | nc_ssh_init(); |
| 1140 | |
| 1141 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1142 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1143 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
| 1144 | } |
| 1145 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1146 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1147 | nc_ssh_tls_destroy(void) |
| 1148 | { |
| 1149 | ERR_free_strings(); |
| 1150 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 1151 | |
| 1152 | nc_ssh_destroy(); |
| 1153 | |
| 1154 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1155 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1156 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 1157 | } |
| 1158 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1159 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1160 | |
| 1161 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1162 | |
| 1163 | API void |
| 1164 | nc_thread_destroy(void) |
| 1165 | { |
| 1166 | CRYPTO_THREADID crypto_tid; |
| 1167 | |
| 1168 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1169 | //CRYPTO_cleanup_all_ex_data(); |
| 1170 | |
| 1171 | CRYPTO_THREADID_current(&crypto_tid); |
| 1172 | ERR_remove_thread_state(&crypto_tid); |
| 1173 | } |
| 1174 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1175 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1176 | |
| 1177 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1178 | nc_init(void) |
| 1179 | { |
| 1180 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1181 | nc_ssh_tls_init(); |
| 1182 | #elif defined(NC_ENABLED_SSH) |
| 1183 | nc_ssh_init(); |
| 1184 | #elif defined(NC_ENABLED_TLS) |
| 1185 | nc_tls_init(); |
| 1186 | #endif |
| 1187 | } |
| 1188 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1189 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1190 | nc_destroy(void) |
| 1191 | { |
| 1192 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1193 | nc_ssh_tls_destroy(); |
| 1194 | #elif defined(NC_ENABLED_SSH) |
| 1195 | nc_ssh_destroy(); |
| 1196 | #elif defined(NC_ENABLED_TLS) |
| 1197 | nc_tls_destroy(); |
| 1198 | #endif |
| 1199 | } |