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> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 26 | #include <libyang/libyang.h> |
| 27 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 28 | #ifdef ENABLE_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 30 | # include <libssh/libssh.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 31 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 32 | #endif /* ENABLE_SSH */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 33 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 34 | #include "config.h" |
| 35 | #include "log_p.h" |
| 36 | #include "session.h" |
| 37 | #include "session_p.h" |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 38 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | /* in seconds */ |
| 40 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 41 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 42 | extern struct nc_server_opts server_opts; |
| 43 | |
| 44 | NC_MSG_TYPE |
| 45 | nc_send_msg(struct nc_session *session, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 46 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 47 | int r; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 48 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 49 | if (session->ctx != op->schema->module->ctx) { |
| 50 | ERR("RPC \"%s\" was created in different context than that of \"%s\" session %u.", |
| 51 | op->schema->name, session->username, session->id); |
| 52 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 55 | r = nc_write_msg(session, NC_MSG_RPC, op, NULL); |
| 56 | |
| 57 | if (r) { |
| 58 | return NC_MSG_ERROR; |
| 59 | } |
| 60 | |
| 61 | return NC_MSG_RPC; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 64 | /* |
| 65 | * @return 0 - success |
| 66 | * -1 - timeout |
| 67 | * >0 - error |
| 68 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 69 | int |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 70 | session_ti_lock(struct nc_session *session, int32_t timeout) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 71 | { |
| 72 | int r; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 73 | |
| 74 | if (timeout >= 0) { |
| 75 | /* limited waiting for lock */ |
| 76 | do { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 77 | r = pthread_mutex_trylock(session->ti_lock); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 78 | if (r == EBUSY) { |
| 79 | /* try later until timeout passes */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 80 | usleep(NC_TIMEOUT_STEP); |
| 81 | timeout = timeout - NC_TIMEOUT_STEP; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 82 | continue; |
| 83 | } else if (r) { |
| 84 | /* error */ |
| 85 | ERR("Acquiring session (%u) TI lock failed (%s).", session->id, strerror(r)); |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 86 | return r; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 87 | } else { |
| 88 | /* lock acquired */ |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 89 | return 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 90 | } |
| 91 | } while(timeout > 0); |
| 92 | |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 93 | /* timeout has passed */ |
| 94 | return -1; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 95 | } else { |
| 96 | /* infinite waiting for lock */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 97 | return pthread_mutex_lock(session->ti_lock); |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 101 | int |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 102 | session_ti_unlock(struct nc_session *session) |
| 103 | { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 104 | return pthread_mutex_unlock(session->ti_lock); |
| 105 | } |
| 106 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 107 | API void |
| 108 | nc_session_free(struct nc_session *session) |
| 109 | { |
| 110 | int r, i; |
| 111 | int multisession = 0; /* flag for more NETCONF session on a single SSH session */ |
| 112 | struct nc_session *siter; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 113 | struct nc_msg_cont *contiter; |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 114 | struct lyxml_elem *rpl, *child; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 115 | struct lyd_node *close_rpc; |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 116 | const struct lys_module *ietfnc; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 117 | void *p; |
| 118 | |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 119 | if (!session || session->status == NC_STATUS_CLOSING) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
| 123 | /* mark session for closing */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 124 | if (session->ti_lock) { |
| 125 | do { |
| 126 | r = session_ti_lock(session, 0); |
| 127 | } while (r < 0); |
| 128 | if (r) { |
| 129 | return; |
| 130 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* stop notifications loop if any */ |
| 134 | if (session->notif) { |
| 135 | pthread_cancel(*session->notif); |
| 136 | pthread_join(*session->notif, NULL); |
| 137 | } |
| 138 | |
| 139 | if (session->side == NC_CLIENT && session->status == NC_STATUS_RUNNING) { |
| 140 | /* cleanup message queues */ |
| 141 | /* notifications */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 142 | for (contiter = session->notifs; contiter; ) { |
| 143 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 144 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 145 | p = contiter; |
| 146 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 147 | free(p); |
| 148 | } |
| 149 | |
| 150 | /* rpc replies */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 151 | for (contiter = session->replies; contiter; ) { |
| 152 | lyxml_free(session->ctx, contiter->msg); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 153 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 154 | p = contiter; |
| 155 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 156 | free(p); |
| 157 | } |
| 158 | |
| 159 | /* send closing info to the other side */ |
| 160 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 161 | if (!ietfnc) { |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 162 | WRN("%s: Missing ietf-netconf schema in context (session %u), unable to send <close-session\\>", __func__, session->id); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 163 | } else { |
| 164 | close_rpc = lyd_new(NULL, ietfnc, "close-session"); |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 165 | nc_send_msg(session, close_rpc); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 166 | lyd_free(close_rpc); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 167 | switch (nc_read_msg(session, 200, &rpl)) { |
| 168 | case NC_MSG_REPLY: |
| 169 | LY_TREE_FOR(rpl->child, child) { |
| 170 | if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) { |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | if (!child) { |
| 175 | WRN("The reply to <close-session\\> was not <ok\\> as expected."); |
| 176 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 177 | lyxml_free(session->ctx, rpl); |
Michal Vasko | fad6e91 | 2015-10-26 15:37:22 +0100 | [diff] [blame] | 178 | break; |
| 179 | case NC_MSG_WOULDBLOCK: |
| 180 | WRN("Timeout for receiving a reply to <close-session\\> elapsed."); |
| 181 | break; |
| 182 | case NC_MSG_ERROR: |
| 183 | ERR("Failed to receive a reply to <close-session\\>."); |
| 184 | break; |
| 185 | default: |
| 186 | /* cannot happen */ |
| 187 | break; |
| 188 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* list of server's capabilities */ |
| 192 | if (session->cpblts) { |
| 193 | for (i = 0; session->cpblts[i]; i++) { |
| 194 | lydict_remove(session->ctx, session->cpblts[i]); |
| 195 | } |
| 196 | free(session->cpblts); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | session->status = NC_STATUS_CLOSING; |
| 201 | |
| 202 | /* transport implementation cleanup */ |
| 203 | switch (session->ti_type) { |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 204 | case NC_TI_NONE: |
| 205 | break; |
| 206 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 207 | case NC_TI_FD: |
| 208 | /* nothing needed - file descriptors were provided by caller, |
| 209 | * so it is up to the caller to close them correctly |
| 210 | * TODO use callbacks |
| 211 | */ |
| 212 | break; |
| 213 | |
Michal Vasko | fb2fb76 | 2015-10-27 11:44:32 +0100 | [diff] [blame] | 214 | #ifdef ENABLE_SSH |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 215 | case NC_TI_LIBSSH: |
| 216 | ssh_channel_free(session->ti.libssh.channel); |
| 217 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 218 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 219 | * it. |
| 220 | */ |
| 221 | if (!session->ti.libssh.next) { |
| 222 | ssh_disconnect(session->ti.libssh.session); |
| 223 | ssh_free(session->ti.libssh.session); |
| 224 | } else { |
| 225 | /* multiple NETCONF sessions on a single SSH session */ |
| 226 | multisession = 1; |
| 227 | /* remove the session from the list */ |
| 228 | 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] | 229 | if (session->ti.libssh.next == siter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 230 | /* there will be only one session */ |
| 231 | siter->ti.libssh.next = NULL; |
| 232 | } else { |
| 233 | /* there are still multiple sessions, keep the ring list */ |
| 234 | siter->ti.libssh.next = session->ti.libssh.next; |
| 235 | } |
| 236 | } |
| 237 | break; |
| 238 | #endif |
| 239 | |
| 240 | #ifdef ENABLE_TLS |
| 241 | case NC_TI_OPENSSL: |
| 242 | SSL_shutdown(session->ti.tls); |
| 243 | SSL_free(session->ti.tls); |
| 244 | break; |
| 245 | #endif |
| 246 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 247 | lydict_remove(session->ctx, session->username); |
| 248 | lydict_remove(session->ctx, session->host); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 249 | |
| 250 | /* final cleanup */ |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 251 | if (session->ti_lock) { |
| 252 | if (multisession) { |
| 253 | session_ti_unlock(session); |
| 254 | } else { |
| 255 | pthread_mutex_destroy(session->ti_lock); |
| 256 | free(session->ti_lock); |
| 257 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
| 261 | ly_ctx_destroy(session->ctx); |
| 262 | } |
| 263 | |
| 264 | free(session); |
| 265 | } |
| 266 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 267 | static void |
| 268 | add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count) |
| 269 | { |
| 270 | if (*count == *size) { |
| 271 | *size += 5; |
| 272 | *cpblts = realloc(*cpblts, *size * sizeof **cpblts); |
| 273 | } |
| 274 | |
| 275 | if (capab) { |
| 276 | (*cpblts)[*count] = lydict_insert(ctx, capab, 0); |
| 277 | } else { |
| 278 | (*cpblts)[*count] = NULL; |
| 279 | } |
| 280 | ++(*count); |
| 281 | } |
| 282 | |
| 283 | static const char ** |
| 284 | create_cpblts(struct ly_ctx *ctx) |
| 285 | { |
| 286 | struct lyd_node *child, *child2, *yanglib; |
| 287 | struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL; |
| 288 | const char **cpblts; |
| 289 | const struct lys_module *mod; |
| 290 | int size = 10, count, feat_count = 0, i; |
| 291 | char str[512]; |
| 292 | |
| 293 | yanglib = ly_ctx_info(ctx); |
| 294 | if (!yanglib) { |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | cpblts = malloc(size * sizeof *cpblts); |
| 299 | cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 300 | cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 301 | count = 2; |
| 302 | |
| 303 | /* capabilities */ |
| 304 | |
| 305 | mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 306 | if (mod) { |
| 307 | if (lys_features_state(mod, "writable-running") == 1) { |
| 308 | add_cpblt(ctx, "urn:ietf:params:netconf:writable-running:1.0", &cpblts, &size, &count); |
| 309 | } |
| 310 | if (lys_features_state(mod, "candidate") == 1) { |
| 311 | add_cpblt(ctx, "urn:ietf:params:netconf:candidate:1.0", &cpblts, &size, &count); |
| 312 | if (lys_features_state(mod, "confirmed-commit") == 1) { |
| 313 | add_cpblt(ctx, "urn:ietf:params:netconf:confirmed-commit:1.1", &cpblts, &size, &count); |
| 314 | } |
| 315 | } |
| 316 | if (lys_features_state(mod, "rollback-on-error") == 1) { |
| 317 | add_cpblt(ctx, "urn:ietf:params:netconf:rollback-on-error:1.0", &cpblts, &size, &count); |
| 318 | } |
| 319 | if (lys_features_state(mod, "validate") == 1) { |
| 320 | add_cpblt(ctx, "urn:ietf:params:netconf:validate:1.1", &cpblts, &size, &count); |
| 321 | } |
| 322 | if (lys_features_state(mod, "startup") == 1) { |
| 323 | add_cpblt(ctx, "urn:ietf:params:netconf:startup:1.0", &cpblts, &size, &count); |
| 324 | } |
| 325 | if (lys_features_state(mod, "url") == 1) { |
| 326 | add_cpblt(ctx, "urn:ietf:params:netconf:url:1.0", &cpblts, &size, &count); |
| 327 | } |
| 328 | if (lys_features_state(mod, "xpath") == 1) { |
| 329 | add_cpblt(ctx, "urn:ietf:params:netconf:xpath:1.0", &cpblts, &size, &count); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL); |
| 334 | if (mod) { |
| 335 | if (!server_opts.wd_basic_mode) { |
| 336 | VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
| 337 | } else { |
| 338 | strcpy(str, "urn:ietf:params:netconf:with-defaults:1.0"); |
| 339 | switch (server_opts.wd_basic_mode) { |
| 340 | case NC_WD_ALL: |
| 341 | strcat(str, "?basic-mode=report-all"); |
| 342 | break; |
| 343 | case NC_WD_TRIM: |
| 344 | strcat(str, "?basic-mode=trim"); |
| 345 | break; |
| 346 | case NC_WD_EXPLICIT: |
| 347 | strcat(str, "?basic-mode=explicit"); |
| 348 | break; |
| 349 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 350 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | |
| 354 | if (server_opts.wd_also_supported) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame^] | 355 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 356 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 357 | strcat(str, "report-all,"); |
| 358 | } |
| 359 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 360 | strcat(str, "report-all-tagged,"); |
| 361 | } |
| 362 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 363 | strcat(str, "trim,"); |
| 364 | } |
| 365 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 366 | strcat(str, "explicit,"); |
| 367 | } |
| 368 | str[strlen(str) - 1] = '\0'; |
| 369 | |
| 370 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | mod = ly_ctx_get_module(ctx, "ietf-netconf-notifications", NULL); |
| 376 | if (mod) { |
| 377 | add_cpblt(ctx, "urn:ietf:params:netconf:notification:1.0", &cpblts, &size, &count); |
| 378 | if (server_opts.interleave_capab) { |
| 379 | add_cpblt(ctx, "urn:ietf:params:netconf:interleave:1.0", &cpblts, &size, &count); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* models */ |
| 384 | |
| 385 | LY_TREE_FOR(yanglib->child, child) { |
| 386 | if (!strcmp(child->schema->name, "module")) { |
| 387 | LY_TREE_FOR(child->child, child2) { |
| 388 | if (!strcmp(child2->schema->name, "namespace")) { |
| 389 | ns = (struct lyd_node_leaf_list *)child2; |
| 390 | } else if (!strcmp(child2->schema->name, "name")) { |
| 391 | name = (struct lyd_node_leaf_list *)child2; |
| 392 | } else if (!strcmp(child2->schema->name, "revision")) { |
| 393 | rev = (struct lyd_node_leaf_list *)child2; |
| 394 | } else if (!strcmp(child2->schema->name, "feature")) { |
| 395 | features = realloc(features, feat_count++ * sizeof *features); |
| 396 | features[feat_count - 1] = (struct lyd_node_leaf_list *)child2; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (!ns || !name || !rev) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 401 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 402 | continue; |
| 403 | } |
| 404 | |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame^] | 405 | 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] | 406 | if (feat_count) { |
Radek Krejci | f9b2832 | 2016-01-08 14:56:43 +0100 | [diff] [blame^] | 407 | strcat(str, "&features="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 408 | for (i = 0; i < feat_count; ++i) { |
| 409 | if (i) { |
| 410 | strcat(str, ","); |
| 411 | } |
| 412 | strcat(str, features[i]->value_str); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | add_cpblt(ctx, str, &cpblts, &size, &count); |
| 417 | |
| 418 | ns = NULL; |
| 419 | name = NULL; |
| 420 | rev = NULL; |
| 421 | free(features); |
| 422 | features = NULL; |
| 423 | feat_count = 0; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | lyd_free(yanglib); |
| 428 | |
| 429 | /* ending NULL capability */ |
| 430 | add_cpblt(ctx, NULL, &cpblts, &size, &count); |
| 431 | |
| 432 | return cpblts; |
| 433 | } |
| 434 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 435 | static int |
| 436 | parse_cpblts(struct lyxml_elem *xml, const char ***list) |
| 437 | { |
| 438 | struct lyxml_elem *cpblt; |
| 439 | int ver = -1; |
| 440 | int i = 0; |
| 441 | |
| 442 | if (list) { |
| 443 | /* get the storage for server's capabilities */ |
| 444 | LY_TREE_FOR(xml->child, cpblt) { |
| 445 | i++; |
| 446 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 447 | /* last item remains NULL */ |
| 448 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 449 | if (!*list) { |
| 450 | ERRMEM; |
| 451 | return -1; |
| 452 | } |
| 453 | i = 0; |
| 454 | } |
| 455 | |
| 456 | LY_TREE_FOR(xml->child, cpblt) { |
| 457 | if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value && |
| 458 | !strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 459 | ERR("Unexpected <%s> element in client's <hello>.", cpblt->name); |
| 460 | return -1; |
| 461 | } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) { |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | /* detect NETCONF version */ |
| 466 | if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) { |
| 467 | ver = 0; |
| 468 | } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) { |
| 469 | ver = 1; |
| 470 | } |
| 471 | |
| 472 | /* store capabilities */ |
| 473 | if (list) { |
| 474 | (*list)[i] = cpblt->content; |
| 475 | cpblt->content = NULL; |
| 476 | i++; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (ver == -1) { |
| 481 | ERR("Peer does not support compatible NETCONF version."); |
| 482 | } |
| 483 | |
| 484 | return ver; |
| 485 | } |
| 486 | |
| 487 | static NC_MSG_TYPE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 488 | nc_send_hello(struct nc_session *session) |
| 489 | { |
| 490 | int r, i; |
| 491 | const char **cpblts; |
| 492 | |
| 493 | if (session->side == NC_CLIENT) { |
| 494 | /* client side hello - send only NETCONF base capabilities */ |
| 495 | cpblts = malloc(3 * sizeof *cpblts); |
| 496 | cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0); |
| 497 | cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0); |
| 498 | cpblts[2] = NULL; |
| 499 | } else { |
| 500 | cpblts = create_cpblts(session->ctx); |
| 501 | } |
| 502 | |
| 503 | r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL); |
| 504 | for (i = 0; cpblts[i]; ++i) { |
| 505 | lydict_remove(session->ctx, cpblts[i]); |
| 506 | } |
| 507 | free(cpblts); |
| 508 | |
| 509 | if (r) { |
| 510 | return NC_MSG_ERROR; |
| 511 | } else { |
| 512 | return NC_MSG_HELLO; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | static NC_MSG_TYPE |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 517 | nc_recv_hello(struct nc_session *session) |
| 518 | { |
| 519 | struct lyxml_elem *xml = NULL, *node; |
| 520 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 521 | int ver = -1; |
| 522 | char *str; |
| 523 | long long int id; |
| 524 | int flag = 0; |
| 525 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 526 | msgtype = nc_read_msg(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 527 | |
| 528 | switch(msgtype) { |
| 529 | case NC_MSG_HELLO: |
| 530 | /* parse <hello> data */ |
| 531 | if (session->side == NC_SERVER) { |
| 532 | /* get know NETCONF version */ |
| 533 | LY_TREE_FOR(xml->child, node) { |
| 534 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 535 | continue; |
| 536 | } else if (strcmp(node->name, "capabilities")) { |
| 537 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
| 538 | goto error; |
| 539 | } |
| 540 | |
| 541 | if (flag) { |
| 542 | /* multiple capabilities elements */ |
| 543 | ERR("Invalid <hello> message (multiple <capabilities> elements)"); |
| 544 | goto error; |
| 545 | } |
| 546 | flag = 1; |
| 547 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 548 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 549 | goto error; |
| 550 | } |
| 551 | session->version = ver; |
| 552 | } |
| 553 | } else { /* NC_CLIENT */ |
| 554 | LY_TREE_FOR(xml->child, node) { |
| 555 | if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) { |
| 556 | continue; |
| 557 | } else if (!strcmp(node->name, "session-id")) { |
| 558 | if (!node->content || !strlen(node->content)) { |
| 559 | ERR("No value of <session-id> element in server's <hello>"); |
| 560 | goto error; |
| 561 | } |
| 562 | str = NULL; |
| 563 | id = strtoll(node->content, &str, 10); |
| 564 | if (*str || id < 1 || id > UINT32_MAX) { |
| 565 | ERR("Invalid value of <session-id> element in server's <hello>"); |
| 566 | goto error; |
| 567 | } |
| 568 | session->id = (uint32_t)id; |
| 569 | continue; |
| 570 | } else if (strcmp(node->name, "capabilities")) { |
| 571 | ERR("Unexpected <%s> element in client's <hello>.", node->name); |
| 572 | goto error; |
| 573 | } |
| 574 | |
| 575 | if (flag) { |
| 576 | /* multiple capabilities elements */ |
| 577 | ERR("Invalid <hello> message (multiple <capabilities> elements)"); |
| 578 | goto error; |
| 579 | } |
| 580 | flag = 1; |
| 581 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 582 | if ((ver = parse_cpblts(node, &session->cpblts)) < 0) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 583 | goto error; |
| 584 | } |
| 585 | session->version = ver; |
| 586 | } |
| 587 | |
| 588 | if (!session->id) { |
| 589 | ERR("Missing <session-id> in server's <hello>"); |
| 590 | goto error; |
| 591 | } |
| 592 | } |
| 593 | break; |
| 594 | case NC_MSG_ERROR: |
| 595 | /* nothing special, just pass it out */ |
| 596 | break; |
| 597 | default: |
| 598 | ERR("Unexpected message received instead of <hello>."); |
| 599 | msgtype = NC_MSG_ERROR; |
| 600 | } |
| 601 | |
| 602 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 603 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 604 | |
| 605 | return msgtype; |
| 606 | |
| 607 | error: |
| 608 | /* cleanup */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 609 | lyxml_free(session->ctx, xml); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 610 | |
| 611 | return NC_MSG_ERROR; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 614 | int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 615 | nc_handshake(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 616 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 617 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 618 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 619 | type = nc_send_hello(session); |
| 620 | if (type != NC_MSG_HELLO) { |
| 621 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 624 | type = nc_recv_hello(session); |
| 625 | if (type != NC_MSG_HELLO) { |
| 626 | return 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 629 | return 0; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 630 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 631 | |
| 632 | #ifdef ENABLE_SSH |
| 633 | |
| 634 | API void |
| 635 | nc_ssh_init(void) |
| 636 | { |
| 637 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 638 | ssh_init(); |
| 639 | ssh_set_log_level(verbose_level); |
| 640 | } |
| 641 | |
| 642 | API void |
| 643 | nc_ssh_destroy(void) |
| 644 | { |
| 645 | ssh_finalize(); |
| 646 | } |
| 647 | |
| 648 | #endif /* ENABLE_SSH */ |