blob: c069108422b21df5d3d01ac48662e4492eeb9c05 [file] [log] [blame]
Michal Vasko45f298f2016-01-29 10:26:26 +01001/**
2 * \file session_server_ch.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 Call Home session server manipulation
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko45f298f2016-01-29 10:26:26 +010013 */
14
15#ifndef NC_SESSION_SERVER_CH_H_
16#define NC_SESSION_SERVER_CH_H_
17
Michal Vaskoc09730e2019-01-17 10:07:26 +010018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michal Vasko45f298f2016-01-29 10:26:26 +010022#include <stdint.h>
23#include <libyang/libyang.h>
24
25#include "session.h"
26#include "netconf.h"
27
Michal Vasko2e6defd2016-10-07 15:48:15 +020028#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko45f298f2016-01-29 10:26:26 +010029
30/**
Radek Krejci6799a052017-05-19 14:23:23 +020031 * @defgroup server_ch Server-side Call Home
32 * @ingroup server
33 *
34 * @brief Call Home functionality for server-side applications.
35 * @{
36 */
37
38/**
Michal Vasko2e6defd2016-10-07 15:48:15 +020039 * @brief Add a new Call Home client.
Michal Vasko45f298f2016-01-29 10:26:26 +010040 *
Michal Vasko2e6defd2016-10-07 15:48:15 +020041 * @param[in] name Arbitrary unique client name.
42 * @param[in] ti Transport protocol to use.
43 * @return 0 on success, -1 on error.
Michal Vasko45f298f2016-01-29 10:26:26 +010044 */
Michal Vasko2e6defd2016-10-07 15:48:15 +020045int nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti);
46
47/**
48 * @brief Drop any connections, stop connecting and remove a client.
49 *
50 * @param[in] name Client name. NULL matches all the clients.
Michal Vasko59050372016-11-22 14:33:55 +010051 * @param[in] ti Client transport protocol. NULL matches any protocol.
52 * Redundant to set if \p name is set, client names are
53 * unique disregarding their protocol.
Michal Vasko2e6defd2016-10-07 15:48:15 +020054 * @return 0 on success, -1 on not finding any match.
55 */
Michal Vasko59050372016-11-22 14:33:55 +010056int nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti);
Michal Vasko2e6defd2016-10-07 15:48:15 +020057
58/**
59 * @brief Add a new Call Home client endpoint.
60 *
61 * @param[in] client_name Existing client name.
62 * @param[in] endpt_name Arbitrary unique (within the client) endpoint name.
63 * @return 0 on success, -1 on error.
64 */
65int nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name);
66
67/**
68 * @brief Remove a Call Home client endpoint.
69 *
70 * @param[in] client_name Existing client name.
71 * @param[in] endpt_name Existing endpoint of \p client_name. NULL matches all endpoints.
72 * @return 0 on success, -1 on error.
73 */
74int nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name);
75
76/**
77 * @brief Change Call Home client endpoint listening address.
78 *
79 * On error the previous listening socket (if any) is left untouched.
80 *
81 * @param[in] client_name Existing Call Home client name.
82 * @param[in] endpt_name Existing endpoint name of \p client_name.
83 * @param[in] address New listening address.
84 * @return 0 on success, -1 on error.
85 */
86int nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address);
87
88/**
89 * @brief Change Call Home client endpoint listening port.
90 *
91 * On error the previous listening socket (if any) is left untouched.
92 *
93 * @param[in] client_name Existing Call Home client name.
94 * @param[in] endpt_name Existing endpoint name of \p client_name.
95 * @param[in] port New listening port.
96 * @return 0 on success, -1 on error.
97 */
98int nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port);
99
100/**
101 * @brief Set Call Home client connection type.
102 *
103 * @param[in] client_name Existing Call Home client name.
104 * @param[in] conn_type Call Home connection type.
105 * @return 0 on success, -1 on error.
106 */
107int nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type);
108
109/**
110 * @brief Set Call Home client persistent connection idle timeout.
111 *
112 * @param[in] client_name Existing Call Home client name.
113 * @param[in] idle_timeout Call Home persistent idle timeout.
114 * @return 0 on success, -1 on error.
115 */
116int nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout);
117
118/**
119 * @brief Set Call Home client persistent connection keep-alive max wait time.
120 *
121 * @param[in] client_name Existing Call Home client name.
122 * @param[in] max_wait Call Home persistent max wait time for keep-alive reply.
123 * @return 0 on success, -1 on error.
124 */
125int nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait);
126
127/**
128 * @brief Set Call Home client persistent connection keep-alive max attempts.
129 *
130 * @param[in] client_name Existing Call Home client name.
131 * @param[in] max_attempts Call Home persistent keep-alive maximum contact attempts.
132 * @return 0 on success, -1 on error.
133 */
134int nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts);
135
136/**
137 * @brief Set Call Home client periodic connection idle timeout.
138 *
139 * @param[in] client_name Existing Call Home client name.
140 * @param[in] idle_timeout Call Home periodic idle timeout.
141 * @return 0 on success, -1 on error.
142 */
143int nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout);
144
145/**
146 * @brief Set Call Home client periodic reconnect timeout.
147 *
148 * @param[in] client_name Existing Call Home client name.
149 * @param[in] reconnect_timeout Call Home periodic reconnect timeout.
150 * @return 0 on success, -1 on error.
151 */
152int nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout);
153
154/**
155 * @brief Set Call Home client start-with policy.
156 *
157 * @param[in] client_name Existing Call Home client name.
158 * @param[in] start_with Call Home client start-with.
159 * @return 0 on success, -1 on error.
160 */
161int nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with);
162
163/**
164 * @brief Set Call Home client overall max attempts.
165 *
166 * @param[in] client_name Existing Call Home client name.
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100167 * @param[in] max_attempts Call Home overall max reconnect attempts.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200168 * @return 0 on success, -1 on error.
169 */
170int nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts);
171
172/**
173 * @brief Establish a Call Home connection with a listening NETCONF client.
174 *
175 * @param[in] client_name Existing client name.
176 * @param[out] session_clb Function that is called for every established session on the client. \p new_session
177 * pointer is internally discarded afterwards.
178 * @return 0 if the thread was successfully created, -1 on error.
179 */
180int nc_connect_ch_client_dispatch(const char *client_name,
181 void (*session_clb)(const char *client_name, struct nc_session *new_session));
182
Radek Krejci6799a052017-05-19 14:23:23 +0200183/** @} Server-side Call Home */
184
Michal Vasko2e6defd2016-10-07 15:48:15 +0200185#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
186
187#ifdef NC_ENABLED_SSH
Michal Vasko45f298f2016-01-29 10:26:26 +0100188
189/**
Radek Krejci6799a052017-05-19 14:23:23 +0200190 * @defgroup server_ch_ssh Server-side Call Home on SSH
191 * @ingroup server_ch
192 *
193 * @brief SSH settings for the Call Home functionality
194 * @{
195 */
196
197/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100198 * @brief Add Call Home SSH host keys the server will identify itself with. Only the name is set, the key itself
199 * wil be retrieved using a callback.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200200 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200201 * @param[in] client_name Existing Call Home client name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100202 * @param[in] name Arbitrary name of the host key.
Michal Vasko7d255882017-02-09 13:35:08 +0100203 * @param[in] idx Optional index where to add the key. -1 adds at the end.
Michal Vaskoe2713da2016-08-22 16:06:40 +0200204 * @return 0 on success, -1 on error.
205 */
Michal Vasko7d255882017-02-09 13:35:08 +0100206int nc_server_ssh_ch_client_add_hostkey(const char *client_name, const char *name, int16_t idx);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200207
208/**
209 * @brief Delete Call Home SSH host keys. Their order is preserved.
Michal Vasko45f298f2016-01-29 10:26:26 +0100210 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200211 * @param[in] client_name Existing Call Home client name.
Michal Vasko7d255882017-02-09 13:35:08 +0100212 * @param[in] name Name of the host key. NULL matches all the keys, but if \p idx != -1 then this must be NULL.
213 * @param[in] idx Index of the hostkey. -1 matches all indices, but if \p name != NULL then this must be -1.
Michal Vasko45f298f2016-01-29 10:26:26 +0100214 * @return 0 on success, -1 on error.
215 */
Michal Vasko7d255882017-02-09 13:35:08 +0100216int nc_server_ssh_ch_client_del_hostkey(const char *client_name, const char *name, int16_t idx);
Michal Vasko45f298f2016-01-29 10:26:26 +0100217
218/**
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100219 * @brief Move Call Home SSH host key.
220 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100221 * @param[in] client_name Exisitng Call Home client name.
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100222 * @param[in] key_mov Name of the host key that will be moved.
223 * @param[in] key_after Name of the key that will preceed \p key_mov. NULL if \p key_mov is to be moved at the beginning.
224 * @return 0 in success, -1 on error.
225 */
226int nc_server_ssh_ch_client_mov_hostkey(const char *client_name, const char *key_mov, const char *key_after);
227
228/**
229 * @brief Modify Call Home SSH host key.
230 *
231 * @param[in] endpt_name Exisitng endpoint name.
232 * @param[in] name Name of an existing host key.
233 * @param[in] new_name New name of the host key \p name.
234 * @return 0 in success, -1 on error.
235 */
236int nc_server_ssh_ch_client_mod_hostkey(const char *endpt_name, const char *name, const char *new_name);
237
238/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100239 * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive)
Michal Vaskof0537d82016-01-29 14:42:38 +0100240 * are supported by default.
Michal Vasko45f298f2016-01-29 10:26:26 +0100241 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200242 * @param[in] client_name Existing Call Home client name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100243 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
244 * @return 0 on success, -1 on error.
245 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200246int nc_server_ssh_ch_client_set_auth_methods(const char *client_name, int auth_methods);
Michal Vasko45f298f2016-01-29 10:26:26 +0100247
248/**
249 * @brief Set Call Home SSH authentication attempts of every client. 3 by default.
250 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200251 * @param[in] client_name Existing Call Home client name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100252 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
253 * @return 0 on success, -1 on error.
254 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200255int nc_server_ssh_ch_client_set_auth_attempts(const char *client_name, uint16_t auth_attempts);
Michal Vasko45f298f2016-01-29 10:26:26 +0100256
257/**
258 * @brief Set Call Home SSH authentication timeout. 10 seconds by default.
259 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200260 * @param[in] client_name Existing Call Home client name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100261 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
262 * @return 0 on success, -1 on error.
263 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200264int nc_server_ssh_ch_client_set_auth_timeout(const char *client_name, uint16_t auth_timeout);
Michal Vasko45f298f2016-01-29 10:26:26 +0100265
Radek Krejci6799a052017-05-19 14:23:23 +0200266/** @} Server-side Call Home on SSH */
267
Radek Krejci53691be2016-02-22 13:58:37 +0100268#endif /* NC_ENABLED_SSH */
Michal Vasko45f298f2016-01-29 10:26:26 +0100269
Radek Krejci53691be2016-02-22 13:58:37 +0100270#ifdef NC_ENABLED_TLS
Michal Vasko45f298f2016-01-29 10:26:26 +0100271
272/**
Radek Krejci6799a052017-05-19 14:23:23 +0200273 * @defgroup server_ch_tls Server-side Call Home on TLS
274 * @ingroup server_ch
275 *
276 * @brief TLS settings for the Call Home functionality
277 * @{
278 */
279
280/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100281 * @brief Set the server Call Home TLS certificate. Only the name is set, the certificate itself
282 * wil be retrieved using a callback.
Michal Vasko45f298f2016-01-29 10:26:26 +0100283 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200284 * @param[in] client_name Existing Call Home client name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100285 * @param[in] name Arbitrary certificate name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100286 * @return 0 on success, -1 on error.
287 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100288int nc_server_tls_ch_client_set_server_cert(const char *client_name, const char *name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100289
290/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100291 * @brief Add a Call Home trusted certificate list. Can be both a CA or a client one.
Michal Vasko45f298f2016-01-29 10:26:26 +0100292 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200293 * @param[in] client_name Existing Call Home client name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100294 * @param[in] name Arbitary name identifying this certificate list.
Michal Vasko45f298f2016-01-29 10:26:26 +0100295 * @return 0 on success, -1 on error.
296 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100297int nc_server_tls_ch_client_add_trusted_cert_list(const char *client_name, const char *name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100298
299/**
Michal Vasko4c1fb492017-01-30 14:31:07 +0100300 * @brief Remove a set Call Home trusted certificate list. CRLs and CTN entries are not affected.
Michal Vasko45f298f2016-01-29 10:26:26 +0100301 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200302 * @param[in] client_name Existing Call Home client name.
Michal Vasko4c1fb492017-01-30 14:31:07 +0100303 * @param[in] name Name of the certificate list to delete. NULL deletes all the lists.
304 * @return 0 on success, -1 on not found.
Michal Vasko45f298f2016-01-29 10:26:26 +0100305 */
Michal Vasko4c1fb492017-01-30 14:31:07 +0100306int nc_server_tls_ch_client_del_trusted_cert_list(const char *client_name, const char *name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100307
308/**
309 * @brief Set trusted Call Home Certificate Authority certificate locations. There
Michal Vaskof0537d82016-01-29 14:42:38 +0100310 * can only be one file and one directory, they are replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100311 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200312 * @param[in] client_name Existing Call Home client name.
Michal Vaskob34b3e82016-02-26 15:04:58 +0100313 * @param[in] ca_file Path to a trusted CA cert store file in PEM format.
314 * Can be NULL.
315 * @param[in] ca_dir Path to a trusted CA cert store hashed directory
316 * (c_rehash utility can be used to create hashes)
317 * with PEM files. Can be NULL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100318 * @return 0 on success, -1 on error.
319 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200320int nc_server_tls_ch_client_set_trusted_ca_paths(const char *client_name, const char *ca_file, const char *ca_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100321
322/**
Michal Vasko45f298f2016-01-29 10:26:26 +0100323 * @brief Set Call Home Certificate Revocation List locations. There can only be
Michal Vaskof0537d82016-01-29 14:42:38 +0100324 * one file and one directory, they are replaced if already set.
Michal Vasko45f298f2016-01-29 10:26:26 +0100325 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200326 * @param[in] client_name Existing Call Home client name.
Michal Vaskob34b3e82016-02-26 15:04:58 +0100327 * @param[in] crl_file Path to a CRL store file in PEM format. Can be NULL.
328 * @param[in] crl_dir Path to a CRL store hashed directory (c_rehash utility
329 * can be used to create hashes) with PEM files. Can be NULL.
Michal Vasko45f298f2016-01-29 10:26:26 +0100330 * @return 0 on success, -1 on error.
331 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200332int nc_server_tls_ch_client_set_crl_paths(const char *client_name, const char *crl_file, const char *crl_dir);
Michal Vasko45f298f2016-01-29 10:26:26 +0100333
334/**
335 * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys,
Michal Vaskof0537d82016-01-29 14:42:38 +0100336 * and CTN entries are not affected.
Michal Vasko2e6defd2016-10-07 15:48:15 +0200337 *
338 * @param[in] client_name Existing Call Home client name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100339 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200340void nc_server_tls_ch_client_clear_crls(const char *client_name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100341
342/**
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100343 * @brief Add a cert-to-name entry.
Michal Vasko45f298f2016-01-29 10:26:26 +0100344 *
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100345 * It is possible to add an entry step-by-step, specifying first only \p ip and in later calls
346 * \p fingerprint, \p map_type, and optionally \p name spearately.
347 *
Michal Vasko50d2a5c2017-02-14 10:29:49 +0100348 * @param[in] client_name Existing Call Home client name.
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100349 * @param[in] id Priority of the entry. It must be unique. If already exists, the entry with this id
350 * is modified.
351 * @param[in] fingerprint Matching certificate fingerprint. If NULL, kept temporarily unset.
352 * @param[in] map_type Type of username-certificate mapping. If 0, kept temporarily unset.
353 * @param[in] name Specific username used only if \p map_type == NC_TLS_CTN_SPECIFED.
Michal Vasko45f298f2016-01-29 10:26:26 +0100354 * @return 0 on success, -1 on error.
355 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200356int nc_server_tls_ch_client_add_ctn(const char *client_name, uint32_t id, const char *fingerprint,
357 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100358
359/**
Michal Vasko58c22a22016-11-23 13:49:53 +0100360 * @brief Remove a Call Home cert-to-name entry.
Michal Vasko45f298f2016-01-29 10:26:26 +0100361 *
Michal Vasko2e6defd2016-10-07 15:48:15 +0200362 * @param[in] client_name Existing Call Home client name.
Michal Vasko45f298f2016-01-29 10:26:26 +0100363 * @param[in] id Priority of the entry. -1 matches all the priorities.
364 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
365 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
366 * @param[in] name Specific username for the entry. NULL matches all the usernames.
367 * @return 0 on success, -1 on not finding any match.
368 */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200369int nc_server_tls_ch_client_del_ctn(const char *client_name, int64_t id, const char *fingerprint,
370 NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko45f298f2016-01-29 10:26:26 +0100371
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100372/**
373 * @brief Get a Call Home cert-to-name entry.
374 *
375 * If a parameter is NULL, it is ignored. If its dereferenced value is NULL,
376 * it is filled and returned. If the value is set, it is used as a filter.
377 * Returns first matching entry.
378 *
379 * @param[in] client_name Existing Call Home client name.
380 * @param[in,out] id Priority of the entry.
381 * @param[in,out] fingerprint Fingerprint fo the entry.
382 * @param[in,out] map_type Mapping type of the entry.
383 * @param[in,out] name Specific username for the entry.
384 * @return 0 on success, -1 on not finding any match.
385 */
Michal Vaskof585ac72016-11-25 15:16:38 +0100386int nc_server_tls_ch_client_get_ctn(const char *client_name, uint32_t *id, char **fingerprint,
387 NC_TLS_CTN_MAPTYPE *map_type, char **name);
Michal Vaskodf5e6af2016-11-23 13:50:56 +0100388
Radek Krejci6799a052017-05-19 14:23:23 +0200389/** @} Server-side Call Home on TLS */
390
Radek Krejci53691be2016-02-22 13:58:37 +0100391#endif /* NC_ENABLED_TLS */
Michal Vasko45f298f2016-01-29 10:26:26 +0100392
Michal Vaskoc09730e2019-01-17 10:07:26 +0100393#ifdef __cplusplus
394}
395#endif
396
Michal Vasko45f298f2016-01-29 10:26:26 +0100397#endif /* NC_SESSION_SERVER_CH_H_ */