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