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; |
Michal Vasko | a8a66b6 | 2016-10-05 14:14:19 +0200 | [diff] [blame] | 633 | free(cpblts); |
| 634 | free(deviations); |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | module_set_id = (struct lyd_node_leaf_list *)child->prev; |
| 638 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 639 | if (!strcmp(child->schema->name, "module")) { |
| 640 | LY_TREE_FOR(child->child, child2) { |
| 641 | if (!strcmp(child2->schema->name, "namespace")) { |
| 642 | ns = (struct lyd_node_leaf_list *)child2; |
| 643 | } else if (!strcmp(child2->schema->name, "name")) { |
| 644 | name = (struct lyd_node_leaf_list *)child2; |
| 645 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 646 | rev = (struct lyd_node_leaf_list *)child2; |
| 647 | } else if (!strcmp(child2->schema->name, "feature")) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 648 | features = nc_realloc(features, ++feat_count * sizeof *features); |
| 649 | if (!features) { |
| 650 | ERRMEM; |
| 651 | free(cpblts); |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 652 | free(deviations); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 655 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 656 | } else if (!strcmp(child2->schema->name, "deviation")) { |
| 657 | deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations); |
| 658 | if (!deviations) { |
| 659 | ERRMEM; |
| 660 | free(cpblts); |
| 661 | free(features); |
| 662 | return NULL; |
| 663 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 668 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 669 | continue; |
| 670 | } |
| 671 | |
Radek Krejci | df7ba52 | 2016-03-01 16:05:25 +0100 | [diff] [blame] | 672 | 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] | 673 | rev->value_str[0] ? "&revision=" : "", rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 674 | if (feat_count) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 675 | strcat(str, "&features="); |
| 676 | str_len += 10; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 677 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 678 | 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] | 679 | ERRINT; |
| 680 | break; |
| 681 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 682 | if (i) { |
| 683 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 684 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 685 | } |
| 686 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 687 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 688 | } |
| 689 | } |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 690 | if (dev_count) { |
| 691 | strcat(str, "&deviations="); |
| 692 | str_len += 12; |
| 693 | for (i = 0; i < dev_count; ++i) { |
| 694 | if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) { |
| 695 | ERRINT; |
| 696 | break; |
| 697 | } |
| 698 | if (i) { |
| 699 | strcat(str, ","); |
| 700 | ++str_len; |
| 701 | } |
| 702 | strcat(str, deviations[i]->value_str); |
| 703 | str_len += strlen(deviations[i]->value_str); |
| 704 | } |
| 705 | } |
Michal Vasko | dd9fe65 | 2016-09-14 09:24:32 +0200 | [diff] [blame] | 706 | if (!strcmp(name->value_str, "ietf-yang-library")) { |
| 707 | str_len += sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str); |
| 708 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 709 | |
| 710 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 711 | |
| 712 | ns = NULL; |
| 713 | name = NULL; |
| 714 | rev = NULL; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 715 | if (features || feat_count) { |
| 716 | free(features); |
| 717 | features = NULL; |
| 718 | feat_count = 0; |
| 719 | } |
| 720 | if (deviations || dev_count) { |
| 721 | free(deviations); |
| 722 | deviations = NULL; |
| 723 | dev_count = 0; |
| 724 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | lyd_free(yanglib); |
| 729 | |
| 730 | /* ending NULL capability */ |
| 731 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 732 | |
| 733 | return cpblts; |
| 734 | } |
| 735 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 736 | static int |
| 737 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 738 | { |
| 739 | struct lyxml_elem *cpblt; |
| 740 | int ver = -1; |
| 741 | int i = 0; |
| 742 | |
| 743 | if (list) { |
| 744 | /* get the storage for server's capabilities */ |
| 745 | LY_TREE_FOR(xml->child, cpblt) { |
| 746 | i++; |
| 747 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 748 | /* last item remains NULL */ |
| 749 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 750 | if (!*list) { |
| 751 | ERRMEM; |
| 752 | return -1; |
| 753 | } |
| 754 | i = 0; |
| 755 | } |
| 756 | |
| 757 | LY_TREE_FOR(xml->child, cpblt) { |
| 758 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 759 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 760 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 761 | return -1; |
| 762 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | /* detect NETCONF version */ |
| 767 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 768 | ver = 0; |
| 769 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 770 | ver = 1; |
| 771 | } |
| 772 | |
| 773 | /* store capabilities */ |
| 774 | if (list) { |
| 775 | (*list)[i] = cpblt->content; |
| 776 | cpblt->content = NULL; |
| 777 | i++; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 782 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | return ver; |
| 786 | } |
| 787 | |
| 788 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 789 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 790 | { |
| 791 | int r, i; |
| 792 | const char **cpblts; |
| 793 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 794 | /* client side hello - send only NETCONF base capabilities */ |
| 795 | cpblts = malloc(3 * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 796 | if (!cpblts) { |
| 797 | ERRMEM; |
| 798 | return NC_MSG_ERROR; |
| 799 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 800 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 801 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 802 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 803 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 804 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 805 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 806 | for (i = 0; cpblts[i]; ++i) { |
| 807 | lydict_remove(session->ctx, cpblts[i]); |
| 808 | } |
| 809 | free(cpblts); |
| 810 | |
| 811 | if (r) { |
| 812 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 813 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 814 | |
| 815 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 819 | nc_send_server_hello(struct nc_session *session) |
| 820 | { |
| 821 | int r, i; |
| 822 | const char **cpblts; |
| 823 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 824 | cpblts = nc_server_get_cpblts(session->ctx); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 825 | |
| 826 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 827 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 828 | for (i = 0; cpblts[i]; ++i) { |
| 829 | lydict_remove(session->ctx, cpblts[i]); |
| 830 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 831 | free(cpblts); |
| 832 | |
| 833 | if (r) { |
| 834 | return NC_MSG_ERROR; |
| 835 | } |
| 836 | |
| 837 | return NC_MSG_HELLO; |
| 838 | } |
| 839 | |
| 840 | static NC_MSG_TYPE |
| 841 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 842 | { |
| 843 | struct lyxml_elem *xml = NULL, *node; |
| 844 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 845 | int ver = -1; |
| 846 | char *str; |
| 847 | long long int id; |
| 848 | int flag = 0; |
| 849 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 850 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 851 | |
| 852 | switch(msgtype) { |
| 853 | case NC_MSG_HELLO: |
| 854 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 855 | LY_TREE_FOR(xml->child, node) { |
| 856 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 857 | continue; |
| 858 | } else if (!strcmp(node->name, "session-id")) { |
| 859 | if (!node->content || !strlen(node->content)) { |
| 860 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 861 | goto error; |
| 862 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 863 | str = NULL; |
| 864 | id = strtoll(node->content, &str, 10); |
| 865 | if (*str || id < 1 || id > UINT32_MAX) { |
| 866 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 867 | goto error; |
| 868 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 869 | session->id = (uint32_t)id; |
| 870 | continue; |
| 871 | } else if (strcmp(node->name, "capabilities")) { |
| 872 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 873 | goto error; |
| 874 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 875 | |
| 876 | if (flag) { |
| 877 | /* multiple capabilities elements */ |
| 878 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 879 | goto error; |
| 880 | } |
| 881 | flag = 1; |
| 882 | |
| 883 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 884 | goto error; |
| 885 | } |
| 886 | session->version = ver; |
| 887 | } |
| 888 | |
| 889 | if (!session->id) { |
| 890 | ERR("Missing <session-id> in server's <hello>."); |
| 891 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 892 | } |
| 893 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 894 | case NC_MSG_WOULDBLOCK: |
| 895 | ERR("Server's <hello> timeout elapsed."); |
| 896 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 897 | case NC_MSG_ERROR: |
| 898 | /* nothing special, just pass it out */ |
| 899 | break; |
| 900 | default: |
| 901 | ERR("Unexpected message received instead of <hello>."); |
| 902 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 903 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 907 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 908 | |
| 909 | return msgtype; |
| 910 | |
| 911 | error: |
| 912 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 913 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 914 | |
| 915 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 918 | static NC_MSG_TYPE |
| 919 | nc_recv_server_hello(struct nc_session *session) |
| 920 | { |
| 921 | struct lyxml_elem *xml = NULL, *node; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 922 | NC_MSG_TYPE msgtype; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 923 | int ver = -1; |
| 924 | int flag = 0; |
| 925 | |
Michal Vasko | adb850e | 2016-01-20 14:06:32 +0100 | [diff] [blame] | 926 | 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] | 927 | |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 928 | switch (msgtype) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 929 | case NC_MSG_HELLO: |
| 930 | /* get know NETCONF version */ |
| 931 | LY_TREE_FOR(xml->child, node) { |
| 932 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 933 | continue; |
| 934 | } else if (strcmp(node->name, "capabilities")) { |
| 935 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 936 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 937 | goto cleanup; |
| 938 | } |
| 939 | |
| 940 | if (flag) { |
| 941 | /* multiple capabilities elements */ |
| 942 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 943 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 944 | goto cleanup; |
| 945 | } |
| 946 | flag = 1; |
| 947 | |
| 948 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 949 | msgtype = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 950 | goto cleanup; |
| 951 | } |
| 952 | session->version = ver; |
| 953 | } |
| 954 | break; |
| 955 | case NC_MSG_ERROR: |
| 956 | /* nothing special, just pass it out */ |
| 957 | break; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 958 | case NC_MSG_WOULDBLOCK: |
| 959 | ERR("Client's <hello> timeout elapsed."); |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 960 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 961 | default: |
| 962 | ERR("Unexpected message received instead of <hello>."); |
| 963 | msgtype = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 964 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | cleanup: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 968 | lyxml_free(session->ctx, xml); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 969 | |
| 970 | return msgtype; |
| 971 | } |
| 972 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 973 | NC_MSG_TYPE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 974 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 975 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 976 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 977 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 978 | if (session->side == NC_CLIENT) { |
| 979 | type = nc_send_client_hello(session); |
| 980 | } else { |
| 981 | type = nc_send_server_hello(session); |
| 982 | } |
| 983 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 984 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 985 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 988 | if (session->side == NC_CLIENT) { |
| 989 | type = nc_recv_client_hello(session); |
| 990 | } else { |
| 991 | type = nc_recv_server_hello(session); |
| 992 | } |
| 993 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 994 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 995 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 996 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 997 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 999 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1000 | nc_ssh_init(void) |
| 1001 | { |
| 1002 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1003 | ssh_init(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1006 | static void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1007 | nc_ssh_destroy(void) |
| 1008 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1009 | FIPS_mode_set(0); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1010 | ENGINE_cleanup(); |
| 1011 | CONF_modules_unload(1); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1012 | nc_thread_destroy(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1013 | ssh_finalize(); |
| 1014 | } |
| 1015 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1016 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1017 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1018 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1019 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1020 | struct CRYPTO_dynlock_value { |
| 1021 | pthread_mutex_t lock; |
| 1022 | }; |
| 1023 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1024 | static struct CRYPTO_dynlock_value * |
| 1025 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1026 | { |
| 1027 | struct CRYPTO_dynlock_value *value; |
| 1028 | |
| 1029 | value = malloc(sizeof *value); |
| 1030 | if (!value) { |
| 1031 | ERRMEM; |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | pthread_mutex_init(&value->lock, NULL); |
| 1035 | |
| 1036 | return value; |
| 1037 | } |
| 1038 | |
| 1039 | static void |
| 1040 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1041 | { |
| 1042 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1043 | * I found ignored this fact, what do I know... */ |
| 1044 | if (mode & CRYPTO_LOCK) { |
| 1045 | pthread_mutex_lock(&l->lock); |
| 1046 | } else { |
| 1047 | pthread_mutex_unlock(&l->lock); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | static void |
| 1052 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1053 | { |
| 1054 | pthread_mutex_destroy(&l->lock); |
| 1055 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1058 | #endif /* NC_ENABLED_TLS */ |
| 1059 | |
| 1060 | #if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH) |
| 1061 | |
| 1062 | static pthread_mutex_t *tls_locks; |
| 1063 | |
| 1064 | static void |
| 1065 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1066 | { |
| 1067 | if (mode & CRYPTO_LOCK) { |
| 1068 | pthread_mutex_lock(tls_locks + n); |
| 1069 | } else { |
| 1070 | pthread_mutex_unlock(tls_locks + n); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | static void |
| 1075 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1076 | { |
| 1077 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 1078 | } |
| 1079 | |
| 1080 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1081 | nc_tls_init(void) |
| 1082 | { |
| 1083 | int i; |
| 1084 | |
| 1085 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1086 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1087 | SSL_library_init(); |
| 1088 | |
| 1089 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1090 | if (!tls_locks) { |
| 1091 | ERRMEM; |
| 1092 | return; |
| 1093 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1094 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1095 | pthread_mutex_init(tls_locks + i, NULL); |
| 1096 | } |
| 1097 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1098 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1099 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1100 | |
| 1101 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1102 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1103 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1106 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1107 | nc_tls_destroy(void) |
| 1108 | { |
| 1109 | int i; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1110 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1111 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1112 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1113 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1114 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1115 | ERR_free_strings(); |
| 1116 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1117 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1118 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1119 | CRYPTO_set_locking_callback(NULL); |
| 1120 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1121 | pthread_mutex_destroy(tls_locks + i); |
| 1122 | } |
| 1123 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1124 | |
| 1125 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1126 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1127 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1130 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1131 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1132 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1133 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1134 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1135 | nc_ssh_tls_init(void) |
| 1136 | { |
| 1137 | SSL_load_error_strings(); |
| 1138 | ERR_load_BIO_strings(); |
| 1139 | SSL_library_init(); |
| 1140 | |
| 1141 | nc_ssh_init(); |
| 1142 | |
| 1143 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1144 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1145 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
| 1146 | } |
| 1147 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1148 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1149 | nc_ssh_tls_destroy(void) |
| 1150 | { |
| 1151 | ERR_free_strings(); |
| 1152 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 1153 | |
| 1154 | nc_ssh_destroy(); |
| 1155 | |
| 1156 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1157 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1158 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 1159 | } |
| 1160 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1161 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1162 | |
| 1163 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1164 | |
| 1165 | API void |
| 1166 | nc_thread_destroy(void) |
| 1167 | { |
| 1168 | CRYPTO_THREADID crypto_tid; |
| 1169 | |
| 1170 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
| 1171 | //CRYPTO_cleanup_all_ex_data(); |
| 1172 | |
| 1173 | CRYPTO_THREADID_current(&crypto_tid); |
| 1174 | ERR_remove_thread_state(&crypto_tid); |
| 1175 | } |
| 1176 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1177 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1178 | |
| 1179 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1180 | nc_init(void) |
| 1181 | { |
| 1182 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1183 | nc_ssh_tls_init(); |
| 1184 | #elif defined(NC_ENABLED_SSH) |
| 1185 | nc_ssh_init(); |
| 1186 | #elif defined(NC_ENABLED_TLS) |
| 1187 | nc_tls_init(); |
| 1188 | #endif |
| 1189 | } |
| 1190 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1191 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1192 | nc_destroy(void) |
| 1193 | { |
| 1194 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
| 1195 | nc_ssh_tls_destroy(); |
| 1196 | #elif defined(NC_ENABLED_SSH) |
| 1197 | nc_ssh_destroy(); |
| 1198 | #elif defined(NC_ENABLED_TLS) |
| 1199 | nc_tls_destroy(); |
| 1200 | #endif |
| 1201 | } |