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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 23 | #include <errno.h> |
Radek Krejci | 952eb86 | 2016-01-08 14:22:55 +0100 | [diff] [blame] | 24 | #include <stdlib.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 25 | #include <pthread.h> |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 26 | #include <time.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 27 | #include <libyang/libyang.h> |
| 28 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 29 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 30 | #include "session_server.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 31 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 32 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 33 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 34 | # include <libssh/libssh.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 35 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | #endif /* ENABLE_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 37 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 38 | #ifdef ENABLE_TLS |
| 39 | |
| 40 | # include <openssl/err.h> |
| 41 | |
| 42 | #endif /* ENABLE_TLS */ |
| 43 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 44 | /* in seconds */ |
| 45 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 46 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 47 | /* in milliseconds */ |
| 48 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 49 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 50 | extern struct nc_server_opts server_opts; |
| 51 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 52 | API NC_STATUS |
| 53 | nc_session_get_status(const struct nc_session *session) |
| 54 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 55 | if (!session) { |
| 56 | ERRARG; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 60 | return session->status; |
| 61 | } |
| 62 | |
| 63 | API uint32_t |
| 64 | nc_session_get_id(const struct nc_session *session) |
| 65 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 66 | if (!session) { |
| 67 | ERRARG; |
| 68 | return 0; |
| 69 | } |
| 70 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 71 | return session->id; |
| 72 | } |
| 73 | |
| 74 | API NC_TRANSPORT_IMPL |
| 75 | nc_session_get_ti(const struct nc_session *session) |
| 76 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 77 | if (!session) { |
| 78 | ERRARG; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 82 | return session->ti_type; |
| 83 | } |
| 84 | |
| 85 | API const char * |
| 86 | nc_session_get_username(const struct nc_session *session) |
| 87 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 88 | if (!session) { |
| 89 | ERRARG; |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 93 | return session->username; |
| 94 | } |
| 95 | |
| 96 | API const char * |
| 97 | nc_session_get_host(const struct nc_session *session) |
| 98 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 99 | if (!session) { |
| 100 | ERRARG; |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 104 | return session->host; |
| 105 | } |
| 106 | |
| 107 | API uint16_t |
| 108 | nc_session_get_port(const struct nc_session *session) |
| 109 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 110 | if (!session) { |
| 111 | ERRARG; |
| 112 | return 0; |
| 113 | } |
| 114 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 115 | return session->port; |
| 116 | } |
| 117 | |
| 118 | API const char ** |
| 119 | nc_session_get_cpblts(const struct nc_session *session) |
| 120 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 121 | if (!session) { |
| 122 | ERRARG; |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 126 | return session->cpblts; |
| 127 | } |
| 128 | |
| 129 | API const char * |
| 130 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 131 | { |
| 132 | int i, len; |
| 133 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 134 | if (!session || !capab) { |
| 135 | ERRARG; |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 139 | len = strlen(capab); |
| 140 | for (i = 0; session->cpblts[i]; ++i) { |
| 141 | if (!strncmp(session->cpblts[i], capab, len)) { |
| 142 | return session->cpblts[i]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 149 | NC_MSG_TYPE |
| 150 | nc_send_msg(struct nc_session *session, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 151 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 152 | int r; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 153 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 154 | if (session->ctx != op->schema->module->ctx) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 155 | ERR("Session %u: RPC \"%s\" was created in different context than that of the session.", |
| 156 | session->id, op->schema->name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 157 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 160 | r = nc_write_msg(session, NC_MSG_RPC, op, NULL); |
| 161 | |
| 162 | if (r) { |
| 163 | return NC_MSG_ERROR; |
| 164 | } |
| 165 | |
| 166 | return NC_MSG_RPC; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 169 | /* |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 170 | * @return 1 - success |
| 171 | * 0 - timeout |
| 172 | * -1 - error |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 173 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 174 | int |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 175 | nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 176 | { |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 177 | int ret; |
| 178 | struct timespec ts_timeout, ts_old, ts_new; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 179 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 180 | if (timeout > 0) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 181 | clock_gettime(CLOCK_REALTIME, &ts_timeout); |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 182 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 183 | if (elapsed) { |
| 184 | ts_old = ts_timeout; |
| 185 | } |
| 186 | |
| 187 | ts_timeout.tv_sec += timeout / 1000; |
| 188 | ts_timeout.tv_nsec += (timeout % 1000) * 1000000; |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 189 | |
| 190 | ret = pthread_mutex_timedlock(lock, &ts_timeout); |
| 191 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 192 | if (elapsed) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 193 | clock_gettime(CLOCK_REALTIME, &ts_new); |
| 194 | |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 195 | *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000; |
| 196 | *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 197 | } |
| 198 | } else if (!timeout) { |
| 199 | ret = pthread_mutex_trylock(lock); |
| 200 | } else { /* timeout == -1 */ |
| 201 | ret = pthread_mutex_lock(lock); |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 202 | } |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 203 | |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 204 | if (ret == ETIMEDOUT) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 205 | /* timeout */ |
| 206 | return 0; |
| 207 | } else if (ret) { |
| 208 | /* error */ |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 209 | ERR("Mutex lock failed (%s).", strerror(errno)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | /* ok */ |
| 214 | return 1; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 217 | API void |
| 218 | nc_session_free(struct nc_session *session) |
| 219 | { |
| 220 | int r, i; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 221 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 222 | int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 223 | struct nc_session *siter; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 224 | struct nc_msg_cont *contiter; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 225 | struct lyxml_elem *rpl, *child; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 226 | struct lyd_node *close_rpc; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 227 | const struct lys_module *ietfnc; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 228 | void *p; |
| 229 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 230 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | /* mark session for closing */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 235 | if (session->ti_lock) { |
| 236 | do { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 237 | r = nc_timedlock(session->ti_lock, 0, NULL); |
| 238 | } while (!r); |
| 239 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 240 | return; |
| 241 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* stop notifications loop if any */ |
| 245 | if (session->notif) { |
| 246 | pthread_cancel(*session->notif); |
| 247 | pthread_join(*session->notif, NULL); |
| 248 | } |
| 249 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 250 | if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 251 | /* cleanup message queues */ |
| 252 | /* notifications */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 253 | for (contiter = session->notifs; contiter; ) { |
| 254 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 255 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 256 | p = contiter; |
| 257 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 258 | free(p); |
| 259 | } |
| 260 | |
| 261 | /* rpc replies */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 262 | for (contiter = session->replies; contiter; ) { |
| 263 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 264 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 265 | p = contiter; |
| 266 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 267 | free(p); |
| 268 | } |
| 269 | |
| 270 | /* send closing info to the other side */ |
| 271 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 272 | if (!ietfnc) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 273 | 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] | 274 | } else { |
| 275 | close_rpc = lyd_new(NULL, ietfnc, "close-session"); |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 276 | nc_send_msg(session, close_rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 277 | lyd_free(close_rpc); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 278 | switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) { |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 279 | case NC_MSG_REPLY: |
| 280 | LY_TREE_FOR(rpl->child, child) { |
| 281 | if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) { |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | if (!child) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 286 | 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] | 287 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 288 | lyxml_free(session->ctx, rpl); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 289 | break; |
| 290 | case NC_MSG_WOULDBLOCK: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 291 | 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] | 292 | break; |
| 293 | case NC_MSG_ERROR: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 294 | 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] | 295 | break; |
| 296 | default: |
| 297 | /* cannot happen */ |
| 298 | break; |
| 299 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* list of server's capabilities */ |
| 303 | if (session->cpblts) { |
| 304 | for (i = 0; session->cpblts[i]; i++) { |
| 305 | lydict_remove(session->ctx, session->cpblts[i]); |
| 306 | } |
| 307 | free(session->cpblts); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 312 | connected = nc_session_is_connected(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 313 | |
| 314 | /* transport implementation cleanup */ |
| 315 | switch (session->ti_type) { |
| 316 | case NC_TI_FD: |
| 317 | /* nothing needed - file descriptors were provided by caller, |
| 318 | * so it is up to the caller to close them correctly |
| 319 | * TODO use callbacks |
| 320 | */ |
| 321 | break; |
| 322 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 323 | #ifdef ENABLE_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 324 | case NC_TI_LIBSSH: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 325 | if (connected) { |
| 326 | ssh_channel_free(session->ti.libssh.channel); |
| 327 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 328 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 329 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 330 | * it. |
| 331 | */ |
| 332 | if (!session->ti.libssh.next) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 333 | if (connected) { |
| 334 | ssh_disconnect(session->ti.libssh.session); |
| 335 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 336 | ssh_free(session->ti.libssh.session); |
| 337 | } else { |
| 338 | /* multiple NETCONF sessions on a single SSH session */ |
| 339 | multisession = 1; |
| 340 | /* remove the session from the list */ |
| 341 | 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] | 342 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 343 | /* there will be only one session */ |
| 344 | siter->ti.libssh.next = NULL; |
| 345 | } else { |
| 346 | /* there are still multiple sessions, keep the ring list */ |
| 347 | siter->ti.libssh.next = session->ti.libssh.next; |
| 348 | } |
| 349 | } |
| 350 | break; |
| 351 | #endif |
| 352 | |
| 353 | #ifdef ENABLE_TLS |
| 354 | case NC_TI_OPENSSL: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 355 | if (connected) { |
| 356 | SSL_shutdown(session->ti.tls); |
| 357 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 358 | SSL_free(session->ti.tls); |
Michal Vasko | 06e2243 | 2016-01-15 10:30:06 +0100 | [diff] [blame] | 359 | |
| 360 | X509_free(session->tls_cert); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 361 | break; |
| 362 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 363 | case NC_TI_NONE: |
| 364 | ERRINT; |
| 365 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 366 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 367 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 368 | if (session->side == NC_SERVER) { |
| 369 | nc_ctx_lock(-1, NULL); |
| 370 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 371 | lydict_remove(session->ctx, session->username); |
| 372 | lydict_remove(session->ctx, session->host); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 373 | if (session->side == NC_SERVER) { |
| 374 | nc_ctx_unlock(); |
| 375 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 376 | |
| 377 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 378 | if (session->ti_lock) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 379 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 380 | if (!multisession) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 381 | pthread_mutex_destroy(session->ti_lock); |
| 382 | free(session->ti_lock); |
| 383 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
| 387 | ly_ctx_destroy(session->ctx); |
| 388 | } |
| 389 | |
| 390 | free(session); |
| 391 | } |
| 392 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 393 | static void |
| 394 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 395 | { |
| 396 | if (*count == *size) { |
| 397 | *size += 5; |
| 398 | *cpblts = realloc(*cpblts, *size * sizeof **cpblts); |
| 399 | } |
| 400 | |
| 401 | if (capab) { |
| 402 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 403 | } else { |
| 404 | (*cpblts)[*count] = NULL; |
| 405 | } |
| 406 | ++(*count); |
| 407 | } |
| 408 | |
| 409 | static const char ** |
| 410 | create_cpblts(struct ly_ctx *ctx) |
| 411 | { |
| 412 | struct lyd_node *child, *child2, *yanglib; |
| 413 | struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL; |
| 414 | const char **cpblts; |
| 415 | const struct lys_module *mod; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 416 | int size = 10, count, feat_count = 0, i, str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 417 | char str[512]; |
| 418 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 419 | nc_ctx_lock(-1, NULL); |
| 420 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 421 | yanglib = ly_ctx_info(ctx); |
| 422 | if (!yanglib) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 423 | nc_ctx_unlock(); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | cpblts = malloc(size * sizeof *cpblts); |
| 428 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 429 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 430 | count = 2; |
| 431 | |
| 432 | /* capabilities */ |
| 433 | |
| 434 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 435 | if (mod) { |
| 436 | if (lys_features_state(mod, "writable-running") == 1) { |
| 437 | add_cpblt(ctx, "urn:ietf:params:netconf:writable-running:1.0", &cpblts, &size, &count); |
| 438 | } |
| 439 | if (lys_features_state(mod, "candidate") == 1) { |
| 440 | add_cpblt(ctx, "urn:ietf:params:netconf:candidate:1.0", &cpblts, &size, &count); |
| 441 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
| 442 | add_cpblt(ctx, "urn:ietf:params:netconf:confirmed-commit:1.1", &cpblts, &size, &count); |
| 443 | } |
| 444 | } |
| 445 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
| 446 | add_cpblt(ctx, "urn:ietf:params:netconf:rollback-on-error:1.0", &cpblts, &size, &count); |
| 447 | } |
| 448 | if (lys_features_state(mod, "validate") == 1) { |
| 449 | add_cpblt(ctx, "urn:ietf:params:netconf:validate:1.1", &cpblts, &size, &count); |
| 450 | } |
| 451 | if (lys_features_state(mod, "startup") == 1) { |
| 452 | add_cpblt(ctx, "urn:ietf:params:netconf:startup:1.0", &cpblts, &size, &count); |
| 453 | } |
| 454 | if (lys_features_state(mod, "url") == 1) { |
| 455 | add_cpblt(ctx, "urn:ietf:params:netconf:url:1.0", &cpblts, &size, &count); |
| 456 | } |
| 457 | if (lys_features_state(mod, "xpath") == 1) { |
| 458 | add_cpblt(ctx, "urn:ietf:params:netconf:xpath:1.0", &cpblts, &size, &count); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 463 | if (mod) { |
| 464 | if (!server_opts.wd_basic_mode) { |
| 465 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 466 | } else { |
| 467 | strcpy(str, "urn:ietf:params:netconf:with-defaults:1.0"); |
| 468 | switch (server_opts.wd_basic_mode) { |
| 469 | case NC_WD_ALL: |
| 470 | strcat(str, "?basic-mode=report-all"); |
| 471 | break; |
| 472 | case NC_WD_TRIM: |
| 473 | strcat(str, "?basic-mode=trim"); |
| 474 | break; |
| 475 | case NC_WD_EXPLICIT: |
| 476 | strcat(str, "?basic-mode=explicit"); |
| 477 | break; |
| 478 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 479 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | |
| 483 | if (server_opts.wd_also_supported) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 484 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 485 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 486 | strcat(str, "report-all,"); |
| 487 | } |
| 488 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 489 | strcat(str, "report-all-tagged,"); |
| 490 | } |
| 491 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 492 | strcat(str, "trim,"); |
| 493 | } |
| 494 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 495 | strcat(str, "explicit,"); |
| 496 | } |
| 497 | str[strlen(str) - 1] = '\0'; |
| 498 | |
| 499 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 504 | mod = ly_ctx_get_module(ctx, "nc-notifications", NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 505 | if (mod) { |
| 506 | add_cpblt(ctx, "urn:ietf:params:netconf:notification:1.0", &cpblts, &size, &count); |
| 507 | if (server_opts.interleave_capab) { |
| 508 | add_cpblt(ctx, "urn:ietf:params:netconf:interleave:1.0", &cpblts, &size, &count); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* models */ |
| 513 | |
| 514 | LY_TREE_FOR(yanglib->child, child) { |
| 515 | if (!strcmp(child->schema->name, "module")) { |
| 516 | LY_TREE_FOR(child->child, child2) { |
| 517 | if (!strcmp(child2->schema->name, "namespace")) { |
| 518 | ns = (struct lyd_node_leaf_list *)child2; |
| 519 | } else if (!strcmp(child2->schema->name, "name")) { |
| 520 | name = (struct lyd_node_leaf_list *)child2; |
| 521 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 522 | rev = (struct lyd_node_leaf_list *)child2; |
| 523 | } else if (!strcmp(child2->schema->name, "feature")) { |
| 524 | features = realloc(features, feat_count++ * sizeof *features); |
| 525 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 530 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 531 | continue; |
| 532 | } |
| 533 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 534 | str_len = sprintf(str, "%s?module=%s&revision=%s", ns->value_str, name->value_str, rev->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 535 | if (feat_count) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame] | 536 | strcat(str, "&features="); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 537 | str_len += 14; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 538 | for (i = 0; i < feat_count; ++i) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 539 | if (str_len + 1 + strlen(features[i]->value_str) >= 512) { |
| 540 | ERRINT; |
| 541 | break; |
| 542 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 543 | if (i) { |
| 544 | strcat(str, ","); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 545 | ++str_len; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 546 | } |
| 547 | strcat(str, features[i]->value_str); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 548 | str_len += strlen(features[i]->value_str); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 553 | |
| 554 | ns = NULL; |
| 555 | name = NULL; |
| 556 | rev = NULL; |
| 557 | free(features); |
| 558 | features = NULL; |
| 559 | feat_count = 0; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | lyd_free(yanglib); |
| 564 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 565 | nc_ctx_unlock(); |
| 566 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 567 | /* ending NULL capability */ |
| 568 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 569 | |
| 570 | return cpblts; |
| 571 | } |
| 572 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 573 | static int |
| 574 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 575 | { |
| 576 | struct lyxml_elem *cpblt; |
| 577 | int ver = -1; |
| 578 | int i = 0; |
| 579 | |
| 580 | if (list) { |
| 581 | /* get the storage for server's capabilities */ |
| 582 | LY_TREE_FOR(xml->child, cpblt) { |
| 583 | i++; |
| 584 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 585 | /* last item remains NULL */ |
| 586 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 587 | if (!*list) { |
| 588 | ERRMEM; |
| 589 | return -1; |
| 590 | } |
| 591 | i = 0; |
| 592 | } |
| 593 | |
| 594 | LY_TREE_FOR(xml->child, cpblt) { |
| 595 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 596 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 597 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 598 | return -1; |
| 599 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | /* detect NETCONF version */ |
| 604 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 605 | ver = 0; |
| 606 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 607 | ver = 1; |
| 608 | } |
| 609 | |
| 610 | /* store capabilities */ |
| 611 | if (list) { |
| 612 | (*list)[i] = cpblt->content; |
| 613 | cpblt->content = NULL; |
| 614 | i++; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (ver == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 619 | ERR("Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | return ver; |
| 623 | } |
| 624 | |
| 625 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 626 | nc_send_client_hello(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 627 | { |
| 628 | int r, i; |
| 629 | const char **cpblts; |
| 630 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 631 | /* client side hello - send only NETCONF base capabilities */ |
| 632 | cpblts = malloc(3 * sizeof *cpblts); |
| 633 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 634 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 635 | cpblts[2] = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 636 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 637 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 638 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 639 | for (i = 0; cpblts[i]; ++i) { |
| 640 | lydict_remove(session->ctx, cpblts[i]); |
| 641 | } |
| 642 | free(cpblts); |
| 643 | |
| 644 | if (r) { |
| 645 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 646 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 647 | |
| 648 | return NC_MSG_HELLO; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static NC_MSG_TYPE |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 652 | nc_send_server_hello(struct nc_session *session) |
| 653 | { |
| 654 | int r, i; |
| 655 | const char **cpblts; |
| 656 | |
| 657 | cpblts = create_cpblts(session->ctx); |
| 658 | |
| 659 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id); |
| 660 | |
| 661 | nc_ctx_lock(-1, NULL); |
| 662 | for (i = 0; cpblts[i]; ++i) { |
| 663 | lydict_remove(session->ctx, cpblts[i]); |
| 664 | } |
| 665 | nc_ctx_unlock(); |
| 666 | free(cpblts); |
| 667 | |
| 668 | if (r) { |
| 669 | return NC_MSG_ERROR; |
| 670 | } |
| 671 | |
| 672 | return NC_MSG_HELLO; |
| 673 | } |
| 674 | |
| 675 | static NC_MSG_TYPE |
| 676 | nc_recv_client_hello(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 677 | { |
| 678 | struct lyxml_elem *xml = NULL, *node; |
| 679 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 680 | int ver = -1; |
| 681 | char *str; |
| 682 | long long int id; |
| 683 | int flag = 0; |
| 684 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 685 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 686 | |
| 687 | switch(msgtype) { |
| 688 | case NC_MSG_HELLO: |
| 689 | /* parse <hello> data */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 690 | LY_TREE_FOR(xml->child, node) { |
| 691 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 692 | continue; |
| 693 | } else if (!strcmp(node->name, "session-id")) { |
| 694 | if (!node->content || !strlen(node->content)) { |
| 695 | ERR("No value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 696 | goto error; |
| 697 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 698 | str = NULL; |
| 699 | id = strtoll(node->content, &str, 10); |
| 700 | if (*str || id < 1 || id > UINT32_MAX) { |
| 701 | ERR("Invalid value of <session-id> element in server's <hello>."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 702 | goto error; |
| 703 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 704 | session->id = (uint32_t)id; |
| 705 | continue; |
| 706 | } else if (strcmp(node->name, "capabilities")) { |
| 707 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 708 | goto error; |
| 709 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 710 | |
| 711 | if (flag) { |
| 712 | /* multiple capabilities elements */ |
| 713 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 714 | goto error; |
| 715 | } |
| 716 | flag = 1; |
| 717 | |
| 718 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
| 719 | goto error; |
| 720 | } |
| 721 | session->version = ver; |
| 722 | } |
| 723 | |
| 724 | if (!session->id) { |
| 725 | ERR("Missing <session-id> in server's <hello>."); |
| 726 | goto error; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 727 | } |
| 728 | break; |
| 729 | case NC_MSG_ERROR: |
| 730 | /* nothing special, just pass it out */ |
| 731 | break; |
| 732 | default: |
| 733 | ERR("Unexpected message received instead of <hello>."); |
| 734 | msgtype = NC_MSG_ERROR; |
| 735 | } |
| 736 | |
| 737 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 738 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 739 | |
| 740 | return msgtype; |
| 741 | |
| 742 | error: |
| 743 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 744 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 745 | |
| 746 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 749 | static NC_MSG_TYPE |
| 750 | nc_recv_server_hello(struct nc_session *session) |
| 751 | { |
| 752 | struct lyxml_elem *xml = NULL, *node; |
| 753 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 754 | int ver = -1; |
| 755 | int flag = 0; |
| 756 | |
| 757 | /* TODO */ |
| 758 | msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
| 759 | |
| 760 | switch(msgtype) { |
| 761 | case NC_MSG_HELLO: |
| 762 | /* get know NETCONF version */ |
| 763 | LY_TREE_FOR(xml->child, node) { |
| 764 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 765 | continue; |
| 766 | } else if (strcmp(node->name, "capabilities")) { |
| 767 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
| 768 | msgtype = NC_MSG_ERROR; |
| 769 | goto cleanup; |
| 770 | } |
| 771 | |
| 772 | if (flag) { |
| 773 | /* multiple capabilities elements */ |
| 774 | ERR("Invalid <hello> message (multiple <capabilities> elements)."); |
| 775 | msgtype = NC_MSG_ERROR; |
| 776 | goto cleanup; |
| 777 | } |
| 778 | flag = 1; |
| 779 | |
| 780 | if ((ver = parse_cpblts(node, NULL)) < 0) { |
| 781 | msgtype = NC_MSG_ERROR; |
| 782 | goto cleanup; |
| 783 | } |
| 784 | session->version = ver; |
| 785 | } |
| 786 | break; |
| 787 | case NC_MSG_ERROR: |
| 788 | /* nothing special, just pass it out */ |
| 789 | break; |
| 790 | default: |
| 791 | ERR("Unexpected message received instead of <hello>."); |
| 792 | msgtype = NC_MSG_ERROR; |
| 793 | } |
| 794 | |
| 795 | cleanup: |
| 796 | nc_ctx_lock(-1, NULL); |
| 797 | lyxml_free(session->ctx, xml); |
| 798 | nc_ctx_unlock(); |
| 799 | |
| 800 | return msgtype; |
| 801 | } |
| 802 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 803 | int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 804 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 805 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 806 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 807 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 808 | if (session->side == NC_CLIENT) { |
| 809 | type = nc_send_client_hello(session); |
| 810 | } else { |
| 811 | type = nc_send_server_hello(session); |
| 812 | } |
| 813 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 814 | if (type != NC_MSG_HELLO) { |
| 815 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 818 | if (session->side == NC_CLIENT) { |
| 819 | type = nc_recv_client_hello(session); |
| 820 | } else { |
| 821 | type = nc_recv_server_hello(session); |
| 822 | } |
| 823 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 824 | if (type != NC_MSG_HELLO) { |
| 825 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 828 | return 0; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 829 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 830 | |
| 831 | #ifdef ENABLE_SSH |
| 832 | |
| 833 | API void |
| 834 | nc_ssh_init(void) |
| 835 | { |
| 836 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 837 | ssh_init(); |
| 838 | ssh_set_log_level(verbose_level); |
| 839 | } |
| 840 | |
| 841 | API void |
| 842 | nc_ssh_destroy(void) |
| 843 | { |
| 844 | ssh_finalize(); |
| 845 | } |
| 846 | |
| 847 | #endif /* ENABLE_SSH */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 848 | |
| 849 | #ifdef ENABLE_TLS |
| 850 | |
| 851 | static pthread_mutex_t *tls_locks; |
| 852 | |
| 853 | static void |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 854 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 855 | { |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 856 | if (mode & CRYPTO_LOCK) { |
| 857 | pthread_mutex_lock(tls_locks + n); |
| 858 | } else { |
| 859 | pthread_mutex_unlock(tls_locks + n); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | static unsigned long |
| 864 | tls_thread_id_func(void) |
| 865 | { |
| 866 | return (unsigned long)pthread_self(); |
| 867 | } |
| 868 | |
| 869 | API void |
| 870 | nc_tls_init(void) |
| 871 | { |
| 872 | int i; |
| 873 | |
| 874 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 875 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 876 | SSL_library_init(); |
| 877 | |
| 878 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
| 879 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 880 | pthread_mutex_init(tls_locks + i, NULL); |
| 881 | } |
| 882 | |
| 883 | CRYPTO_set_id_callback(tls_thread_id_func); |
| 884 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
| 885 | } |
| 886 | |
| 887 | API void |
| 888 | nc_tls_destroy(void) |
| 889 | { |
| 890 | int i; |
| 891 | |
| 892 | CRYPTO_THREADID crypto_tid; |
| 893 | |
| 894 | EVP_cleanup(); |
| 895 | CRYPTO_cleanup_all_ex_data(); |
| 896 | ERR_free_strings(); |
| 897 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
| 898 | CRYPTO_THREADID_current(&crypto_tid); |
| 899 | ERR_remove_thread_state(&crypto_tid); |
| 900 | |
| 901 | CRYPTO_set_id_callback(NULL); |
| 902 | CRYPTO_set_locking_callback(NULL); |
| 903 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 904 | pthread_mutex_destroy(tls_locks + i); |
| 905 | } |
| 906 | free(tls_locks); |
| 907 | } |
| 908 | |
| 909 | #endif /* ENABLE_TLS */ |