blob: 45cdcda38833eed2ed6361774138e93260bdcbe0 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 session server manipulation
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
23#ifndef NC_SESSION_SERVER_H_
24#define NC_SESSION_SERVER_H_
25
26#include <stdint.h>
Michal Vasko05ba9df2016-01-13 14:40:27 +010027#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
29#include "session.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "netconf.h"
31
Michal Vasko1a38c862016-01-15 15:50:07 +010032/**
33 * @brief Prototype of callbacks that are called if some RPCs are received.
34 *
35 * If \p session termination reason is changed in the callback, one last reply
36 * is sent and then the session is considered invalid.
37 *
38 * @param[in] rpc Parsed client RPC request.
39 * @param[in] session Session the RPC arrived on.
40 * @return Server reply. If NULL, an operation-failed error will be sent to the client.
41 */
Michal Vasko05ba9df2016-01-13 14:40:27 +010042typedef struct nc_server_reply *(*nc_rpc_clb)(struct lyd_node *rpc, struct nc_session *session);
43
Michal Vasko1a38c862016-01-15 15:50:07 +010044/**
45 * @brief Set the termination reason for a session.
46 *
47 * @param[in] session Session to modify.
48 * @param[in] reason Reason of termination.
49 */
50void nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason);
51
52/**
53 * @brief Initialize the server using a libyang context.
54 *
55 * The context is not modified internally, only its dictionary is used for holding
Michal Vasko11d142a2016-01-19 15:58:24 +010056 * all the strings. When the dictionary is being written to or removed from,
57 * libnetconf2 always holds ctx lock using nc_ctx_lock(). Reading models is considered
58 * thread-safe as models cannot be removed and are rarely modified.
Michal Vasko1a38c862016-01-15 15:50:07 +010059 *
60 * Server capabilities are generated based on its content. Changing the context
61 * in ways that result in changed capabilities (adding models, changing features)
62 * is discouraged after sessions are established as it is not possible to change
63 * capabilities of a session.
64 *
65 * This context can safely be destroyed only after calling the last libnetconf2
66 * function in an application.
67 *
68 * Supported RPCs of models in the context are expected to have the private field
69 * in the corresponding RPC schema node set to a nc_rpc_clb function callback.
70 * This callback is called by nc_ps_poll() if the particular RPC request is
71 * received. Callbacks for ietf-netconf:get-schema (supporting YANG and YIN format
72 * only) and ietf-netconf:close-session are set internally if left unset.
73 *
74 * Access to the context in libnetconf2 functions is not managed in any way,
75 * the application is responsible for handling it in a thread-safe manner.
76 *
77 * @param[in] ctx Core NETCONF server context.
78 * @return 0 on success, -1 on error.
79 */
Michal Vasko086311b2016-01-08 09:53:11 +010080int nc_server_init(struct ly_ctx *ctx);
81
Michal Vasko1a38c862016-01-15 15:50:07 +010082/**
Michal Vaskob48aa812016-01-18 14:13:09 +010083 * @brief Destroy any dynamically allocated server resources.
84 */
85void nc_server_destroy(void);
86
87/**
Michal Vasko1a38c862016-01-15 15:50:07 +010088 * @brief Set the with-defaults capability extra parameters.
89 *
90 * For the capability to be actually advertised, the server context must also
91 * include the ietf-netconf-with-defaults model.
92 *
93 * Changing this option has the same ill effects as changing capabilities while
94 * sessions are already established.
95 *
96 * @param[in] basic_mode basic-mode with-defaults parameter.
97 * @param[in] also_supported NC_WD_MODE bit array, also-supported with-defaults
98 * parameter.
99 * @return 0 on success, -1 on error.
100 */
Michal Vasko086311b2016-01-08 09:53:11 +0100101int nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported);
102
Michal Vasko1a38c862016-01-15 15:50:07 +0100103/**
104 * @brief Set the interleave capability.
105 *
106 * For the capability to be actually advertised, the server context must also
107 * include the nc-notifications model.
108 *
109 * Changing this option has the same ill effects as changing capabilities while
110 * sessions are already established.
111 *
112 * @param[in] interleave_support 1 to suport interleave, 0 to not.
113 */
114void nc_server_set_capab_interleave(int interleave_support);
Michal Vasko086311b2016-01-08 09:53:11 +0100115
Michal Vasko1a38c862016-01-15 15:50:07 +0100116/**
117 * @brief Set server timeout for receiving a hello message.
118 *
119 * @param[in] hello_timeout Hello message timeout. 0 for infinite waiting.
120 */
121void nc_server_set_hello_timeout(uint16_t hello_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100122
Michal Vasko1a38c862016-01-15 15:50:07 +0100123/**
124 * @brief Set server timeout for dropping an idle session.
125 *
126 * @param[in] idle_timeout Idle session timeout. 0 to never drop a session
127 * because of inactivity.
128 */
129void nc_server_set_idle_timeout(uint16_t idle_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100130
Michal Vasko1a38c862016-01-15 15:50:07 +0100131/**
132 * @brief Accept a new session on a pre-established transport session.
133 *
134 * @param[in] fdin File descriptor to read (unencrypted) XML data from.
135 * @param[in] fdout File descriptor to write (unencrypted) XML data to.
136 * @param[in] username NETCONF username as provided by the transport protocol.
137 * @param[out] session New session on success.
138 * @return 0 on success, -1 on error.
139 */
140int nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session);
Michal Vasko086311b2016-01-08 09:53:11 +0100141
Michal Vasko1a38c862016-01-15 15:50:07 +0100142/**
143 * @brief Create an empty structure for polling sessions.
144 *
145 * @return Empty pollsession structure, NULL on error.
146 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100147struct nc_pollsession *nc_ps_new(void);
Michal Vaskofb89d772016-01-08 12:25:35 +0100148
Michal Vasko1a38c862016-01-15 15:50:07 +0100149/**
150 * @brief Free a pollsession structure.
151 *
152 * @param[in] ps Pollsession structure to free.
153 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100154void nc_ps_free(struct nc_pollsession *ps);
Michal Vaskofb89d772016-01-08 12:25:35 +0100155
Michal Vasko1a38c862016-01-15 15:50:07 +0100156/**
157 * @brief Add a session to a pollsession structure.
158 *
159 * @param[in] ps Pollsession structure to modify.
160 * @param[in] session Session to add to \p ps.
161 * @return 0 on success, -1 on error.
162 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100163int nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session);
Michal Vaskofb89d772016-01-08 12:25:35 +0100164
Michal Vasko1a38c862016-01-15 15:50:07 +0100165/**
166 * @brief Remove a session from a pollsession structure.
167 *
168 * @param[in] ps Pollsession structure to modify.
169 * @param[in] session Session to remove from \p ps.
170 * @return 0 on success, 1 if not found, -1 on error.
171 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100172int nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session);
173
Michal Vasko1a38c862016-01-15 15:50:07 +0100174/**
175 * @brief Poll sessions and process any received RPCs.
176 *
177 * All the sessions must be running. If a session fails causing it to change its
Michal Vasko96164bf2016-01-21 15:41:58 +0100178 * status, it can be learnt from the return value. Only one event on one session
179 * is handled in one function call.
Michal Vasko1a38c862016-01-15 15:50:07 +0100180 *
181 * @param[in] ps Pollsession structure to use.
Michal Vasko11d142a2016-01-19 15:58:24 +0100182 * @param[in] timeout Poll timeout in milliseconds. 0 for non-blocking call, -1 for
183 * infinite waiting.
Michal Vaskobd8ef262016-01-20 11:09:27 +0100184 * @return 0 on elapsed timeout,
185 * 1 if an RPC was processed,
186 * 2 if an RPC was processed and there are unhandled events on other sessions,
187 * 3 if a session from \p ps changed its status (was invalidated),
188 * -1 on error.
Michal Vasko96164bf2016-01-21 15:41:58 +0100189 *
190 * Only with SSH support:
191 * 4 if an SSH message was processed,
192 * 5 if a new NETCONF SSH channel was created; call nc_ssh_ps_accept_channel()
193 * to establish a new NETCONF session.
Michal Vasko1a38c862016-01-15 15:50:07 +0100194 */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100195int nc_ps_poll(struct nc_pollsession *ps, int timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100196
Michal Vasko11d142a2016-01-19 15:58:24 +0100197/**
198 * @brief Lock server context.
199 *
200 * @param[in] timeout Timeout in milliseconds. 0 for non-blocking call, -1 for
201 * infinite waiting.
202 * @param[out] elapsed Elapsed milliseconds will be added to this variable.
203 * Can be NULL.
204 * @return 0 on elapsed timeout, 1 on success, -1 on error.
205 */
206int nc_ctx_lock(int timeout, int *elapsed);
207
208/**
209 * @brief Unlock server context.
210 *
211 * @return 0 on success, -1 on error.
212 */
213int nc_ctx_unlock(void);
214
Michal Vasko9e036d52016-01-08 10:49:26 +0100215#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
216
Michal Vasko1a38c862016-01-15 15:50:07 +0100217/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100218 * @brief Accept new sessions on all the listening endpoints.
Michal Vasko1a38c862016-01-15 15:50:07 +0100219 *
Michal Vasko11d142a2016-01-19 15:58:24 +0100220 * @param[in] timeout Timeout for receiving a new connection in milliseconds, 0 for
221 * non-blocking call, -1 for infinite waiting.
Michal Vasko96164bf2016-01-21 15:41:58 +0100222 * @param[out] session New session.
Michal Vaskoc61c4492016-01-25 11:13:34 +0100223 * @return 1 on success, 0 on timeout, -1 on error.
Michal Vasko1a38c862016-01-15 15:50:07 +0100224 */
225int nc_accept(int timeout, struct nc_session **session);
Michal Vasko9e036d52016-01-08 10:49:26 +0100226
Michal Vaskofb89d772016-01-08 12:25:35 +0100227#endif /* ENABLE_SSH || ENABLE_TLS */
Michal Vasko9e036d52016-01-08 10:49:26 +0100228
Michal Vasko086311b2016-01-08 09:53:11 +0100229#ifdef ENABLE_SSH
230
Michal Vasko1a38c862016-01-15 15:50:07 +0100231/**
Michal Vasko96164bf2016-01-21 15:41:58 +0100232 * @brief Accept a new NETCONF session on an SSH session of a running NETCONF session
233 * that was polled in \p ps. Call this function only when nc_ps_poll() on \p ps returns 5.
234 *
235 * @param[in] ps Unmodified pollsession structure from the previous nc_ps_poll() call.
236 * @param[out] session New session.
237 * @return 1 on success, -1 on error.
238 */
239int nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session);
240
241/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100242 * @brief Add a new SSH endpoint and start listening on it.
243 *
244 * @param[in] name Arbitrary unique endpoint name. There can be a TLS endpoint with
245 * the same name.
246 * @param[in] address IP address to listen on.
247 * @param[in] port Port to listen on.
248 * @return 0 on success, -1 on error.
249 */
250int nc_server_ssh_add_endpt_listen(const char *name, const char *address, uint16_t port);
251
252/**
253 * @brief Stop listening on and remove an SSH endpoint.
254 *
255 * @param[in] name Endpoint name. NULL matches all (SSH) endpoints.
256 * @return 0 on success, -1 on not finding any match.
257 */
258int nc_server_ssh_del_endpt(const char *name);
259
260/**
261 * @brief Set endpoint SSH host keys the server will identify itself with. Each of RSA, DSA, and
Michal Vasko5c2f7952016-01-22 13:16:31 +0100262 * ECDSA keys can be set. If the particular type was already set, it is replaced.
Michal Vasko1a38c862016-01-15 15:50:07 +0100263 *
264 * @param[in] privkey_path Path to a private key.
265 * @return 0 on success, -1 on error.
266 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100267int nc_server_ssh_endpt_set_hostkey(const char *endpt_name, const char *privkey_path);
Michal Vasko086311b2016-01-08 09:53:11 +0100268
Michal Vasko1a38c862016-01-15 15:50:07 +0100269/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100270 * @brief Set endpoint SSH banner the server will send to every client.
Michal Vasko1a38c862016-01-15 15:50:07 +0100271 *
272 * @param[in] banner SSH banner.
273 * @return 0 on success, -1 on error.
274 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100275int nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner);
Michal Vasko086311b2016-01-08 09:53:11 +0100276
Michal Vasko1a38c862016-01-15 15:50:07 +0100277/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100278 * @brief Set endpoint accepted SSH authentication methods. All (publickey, password, interactive)
Michal Vasko1a38c862016-01-15 15:50:07 +0100279 * are supported by default.
280 *
281 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
282 * @return 0 on success, -1 on error.
283 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100284int nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods);
Michal Vasko086311b2016-01-08 09:53:11 +0100285
Michal Vasko1a38c862016-01-15 15:50:07 +0100286/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100287 * @brief Set endpoint SSH authentication attempts of every client. 3 by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100288 *
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100289 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
Michal Vasko1a38c862016-01-15 15:50:07 +0100290 * @return 0 on success, -1 on error.
291 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100292int nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100293
Michal Vasko1a38c862016-01-15 15:50:07 +0100294/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100295 * @brief Set endpoint SSH authentication timeout. 10 seconds by default.
Michal Vasko1a38c862016-01-15 15:50:07 +0100296 *
297 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
298 * @return 0 on success, -1 on error.
299 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100300int nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100301
Michal Vasko1a38c862016-01-15 15:50:07 +0100302/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100303 * @brief Add an endpoint authorized client SSH public key. This public key can be used for
Michal Vasko1a38c862016-01-15 15:50:07 +0100304 * publickey authentication afterwards.
305 *
306 * @param[in] pubkey_path Path to the public key.
307 * @param[in] username Username that the client with the public key must use.
308 * @return 0 on success, -1 on error.
309 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100310int nc_server_ssh_endpt_add_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
Michal Vasko086311b2016-01-08 09:53:11 +0100311
Michal Vasko1a38c862016-01-15 15:50:07 +0100312/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100313 * @brief Remove an endpoint authorized client SSH public key.
Michal Vasko1a38c862016-01-15 15:50:07 +0100314 *
315 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
316 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
317 * @return 0 on success, -1 on not finding any match.
318 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100319int nc_server_ssh_endpt_del_authkey(const char *endpt_name, const char *pubkey_path, const char *username);
320
321/**
322 * @brief Free all the various SSH server options (excluding Call Home).
323 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100324void nc_server_ssh_destroy_opts(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100325
Michal Vaskob05053d2016-01-22 16:12:06 +0100326/*
327 * Call Home
328 */
329
Michal Vasko1a38c862016-01-15 15:50:07 +0100330/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100331 * @brief Establish an SSH Call Home connection with a listening NETCONF client.
332 *
333 * @param[in] host Host the client is listening on.
334 * @param[in] port Port the client is listening on.
335 * @param[in] timeout Timeout for transport-related operations, 0 for non-blocking
336 * call, -1 for infinite waiting.
337 * @param[out] session New Call Home session.
338 * @return 1 on success, 0 on timeout, -1 on error.
339 */
340int nc_connect_callhome_ssh(const char *host, uint16_t port, int timeout, struct nc_session **session);
341
342/**
Michal Vaskob05053d2016-01-22 16:12:06 +0100343 * @brief Set Call Home SSH host keys the server will identify itself with. Each of RSA, DSA, and
344 * ECDSA keys can be set. If the particular type was already set, it is replaced.
345 *
346 * @param[in] privkey_path Path to a private key.
347 * @return 0 on success, -1 on error.
348 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100349int nc_server_ssh_ch_set_hostkey(const char *privkey_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100350
351/**
352 * @brief Set Call Home SSH banner the server will send to every client.
353 *
354 * @param[in] banner SSH banner.
355 * @return 0 on success, -1 on error.
356 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100357int nc_server_ssh_ch_set_banner(const char *banner);
Michal Vaskob05053d2016-01-22 16:12:06 +0100358
359/**
360 * @brief Set accepted Call Home SSH authentication methods. All (publickey, password, interactive)
361 * are supported by default.
362 *
363 * @param[in] auth_methods Accepted authentication methods bit field of NC_SSH_AUTH_TYPE.
364 * @return 0 on success, -1 on error.
365 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100366int nc_server_ssh_ch_set_auth_methods(int auth_methods);
Michal Vaskob05053d2016-01-22 16:12:06 +0100367
368/**
369 * @brief Set Call Home SSH authentication attempts of every client. 3 by default.
370 *
371 * @param[in] auth_attempts Failed authentication attempts before a client is dropped.
372 * @return 0 on success, -1 on error.
373 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100374int nc_server_ssh_ch_set_auth_attempts(uint16_t auth_attempts);
Michal Vaskob05053d2016-01-22 16:12:06 +0100375
376/**
377 * @brief Set Call Home SSH authentication timeout. 10 seconds by default.
378 *
379 * @param[in] auth_timeout Number of seconds before an unauthenticated client is dropped.
380 * @return 0 on success, -1 on error.
381 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100382int nc_server_ssh_ch_set_auth_timeout(uint16_t auth_timeout);
Michal Vaskob05053d2016-01-22 16:12:06 +0100383
384/**
385 * @brief Add an authorized Call Home client SSH public key. This public key can be used for
386 * publickey authentication afterwards.
387 *
388 * @param[in] pubkey_path Path to the public key.
389 * @param[in] username Username that the client with the public key must use.
390 * @return 0 on success, -1 on error.
391 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100392int nc_server_ssh_ch_add_authkey(const char *pubkey_path, const char *username);
Michal Vaskob05053d2016-01-22 16:12:06 +0100393
394/**
395 * @brief Remove an authorized Call Home client SSH public key.
396 *
397 * @param[in] pubkey_path Path to an authorized public key. NULL matches all the keys.
398 * @param[in] username Username for an authorized public key. NULL matches all the usernames.
399 * @return 0 on success, -1 on not finding any match.
400 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100401int nc_server_ssh_ch_del_authkey(const char *pubkey_path, const char *username);
Michal Vaskob05053d2016-01-22 16:12:06 +0100402
Michal Vasko3031aae2016-01-27 16:07:18 +0100403/* TODO */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100404void nc_server_ssh_ch_clear_opts(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100405
Michal Vasko086311b2016-01-08 09:53:11 +0100406#endif /* ENABLE_SSH */
407
408#ifdef ENABLE_TLS
409
Michal Vasko1a38c862016-01-15 15:50:07 +0100410/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100411 * @brief Add a new TLS endpoint and start listening on it.
412 *
413 * @param[in] name Arbitrary unique endpoint name. There can be an SSH endpoint with
414 * the same name.
415 * @param[in] address IP address to listen on.
416 * @param[in] port Port to listen on.
417 * @return 0 on success, -1 on error.
418 */
419int nc_server_tls_add_endpt_listen(const char *name, const char *address, uint16_t port);
420
421/**
422 * @brief Stop listening on and remove a TLS endpoint.
423 *
424 * @param[in] name Endpoint name. NULL matches all (TLS) endpoints.
425 * @return 0 on success, -1 on not finding any match.
426 */
427int nc_server_tls_del_endpt(const char *name);
428
429/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100430 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert_path().
431 * There can only be one certificate for each key type, it is replaced if already set.
432 *
433 * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding.
434 * @return 0 on success, -1 on error.
435 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100436int nc_server_tls_endpt_set_cert(const char *endpt_name, const char *cert);
Michal Vasko086311b2016-01-08 09:53:11 +0100437
Michal Vasko1a38c862016-01-15 15:50:07 +0100438/**
439 * @brief Set server TLS certificate. Alternative to nc_tls_server_set_cert().
440 * There can only be one certificate for each key type, it is replaced if already set.
441 *
442 * @param[in] cert_path Path to a certificate file in PEM format.
443 * @return 0 on success, -1 on error.
444 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100445int nc_server_tls_endpt_set_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100446
Michal Vasko1a38c862016-01-15 15:50:07 +0100447/**
448 * @brief Set server TLS private key matching the certificate.
449 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
450 * type, it is replaced if already set.
451 *
452 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
453 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
454 * @return 0 on success, -1 on error.
455 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100456int nc_server_tls_endpt_set_key(const char *endpt_name, const char *privkey, int is_rsa);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100457
Michal Vasko1a38c862016-01-15 15:50:07 +0100458/**
459 * @brief Set server TLS private key matching the certificate.
460 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
461 * type, it is replaced if already set.
462 *
463 * @param[in] privkey_path Path to a private key file in PEM format.
464 * @return 0 on success, -1 on error.
465 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100466int nc_server_tls_endpt_set_key_path(const char *endpt_name, const char *privkey_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100467
Michal Vasko1a38c862016-01-15 15:50:07 +0100468/**
469 * @brief Add a trusted certificate. Can be both a CA or a client one.
470 *
471 * @param[in] cert Base64-enocded certificate in ASN.1DER encoding.
472 * @return 0 on success, -1 on error.
473 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100474int nc_server_tls_endpt_add_trusted_cert(const char *endpt_name, const char *cert);
Michal Vasko0457bb42016-01-08 15:49:13 +0100475
Michal Vasko1a38c862016-01-15 15:50:07 +0100476/**
477 * @brief Add a trusted certificate. Can be both a CA or a client one.
478 *
479 * @param[in] cert_path Path to a trusted certificate file in PEM format.
480 * @return 0 on success, -1 on error.
481 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100482int nc_server_tls_endpt_add_trusted_cert_path(const char *endpt_name, const char *cert_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100483
Michal Vasko1a38c862016-01-15 15:50:07 +0100484/**
485 * @brief Set trusted Certificate Authority certificate locations. There can only be
486 * one file and one directory, they are replaced if already set.
487 *
488 * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format.
489 * Can be NULL.
490 * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory
491 * (c_rehash utility can be used to create hashes) with PEM files. Can be NULL.
492 * @return 0 on success, -1 on error.
493 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100494int nc_server_tls_endpt_set_trusted_cacert_locations(const char *endpt_name, const char *cacert_file_path, const char *cacert_dir_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100495
Michal Vasko1a38c862016-01-15 15:50:07 +0100496/**
497 * @brief Destroy and clean all the set certificates and private keys. CRLs and
498 * CTN entries are not affected.
499 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100500void nc_server_tls_endpt_clear_certs(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100501
Michal Vasko1a38c862016-01-15 15:50:07 +0100502/**
503 * @brief Set Certificate Revocation List locations. There can only be one file
504 * and one directory, they are replaced if already set.
505 *
506 * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL.
507 * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility
508 * can be used to create hashes) with PEM files. Can be NULL.
509 * @return 0 on success, -1 on error.
510 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100511int nc_server_tls_endpt_set_crl_locations(const char *endpt_name, const char *crl_file_path, const char *crl_dir_path);
Michal Vasko0457bb42016-01-08 15:49:13 +0100512
Michal Vasko1a38c862016-01-15 15:50:07 +0100513/**
514 * @brief Destroy and clean CRLs. Certificates, priavte keys, and CTN entries are
515 * not affected.
516 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100517void nc_server_tls_endpt_clear_crls(const char *endpt_name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100518
Michal Vasko1a38c862016-01-15 15:50:07 +0100519/**
520 * @brief Add a Cert-to-name entry.
521 *
522 * @param[in] id Priority of the entry.
523 * @param[in] fingerprint Matching certificate fingerprint.
524 * @param[in] map_type Type of username-certificate mapping.
525 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
526 * @return 0 on success, -1 on error.
527 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100528int nc_server_tls_endpt_add_ctn(const char *endpt_name, uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100529
Michal Vasko1a38c862016-01-15 15:50:07 +0100530/**
531 * @brief Remove a Cert-to-name entry.
532 *
533 * @param[in] id Priority of the entry. -1 matches all the priorities.
534 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
535 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
536 * @param[in] name Specific username for the entry. NULL matches all the usernames.
537 * @return 0 on success, -1 on not finding any match.
538 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100539int nc_server_tls_endpt_del_ctn(const char *endpt_name, int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vasko0457bb42016-01-08 15:49:13 +0100540
Michal Vaskob05053d2016-01-22 16:12:06 +0100541/*
542 * Call Home
543 */
544
Michal Vasko1a38c862016-01-15 15:50:07 +0100545/**
Michal Vasko3031aae2016-01-27 16:07:18 +0100546 * @brief Establish a TLS Call Home connection with a listening NETCONF client.
547 *
548 * @param[in] host Host the client is listening on.
549 * @param[in] port Port the client is listening on.
550 * @param[in] timeout Timeout for transport-related operations, 0 for non-blocking
551 * call, -1 for infinite waiting.
552 * @param[out] session New Call Home session.
553 * @return 1 on success, 0 on timeout, -1 on error.
554 */
555int nc_connect_callhome_tls(const char *host, uint16_t port, int timeout, struct nc_session **session);
556
557/**
Michal Vaskob05053d2016-01-22 16:12:06 +0100558 * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert_path().
559 * There can only be one certificate for each key type, it is replaced if already set.
560 *
561 * @param[in] cert Base64-encoded certificate in ASN.1 DER encoding.
562 * @return 0 on success, -1 on error.
563 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100564int nc_server_tls_ch_set_cert(const char *cert);
Michal Vaskob05053d2016-01-22 16:12:06 +0100565
566/**
567 * @brief Set server Call Home TLS certificate. Alternative to nc_tls_server_set_cert().
568 * There can only be one certificate for each key type, it is replaced if already set.
569 *
570 * @param[in] cert_path Path to a certificate file in PEM format.
571 * @return 0 on success, -1 on error.
572 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100573int nc_server_tls_ch_set_cert_path(const char *cert_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100574
575/**
576 * @brief Set server Call Home TLS private key matching the certificate.
577 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
578 * type, it is replaced if already set.
579 *
580 * @param[in] privkey Base64-encoded certificate in ASN.1 DER encoding.
581 * @param[in] is_rsa Whether \p privkey are the data of an RSA (1) or DSA (0) key.
582 * @return 0 on success, -1 on error.
583 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100584int nc_server_tls_ch_set_key(const char *privkey, int is_rsa);
Michal Vaskob05053d2016-01-22 16:12:06 +0100585
586/**
587 * @brief Set server Call Home TLS private key matching the certificate.
588 * Alternative to nc_tls_server_set_key_path(). There can only be one of every key
589 * type, it is replaced if already set.
590 *
591 * @param[in] privkey_path Path to a private key file in PEM format.
592 * @return 0 on success, -1 on error.
593 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100594int nc_server_tls_ch_set_key_path(const char *privkey_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100595
596/**
597 * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
598 *
599 * @param[in] cert Base64-enocded certificate in ASN.1DER encoding.
600 * @return 0 on success, -1 on error.
601 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100602int nc_server_tls_ch_add_trusted_cert(const char *cert);
Michal Vaskob05053d2016-01-22 16:12:06 +0100603
604/**
605 * @brief Add a Call Home trusted certificate. Can be both a CA or a client one.
606 *
607 * @param[in] cert_path Path to a trusted certificate file in PEM format.
608 * @return 0 on success, -1 on error.
609 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100610int nc_server_tls_ch_add_trusted_cert_path(const char *cert_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100611
612/**
613 * @brief Set trusted Call Home Certificate Authority certificate locations. There
614 * can only be one file and one directory, they are replaced if already set.
615 *
616 * @param[in] cacert_file_path Path to a trusted CA cert store file in PEM format.
617 * Can be NULL.
618 * @param[in] cacert_dir_path Path to a trusted CA cert store hashed directory
619 * (c_rehash utility can be used to create hashes) with PEM files. Can be NULL.
620 * @return 0 on success, -1 on error.
621 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100622int nc_server_tls_ch_set_trusted_cacert_locations(const char *cacert_file_path, const char *cacert_dir_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100623
624/**
625 * @brief Destroy and clean all the set Call Home certificates and private keys.
626 * CRLs and CTN entries are not affected.
627 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100628void nc_server_tls_ch_clear_certs(void);
Michal Vaskob05053d2016-01-22 16:12:06 +0100629
630/**
631 * @brief Set Call Home Certificate Revocation List locations. There can only be
632 * one file and one directory, they are replaced if already set.
633 *
634 * @param[in] crl_file_path Path to a CRL store file in PEM format. Can be NULL.
635 * @param[in] crl_dir_path Path to a CRL store hashed directory (c_rehash utility
636 * can be used to create hashes) with PEM files. Can be NULL.
637 * @return 0 on success, -1 on error.
638 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100639int nc_server_tls_ch_set_crl_locations(const char *crl_file_path, const char *crl_dir_path);
Michal Vaskob05053d2016-01-22 16:12:06 +0100640
641/**
642 * @brief Destroy and clean Call Home CRLs. Call Home certificates, private keys,
643 * and CTN entries are not affected.
644 */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100645void nc_server_tls_ch_clear_crls(void);
Michal Vaskob05053d2016-01-22 16:12:06 +0100646
647/**
648 * @brief Add a Call Home Cert-to-name entry.
649 *
650 * @param[in] id Priority of the entry.
651 * @param[in] fingerprint Matching certificate fingerprint.
652 * @param[in] map_type Type of username-certificate mapping.
653 * @param[in] name Specific username if \p map_type == NC_TLS_CTN_SPECIFED. Must be NULL otherwise.
654 * @return 0 on success, -1 on error.
655 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100656int nc_server_tls_ch_add_ctn(uint32_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskob05053d2016-01-22 16:12:06 +0100657
658/**
659 * @brief Remove a Call Home Cert-to-name entry.
660 *
661 * @param[in] id Priority of the entry. -1 matches all the priorities.
662 * @param[in] fingerprint Fingerprint fo the entry. NULL matches all the fingerprints.
663 * @param[in] map_type Mapping type of the entry. 0 matches all the mapping types.
664 * @param[in] name Specific username for the entry. NULL matches all the usernames.
665 * @return 0 on success, -1 on not finding any match.
666 */
Michal Vasko3031aae2016-01-27 16:07:18 +0100667int nc_server_tls_ch_del_ctn(int64_t id, const char *fingerprint, NC_TLS_CTN_MAPTYPE map_type, const char *name);
Michal Vaskob05053d2016-01-22 16:12:06 +0100668
Michal Vasko3031aae2016-01-27 16:07:18 +0100669/* TODO */
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100670void nc_server_tls_ch_clear_opts(void);
Michal Vasko086311b2016-01-08 09:53:11 +0100671
672#endif /* ENABLE_TLS */
673
674#endif /* NC_SESSION_SERVER_H_ */