blob: 50104b2c368cba082e40e8cc130568576262f0dd [file] [log] [blame]
/**
* \file libnetconf.h
* \author Radek Krejci <rkrejci@cesnet.cz>
* \author Michal Vasko <mvasko@cesnet.cz>
* \brief libnetconf2 main internal header.
*
* Copyright (c) 2015 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#ifndef NC_LIBNETCONF_H_
#define NC_LIBNETCONF_H_
#include "config.h"
#include "netconf.h"
#include "log_p.h"
#include "session_p.h"
#include "messages_p.h"
/* Tests whether string is empty or non-empty. */
#define strisempty(str) ((str)[0] == '\0')
#define strnonempty(str) ((str)[0] != '\0')
/**
* @mainpage About
*
* libnetconf2 is a NETCONF library in C handling NETCONF authentication and all NETCONF
* RPC communication both server and client-side. Note that NETCONF datastore implementation
* is not a part of this library. The library supports both NETCONF 1.0
* ([RFC 4741](https://tools.ietf.org/html/rfc4741)) as well as NETCONF 1.1
* ([RFC 6241](https://tools.ietf.org/html/rfc6241)).
*
* @section about-features Main Features
*
* - Creating SSH ([RFC 4742](https://tools.ietf.org/html/rfc4742), [RFC 6242](https://tools.ietf.org/html/rfc6242)),
* using [libssh](https://www.libssh.org/), or TLS ([RFC 7589](https://tools.ietf.org/html/rfc7589)),
* using [OpenSSL](https://www.openssl.org/), authenticated NETCONF sessions.
* - Creating NETCONF sessions with a pre-established transport protocol
* (using this mechanism the communication can be tunneled through sshd(8), for instance).
* - Creating NETCONF Call Home sessions ([RFC 8071](https://tools.ietf.org/html/rfc8071)).
* - Creating, sending, receiving, and replying to RPCs ([RFC 4741](https://tools.ietf.org/html/rfc4741),
* [RFC 6241](https://tools.ietf.org/html/rfc6241)).
* - Creating, sending and receiving NETCONF Event Notifications ([RFC 5277](https://tools.ietf.org/html/rfc5277)),
*
* @section about-license License
*
* Copyright (c) 2015-2017 CESNET, z.s.p.o.
*
* (The BSD 3-Clause License)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*/
/**
* @page howto How To ...
*
* - @subpage howtoinit
* - @subpage howtoclient
* - @subpage howtoserver
* - @subpage howtoclientcomm
* - @subpage howtoservercomm
* - @subpage howtotimeouts
*/
/**
* @page howtoinit Init and Thread-safety Information
*
* Before working with the library, it must be initialized using nc_client_init()
* or nc_server_init(). Based on how the library was compiled, also _libssh_ and/or
* _libssh_/_libcrypto_ are initialized (for multi-threaded use) too. To prevent
* any reachable memory at the end of your application, there are complementary
* destroy functions (nc_server_destroy() and nc_client_destroy() available. If your
* application is multi-threaded, call the destroy functions in the main thread,
* after all the other threads have ended. In every other thread you should call
* nc_thread_destroy() just before it exits.
*
* If _libnetconf2_ is used in accordance with this information, there should
* not be memory leaks of any kind at program exit. For thread-safety details
* of _libssh_, _libssl_, and _libcrypto_, please refer to the corresponding project
* documentation. _libnetconf2_ thread-safety information is below.
*
* Client
* ------
*
* Optionally, a client can specify two alternative ways to get schemas needed when connecting
* with a server. The primary way is to read local files in searchpath (and its subdirectories)
* specified via nc_client_set_schema_searchpath(). Alternatively, _libnetconf2_ can use callback
* provided via nc_client_set_schema_callback(). If these ways do not succeed and the server
* implements NETCONF \<get-schema\> operation, the schema is retrieved from the server and stored
* localy into the searchpath (if specified) for a future use. If none of these methods succeed to
* load particular schema, the data from this schema are ignored during the communication with the
* server.
*
* Besides the mentioned setters, there are many other @ref howtoclientssh "SSH", @ref howtoclienttls "TLS"
* and @ref howtoclientch "Call Home" getter/setter functions to manipulate with various settings. All these
* settings are internally placed in a thread-specific context so they are independent and
* initialized to the default values within each new thread. However, the context can be shared among
* the threads using nc_client_get_thread_context() and nc_client_set_thread_context() functions. In such
* a case, be careful and avoid concurrent execution of the mentioned setters/getters and functions
* creating connection (no matter if it is a standard NETCONF connection or Call Home).
*
* In the client, it is thread-safe to work with distinguish NETCONF sessions since the client
* settings are thread-specific as described above.
*
* Server
* ------
*
* Server is __FULLY__ thread-safe meaning you can set all the (thread-shared in contrast to
* client) options simultaneously while listening for or accepting new sessions or
* polling the existing ones. It is even safe to poll one session in several
* pollsession structures or one pollsession structure in several threads. Generally,
* servers can use more threads without any problems as long as they keep their workflow sane
* (behavior such as freeing sessions only after no thread uses them or similar).
*
* Functions List
* --------------
*
* Available in __nc_client.h__.
*
* - nc_client_init()
* - nc_client_destroy()
*
* - nc_client_get_schema_searchpath()
* - nc_client_set_schema_searchpath()
* - nc_client_get_schema_callback()
* - nc_client_set_schema_callback()
*
* - nc_client_get_thread_context()
* - nc_client_set_thread_context()
*
* Available in __nc_server.h__.
*
* - nc_server_init()
* - nc_server_destroy()
*
* Available in both __nc_client.h__ and __nc_server.h__.
*
* - nc_thread_destroy()
*/
/**
* @page howtoclient Client sessions
*
* To connect to a NETCONF server, a NETCONF session must be established,
* which requires a working transport session. It is possible to create
* NETCONF sessions with SSH (using _libssh_) or TLS (using _libssl/libcrypto_)
* as the underlying transport protocol. It is also possible to establish
* the transport protocol outside _libnetconf2_ and then provide these file
* descriptors (FD) for full NETCONF session creation.
*
* There are a lot of options for both an SSH and a TLS client. All of them
* have setters and getters so that there is no need to duplicate them in
* a client.
*
* @anchor howtoclientssh
* SSH
* ===
*
* Connecting to a server using SSH does not strictly require to set any
* options, there are sensible default values for all the basic ones.
* Except all the SSH options, optionally some authetication callbacks can be set,
* which are particulary useful in automated clients (passwords cannot be
* asked a user) or simply if any additional information is retrieved some
* other way than from standard terminal input.
*
* Having the default options or changing any unsuitable ones, there are 2 functions
* to use for a new server connection. nc_connect_ssh() is the standard function
* that creates sessions using the set options. If there are some options, which
* cannot be changed with the provided API, there is nc_connect_libssh() available.
* It requires a _libssh_ session, in which all the SSH options can be modified
* and even the connection established. This allows for full customization and
* should fit any specific situation.
*
* New NETCONF sessions can also be created on existing authenticated SSH sessions.
* There is a new SSH channel needed, on which the NETCONF session is then created.
* Use nc_connect_ssh_channel() for this purpose.
*
* Functions List
* --------------
*
* Available in __nc_client.h__.
*
* - nc_client_ssh_set_auth_hostkey_check_clb()
* - nc_client_ssh_set_auth_password_clb()
* - nc_client_ssh_set_auth_interactive_clb()
* - nc_client_ssh_set_auth_privkey_passphrase_clb()
* - nc_client_ssh_add_keypair()
* - nc_client_ssh_del_keypair()
* - nc_client_ssh_get_keypair_count()
* - nc_client_ssh_get_keypair()
* - nc_client_ssh_set_auth_pref()
* - nc_client_ssh_get_auth_pref()
* - nc_client_ssh_set_username()
* - nc_client_ssh_get_username()
*
* - nc_connect_ssh()
* - nc_connect_libssh()
* - nc_connect_ssh_channel()
*
* @anchor howtoclienttls
* TLS
* ===
*
* To connect to a server using TLS, there must be some client identification
* options set. Client must specify its certificate with a private key using
* nc_client_tls_set_cert_key_paths(). Also, the Certificate Authority of
* a server certificate must be considered trusted. Paths to all the trusted
* CA certificates can be set by nc_client_tls_set_trusted_ca_paths().
*
* Then there are again 2 functions for connecting, nc_connect_tls() being
* the standard way of connecting. nc_connect_libssl() again enables
* to customize the TLS session in every way _libssl_ allows.
*
* Functions List
* --------------
*
* Available in __nc_client.h__.
*
* - nc_client_tls_set_cert_key_paths()
* - nc_client_tls_get_cert_key_paths()
* - nc_client_tls_set_trusted_ca_paths()
* - nc_client_tls_get_trusted_ca_paths()
* - nc_client_tls_set_crl_paths()
* - nc_client_tls_get_crl_paths()
*
* - nc_connect_tls()
* - nc_connect_libssl()
*
*
* FD
* ==
*
* If you authenticated the connection using some tunneling software, you
* can pass its file descriptors to _libnetconf2_ using nc_connect_inout(),
* which will continue to establish a full NETCONF session.
*
* Funtions List
* -------------
*
* Available in __nc_client.h__.
*
* - nc_connect_inout()
*
*
* @anchor howtoclientch
* Call Home
* =========
*
* Call Home needs the same options set as standard SSH or TLS and the functions
* reflect it exactly. However, to accept a connection, the client must first
* specify addresses and ports, which to listen on by nc_client_ssh_ch_add_bind_listen()
* and nc_client_tls_ch_add_bind_listen(). Then connections can be
* accepted using nc_accept_callhome().
*
* Functions List
* --------------
*
* Available in __nc_client.h__.
*
* - nc_client_ssh_ch_set_auth_hostkey_check_clb()
* - nc_client_ssh_ch_set_auth_password_clb()
* - nc_client_ssh_ch_set_auth_interactive_clb()
* - nc_client_ssh_ch_set_auth_privkey_passphrase_clb()
* - nc_client_ssh_ch_add_bind_listen()
* - nc_client_ssh_ch_del_bind()
* - nc_client_ssh_ch_add_keypair()
* - nc_client_ssh_ch_del_keypair()
* - nc_client_ssh_ch_get_keypair_count()
* - nc_client_ssh_ch_get_keypair()
* - nc_client_ssh_ch_set_auth_pref()
* - nc_client_ssh_ch_get_auth_pref()
* - nc_client_ssh_ch_set_username()
* - nc_client_ssh_ch_get_username()
*
* - nc_client_tls_ch_add_bind_listen()
* - nc_client_tls_ch_del_bind()
* - nc_client_tls_ch_set_cert_key_paths()
* - nc_client_tls_ch_get_cert_key_paths()
* - nc_client_tls_ch_set_trusted_ca_paths()
* - nc_client_tls_ch_get_trusted_ca_paths()
* - nc_client_tls_ch_set_crl_paths()
* - nc_client_tls_ch_get_crl_paths()
*
* - nc_accept_callhome()
*
*
* Cleanup
* =======
*
* These options and the schema searchpath are stored in dynamically
* allocated memory. They are freed as a part of [destroying the client](@ref howtoinit).
*/
/**
* @page howtoserver Server sessions
*
* Init
* ====
*
* Server takes an argument for its [initialization function](@ref howtoinit).
* In it, you set the server context, which determines what modules it
* supports and what capabilities to advertise. Few capabilities that
* cannot be learnt from the context are set with separate functions
* nc_server_set_capab_withdefaults() and generally nc_server_set_capability().
* Timeout for receiving the _hello_ message on a new session can be set
* by nc_server_set_hello_timeout() and the timeout for disconnecting
* an inactive session by nc_server_set_idle_timeout().
*
* Context does not only determine server modules, but its overall
* functionality as well. For every RPC the server should support,
* an nc_rpc_clb callback should be set on that node in the context using nc_set_rpc_callback().
* Server then calls these as appropriate [during poll](@ref howtoservercomm).
*
* Just like in the [client](@ref howtoclient), you can let _libnetconf2_
* establish SSH or TLS transport or do it yourself and only provide the file
* descriptors of the connection.
*
* Server options can be only set, there are no getters.
*
* To be able to accept any connections, endpoints must first be added
* with nc_server_add_endpt() and configured with nc_server_endpt_set_address()
* and nc_server_endpt_set_port().
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_server_set_capab_withdefaults()
* - nc_server_set_capability()
* - nc_server_set_hello_timeout()
* - nc_server_set_idle_timeout()
*
* - nc_server_add_endpt()
* - nc_server_del_endpt()
* - nc_server_endpt_set_address()
* - nc_server_endpt_set_port()
*
*
* SSH
* ===
*
* To successfully accept an SSH session you must set at least the host key using
* nc_server_ssh_endpt_add_hostkey(), which are ordered. This way you simply add
* some hostkey identifier, but the key itself will be retrieved always when needed
* by calling the callback set by nc_server_ssh_set_hostkey_clb().
*
* There are also some other optional settings. Note that authorized
* public keys are set for the server as a whole, not endpoint-specifically.
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_server_ssh_endpt_add_hostkey()
* - nc_server_ssh_endpt_del_hostkey()
* - nc_server_ssh_endpt_mov_hostkey()
* - nc_server_ssh_endpt_mod_hostkey()
* - nc_server_ssh_endpt_set_banner()
* - nc_server_ssh_endpt_set_auth_methods()
* - nc_server_ssh_endpt_set_auth_attempts()
* - nc_server_ssh_endpt_set_auth_timeout()
*
* - nc_server_ssh_set_hostkey_clb()
*
* - nc_server_ssh_add_authkey()
* - nc_server_ssh_add_authkey_path()
* - nc_server_ssh_del_authkey()
*
*
* TLS
* ===
*
* TLS works with endpoints too, but its options differ
* significantly from the SSH ones, especially in the _cert-to-name_
* options that TLS uses to derive usernames from client certificates.
* So, after starting listening on an endpoint you need to set the server
* certificate (nc_server_tls_endpt_set_server_cert()). Its actual content
* together with the matching private key will be loaded using a callback
* from nc_server_tls_set_server_cert_clb().
*
* To accept client certificates, they must first be considered trusted,
* which you have three ways of achieving. You can add each of their Certificate Authority
* certificates to the trusted ones or mark a specific client certificate
* as trusted. Lastly, you can set paths with all the trusted CA certificates
* with nc_server_tls_endpt_set_trusted_ca_paths(). Adding specific certificates
* is also performed only as an arbitrary identificator and later retrieved from
* callback set by nc_server_tls_set_trusted_cert_list_clb(). But, you can add
* certficates as whole lists, not one-by-one.
*
* Then, from each trusted client certificate a username must be derived
* for the NETCONF session. This is accomplished by finding a matching
* _cert-to-name_ entry. They are added using nc_server_tls_endpt_add_ctn().
*
* If you need to remove trusted certificates, you can do so with nc_server_tls_endpt_del_trusted_cert_list().
* To clear all Certificate Revocation Lists use nc_server_tls_endpt_clear_crls().
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_server_tls_endpt_set_server_cert()
* - nc_server_tls_endpt_add_trusted_cert_list()
* - nc_server_tls_endpt_del_trusted_cert_list()
* - nc_server_tls_endpt_set_trusted_ca_paths()
* - nc_server_tls_endpt_set_crl_paths()
* - nc_server_tls_endpt_clear_crls()
* - nc_server_tls_endpt_add_ctn()
* - nc_server_tls_endpt_del_ctn()
* - nc_server_tls_endpt_get_ctn()
*
* - nc_server_tls_set_server_cert_clb()
* - nc_server_tls_set_trusted_cert_list_clb()
*
* FD
* ==
*
* If you used a tunneling software, which does its own authentication,
* you can accept a NETCONF session on its file descriptors with
* nc_accept_inout().
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_accept_inout()
*
*
* Call Home
* =========
*
* _Call Home_ works with endpoints just like standard sessions, but
* the options are organized a bit differently and endpoints are added
* for CH clients. However, one important difference is that
* once all the mandatory options are set, _libnetconf2_ __will not__
* immediately start connecting to a client. It will do so only after
* calling nc_connect_ch_client_dispatch() in a separate thread.
*
* Lastly, monitoring of these sessions is up to the application.
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_server_ch_add_client()
* - nc_server_ch_del_client()
* - nc_server_ch_client_add_endpt()
* - nc_server_ch_client_del_endpt()
* - nc_server_ch_client_endpt_set_address()
* - nc_server_ch_client_endpt_set_port()
* - nc_server_ch_client_set_conn_type()
* - nc_server_ch_client_persist_set_idle_timeout()
* - nc_server_ch_client_persist_set_keep_alive_max_wait()
* - nc_server_ch_client_persist_set_keep_alive_max_attempts()
* - nc_server_ch_client_period_set_idle_timeout()
* - nc_server_ch_client_period_set_reconnect_timeout()
* - nc_server_ch_client_set_start_with()
* - nc_server_ch_client_set_max_attempts()
* - nc_connect_ch_client_dispatch()
*
* - nc_server_ssh_ch_client_add_hostkey()
* - nc_server_ssh_ch_client_del_hostkey()
* - nc_server_ssh_ch_client_mov_hostkey()
* - nc_server_ssh_ch_client_mod_hostkey()
* - nc_server_ssh_ch_client_set_banner()
* - nc_server_ssh_ch_client_set_auth_methods()
* - nc_server_ssh_ch_client_set_auth_attempts()
* - nc_server_ssh_ch_client_set_auth_timeout()
*
* - nc_server_tls_ch_client_set_server_cert()
* - nc_server_tls_ch_client_add_trusted_cert_list()
* - nc_server_tls_ch_client_del_trusted_cert_list()
* - nc_server_tls_ch_client_set_trusted_ca_paths()
* - nc_server_tls_ch_client_set_crl_paths()
* - nc_server_tls_ch_client_clear_crls()
* - nc_server_tls_ch_client_add_ctn()
* - nc_server_tls_ch_client_del_ctn()
* - nc_server_tls_ch_client_get_ctn()
*
*
* Connecting And Cleanup
* ======================
*
* When accepting connections with nc_accept(), all the endpoints are examined
* and the first with a pending connection is used. To remove all CH clients,
* endpoints, and free any used dynamic memory, [destroy](@ref howtoinit) the server.
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_accept()
*/
/**
* @page howtoclientcomm Client communication
*
* To send RPCs on a session, you simply create an RPC, send it using nc_send_rpc(),
* and then wait for a reply using nc_recv_reply(). If you are subscribed, there are 2 ways
* of receiving notifications. Either you wait for them the same way
* as for standard replies with nc_recv_notif() or you create a dispatcher
* with nc_recv_notif_dispatch() that asynchronously (in a separate thread)
* reads notifications and passes them to your callback.
*
* Functions List
* --------------
*
* Available in __nc_client.h__.
*
* - nc_rpc_act_generic()
* - nc_rpc_act_generic_xml()
* - nc_rpc_getconfig()
* - nc_rpc_edit()
* - nc_rpc_copy()
* - nc_rpc_delete()
* - nc_rpc_lock()
* - nc_rpc_unlock()
* - nc_rpc_get()
* - nc_rpc_kill()
* - nc_rpc_commit()
* - nc_rpc_discard()
* - nc_rpc_cancel()
* - nc_rpc_validate()
* - nc_rpc_getschema()
* - nc_rpc_subscribe()
*
* - nc_send_rpc()
* - nc_recv_reply()
* - nc_recv_notif()
* - nc_recv_notif_dispatch()
*/
/**
* @page howtoservercomm Server communication
*
* Once at least one session is established, an nc_pollsession structure
* should be created with nc_ps_new(), filled with the session using
* nc_ps_add_session() and finally polled with nc_ps_poll(). Based on
* the return value from the poll, further actions can be taken. More
* sessions can be polled at the same time and any requests received on
* the sessions are [handled internally](@ref howtoserver).
*
* If an SSH NETCONF session asks for a new channel, you can accept
* this request with nc_ps_accept_ssh_channel() or nc_session_accept_ssh_channel()
* depending on the structure you want to use as the argument.
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_ps_new()
* - nc_ps_add_session()
* - nc_ps_del_session()
* - nc_ps_session_count()
* - nc_ps_free()
*
* - nc_ps_poll()
* - nc_ps_clear()
* - nc_ps_accept_ssh_channel()
* - nc_session_accept_ssh_channel()
*/
/**
* @page howtotimeouts Timeouts
*
* There are several timeouts which are used throughout _libnetconf2_ to
* assure that it will never indefinitely hang on any operation. Normally,
* you should not need to worry about them much necause they are set by
* default to reasonable values for common systems. However, if your
* platform is not common (embedded, ...), adjusting these timeouts may
* save a lot of debugging and time.
*
* Compile Options
* ---------------
*
* You can adjust active and inactive read timeout using `cmake` variables.
* For details look into `README.md`.
*
* API Functions
* -------------
*
* Once a new connection is established including transport protocol negotiations,
* _hello_ message is exchanged. You can set how long will the server wait for
* receiving this message from a client before dropping it.
*
* Having a NETCONF session working, it may not communicate for a longer time.
* To free up some resources, it is possible to adjust the maximum idle period
* of a session before it is disconnected. In _Call Home_, for both a persistent
* and periodic connection can this idle timeout be specified separately for each
* client using corresponding functions.
*
* Lastly, SSH user authentication timeout can be also modified. It is the time
* a client has to successfully authenticate after connecting before it is disconnected.
*
* Functions List
* --------------
*
* Available in __nc_server.h__.
*
* - nc_server_set_hello_timeout()
* - nc_server_set_idle_timeout()
* - nc_server_ch_client_persist_set_idle_timeout()
* - nc_server_ch_client_period_set_idle_timeout()
* - nc_server_ch_client_period_set_reconnect_timeout()
* - nc_server_ssh_endpt_set_auth_timeout()
* - nc_server_ssh_ch_client_set_auth_timeout()
*/
/**
* @defgroup misc Miscellaneous
* @brief Miscellaneous macros, types, structure and functions for a generic use by both server and client applications.
*/
/**
* @defgroup client Client
* @brief NETCONF client functionality.
*/
/**
* @defgroup server Server
* @brief NETCONF server functionality.
*/
#endif /* NC_LIBNETCONF_H_ */