Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 9 | #include <cstring> |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 10 | #include <libyang/Tree_Data.hpp> |
| 11 | #include <mutex> |
| 12 | extern "C" { |
| 13 | #include <nc_client.h> |
| 14 | } |
| 15 | #include <sstream> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 16 | #include "netconf-client.hpp" |
| 17 | #include "UniqueResource.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 18 | |
| 19 | namespace libnetconf { |
| 20 | |
| 21 | namespace impl { |
| 22 | |
| 23 | /** @short Initialization of the libnetconf2 library client |
| 24 | |
| 25 | Just a safe wrapper over nc_client_init and nc_client_destroy, really. |
| 26 | */ |
| 27 | class ClientInit { |
| 28 | ClientInit() |
| 29 | { |
| 30 | nc_client_init(); |
| 31 | nc_verbosity(NC_VERB_DEBUG); |
| 32 | } |
| 33 | |
| 34 | ~ClientInit() |
| 35 | { |
| 36 | nc_client_destroy(); |
| 37 | } |
| 38 | |
| 39 | public: |
| 40 | static ClientInit& instance() |
| 41 | { |
| 42 | static ClientInit lib; |
| 43 | return lib; |
| 44 | } |
| 45 | |
| 46 | ClientInit(const ClientInit&) = delete; |
| 47 | ClientInit(ClientInit&&) = delete; |
| 48 | ClientInit& operator=(const ClientInit&) = delete; |
| 49 | ClientInit& operator=(ClientInit&&) = delete; |
| 50 | }; |
| 51 | |
| 52 | static std::mutex clientOptions; |
| 53 | |
| 54 | inline void custom_free_nc_reply_data(nc_reply_data* reply) |
| 55 | { |
| 56 | nc_reply_free(reinterpret_cast<nc_reply*>(reply)); |
| 57 | } |
| 58 | inline void custom_free_nc_reply_error(nc_reply_error* reply) |
| 59 | { |
| 60 | nc_reply_free(reinterpret_cast<nc_reply*>(reply)); |
| 61 | } |
| 62 | |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 63 | char *ssh_auth_interactive_cb(const char *auth_name, const char *instruction, const char *prompt, int echo, void *priv) |
| 64 | { |
| 65 | const auto cb = static_cast<const client::KbdInteractiveCb*>(priv); |
| 66 | auto res = (*cb)(auth_name, instruction, prompt, echo); |
| 67 | return ::strdup(res.c_str()); |
| 68 | } |
| 69 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 70 | template <auto fn> |
| 71 | using deleter_from_fn = std::integral_constant<decltype(fn), fn>; |
| 72 | |
| 73 | template <typename T> |
| 74 | struct deleter_type_for { |
| 75 | using func_type = void (*)(T*); |
| 76 | }; |
| 77 | |
| 78 | template <typename T> |
| 79 | struct deleter_for { |
| 80 | }; |
| 81 | |
| 82 | template <> |
| 83 | struct deleter_for<struct nc_rpc> { |
| 84 | static constexpr void (*free)(struct nc_rpc*) = deleter_from_fn<nc_rpc_free>(); |
| 85 | }; |
| 86 | template <> |
| 87 | struct deleter_for<struct nc_reply> { |
| 88 | static constexpr void (*free)(struct nc_reply*) = deleter_from_fn<nc_reply_free>(); |
| 89 | }; |
| 90 | template <> |
| 91 | struct deleter_for<struct nc_reply_data> { |
| 92 | static constexpr void (*free)(struct nc_reply_data*) = deleter_from_fn<custom_free_nc_reply_data>(); |
| 93 | }; |
| 94 | template <> |
| 95 | struct deleter_for<struct nc_reply_error> { |
| 96 | static constexpr void (*free)(struct nc_reply_error*) = deleter_from_fn<custom_free_nc_reply_error>(); |
| 97 | }; |
| 98 | |
| 99 | template <typename T> |
| 100 | using unique_ptr_for = std::unique_ptr<T, decltype(deleter_for<T>::free)>; |
| 101 | |
| 102 | template <typename T> |
| 103 | auto guarded(T* ptr) |
| 104 | { |
| 105 | return unique_ptr_for<T>(ptr, deleter_for<T>::free); |
| 106 | } |
| 107 | |
| 108 | unique_ptr_for<struct nc_reply> do_rpc(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc) |
| 109 | { |
| 110 | uint64_t msgid; |
| 111 | NC_MSG_TYPE msgtype; |
| 112 | |
| 113 | msgtype = nc_send_rpc(session->session_internal(), rpc.get(), 1000, &msgid); |
| 114 | if (msgtype == NC_MSG_ERROR) { |
| 115 | throw std::runtime_error{"Failed to send RPC"}; |
| 116 | } |
| 117 | if (msgtype == NC_MSG_WOULDBLOCK) { |
| 118 | throw std::runtime_error{"Timeout sending an RPC"}; |
| 119 | } |
| 120 | |
| 121 | struct nc_reply* raw_reply; |
| 122 | while (true) { |
| 123 | msgtype = nc_recv_reply(session->session_internal(), rpc.get(), msgid, 20000, LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS, &raw_reply); |
| 124 | auto reply = guarded(raw_reply); |
| 125 | raw_reply = nullptr; |
| 126 | |
| 127 | switch (msgtype) { |
| 128 | case NC_MSG_ERROR: |
| 129 | throw std::runtime_error{"Failed to receive an RPC reply"}; |
| 130 | case NC_MSG_WOULDBLOCK: |
| 131 | throw std::runtime_error{"Timed out waiting for RPC reply"}; |
| 132 | case NC_MSG_REPLY_ERR_MSGID: |
| 133 | throw std::runtime_error{"Received a wrong reply -- msgid mismatch"}; |
| 134 | case NC_MSG_NOTIF: |
| 135 | continue; |
| 136 | default: |
| 137 | return reply; |
| 138 | } |
| 139 | } |
| 140 | __builtin_unreachable(); |
| 141 | } |
| 142 | |
| 143 | client::ReportedError make_error(unique_ptr_for<struct nc_reply>&& reply) |
| 144 | { |
| 145 | if (reply->type != NC_RPL_ERROR) { |
| 146 | throw std::logic_error{"Cannot extract an error from a non-error server reply"}; |
| 147 | } |
| 148 | |
| 149 | auto errorReply = guarded(reinterpret_cast<struct nc_reply_error*>(reply.release())); |
| 150 | |
| 151 | // TODO: capture the error details, not just that human-readable string |
| 152 | std::ostringstream ss; |
| 153 | ss << "Server error:" << std::endl; |
| 154 | for (uint32_t i = 0; i < errorReply->count; ++i) { |
| 155 | const auto e = errorReply->err[i]; |
| 156 | ss << " #" << i << ": " << e.message; |
| 157 | if (e.path) { |
| 158 | ss << " (XPath " << e.path << ")"; |
| 159 | } |
| 160 | ss << std::endl; |
| 161 | } |
| 162 | return client::ReportedError{ss.str()}; |
| 163 | } |
| 164 | |
Jan Kundrát | a2a4414 | 2020-01-24 01:31:54 +0100 | [diff] [blame] | 165 | std::optional<unique_ptr_for<struct nc_reply_data>> do_rpc_data_or_ok(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 166 | { |
| 167 | auto x = do_rpc(session, std::move(rpc)); |
| 168 | |
| 169 | switch (x->type) { |
| 170 | case NC_RPL_DATA: |
| 171 | return guarded(reinterpret_cast<struct nc_reply_data*>(x.release())); |
| 172 | case NC_RPL_OK: |
Jan Kundrát | a2a4414 | 2020-01-24 01:31:54 +0100 | [diff] [blame] | 173 | return std::nullopt; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 174 | case NC_RPL_ERROR: |
| 175 | throw make_error(std::move(x)); |
| 176 | default: |
| 177 | throw std::runtime_error{"Unhandled reply type"}; |
| 178 | } |
Jan Kundrát | a2a4414 | 2020-01-24 01:31:54 +0100 | [diff] [blame] | 179 | |
| 180 | } |
| 181 | |
| 182 | unique_ptr_for<struct nc_reply_data> do_rpc_data(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc) |
| 183 | { |
| 184 | auto x = do_rpc_data_or_ok(session, std::move(rpc)); |
| 185 | if (!x) { |
| 186 | throw std::runtime_error{"Received OK instead of a data reply"}; |
| 187 | } |
| 188 | return std::move(*x); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void do_rpc_ok(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc) |
| 192 | { |
Jan Kundrát | a2a4414 | 2020-01-24 01:31:54 +0100 | [diff] [blame] | 193 | auto x = do_rpc_data_or_ok(session, std::move(rpc)); |
| 194 | if (x) { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 195 | throw std::runtime_error{"Unexpected DATA reply"}; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | namespace client { |
| 201 | |
| 202 | struct nc_session* Session::session_internal() |
| 203 | { |
| 204 | return m_session; |
| 205 | } |
| 206 | |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 207 | libyang::S_Context Session::libyangContext() |
| 208 | { |
| 209 | return std::make_shared<libyang::Context>(nc_session_get_ctx(m_session), nullptr); |
| 210 | } |
| 211 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 212 | Session::Session(struct nc_session* session) |
| 213 | : m_session(session) |
| 214 | { |
| 215 | impl::ClientInit::instance(); |
| 216 | } |
| 217 | |
| 218 | Session::~Session() |
| 219 | { |
| 220 | ::nc_session_free(m_session, nullptr); |
| 221 | } |
| 222 | |
| 223 | std::unique_ptr<Session> Session::connectPubkey(const std::string& host, const uint16_t port, const std::string& user, const std::string& pubPath, const std::string& privPath) |
| 224 | { |
| 225 | impl::ClientInit::instance(); |
| 226 | |
| 227 | { |
| 228 | // FIXME: this is still horribly not enough. libnetconf *must* provide us with something better. |
| 229 | std::lock_guard lk(impl::clientOptions); |
| 230 | nc_client_ssh_set_username(user.c_str()); |
| 231 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 5); |
| 232 | nc_client_ssh_add_keypair(pubPath.c_str(), privPath.c_str()); |
| 233 | } |
| 234 | auto session = std::make_unique<Session>(nc_connect_ssh(host.c_str(), port, nullptr)); |
| 235 | if (!session->m_session) { |
| 236 | throw std::runtime_error{"nc_connect_ssh failed"}; |
| 237 | } |
| 238 | return session; |
| 239 | } |
| 240 | |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 241 | std::unique_ptr<Session> Session::connectKbdInteractive(const std::string& host, const uint16_t port, const std::string& user, const KbdInteractiveCb& callback) |
| 242 | { |
| 243 | impl::ClientInit::instance(); |
| 244 | |
| 245 | std::lock_guard lk(impl::clientOptions); |
| 246 | auto cb_guard = make_unique_resource([user, &callback]() { |
| 247 | nc_client_ssh_set_username(user.c_str()); |
| 248 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 5); |
| 249 | nc_client_ssh_set_auth_interactive_clb(impl::ssh_auth_interactive_cb, static_cast<void *>(&const_cast<KbdInteractiveCb&>(callback))); |
| 250 | }, []() { |
| 251 | nc_client_ssh_set_auth_interactive_clb(nullptr, nullptr); |
| 252 | nc_client_ssh_set_username(nullptr); |
| 253 | }); |
| 254 | |
| 255 | auto session = std::make_unique<Session>(nc_connect_ssh(host.c_str(), port, nullptr)); |
| 256 | if (!session->m_session) { |
| 257 | throw std::runtime_error{"nc_connect_ssh failed"}; |
| 258 | } |
| 259 | return session; |
| 260 | } |
| 261 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 262 | std::unique_ptr<Session> Session::connectSocket(const std::string& path) |
| 263 | { |
| 264 | impl::ClientInit::instance(); |
| 265 | |
| 266 | auto session = std::make_unique<Session>(nc_connect_unix(path.c_str(), nullptr)); |
| 267 | if (!session->m_session) { |
| 268 | throw std::runtime_error{"nc_connect_unix failed"}; |
| 269 | } |
| 270 | return session; |
| 271 | } |
| 272 | |
| 273 | std::vector<std::string_view> Session::capabilities() const |
| 274 | { |
| 275 | std::vector<std::string_view> res; |
| 276 | auto caps = nc_session_get_cpblts(m_session); |
| 277 | while (*caps) { |
| 278 | res.emplace_back(*caps); |
| 279 | ++caps; |
| 280 | } |
| 281 | return res; |
| 282 | } |
| 283 | |
Jan Kundrát | 9138b16 | 2020-01-30 09:27:51 +0100 | [diff] [blame] | 284 | std::shared_ptr<libyang::Data_Node> Session::get(const std::optional<std::string>& filter) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 285 | { |
Jan Kundrát | 9138b16 | 2020-01-30 09:27:51 +0100 | [diff] [blame] | 286 | auto rpc = impl::guarded(nc_rpc_get(filter ? filter->c_str() : nullptr, NC_WD_ALL, NC_PARAMTYPE_CONST)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 287 | if (!rpc) { |
| 288 | throw std::runtime_error("Cannot create get RPC"); |
| 289 | } |
| 290 | auto reply = impl::do_rpc_data(this, std::move(rpc)); |
Jan Kundrát | a099fb9 | 2020-02-03 08:51:56 +0100 | [diff] [blame] | 291 | auto dataNode = libyang::create_new_Data_Node(reply->data); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 292 | // TODO: can we do without copying? |
| 293 | // If we just default-construct a new node (or use the create_new_Data_Node) and then set reply->data to nullptr, |
| 294 | // there are mem leaks and even libnetconf2 complains loudly. |
Jan Kundrát | a099fb9 | 2020-02-03 08:51:56 +0100 | [diff] [blame] | 295 | return dataNode ? dataNode->dup_withsiblings(1) : nullptr; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | std::string Session::getSchema(const std::string_view identifier, const std::optional<std::string_view> version) |
| 299 | { |
| 300 | auto rpc = impl::guarded(nc_rpc_getschema(identifier.data(), version ? version.value().data() : nullptr, nullptr, NC_PARAMTYPE_CONST)); |
| 301 | if (!rpc) { |
| 302 | throw std::runtime_error("Cannot create get-schema RPC"); |
| 303 | } |
| 304 | auto reply = impl::do_rpc_data(this, std::move(rpc)); |
| 305 | |
| 306 | auto node = libyang::create_new_Data_Node(reply->data)->dup_withsiblings(1); |
| 307 | auto set = node->find_path("data"); |
| 308 | for (auto node : set->data()) { |
| 309 | if (node->schema()->nodetype() == LYS_ANYXML) { |
| 310 | libyang::Data_Node_Anydata anydata(node); |
| 311 | return anydata.value().str; |
| 312 | } |
| 313 | } |
| 314 | throw std::runtime_error("Got a reply to get-schema, but it didn't contain the required schema"); |
| 315 | } |
| 316 | |
| 317 | std::shared_ptr<libyang::Data_Node> Session::getConfig(const NC_DATASTORE datastore, const std::optional<const std::string> filter) |
| 318 | { |
| 319 | auto rpc = impl::guarded(nc_rpc_getconfig(datastore, filter ? filter->c_str() : nullptr, NC_WD_ALL, NC_PARAMTYPE_CONST)); |
| 320 | if (!rpc) { |
| 321 | throw std::runtime_error("Cannot create get-config RPC"); |
| 322 | } |
| 323 | auto reply = impl::do_rpc_data(this, std::move(rpc)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 324 | auto dataNode = libyang::create_new_Data_Node(reply->data); |
Jan Kundrát | f09a092 | 2020-02-03 08:52:32 +0100 | [diff] [blame] | 325 | // TODO: can we do without copying? See Session::get() for details. |
| 326 | return dataNode ? dataNode->dup_withsiblings(1) : nullptr; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void Session::editConfig(const NC_DATASTORE datastore, |
| 330 | const NC_RPC_EDIT_DFLTOP defaultOperation, |
| 331 | const NC_RPC_EDIT_TESTOPT testOption, |
| 332 | const NC_RPC_EDIT_ERROPT errorOption, |
| 333 | const std::string& data) |
| 334 | { |
| 335 | auto rpc = impl::guarded(nc_rpc_edit(datastore, defaultOperation, testOption, errorOption, data.c_str(), NC_PARAMTYPE_CONST)); |
| 336 | if (!rpc) { |
| 337 | throw std::runtime_error("Cannot create edit-config RPC"); |
| 338 | } |
| 339 | impl::do_rpc_ok(this, std::move(rpc)); |
| 340 | } |
| 341 | |
| 342 | void Session::copyConfigFromString(const NC_DATASTORE target, const std::string& data) |
| 343 | { |
| 344 | auto rpc = impl::guarded(nc_rpc_copy(target, nullptr, target /* yeah, cannot be 0... */, data.c_str(), NC_WD_UNKNOWN, NC_PARAMTYPE_CONST)); |
| 345 | if (!rpc) { |
| 346 | throw std::runtime_error("Cannot create copy-config RPC"); |
| 347 | } |
| 348 | impl::do_rpc_ok(this, std::move(rpc)); |
| 349 | } |
| 350 | |
| 351 | void Session::commit() |
| 352 | { |
| 353 | auto rpc = impl::guarded(nc_rpc_commit(1, /* "Optional confirm timeout" how do you optional an uint32_t? */ 0, nullptr, nullptr, NC_PARAMTYPE_CONST)); |
| 354 | if (!rpc) { |
| 355 | throw std::runtime_error("Cannot create commit RPC"); |
| 356 | } |
| 357 | impl::do_rpc_ok(this, std::move(rpc)); |
| 358 | } |
| 359 | |
| 360 | void Session::discard() |
| 361 | { |
| 362 | auto rpc = impl::guarded(nc_rpc_discard()); |
| 363 | if (!rpc) { |
| 364 | throw std::runtime_error("Cannot create discard RPC"); |
| 365 | } |
| 366 | impl::do_rpc_ok(this, std::move(rpc)); |
| 367 | } |
| 368 | |
Jan Kundrát | 59acc2f | 2020-01-23 15:12:59 +0100 | [diff] [blame] | 369 | std::shared_ptr<libyang::Data_Node> Session::rpc(const std::string& xmlData) |
| 370 | { |
| 371 | auto rpc = impl::guarded(nc_rpc_act_generic_xml(xmlData.c_str(), NC_PARAMTYPE_CONST)); |
| 372 | if (!rpc) { |
| 373 | throw std::runtime_error("Cannot create generic RPC"); |
| 374 | } |
| 375 | auto reply = impl::do_rpc_data_or_ok(this, std::move(rpc)); |
| 376 | if (reply) { |
| 377 | auto dataNode = libyang::create_new_Data_Node((*reply)->data); |
| 378 | return dataNode->dup_withsiblings(1); |
| 379 | } else { |
| 380 | return nullptr; |
| 381 | } |
| 382 | } |
| 383 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 384 | void Session::copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination) |
| 385 | { |
| 386 | auto rpc = impl::guarded(nc_rpc_copy(destination, nullptr, source, nullptr, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST)); |
| 387 | if (!rpc) { |
| 388 | throw std::runtime_error("Cannot create copy-config RPC"); |
| 389 | } |
| 390 | impl::do_rpc_ok(this, std::move(rpc)); |
| 391 | } |
| 392 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 393 | ReportedError::ReportedError(const std::string& what) |
| 394 | : std::runtime_error(what) |
| 395 | { |
| 396 | } |
| 397 | |
| 398 | ReportedError::~ReportedError() = default; |
| 399 | } |
| 400 | } |