Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file libnetconf.h |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 4 | * \author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 5 | * \brief libnetconf2 main internal header. |
| 6 | * |
| 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #ifndef NC_LIBNETCONF_H_ |
| 17 | #define NC_LIBNETCONF_H_ |
| 18 | |
| 19 | #include "config.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 20 | #include "netconf.h" |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 21 | #include "log_p.h" |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 22 | #include "session_p.h" |
| 23 | #include "messages_p.h" |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 24 | |
| 25 | /* Tests whether string is empty or non-empty. */ |
| 26 | #define strisempty(str) ((str)[0] == '\0') |
| 27 | #define strnonempty(str) ((str)[0] != '\0') |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 28 | |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 29 | /** |
| 30 | * @mainpage About |
| 31 | * |
| 32 | * libnetconf2 is a NETCONF library in C handling NETCONF authentication and all NETCONF |
| 33 | * RPC communication both server and client-side. NETCONF datastore and session management is not a part of this library, |
| 34 | * but it helps a lot with the sessions. |
| 35 | * |
| 36 | * @section about-features Main Features |
| 37 | * |
| 38 | * - Creating SSH (using libssh) or TLS (using OpenSSL) authenticated NETCONF sessions. |
| 39 | * - Creating NETCONF sessions with a pre-established transport protocol |
| 40 | * (using this mechanism the communication can be tunneled through sshd(8), for instance). |
| 41 | * - Creating NETCONF Call Home sessions. |
| 42 | * - Creating, sending, receiving, and replying to RPCs. |
| 43 | * - Receiving notifications. |
| 44 | * |
| 45 | * - \todo Creating and sending notifications. |
| 46 | * |
| 47 | * @section about-license License |
| 48 | * |
| 49 | * Copyright (c) 2015-2016 CESNET, z.s.p.o. |
| 50 | * |
| 51 | * (The BSD 3-Clause License) |
| 52 | * |
| 53 | * Redistribution and use in source and binary forms, with or without |
| 54 | * modification, are permitted provided that the following conditions |
| 55 | * are met: |
| 56 | * 1. Redistributions of source code must retain the above copyright |
| 57 | * notice, this list of conditions and the following disclaimer. |
| 58 | * 2. Redistributions in binary form must reproduce the above copyright |
| 59 | * notice, this list of conditions and the following disclaimer in |
| 60 | * the documentation and/or other materials provided with the |
| 61 | * distribution. |
| 62 | * 3. Neither the name of the Company nor the names of its contributors |
| 63 | * may be used to endorse or promote products derived from this |
| 64 | * software without specific prior written permission. |
| 65 | */ |
| 66 | |
| 67 | /** |
| 68 | * @page howto How To ... |
| 69 | * |
| 70 | * - @subpage howtoinit |
| 71 | * - @subpage howtoclient |
| 72 | * - @subpage howtoserver |
| 73 | * - @subpage howtoclientcomm |
| 74 | * - @subpage howtoservercomm |
| 75 | */ |
| 76 | |
| 77 | /** |
| 78 | * @page howtoinit Init and Thread-safety Information |
| 79 | * |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 80 | * Before working with the library, it must be initialized using nc_client_init() |
| 81 | * or nc_server_init(). Based on how the library was compiled, also libssh and/or |
| 82 | * libssh/libcrypto are initialized (for multi-threaded use) too. It is advised |
| 83 | * to compile libnetconf2, for instance, with TLS support even if you do not want |
| 84 | * to use lnc2 TLS functions, but only use libssl/libcrypto functions in your |
Michal Vasko | c25921c | 2016-02-29 10:28:40 +0100 | [diff] [blame] | 85 | * application. You can then use libnetconf2 cleanup function and do not |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 86 | * trouble yourself with the cleanup. |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 87 | * |
| 88 | * To prevent any reachable memory at the end of your application, there |
| 89 | * are complementary destroy functions available. If your application is |
| 90 | * multi-threaded, call the destroy functions in the last thread, after all |
| 91 | * the other threads have ended. In every other thread you should call |
| 92 | * nc_thread_destroy() just before it exits. |
| 93 | * |
| 94 | * If libnetconf2 is used in accordance with this information, there should |
| 95 | * not be memory leaks of any kind at program exit. For thread-safety details |
| 96 | * of libssh, libssl, and libcrypto please refer to the corresponding project |
| 97 | * documentation. libnetconf2 thread-safety information is below. |
| 98 | * |
| 99 | * Client is NOT thread-safe and there is no access control in the client |
| 100 | * functions at all. Server is MOSTLY thread-safe meaning you can set all the |
| 101 | * options simultaneously while listening for or accepting new sessions or |
| 102 | * polling the existing ones. It should even be safe to poll one session in |
| 103 | * several threads, but it is definitely discouraged. Generally, servers can |
| 104 | * use more threads without any problems as long as they keep their workflow sane |
| 105 | * (behavior such as freeing sessions only after no thread uses them or similar). |
| 106 | * |
| 107 | * Functions List |
| 108 | * -------------- |
| 109 | * |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 110 | * Available in __nc_client.h__. |
| 111 | * |
| 112 | * - nc_client_init() |
| 113 | * - nc_client_destroy() |
| 114 | * |
| 115 | * Available in __nc_server.h__. |
| 116 | * |
| 117 | * - nc_server_init() |
| 118 | * - nc_server_destroy() |
| 119 | * |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 120 | * Available in both __nc_client.h__ and __nc_server.h__. |
| 121 | * |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 122 | * - nc_thread_destroy() |
| 123 | */ |
| 124 | |
| 125 | /** |
| 126 | * @page howtoclient Client sessions |
| 127 | * |
| 128 | * There are a lot of options for both an SSH and a TLS client. All of them |
| 129 | * have setters and getters so that there is no need to duplicate them in |
| 130 | * a client. |
| 131 | * |
| 132 | * SSH |
| 133 | * === |
| 134 | * |
| 135 | * It is mostly required to set any SSH options and then simply connect to |
| 136 | * a NETCONF server. Optionally, some authetication callbacks can be set, |
| 137 | * which are particulary useful in automated clients (passwords cannot be |
| 138 | * asked a user) or simply if any additional information is retrieved some |
| 139 | * other way than from standard terminal input. |
| 140 | * |
| 141 | * Afterwards, there are 2 functions to use for a new server connection |
| 142 | * and an additional one for creating a new SSH channel on an existing |
| 143 | * NETCONF session. The libssh variant enables to customize the SSH session |
| 144 | * in every way the libssh allows, although that should not normally be needed. |
| 145 | * |
| 146 | * Functions List |
| 147 | * -------------- |
| 148 | * |
| 149 | * Available in __nc_client.h__. |
| 150 | * |
| 151 | * - nc_client_ssh_set_auth_hostkey_check_clb() |
| 152 | * - nc_client_ssh_set_auth_password_clb() |
| 153 | * - nc_client_ssh_set_auth_interactive_clb() |
| 154 | * - nc_client_ssh_set_auth_privkey_passphrase_clb() |
| 155 | * - nc_client_ssh_add_keypair() |
| 156 | * - nc_client_ssh_del_keypair() |
| 157 | * - nc_client_ssh_get_keypair_count() |
| 158 | * - nc_client_ssh_get_keypair() |
| 159 | * - nc_client_ssh_set_auth_pref() |
| 160 | * - nc_client_ssh_get_auth_pref() |
| 161 | * - nc_client_ssh_set_username() |
| 162 | * - nc_client_ssh_get_username() |
| 163 | * |
| 164 | * - nc_connect_ssh() |
| 165 | * - nc_connect_libssh() |
| 166 | * - nc_connect_ssh_channel() |
| 167 | * |
| 168 | * |
| 169 | * TLS |
| 170 | * === |
| 171 | * |
| 172 | * With TLS authentication, is is mandatory to set the client certificate |
| 173 | * with a private key and additional trusted certificates and revocation lists. |
| 174 | * |
| 175 | * Then there are again 2 functions for connecting, the libssl variant enables |
| 176 | * to customize the TLS session in every way the libssl allows. |
| 177 | * |
| 178 | * Functions List |
| 179 | * -------------- |
| 180 | * |
| 181 | * Available in __nc_client.h__. |
| 182 | * |
| 183 | * - nc_client_tls_set_cert_key_paths() |
| 184 | * - nc_client_tls_get_cert_key_paths() |
| 185 | * - nc_client_tls_set_trusted_ca_paths() |
| 186 | * - nc_client_tls_get_trusted_ca_paths() |
| 187 | * - nc_client_tls_set_crl_paths() |
| 188 | * - nc_client_tls_get_crl_paths() |
| 189 | * |
| 190 | * - nc_connect_tls() |
| 191 | * - nc_connect_libssl() |
| 192 | * |
| 193 | * |
| 194 | * FD |
| 195 | * == |
| 196 | * |
| 197 | * If you authenticated the connection using some tunneling software, you |
| 198 | * can pass its file descriptors to libnetconf2, which will continue to |
| 199 | * establish a full NETCONF session. |
| 200 | * |
| 201 | * Funtions List |
| 202 | * ------------- |
| 203 | * |
| 204 | * Available in __nc_client.h__. |
| 205 | * |
| 206 | * - nc_connect_inout() |
| 207 | * |
| 208 | * |
| 209 | * Call Home |
| 210 | * ========= |
| 211 | * |
| 212 | * Call Home needs the same options set as standard SSH or TLS and the functions |
| 213 | * reflect it exactly. However, to accept a connection, the client must first |
| 214 | * specify addresses and ports, which to listen on. Then connections can be |
| 215 | * accepted. |
| 216 | * |
| 217 | * Functions List |
| 218 | * -------------- |
| 219 | * |
| 220 | * Available in __nc_client.h__. |
| 221 | * |
| 222 | * - nc_client_ssh_ch_set_auth_hostkey_check_clb() |
| 223 | * - nc_client_ssh_ch_set_auth_password_clb() |
| 224 | * - nc_client_ssh_ch_set_auth_interactive_clb() |
| 225 | * - nc_client_ssh_ch_set_auth_privkey_passphrase_clb() |
| 226 | * - nc_client_ssh_ch_add_bind_listen() |
| 227 | * - nc_client_ssh_ch_del_bind() |
| 228 | * - nc_client_ssh_ch_add_keypair() |
| 229 | * - nc_client_ssh_ch_del_keypair() |
| 230 | * - nc_client_ssh_ch_get_keypair_count() |
| 231 | * - nc_client_ssh_ch_get_keypair() |
| 232 | * - nc_client_ssh_ch_set_auth_pref() |
| 233 | * - nc_client_ssh_ch_get_auth_pref() |
| 234 | * - nc_client_ssh_ch_set_username() |
| 235 | * - nc_client_ssh_ch_get_username() |
| 236 | * |
| 237 | * - nc_client_tls_ch_add_bind_listen() |
| 238 | * - nc_client_tls_ch_del_bind() |
| 239 | * - nc_client_tls_ch_set_cert_key_paths() |
| 240 | * - nc_client_tls_ch_get_cert_key_paths() |
| 241 | * - nc_client_tls_ch_set_trusted_ca_paths() |
| 242 | * - nc_client_tls_ch_get_trusted_ca_paths() |
| 243 | * - nc_client_tls_ch_set_crl_paths() |
| 244 | * - nc_client_tls_ch_get_crl_paths() |
| 245 | * |
| 246 | * - nc_accept_callhome() |
| 247 | * |
| 248 | * |
| 249 | * Cleanup |
| 250 | * ======= |
| 251 | * |
| 252 | * These options and the schema searchpath are stored in dynamically |
| 253 | * allocated memory. To free it, destroy the client, it cleans up all |
| 254 | * the options |
| 255 | * |
| 256 | * Functions List |
| 257 | * -------------- |
| 258 | * |
| 259 | * Available in __nc_client.h__. |
| 260 | * |
| 261 | * - nc_client_destroy() |
| 262 | */ |
| 263 | |
| 264 | /** |
| 265 | * @page howtoserver Server sessions |
| 266 | * |
| 267 | * Init |
| 268 | * ==== |
| 269 | * |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 270 | * Server takes an argument for its [initialization function](@ref howtoinit). |
| 271 | * In it, you set the server context, which determines what modules it |
| 272 | * supports and what capabilities to advertise. Few capabilities that |
| 273 | * cannot be learnt from the context are set with separate functions. |
| 274 | * So are several general options. |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 275 | * |
| 276 | * Context does not only determine server modules, but its overall |
| 277 | * functionality as well. For every RPC the server should support, |
| 278 | * an nc_rpc_clb callback should be set on that node in the context. |
| 279 | * Server then calls these as appropriate [during poll](@ref howtoservercomm). |
| 280 | * |
| 281 | * Server options can be only set, there are no getters. |
| 282 | * |
| 283 | * Functions List |
| 284 | * -------------- |
| 285 | * |
| 286 | * Available in __nc_server.h__. |
| 287 | * |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 288 | * - nc_server_set_capab_withdefaults() |
| 289 | * - nc_server_set_capab_interleave() |
| 290 | * - nc_server_set_hello_timeout() |
| 291 | * - nc_server_set_idle_timeout() |
| 292 | * |
| 293 | * |
| 294 | * SSH |
| 295 | * === |
| 296 | * |
| 297 | * To be able to accept SSH connections, an endpoint must be added |
| 298 | * and its options set. |
| 299 | * |
| 300 | * Functions List |
| 301 | * -------------- |
| 302 | * |
| 303 | * Available in __nc_server.h__. |
| 304 | * |
| 305 | * - nc_server_ssh_add_endpt_listen() |
| 306 | * - nc_server_ssh_endpt_set_address() |
| 307 | * - nc_server_ssh_endpt_set_port() |
| 308 | * - nc_server_ssh_del_endpt() |
| 309 | * |
| 310 | * - nc_server_ssh_endpt_set_hostkey() |
| 311 | * - nc_server_ssh_endpt_set_banner() |
| 312 | * - nc_server_ssh_endpt_set_auth_methods() |
| 313 | * - nc_server_ssh_endpt_set_auth_attempts() |
| 314 | * - nc_server_ssh_endpt_set_auth_timeout() |
| 315 | * - nc_server_ssh_endpt_add_authkey() |
| 316 | * - nc_server_ssh_endpt_del_authkey() |
| 317 | * |
| 318 | * |
| 319 | * TLS |
| 320 | * === |
| 321 | * |
| 322 | * TLS requires at least one endpoint too, but its options differ |
| 323 | * significantly from the SSH ones, especially in the cert-to-name |
| 324 | * options that TLS uses to derive usernames from client certificates. |
| 325 | * |
| 326 | * Functions List |
| 327 | * -------------- |
| 328 | * |
| 329 | * Available in __nc_server.h__. |
| 330 | * |
| 331 | * - nc_server_tls_add_endpt_listen() |
| 332 | * - nc_server_tls_endpt_set_address() |
| 333 | * - nc_server_tls_endpt_set_port() |
| 334 | * - nc_server_tls_del_endpt() |
| 335 | * |
| 336 | * - nc_server_tls_endpt_set_cert() |
| 337 | * - nc_server_tls_endpt_set_cert_path() |
| 338 | * - nc_server_tls_endpt_set_key() |
| 339 | * - nc_server_tls_endpt_set_key_path() |
| 340 | * - nc_server_tls_endpt_add_trusted_cert() |
| 341 | * - nc_server_tls_endpt_add_trusted_cert_path() |
| 342 | * - nc_server_tls_endpt_set_trusted_ca_paths() |
| 343 | * - nc_server_tls_endpt_clear_certs() |
| 344 | * - nc_server_tls_endpt_set_crl_paths() |
| 345 | * - nc_server_tls_endpt_clear_crls() |
| 346 | * - nc_server_tls_endpt_add_ctn() |
| 347 | * - nc_server_tls_endpt_del_ctn() |
| 348 | * |
| 349 | * FD |
| 350 | * == |
| 351 | * |
| 352 | * If you used a tunneling software, which does its own authentication, |
| 353 | * you can accept a NETCONF session on its file descriptors. |
| 354 | * |
| 355 | * Functions List |
| 356 | * -------------- |
| 357 | * |
| 358 | * Available in __nc_server.h__. |
| 359 | * |
| 360 | * - nc_accept_inout() |
| 361 | * |
| 362 | * |
| 363 | * Call Home |
| 364 | * ========= |
| 365 | * |
| 366 | * Call Home does not work with endpoints like standard sessions. |
| 367 | * The options must be reset manually after another Call Home session |
| 368 | * (with different options than the previous one) is to be established. |
| 369 | * Also, monitoring of these sessions is up to the application. |
| 370 | * |
| 371 | * Functions List |
| 372 | * -------------- |
| 373 | * |
| 374 | * Available in __nc_server.h__. |
| 375 | * |
| 376 | * - nc_connect_callhome_ssh() |
| 377 | * - nc_connect_callhome_tls() |
| 378 | * |
| 379 | * - nc_server_ssh_ch_set_hostkey() |
| 380 | * - nc_server_ssh_ch_set_banner() |
| 381 | * - nc_server_ssh_ch_set_auth_methods() |
| 382 | * - nc_server_ssh_ch_set_auth_attempts() |
| 383 | * - nc_server_ssh_ch_set_auth_timeout() |
| 384 | * - nc_server_ssh_ch_add_authkey() |
| 385 | * - nc_server_ssh_ch_del_authkey() |
| 386 | * - nc_server_ssh_ch_clear_opts() |
| 387 | * |
| 388 | * - nc_server_tls_ch_set_cert() |
| 389 | * - nc_server_tls_ch_set_cert_path() |
| 390 | * - nc_server_tls_ch_set_key() |
| 391 | * - nc_server_tls_ch_set_key_path() |
| 392 | * - nc_server_tls_ch_add_trusted_cert() |
| 393 | * - nc_server_tls_ch_add_trusted_cert_path() |
| 394 | * - nc_server_tls_ch_set_trusted_ca_paths() |
| 395 | * - nc_server_tls_ch_clear_certs() |
| 396 | * - nc_server_tls_ch_set_crl_paths() |
| 397 | * - nc_server_tls_ch_clear_crls() |
| 398 | * - nc_server_tls_ch_add_ctn() |
| 399 | * - nc_server_tls_ch_del_ctn() |
| 400 | * - nc_server_tls_ch_clear_opts() |
| 401 | * |
| 402 | * |
| 403 | * Connecting And Cleanup |
| 404 | * ====================== |
| 405 | * |
| 406 | * When accepting connections, all the endpoints are examined |
| 407 | * and the first with a pending connection is used. To remove all |
| 408 | * the endpoints and free any used dynamic memory, destroy the server. |
| 409 | * |
| 410 | * Functions List |
| 411 | * -------------- |
| 412 | * |
| 413 | * Available in __nc_server.h__. |
| 414 | * |
| 415 | * - nc_accept() |
| 416 | * |
| 417 | * - nc_server_destroy() |
| 418 | */ |
| 419 | |
| 420 | /** |
| 421 | * @page howtoclientcomm Client communication |
| 422 | * |
| 423 | * To send RPCs on a session, you simply create an RPC, send it, |
| 424 | * and then wait for a reply. If you are subscribed, there are 2 ways |
| 425 | * of receiving notifications. Either you wait for them the same way |
| 426 | * as for standard replies or you create a dispatcher that asynchronously |
| 427 | * (in a separate thread) reads notifications and passes them to your |
| 428 | * callback. |
| 429 | * |
| 430 | * Functions List |
| 431 | * -------------- |
| 432 | * |
| 433 | * Available in __nc_client.h__. |
| 434 | * |
| 435 | * - nc_rpc_generic() |
| 436 | * - nc_rpc_generic_xml() |
| 437 | * - nc_rpc_getconfig() |
| 438 | * - nc_rpc_edit() |
| 439 | * - nc_rpc_copy() |
| 440 | * - nc_rpc_delete() |
| 441 | * - nc_rpc_lock() |
| 442 | * - nc_rpc_unlock() |
| 443 | * - nc_rpc_get() |
| 444 | * - nc_rpc_kill() |
| 445 | * - nc_rpc_commit() |
| 446 | * - nc_rpc_discard() |
| 447 | * - nc_rpc_cancel() |
| 448 | * - nc_rpc_validate() |
| 449 | * - nc_rpc_getschema() |
| 450 | * - nc_rpc_subscribe() |
| 451 | * |
| 452 | * - nc_send_rpc() |
| 453 | * - nc_recv_reply() |
| 454 | * - nc_recv_notif() |
| 455 | * - nc_recv_notif_dispatch() |
| 456 | */ |
| 457 | |
| 458 | /** |
| 459 | * @page howtoservercomm Server communication |
| 460 | * |
| 461 | * Once at least one session is established, an nc_pollsession structure |
| 462 | * should be created, filled with the session and polled. Based on |
| 463 | * the return value from the poll further actions can be taken. More |
| 464 | * sessions can be polled at the same time. Any requests received on |
| 465 | * the sessions are [handled internally](@ref howtoserver). |
| 466 | * |
| 467 | * Functions List |
| 468 | * -------------- |
| 469 | * |
| 470 | * Available in __nc_client.h__. |
| 471 | * |
| 472 | * - nc_ps_new() |
| 473 | * - nc_ps_add_session() |
| 474 | * - nc_ps_del_session() |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 475 | * - nc_ps_session_count() |
Michal Vasko | fdfd9dd | 2016-02-29 10:18:46 +0100 | [diff] [blame] | 476 | * - nc_ps_free() |
| 477 | * |
| 478 | * - nc_ps_poll() |
| 479 | * - nc_ps_clear() |
| 480 | * - nc_ps_accept_ssh_channel() |
| 481 | */ |
| 482 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 483 | #endif /* NC_LIBNETCONF_H_ */ |