blob: 62766cafa042d7a6612dd3457a4fe037de12ce7c [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
Michal Vasko30aa3822015-11-25 10:41:54 +01002 * \file netopeerguid.c
3 * \brief NetopeerGUI daemon
Radek Krejci469aab82012-07-22 18:42:20 +02004 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoa53ef882015-11-24 11:02:01 +01006 * \author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci469aab82012-07-22 18:42:20 +02007 * \date 2011
8 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01009 * \date 2013
Michal Vaskoa53ef882015-11-24 11:02:01 +010010 * \date 2015
Radek Krejci469aab82012-07-22 18:42:20 +020011 */
12/*
Michal Vaskoa53ef882015-11-24 11:02:01 +010013 * Copyright (C) 2011-2015 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020014 *
15 * LICENSE TERMS
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in
24 * the documentation and/or other materials provided with the
25 * distribution.
26 * 3. Neither the name of the Company nor the names of its contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * ALTERNATIVELY, provided that this notice is retained in full, this
31 * product may be distributed under the terms of the GNU General Public
32 * License (GPL) version 2 or later, in which case the provisions
33 * of the GPL apply INSTEAD OF those given above.
34 *
35 * This software is provided ``as is'', and any express or implied
36 * warranties, including, but not limited to, the implied warranties of
37 * merchantability and fitness for a particular purpose are disclaimed.
38 * In no event shall the company or contributors be liable for any
39 * direct, indirect, incidental, special, exemplary, or consequential
40 * damages (including, but not limited to, procurement of substitute
41 * goods or services; loss of use, data, or profits; or business
42 * interruption) however caused and on any theory of liability, whether
43 * in contract, strict liability, or tort (including negligence or
44 * otherwise) arising in any way out of the use of this software, even
45 * if advised of the possibility of such damage.
46 *
47 */
Michal Vaskoc3146782015-11-04 14:46:41 +010048#define _GNU_SOURCE
Radek Krejci469aab82012-07-22 18:42:20 +020049
Radek Krejci7b4ddd02012-07-30 08:09:58 +020050#include <unistd.h>
51#include <poll.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010052#include <time.h>
Michal Vaskob69dde22016-03-09 11:42:20 +010053#include <fcntl.h>
Radek Krejci469aab82012-07-22 18:42:20 +020054#include <sys/types.h>
55#include <sys/socket.h>
56#include <sys/un.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010057#include <sys/stat.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010058#include <pwd.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010059#include <errno.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010060#include <limits.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010061#include <grp.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010062#include <signal.h>
David Kupka8e60a372012-09-04 09:15:20 +020063#include <pthread.h>
64#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020065
Michal Vaskof35ea502016-02-24 10:44:54 +010066#include <nc_client.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020067
Tomas Cejka04e08f42014-03-27 19:52:34 +010068#include "../config.h"
69
Tomas Cejkad340dbf2013-03-24 20:36:57 +010070#ifdef WITH_NOTIFICATIONS
Michal Vaskoa53ef882015-11-24 11:02:01 +010071#include "notification_server.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010072#endif
73
Tomas Cejka94da2c52013-01-08 18:20:30 +010074#include "message_type.h"
Michal Vaskoa53ef882015-11-24 11:02:01 +010075#include "netopeerguid.h"
Radek Krejci469aab82012-07-22 18:42:20 +020076
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010077#define SCHEMA_DIR "/tmp/yang_models"
Radek Krejci469aab82012-07-22 18:42:20 +020078#define MAX_PROCS 5
Michal Vaskoa53ef882015-11-24 11:02:01 +010079#define SOCKET_FILENAME "/var/run/netopeerguid.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020080#define MAX_SOCKET_CL 10
81#define BUFFER_SIZE 4096
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010082#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
83#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020084
Michal Vasko7732dee2015-11-05 10:22:15 +010085/* sleep in master process for non-blocking socket reading, in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020086#define SLEEP_TIME 200
87
88#ifndef offsetof
89#define offsetof(type, member) ((size_t) ((type *) 0)->member)
90#endif
91
Tomas Cejka027f3bc2012-11-10 20:28:36 +010092/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020093struct timeval timeout = { 1, 0 };
94
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010095#define NCWITHDEFAULTS NCWD_MODE_NOTSET
Tomas Cejka5064c232013-01-17 09:30:58 +010096
Radek Krejci469aab82012-07-22 18:42:20 +020097#define MSG_OK 0
98#define MSG_OPEN 1
99#define MSG_DATA 2
100#define MSG_CLOSE 3
101#define MSG_ERROR 4
102#define MSG_UNKNOWN 5
103
Tomas Cejka47387fd2013-06-10 20:37:46 +0200104pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200105pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100106pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100107pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
108
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100109unsigned int session_key_generator = 1;
Michal Vaskoc3146782015-11-04 14:46:41 +0100110struct session_with_mutex *netconf_sessions_list = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100111static const char *sockname;
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200112static pthread_key_t notif_history_key;
Tomas Cejka442258e2014-04-01 18:17:18 +0200113pthread_key_t err_reply_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200114volatile int isterminated = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200115static char* password;
116
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100117json_object *create_ok_reply(void);
118json_object *create_data_reply(const char *data);
119static char *netconf_getschema(unsigned int session_key, const char *identifier, const char *version,
120 const char *format, json_object **err);
Michal Vaskof35ea502016-02-24 10:44:54 +0100121static void node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module,
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100122 json_object *data_json_parent);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100123static void node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100124
125static void
126signal_handler(int sign)
Radek Krejci469aab82012-07-22 18:42:20 +0200127{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100128 switch (sign) {
129 case SIGINT:
130 case SIGTERM:
131 isterminated = 1;
132 break;
133 }
Radek Krejci469aab82012-07-22 18:42:20 +0200134}
135
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100136int
137netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session))
Radek Krejci469aab82012-07-22 18:42:20 +0200138{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100139 /* always approve */
140 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200141}
142
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100143char *
Michal Vaskof35ea502016-02-24 10:44:54 +0100144netconf_callback_sshauth_passphrase(const char *UNUSED(priv_key_file))
Radek Krejci469aab82012-07-22 18:42:20 +0200145{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100146 char *buf;
147 buf = strdup(password);
148 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200149}
150
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100151char *
152netconf_callback_sshauth_password(const char *UNUSED(username), const char *UNUSED(hostname))
Radek Krejci469aab82012-07-22 18:42:20 +0200153{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100154 char *buf;
155 buf = strdup(password);
156 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200157}
158
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100159char *
160netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction),
161 const char *UNUSED(prompt), int UNUSED(echo))
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200162{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100163 char *buf;
164 buf = strdup(password);
165 return (buf);
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200166}
167
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100168void
169netconf_callback_error_process(const char *UNUSED(tag),
170 const char *UNUSED(type),
171 const char *UNUSED(severity),
172 const char *UNUSED(apptag),
173 const char *UNUSED(path),
174 const char *message,
175 const char *UNUSED(attribute),
176 const char *UNUSED(element),
177 const char *UNUSED(ns),
178 const char *UNUSED(sid))
Radek Krejcic11fd862012-07-26 12:41:21 +0200179{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100180 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
181 if (err_reply_p == NULL) {
182 ERROR("Error message was not allocated. %s", __func__);
183 return;
184 }
185 json_object *err_reply = *err_reply_p;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100186
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100187 json_object *array = NULL;
188 if (err_reply == NULL) {
189 ERROR("error calback: empty error list");
190 pthread_mutex_lock(&json_lock);
191 err_reply = json_object_new_object();
192 array = json_object_new_array();
193 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
194 json_object_object_add(err_reply, "errors", array);
195 if (message != NULL) {
196 json_object_array_add(array, json_object_new_string(message));
197 }
198 pthread_mutex_unlock(&json_lock);
199 (*err_reply_p) = err_reply;
200 } else {
201 ERROR("error calback: nonempty error list");
202 pthread_mutex_lock(&json_lock);
203 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
204 if (message != NULL) {
205 json_object_array_add(array, json_object_new_string(message));
206 }
207 }
208 pthread_mutex_unlock(&json_lock);
209 }
210 pthread_setspecific(err_reply_key, err_reply_p);
211 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200212}
213
Tomas Cejka47387fd2013-06-10 20:37:46 +0200214/**
215 * should be used in locked area
216 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100217void
218prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200219{
Michal Vaskof35ea502016-02-24 10:44:54 +0100220 int i;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100221 json_object *json_obj = NULL;
222 json_object *js_tmp = NULL;
223 char *old_sid = NULL;
224 const char *j_old_sid = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +0100225 char str_port[6];
226 const char **cpblts;
227 struct lyd_node *yanglib, *module, *node;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200228
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100229 if (s == NULL) {
230 ERROR("No session given.");
231 return;
232 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200233
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100234 pthread_mutex_lock(&json_lock);
235 if (s->hello_message != NULL) {
236 ERROR("clean previous hello message");
237 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
238 j_old_sid = json_object_get_string(js_tmp);
239 if (j_old_sid != NULL) {
240 old_sid = strdup(j_old_sid);
241 }
242 }
243 json_object_put(s->hello_message);
244 s->hello_message = NULL;
245 }
246 s->hello_message = json_object_new_object();
247 if (session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100248 if (!old_sid) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100249 /* we don't have old sid */
Michal Vaskof35ea502016-02-24 10:44:54 +0100250 asprintf(&old_sid, "%u", nc_session_get_id(session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100251 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100252 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
253 free(old_sid);
254 old_sid = NULL;
255
256 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) ? "1.1":"1.0")));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100257 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
Michal Vaskof35ea502016-02-24 10:44:54 +0100258 sprintf(str_port, "%u", nc_session_get_port(session));
259 json_object_object_add(s->hello_message, "port", json_object_new_string(str_port));
260 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_username(session)));
261 cpblts = nc_session_get_cpblts(session);
262 if (cpblts) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100263 json_obj = json_object_new_array();
Michal Vaskof35ea502016-02-24 10:44:54 +0100264 for (i = 0; cpblts[i]; ++i) {
265 json_object_array_add(json_obj, json_object_new_string(cpblts[i]));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100266 }
267 json_object_object_add(s->hello_message, "capabilities", json_obj);
268 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100269
270 yanglib = ly_ctx_info(nc_session_get_ctx(session));
271 if (yanglib) {
272 json_obj = json_object_new_array();
273 LY_TREE_FOR(yanglib->child, module) {
274 if (!strcmp(module->schema->name, "module")) {
275 LY_TREE_FOR(module->child, node) {
276 if (!strcmp(node->schema->name, "name")) {
277 json_object_array_add(json_obj, json_object_new_string(((struct lyd_node_leaf_list *)node)->value_str));
278 break;
279 }
280 }
281 }
282 }
283 json_object_object_add(s->hello_message, "models", json_obj);
284
Michal Vaskoc04900a2016-03-15 16:20:24 +0100285 lyd_free(yanglib);
Michal Vaskof35ea502016-02-24 10:44:54 +0100286 }
287
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100288 DEBUG("%s", json_object_to_json_string(s->hello_message));
289 } else {
290 ERROR("Session was not given.");
291 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
292 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
293 }
294 DEBUG("Status info from hello message prepared");
295 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200296}
297
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100298void
299create_err_reply_p()
Tomas Cejka442258e2014-04-01 18:17:18 +0200300{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100301 json_object **err_reply = calloc(1, sizeof(json_object **));
302 if (err_reply == NULL) {
303 ERROR("Allocation of err_reply storage failed!");
304 return;
305 }
306 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
307 ERROR("cannot set thread-specific value.");
308 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200309}
310
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100311void
312clean_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200313{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100314 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
315 if (err_reply != NULL) {
316 if (*err_reply != NULL) {
317 pthread_mutex_lock(&json_lock);
318 json_object_put(*err_reply);
319 pthread_mutex_unlock(&json_lock);
320 }
321 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
322 ERROR("Cannot set thread-specific hash value.");
323 }
324 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200325}
326
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100327void
328free_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200329{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100330 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
331 if (err_reply != NULL) {
332 if (*err_reply != NULL) {
333 pthread_mutex_lock(&json_lock);
334 json_object_put(*err_reply);
335 pthread_mutex_unlock(&json_lock);
336 }
337 free(err_reply);
338 err_reply = NULL;
339 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
340 ERROR("Cannot set thread-specific hash value.");
341 }
342 }
343}
344
345static struct session_with_mutex *
346session_get_locked(unsigned int session_key, json_object **err)
347{
348 struct session_with_mutex *locked_session;
349
350 /* get non-exclusive (read) access to sessions_list (conns) */
351 DEBUG("LOCK wrlock %s", __func__);
352 if (pthread_rwlock_rdlock(&session_lock) != 0) {
353 if (*err) {
354 *err = create_error_reply("Locking failed.");
355 }
356 return NULL;
357 }
358 /* get session where send the RPC */
359 for (locked_session = netconf_sessions_list;
360 locked_session && (locked_session->session_key != session_key);
361 locked_session = locked_session->next);
362 if (!locked_session) {
363 if (*err) {
364 *err = create_error_reply("Session not found.");
365 }
366 return NULL;
367 }
368
369 /* get exclusive access to session */
370 DEBUG("LOCK mutex %s", __func__);
371 if (pthread_mutex_lock(&locked_session->lock) != 0) {
372 if (*err) {
373 *err = create_error_reply("Locking failed.");
374 }
375 goto wrlock_fail;
376 }
377 return locked_session;
378
379wrlock_fail:
380 DEBUG("UNLOCK wrlock %s", __func__);
381 pthread_rwlock_unlock(&session_lock);
382 return NULL;
383}
384
385static void
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100386session_user_activity(const char *username)
387{
388 struct session_with_mutex *sess;
389
390 for (sess = netconf_sessions_list; sess; sess = sess->next) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100391 if (!strcmp(nc_session_get_username(sess->session), username)) {
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100392 sess->last_activity = time(NULL);
393 }
394 }
395}
396
397static void
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100398session_unlock(struct session_with_mutex *locked_session)
399{
400 DEBUG("UNLOCK mutex %s", __func__);
401 pthread_mutex_unlock(&locked_session->lock);
402 DEBUG("UNLOCK wrlock %s", __func__);
403 pthread_rwlock_unlock(&session_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +0200404}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200405
Michal Vasko3fda9a92015-11-23 10:10:57 +0100406static void
407node_metadata_text(const char *text, const char *name, json_object *parent)
408{
409 json_object *obj;
410
411 if (!text) {
412 return;
413 }
414
415 obj = json_object_new_string(text);
416 json_object_object_add(parent, name, obj);
417}
418
419static void
420node_metadata_restr(struct lys_restr *restr, const char *name, json_object *parent)
421{
422 json_object *obj;
423
424 if (!restr) {
425 return;
426 }
427
428 obj = json_object_new_string(restr->expr);
429 json_object_object_add(parent, name, obj);
430}
431
432static void
433node_metadata_must(uint8_t must_size, struct lys_restr *must, json_object *parent)
434{
435 uint8_t i;
436 json_object *array, *obj;
437
438 if (!must_size || !must) {
439 return;
440 }
441
442 array = json_object_new_array();
443
444 for (i = 0; i < must_size; ++i) {
445 obj = json_object_new_string(must[i].expr);
446 json_object_array_add(array, obj);
447 }
448
449 json_object_object_add(parent, "must", array);
450}
451
452static void
453node_metadata_basic(struct lys_node *node, json_object *parent)
454{
455 json_object *obj;
456
457 /* description */
458 node_metadata_text(node->dsc, "description", parent);
459
460 /* reference */
461 node_metadata_text(node->ref, "reference", parent);
462
463 /* config */
464 if (node->flags & LYS_CONFIG_R) {
465 obj = json_object_new_boolean(0);
466 } else {
467 obj = json_object_new_boolean(1);
468 }
469 json_object_object_add(parent, "config", obj);
470
471 /* status */
472 if (node->flags & LYS_STATUS_DEPRC) {
473 obj = json_object_new_string("deprecated");
474 } else if (node->flags & LYS_STATUS_OBSLT) {
475 obj = json_object_new_string("obsolete");
476 } else {
477 obj = json_object_new_string("current");
478 }
479 json_object_object_add(parent, "status", obj);
480
481 /* mandatory */
482 if (node->flags & LYS_MAND_TRUE) {
483 obj = json_object_new_boolean(1);
484 } else {
485 obj = json_object_new_boolean(0);
486 }
487 json_object_object_add(parent, "mandatory", obj);
488
489 /* NACM extensions */
490 if (node->nacm) {
491 if (node->nacm & LYS_NACM_DENYW) {
492 obj = json_object_new_string("default-deny-write");
493 } else {
494 obj = json_object_new_string("default-deny-all");
495 }
496 json_object_object_add(parent, "ext", obj);
497 }
498}
499
500static void
501node_metadata_when(struct lys_when *when, json_object *parent)
502{
503 json_object *obj;
504
505 if (!when) {
506 return;
507 }
508
509 obj = json_object_new_string(when->cond);
510 json_object_object_add(parent, "when", obj);
511}
512
513static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100514node_metadata_children_recursive(struct lys_node *node, json_object **child_array, json_object **choice_array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100515{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100516 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100517 struct lys_node *child;
518
519 if (!node->child) {
520 return;
521 }
522
523 LY_TREE_FOR(node->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100524 if (child->nodetype == LYS_USES) {
525 node_metadata_children_recursive(child, child_array, choice_array);
526 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100527 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100528 if (!*child_array) {
529 *child_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100530 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100531 json_object_array_add(*child_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100532 } else if (child->nodetype == LYS_CHOICE) {
533 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100534 if (!*choice_array) {
535 *choice_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100536 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100537 json_object_array_add(*choice_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100538 }
539 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100540}
541
542static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100543node_metadata_cases_recursive(struct lys_node_choice *choice, json_object *array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100544{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100545 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100546 struct lys_node *child;
547
548 if (!choice->child) {
549 return;
550 }
551
Michal Vasko3fda9a92015-11-23 10:10:57 +0100552 LY_TREE_FOR(choice->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100553 if (child->nodetype == LYS_USES) {
554 node_metadata_cases_recursive((struct lys_node_choice *)child, array);
555 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100556 obj = json_object_new_string(child->name);
557 json_object_array_add(array, obj);
558 }
559 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100560}
561
562static void
563node_metadata_min_max(uint32_t min, uint32_t max, json_object *parent)
564{
565 json_object *obj;
566
567 if (min) {
568 obj = json_object_new_int(min);
569 json_object_object_add(parent, "min-elements", obj);
570 }
571
572 if (max) {
573 obj = json_object_new_int(max);
574 json_object_object_add(parent, "max-elements", obj);
575 }
576}
577
578static void
579node_metadata_ident_recursive(struct lys_ident *ident, json_object *array)
580{
581 struct lys_ident_der *cur;
582 json_object *obj;
583
584 if (!ident) {
585 return;
586 }
587
588 obj = json_object_new_string(ident->name);
589 json_object_array_add(array, obj);
590
591 for (cur = ident->der; cur; cur = cur->next) {
592 node_metadata_ident_recursive(cur->ident, array);
593 }
594}
595
596static void
597node_metadata_type(struct lys_type *type, struct lys_module *module, json_object *parent)
598{
599 json_object *obj, *array, *item;
600 char *str;
601 int i;
602
603 /* built-in YANG type */
604 if (!type->der->module) {
605 switch (type->base) {
606 case LY_TYPE_BINARY:
607 node_metadata_text("binary", "type", parent);
608 node_metadata_restr(type->info.binary.length, "length", parent);
609 break;
610 case LY_TYPE_BITS:
611 node_metadata_text("bits", "type", parent);
612
613 array = json_object_new_array();
614 for (i = 0; i < type->info.bits.count; ++i) {
615 item = json_object_new_object();
616 obj = json_object_new_string(type->info.bits.bit[i].name);
617 json_object_object_add(item, "name", obj);
618 obj = json_object_new_int(type->info.bits.bit[i].pos);
619 json_object_object_add(item, "position", obj);
620 json_object_array_add(array, item);
621 }
622 json_object_object_add(parent, "bits", array);
623 break;
624 case LY_TYPE_BOOL:
625 node_metadata_text("bool", "type", parent);
626 break;
627 case LY_TYPE_DEC64:
628 node_metadata_text("decimal64", "type", parent);
629 node_metadata_restr(type->info.dec64.range, "range", parent);
630 obj = json_object_new_int(type->info.dec64.dig);
631 json_object_object_add(parent, "fraction-digits", obj);
632 break;
633 case LY_TYPE_EMPTY:
634 node_metadata_text("empty", "type", parent);
635 break;
636 case LY_TYPE_ENUM:
637 node_metadata_text("enumeration", "type", parent);
638
639 array = json_object_new_array();
640 for (i = 0; i < type->info.enums.count; ++i) {
641 obj = json_object_new_string(type->info.enums.enm[i].name);
642 json_object_array_add(array, obj);
643 }
644 json_object_object_add(parent, "enumval", array);
645 break;
646 case LY_TYPE_IDENT:
647 node_metadata_text("identityref", "type", parent);
648
649 array = json_object_new_array();
650 node_metadata_ident_recursive(type->info.ident.ref, array);
651 json_object_object_add(parent, "identityval", array);
652 break;
653 case LY_TYPE_INST:
654 node_metadata_text("instance-identifier", "type", parent);
655 if (type->info.inst.req == -1) {
656 obj = json_object_new_boolean(0);
657 } else {
658 obj = json_object_new_boolean(1);
659 }
660 json_object_object_add(parent, "require-instance", obj);
661 break;
662 case LY_TYPE_LEAFREF:
663 node_metadata_text("leafref", "type", parent);
664 node_metadata_text(type->info.lref.path, "path", parent);
665 break;
666 case LY_TYPE_STRING:
667 node_metadata_text("string", "type", parent);
668 node_metadata_restr(type->info.str.length, "length", parent);
669 if (type->info.str.pat_count) {
670 array = json_object_new_array();
671 for (i = 0; i < type->info.str.pat_count; ++i) {
672 obj = json_object_new_string(type->info.str.patterns[i].expr);
673 json_object_array_add(array, obj);
674 }
675 json_object_object_add(parent, "pattern", array);
676 }
677 break;
678 case LY_TYPE_UNION:
679 node_metadata_text("union", "type", parent);
680 array = json_object_new_array();
681 for (i = 0; i < type->info.uni.count; ++i) {
682 obj = json_object_new_object();
683 node_metadata_type(&type->info.uni.types[i], module, obj);
684 json_object_array_add(array, obj);
685 }
686 json_object_object_add(parent, "types", array);
687 break;
688 case LY_TYPE_INT8:
689 node_metadata_text("int8", "type", parent);
690 node_metadata_restr(type->info.num.range, "range", parent);
691 break;
692 case LY_TYPE_UINT8:
693 node_metadata_text("uint8", "type", parent);
694 node_metadata_restr(type->info.num.range, "range", parent);
695 break;
696 case LY_TYPE_INT16:
697 node_metadata_text("int16", "type", parent);
698 node_metadata_restr(type->info.num.range, "range", parent);
699 break;
700 case LY_TYPE_UINT16:
701 node_metadata_text("uint16", "type", parent);
702 node_metadata_restr(type->info.num.range, "range", parent);
703 break;
704 case LY_TYPE_INT32:
705 node_metadata_text("int32", "type", parent);
706 node_metadata_restr(type->info.num.range, "range", parent);
707 break;
708 case LY_TYPE_UINT32:
709 node_metadata_text("uint32", "type", parent);
710 node_metadata_restr(type->info.num.range, "range", parent);
711 break;
712 case LY_TYPE_INT64:
713 node_metadata_text("int64", "type", parent);
714 node_metadata_restr(type->info.num.range, "range", parent);
715 break;
716 case LY_TYPE_UINT64:
717 node_metadata_text("uint64", "type", parent);
718 node_metadata_restr(type->info.num.range, "range", parent);
719 break;
720 default:
721 ERROR("Internal: unknown type (%s:%d)", __FILE__, __LINE__);
722 break;
723 }
724
725 /* typedef */
726 } else {
727 if (!module || !type->module_name || !strcmp(type->module_name, module->name)) {
728 node_metadata_text(type->der->name, "type", parent);
729 } else {
730 asprintf(&str, "%s:%s", type->module_name, type->der->name);
731 node_metadata_text(str, "type", parent);
732 free(str);
733 }
734 obj = json_object_new_object();
735 node_metadata_typedef(type->der, obj);
736 json_object_object_add(parent, "typedef", obj);
737 }
738}
739
740static void
741node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent)
742{
743 json_object *obj;
744
745 /* description */
746 node_metadata_text(tpdf->dsc, "description", parent);
747
748 /* reference */
749 node_metadata_text(tpdf->ref, "reference", parent);
750
751 /* status */
752 if (tpdf->flags & LYS_STATUS_DEPRC) {
753 obj = json_object_new_string("deprecated");
754 } else if (tpdf->flags & LYS_STATUS_OBSLT) {
755 obj = json_object_new_string("obsolete");
756 } else {
757 obj = json_object_new_string("current");
758 }
759 json_object_object_add(parent, "status", obj);
760
761 /* type */
762 node_metadata_type(&tpdf->type, tpdf->module, parent);
763
764 /* units */
765 node_metadata_text(tpdf->units, "units", parent);
766
767 /* default */
768 node_metadata_text(tpdf->dflt, "default", parent);
769}
770
771static void
772node_metadata_container(struct lys_node_container *cont, json_object *parent)
773{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100774 json_object *obj, *child_array = NULL, *choice_array = NULL;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100775
776 /* element type */
777 obj = json_object_new_string("container");
778 json_object_object_add(parent, "eltype", obj);
779
780 /* shared info */
781 node_metadata_basic((struct lys_node *)cont, parent);
782
783 /* must */
784 node_metadata_must(cont->must_size, cont->must, parent);
785
786 /* presence */
787 node_metadata_text(cont->presence, "presence", parent);
788
789 /* when */
790 node_metadata_when(cont->when, parent);
791
792 /* children & choice */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100793 node_metadata_children_recursive((struct lys_node *)cont, &child_array, &choice_array);
794 if (child_array) {
795 json_object_object_add(parent, "children", child_array);
796 }
797 if (choice_array) {
798 json_object_object_add(parent, "choice", choice_array);
799 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100800}
801
802static void
803node_metadata_choice(struct lys_node_choice *choice, json_object *parent)
804{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100805 json_object *obj, *array;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100806
807 /* element type */
808 obj = json_object_new_string("choice");
809 json_object_object_add(parent, "eltype", obj);
810
811 /* shared info */
812 node_metadata_basic((struct lys_node *)choice, parent);
813
814 /* default */
Michal Vasko5e1c6052016-03-17 15:43:33 +0100815 if (choice->dflt) {
816 node_metadata_text(choice->dflt->name, "default", parent);
817 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100818
819 /* when */
820 node_metadata_when(choice->when, parent);
821
822 /* cases */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100823 if (choice->child) {
824 array = json_object_new_array();
825 node_metadata_cases_recursive(choice, array);
826 json_object_object_add(parent, "cases", array);
827 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100828}
829
830static void
831node_metadata_leaf(struct lys_node_leaf *leaf, json_object *parent)
832{
833 json_object *obj;
834 struct lys_node_list *list;
835 int is_key, i;
836
837 /* element type */
838 obj = json_object_new_string("leaf");
839 json_object_object_add(parent, "eltype", obj);
840
841 /* shared info */
842 node_metadata_basic((struct lys_node *)leaf, parent);
843
844 /* type */
845 node_metadata_type(&leaf->type, leaf->module, parent);
846
847 /* units */
848 node_metadata_text(leaf->units, "units", parent);
849
850 /* default */
851 node_metadata_text(leaf->dflt, "default", parent);
852
853 /* must */
854 node_metadata_must(leaf->must_size, leaf->must, parent);
855
856 /* when */
857 node_metadata_when(leaf->when, parent);
858
859 /* iskey */
860 is_key = 0;
861 list = (struct lys_node_list *)lys_parent((struct lys_node *)leaf);
862 if (list && (list->nodetype == LYS_LIST)) {
863 for (i = 0; i < list->keys_size; ++i) {
864 if (list->keys[i] == leaf) {
865 is_key = 1;
866 break;
867 }
868 }
869 }
870 obj = json_object_new_boolean(is_key);
871 json_object_object_add(parent, "iskey", obj);
872}
873
874static void
875node_metadata_leaflist(struct lys_node_leaflist *llist, json_object *parent)
876{
877 json_object *obj;
878
879 /* element type */
880 obj = json_object_new_string("leaf-list");
881 json_object_object_add(parent, "eltype", obj);
882
883 /* shared info */
884 node_metadata_basic((struct lys_node *)llist, parent);
885
886 /* type */
887 node_metadata_type(&llist->type, llist->module, parent);
888
889 /* units */
890 node_metadata_text(llist->units, "units", parent);
891
892 /* must */
893 node_metadata_must(llist->must_size, llist->must, parent);
894
895 /* when */
896 node_metadata_when(llist->when, parent);
897
898 /* min/max-elements */
899 node_metadata_min_max(llist->min, llist->max, parent);
900}
901
902static void
903node_metadata_list(struct lys_node_list *list, json_object *parent)
904{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100905 json_object *obj, *array, *child_array = NULL, *choice_array = NULL;;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100906 int i;
907 unsigned int j;
908
909 /* element type */
910 obj = json_object_new_string("list");
911 json_object_object_add(parent, "eltype", obj);
912
913 /* shared info */
914 node_metadata_basic((struct lys_node *)list, parent);
915
916 /* must */
917 node_metadata_must(list->must_size, list->must, parent);
918
919 /* when */
920 node_metadata_when(list->when, parent);
921
922 /* min/max-elements */
923 node_metadata_min_max(list->min, list->max, parent);
924
925 /* keys */
926 if (list->keys_size) {
927 array = json_object_new_array();
928 for (i = 0; i < list->keys_size; ++i) {
929 obj = json_object_new_string(list->keys[i]->name);
930 json_object_array_add(array, obj);
931 }
932 json_object_object_add(parent, "keys", array);
933 }
934
935 /* unique */
936 if (list->unique_size) {
937 array = json_object_new_array();
938 for (i = 0; i < list->unique_size; ++i) {
939 for (j = 0; j < list->unique[i].expr_size; ++j) {
940 obj = json_object_new_string(list->unique[i].expr[j]);
941 json_object_array_add(array, obj);
942 }
943 }
944 json_object_object_add(parent, "unique", array);
945 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100946
947 /* children & choice */
948 node_metadata_children_recursive((struct lys_node *)list, &child_array, &choice_array);
949 if (child_array) {
950 json_object_object_add(parent, "children", child_array);
951 }
952 if (choice_array) {
953 json_object_object_add(parent, "choice", choice_array);
954 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100955}
956
957static void
958node_metadata_anyxml(struct lys_node_anyxml *anyxml, json_object *parent)
959{
960 json_object *obj;
961
962 /* element type */
963 obj = json_object_new_string("anyxml");
964 json_object_object_add(parent, "eltype", obj);
965
966 /* shared info */
967 node_metadata_basic((struct lys_node *)anyxml, parent);
968
969 /* must */
970 node_metadata_must(anyxml->must_size, anyxml->must, parent);
971
972 /* when */
973 node_metadata_when(anyxml->when, parent);
974
975}
976
977static void
978node_metadata_case(struct lys_node_case *cas, json_object *parent)
979{
980 json_object *obj;
981
982 /* element type */
983 obj = json_object_new_string("case");
984 json_object_object_add(parent, "eltype", obj);
985
986 /* shared info */
987 node_metadata_basic((struct lys_node *)cas, parent);
988
989 /* when */
990 node_metadata_when(cas->when, parent);
991}
992
Michal Vaskoa45770b2015-11-23 15:49:41 +0100993static void
994node_metadata_rpc(struct lys_node_rpc *rpc, json_object *parent)
995{
996 json_object *obj;
997
998 /* element type */
999 obj = json_object_new_string("rpc");
1000 json_object_object_add(parent, "eltype", obj);
1001
1002 /* description */
1003 node_metadata_text(rpc->dsc, "description", parent);
1004
1005 /* reference */
1006 node_metadata_text(rpc->ref, "reference", parent);
1007
1008 /* status */
1009 if (rpc->flags & LYS_STATUS_DEPRC) {
1010 obj = json_object_new_string("deprecated");
1011 } else if (rpc->flags & LYS_STATUS_OBSLT) {
1012 obj = json_object_new_string("obsolete");
1013 } else {
1014 obj = json_object_new_string("current");
1015 }
1016 json_object_object_add(parent, "status", obj);
1017}
1018
1019static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001020node_metadata_model(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001021{
1022 json_object *obj, *array, *item;
1023 int i;
1024
1025 /* yang-version */
1026 if (module->version == 2) {
1027 obj = json_object_new_string("1.1");
1028 } else {
1029 obj = json_object_new_string("1.0");
1030 }
1031 json_object_object_add(parent, "yang-version", obj);
1032
1033 /* namespace */
1034 node_metadata_text(module->ns, "namespace", parent);
1035
1036 /* prefix */
1037 node_metadata_text(module->prefix, "prefix", parent);
1038
1039 /* contact */
1040 node_metadata_text(module->contact, "contact", parent);
1041
1042 /* organization */
1043 node_metadata_text(module->org, "organization", parent);
1044
1045 /* revision */
1046 if (module->rev_size) {
1047 node_metadata_text(module->rev[0].date, "revision", parent);
1048 }
1049
1050 /* description */
1051 node_metadata_text(module->dsc, "description", parent);
1052
1053 /* import */
1054 if (module->imp_size) {
1055 array = json_object_new_array();
1056 for (i = 0; i < module->imp_size; ++i) {
1057 item = json_object_new_object();
1058
1059 node_metadata_text(module->imp[i].module->name, "name", item);
1060 node_metadata_text(module->imp[i].prefix, "prefix", item);
1061 if (module->imp[i].rev && module->imp[i].rev[0]) {
1062 node_metadata_text(module->imp[i].rev, "revision", item);
1063 }
1064
1065 json_object_array_add(array, item);
1066 }
1067 json_object_object_add(parent, "imports", array);
1068 }
1069
1070 /* include */
1071 if (module->inc_size) {
1072 array = json_object_new_array();
1073 for (i = 0; i < module->inc_size; ++i) {
1074 item = json_object_new_object();
1075
1076 node_metadata_text(module->inc[i].submodule->name, "name", item);
1077 if (module->inc[i].rev && module->inc[i].rev[0]) {
1078 node_metadata_text(module->inc[i].rev, "revision", item);
1079 }
1080
1081 json_object_array_add(array, item);
1082 }
1083 json_object_object_add(parent, "includes", array);
1084 }
1085}
1086
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001087/**
1088 * \defgroup netconf_operations NETCONF operations
1089 * The list of NETCONF operations that mod_netconf supports.
1090 * @{
1091 */
1092
1093/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001094 * \brief Send RPC and wait for reply with timeout.
1095 *
1096 * \param[in] session libnetconf session
1097 * \param[in] rpc prepared RPC message
1098 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
1099 * \param[out] reply reply from the server
Michal Vaskof35ea502016-02-24 10:44:54 +01001100 * \return NC_MSG_WOULDBLOCK or NC_MSG_ERROR.
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001101 * On success, it returns NC_MSG_REPLY.
1102 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001103NC_MSG_TYPE
Michal Vaskof35ea502016-02-24 10:44:54 +01001104netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout, int strict, struct nc_reply **reply)
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001105{
Michal Vaskof35ea502016-02-24 10:44:54 +01001106 uint64_t msgid;
1107 NC_MSG_TYPE ret;
1108 ret = nc_send_rpc(session, rpc, timeout, &msgid);
1109 if (ret != NC_MSG_RPC) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001110 return ret;
1111 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001112
1113 while ((ret = nc_recv_reply(session, rpc, msgid, timeout, (strict ? LYD_OPT_STRICT : 0), reply)) == NC_MSG_NOTIF);
1114
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001115 return ret;
1116}
1117
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001118/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001119 * \brief Connect to NETCONF server
1120 *
1121 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
1122 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001123static unsigned int
Michal Vaskof35ea502016-02-24 10:44:54 +01001124netconf_connect(const char *host, const char *port, const char *user, const char *pass, const char *privkey)
Radek Krejci469aab82012-07-22 18:42:20 +02001125{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001126 struct nc_session* session = NULL;
1127 struct session_with_mutex *locked_session, *last_session;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001128 char *pubkey;
Radek Krejci469aab82012-07-22 18:42:20 +02001129
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001130 /* connect to the requested NETCONF server */
1131 password = (char*)pass;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001132 if (privkey) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001133 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 3);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001134 asprintf(&pubkey, "%s.pub", privkey);
Michal Vaskof35ea502016-02-24 10:44:54 +01001135 nc_client_ssh_add_keypair(pubkey, privkey);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001136 free(pubkey);
1137 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001138 nc_client_ssh_set_username(user);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001139 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Michal Vaskof35ea502016-02-24 10:44:54 +01001140 session = nc_connect_ssh(host, (unsigned short)atoi(port), NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001141 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +02001142
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001143 /* if connected successful, add session to the list */
1144 if (session != NULL) {
1145 if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001146 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001147 session = NULL;
1148 free(locked_session);
1149 locked_session = NULL;
1150 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
1151 return 0;
1152 }
1153 locked_session->session = session;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001154 locked_session->hello_message = NULL;
1155 locked_session->closed = 0;
1156 pthread_mutex_init(&locked_session->lock, NULL);
1157 DEBUG("Before session_lock");
1158 /* get exclusive access to sessions_list (conns) */
1159 DEBUG("LOCK wrlock %s", __func__);
1160 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001161 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001162 free(locked_session);
1163 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
1164 return 0;
1165 }
1166 locked_session->ntfc_subscribed = 0;
1167 DEBUG("Add connection to the list");
1168 if (!netconf_sessions_list) {
Michal Vaskoc3146782015-11-04 14:46:41 +01001169 netconf_sessions_list = locked_session;
1170 } else {
1171 for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next);
1172 last_session->next = locked_session;
1173 locked_session->prev = last_session;
1174 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001175 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001176
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001177 /* no need to lock session, noone can read it while we have wrlock */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001178
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001179 /* store information about session from hello message for future usage */
1180 prepare_status_message(locked_session, session);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001181
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001182 DEBUG("NETCONF session established");
1183 locked_session->session_key = session_key_generator;
1184 ++session_key_generator;
1185 if (session_key_generator == UINT_MAX) {
1186 session_key_generator = 1;
1187 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001188
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001189 DEBUG("Before session_unlock");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001190 /* unlock session list */
1191 DEBUG("UNLOCK wrlock %s", __func__);
1192 if (pthread_rwlock_unlock(&session_lock) != 0) {
1193 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1194 }
Radek Krejci469aab82012-07-22 18:42:20 +02001195
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001196 return locked_session->session_key;
1197 }
1198
1199 ERROR("Connection could not be established");
1200 return 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001201}
1202
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001203static int
1204close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +02001205{
Michal Vaskoa53ef882015-11-24 11:02:01 +01001206 int i;
1207
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001208 DEBUG("lock private lock.");
1209 DEBUG("LOCK mutex %s", __func__);
1210 if (pthread_mutex_lock(&locked_session->lock) != 0) {
1211 ERROR("Error while locking rwlock");
1212 }
1213 locked_session->ntfc_subscribed = 0;
1214 locked_session->closed = 1;
1215 if (locked_session->session != NULL) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001216 nc_session_free(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001217 locked_session->session = NULL;
1218 }
1219 DEBUG("session closed.");
1220 DEBUG("unlock private lock.");
1221 DEBUG("UNLOCK mutex %s", __func__);
1222 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
1223 ERROR("Error while locking rwlock");
1224 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001225
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001226 DEBUG("unlock session lock.");
1227 DEBUG("closed session, disabled notif(?), wait 0.5s");
1228 usleep(500000); /* let notification thread stop */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001229
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001230 /* session shouldn't be used by now */
Michal Vaskoa53ef882015-11-24 11:02:01 +01001231 for (i = 0; i < locked_session->notif_count; ++i) {
1232 free(locked_session->notifications[i].content);
1233 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001234 free(locked_session->notifications);
1235 pthread_mutex_destroy(&locked_session->lock);
1236 if (locked_session->hello_message != NULL) {
1237 json_object_put(locked_session->hello_message);
1238 locked_session->hello_message = NULL;
1239 }
1240 locked_session->session = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001241 free(locked_session);
1242 locked_session = NULL;
1243 DEBUG("NETCONF session closed, everything cleared.");
1244 return (EXIT_SUCCESS);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001245}
1246
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001247static int
1248netconf_close(unsigned int session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +02001249{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001250 struct session_with_mutex *locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +02001251
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001252 DEBUG("Session to close: %u", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001253
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001254 /* get exclusive (write) access to sessions_list (conns) */
1255 DEBUG("lock session lock.");
1256 DEBUG("LOCK wrlock %s", __func__);
1257 if (pthread_rwlock_wrlock (&session_lock) != 0) {
1258 ERROR("Error while locking rwlock");
1259 (*reply) = create_error_reply("Internal: Error while locking.");
1260 return EXIT_FAILURE;
1261 }
1262 /* remove session from the active sessions list -> nobody new can now work with session */
1263 for (locked_session = netconf_sessions_list;
1264 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01001265 locked_session = locked_session->next);
1266
1267 if (!locked_session) {
Michal Vasko642cad02016-03-17 12:31:18 +01001268 pthread_rwlock_unlock(&session_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001269 ERROR("Could not find the session %u to close.", session_key);
1270 (*reply) = create_error_reply("Internal: Error while finding a session.");
Michal Vaskoc3146782015-11-04 14:46:41 +01001271 return EXIT_FAILURE;
1272 }
1273
1274 if (!locked_session->prev) {
1275 netconf_sessions_list = netconf_sessions_list->next;
Michal Vaskof0b6fbb2016-03-08 12:04:34 +01001276 if (netconf_sessions_list) {
1277 netconf_sessions_list->prev = NULL;
1278 }
Michal Vaskoc3146782015-11-04 14:46:41 +01001279 } else {
1280 locked_session->prev->next = locked_session->next;
1281 if (locked_session->next) {
1282 locked_session->next->prev = locked_session->prev;
1283 }
1284 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001285
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001286 DEBUG("UNLOCK wrlock %s", __func__);
1287 if (pthread_rwlock_unlock (&session_lock) != 0) {
1288 ERROR("Error while unlocking rwlock");
1289 (*reply) = create_error_reply("Internal: Error while unlocking.");
1290 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001291
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001292 if ((locked_session != NULL) && (locked_session->session != NULL)) {
1293 return close_and_free_session(locked_session);
1294 } else {
1295 ERROR("Unknown session to close");
1296 (*reply) = create_error_reply("Internal: Unkown session to close.");
1297 return (EXIT_FAILURE);
1298 }
1299 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +02001300}
1301
Tomas Cejkac7929632013-10-24 19:25:15 +02001302/**
1303 * Test reply message type and return error message.
1304 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001305 * \param[in] session nc_session internal struct
Michal Vaskoc3146782015-11-04 14:46:41 +01001306 * \param[in] session_key session ID, 0 to disable disconnect on error
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001307 * \param[in] msgt RPC-REPLY message type
Tomas Cejkac7929632013-10-24 19:25:15 +02001308 * \param[out] data
1309 * \return NULL on success
1310 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001311json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001312netconf_test_reply(struct nc_session *session, unsigned int session_key, NC_MSG_TYPE msgt, struct nc_reply *reply, struct lyd_node **data)
Tomas Cejkac7929632013-10-24 19:25:15 +02001313{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001314 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001315
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001316 /* process the result of the operation */
1317 switch (msgt) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001318 case NC_MSG_ERROR:
1319 if (nc_session_get_status(session) != NC_STATUS_RUNNING) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001320 ERROR("mod_netconf: receiving rpc-reply failed");
1321 if (session_key) {
1322 netconf_close(session_key, &err);
1323 }
1324 if (err != NULL) {
1325 return err;
1326 }
1327 return create_error_reply("Internal: Receiving RPC-REPLY failed.");
1328 }
1329 case NC_MSG_NONE:
1330 /* there is error handled by callback */
1331 if (data != NULL) {
1332 free(*data);
1333 (*data) = NULL;
1334 }
1335 return NULL;
1336 case NC_MSG_REPLY:
Michal Vaskof35ea502016-02-24 10:44:54 +01001337 switch (reply->type) {
1338 case NC_RPL_OK:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001339 if ((data != NULL) && (*data != NULL)) {
1340 free(*data);
1341 (*data) = NULL;
1342 }
1343 return create_ok_reply();
Michal Vaskof35ea502016-02-24 10:44:54 +01001344 case NC_RPL_DATA:
1345 if (((*data) = ((struct nc_reply_data *)reply)->data) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001346 ERROR("mod_netconf: no data from reply");
1347 return create_error_reply("Internal: No data from reply received.");
1348 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01001349 ((struct nc_reply_data *)reply)->data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001350 return NULL;
1351 }
1352 break;
Michal Vaskof35ea502016-02-24 10:44:54 +01001353 case NC_RPL_ERROR:
1354 ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001355 if (data != NULL) {
1356 free(*data);
1357 (*data) = NULL;
1358 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001359 return create_error_reply(((struct nc_reply_error *)reply)->err[0].message);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001360 default:
Michal Vaskof35ea502016-02-24 10:44:54 +01001361 ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001362 if (data != NULL) {
1363 free(*data);
1364 (*data) = NULL;
1365 }
1366 return create_error_reply("Unknown type of NETCONF reply.");
1367 }
1368 break;
1369 default:
1370 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
1371 if (data != NULL) {
1372 free(*data);
1373 (*data) = NULL;
1374 }
1375 return create_error_reply("Internal: Unexpected RPC-REPLY message type.");
1376 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001377}
1378
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001379json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001380netconf_unlocked_op(struct nc_session *session, struct nc_rpc *rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001381{
Michal Vaskof35ea502016-02-24 10:44:54 +01001382 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001383 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001384
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001385 /* check requests */
1386 if (rpc == NULL) {
1387 ERROR("mod_netconf: rpc is not created");
1388 return create_error_reply("Internal error: RPC is not created");
1389 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001390
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001391 if (session != NULL) {
1392 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001393 msgt = netconf_send_recv_timed(session, rpc, 50000, 0, &reply);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001394 /* process the result of the operation */
1395 return netconf_test_reply(session, 0, msgt, reply, NULL);
1396 } else {
1397 ERROR("Unknown session to process.");
1398 return create_error_reply("Internal error: Unknown session to process.");
1399 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001400}
1401
Tomas Cejkac7929632013-10-24 19:25:15 +02001402/**
1403 * Perform RPC method that returns data.
1404 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001405 * \param[in] session_id session identifier
1406 * \param[in] rpc RPC message to perform
1407 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
Tomas Cejkac7929632013-10-24 19:25:15 +02001408 * \return NULL on success, json object with error otherwise
1409 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001410static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001411netconf_op(unsigned int session_key, struct nc_rpc *rpc, int strict, struct lyd_node **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +02001412{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001413 struct session_with_mutex * locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001414 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001415 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001416 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001417 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +02001418
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001419 /* check requests */
1420 if (rpc == NULL) {
1421 ERROR("mod_netconf: rpc is not created");
1422 res = create_error_reply("Internal: RPC could not be created.");
1423 data = NULL;
1424 goto finished;
1425 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001426
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001427 locked_session = session_get_locked(session_key, &res);
1428 if (!locked_session) {
1429 ERROR("Unknown session or locking failed.");
1430 goto finished;
1431 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001432
Michal Vaskof35ea502016-02-24 10:44:54 +01001433 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejkac7929632013-10-24 19:25:15 +02001434
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001435 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001436 msgt = netconf_send_recv_timed(locked_session->session, rpc, 2000000, strict, &reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001437
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001438 session_unlock(locked_session);
Tomas Cejkac7929632013-10-24 19:25:15 +02001439
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001440 res = netconf_test_reply(locked_session->session, session_key, msgt, reply, &data);
Radek Krejcia332b692012-11-12 16:15:54 +01001441
Tomas Cejkac7929632013-10-24 19:25:15 +02001442finished:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001443 nc_reply_free(reply);
1444 if (received_data != NULL) {
1445 (*received_data) = data;
1446 } else {
1447 if (data != NULL) {
1448 free(data);
1449 data = NULL;
1450 }
1451 }
1452 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001453}
1454
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001455static char *
1456netconf_getconfig(unsigned int session_key, NC_DATASTORE source, const char *filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001457{
Michal Vaskof35ea502016-02-24 10:44:54 +01001458 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001459 struct session_with_mutex *locked_session;
Michal Vasko04245ee2016-03-16 09:32:59 +01001460 json_object *res = NULL, *data_cjson;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001461 enum json_tokener_error tok_err;
Michal Vasko04245ee2016-03-16 09:32:59 +01001462 char *data_json = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001463 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001464
Michal Vaskof35ea502016-02-24 10:44:54 +01001465 /* tell server to show all elements even if they have default values */
1466#ifdef HAVE_WITHDEFAULTS_TAGGED
1467 rpc = nc_rpc_getconfig(source, filter, NC_WD_MODE_ALL_TAG, NC_PARAMTYPE_CONST);
1468#else
1469 rpc = nc_rpc_getconfig(source, filter, 0, NC_PARAMTYPE_CONST);
1470#endif
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001471 if (rpc == NULL) {
1472 ERROR("mod_netconf: creating rpc request failed");
1473 return (NULL);
1474 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001475
Michal Vaskof35ea502016-02-24 10:44:54 +01001476 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001477 nc_rpc_free(rpc);
1478 if (res != NULL) {
1479 (*err) = res;
1480 } else {
1481 (*err) = NULL;
1482 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001483
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001484 if (data) {
1485 for (locked_session = netconf_sessions_list;
1486 locked_session && (locked_session->session_key != session_key);
1487 locked_session = locked_session->next);
1488 /* won't fail */
1489
Michal Vaskof35ea502016-02-24 10:44:54 +01001490 /* print data into JSON */
1491 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001492 ERROR("Printing JSON <get-config> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001493 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001494 return NULL;
1495 }
1496
1497 /* parse JSON data into cjson */
1498 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001499 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001500 if (!data_cjson) {
1501 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1502 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001503 lyd_free_withsiblings(data);
1504 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001505 return NULL;
1506 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001507 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001508
1509 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001510 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001511 node_add_metadata_recursive(sibling, NULL, data_cjson);
1512 lyd_free(sibling);
1513 }
1514
Michal Vaskof35ea502016-02-24 10:44:54 +01001515 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001516 json_object_put(data_cjson);
1517 pthread_mutex_unlock(&json_lock);
1518 }
1519
Michal Vaskof35ea502016-02-24 10:44:54 +01001520 return (data_json);
Radek Krejci8e4632a2012-07-26 13:40:34 +02001521}
1522
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001523static char *
1524netconf_getschema(unsigned int session_key, const char *identifier, const char *version, const char *format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001525{
Michal Vaskof35ea502016-02-24 10:44:54 +01001526 struct nc_rpc *rpc;
1527 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001528 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001529 char *model_data = NULL, *anyxml, *ptr, *ptr2;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001530
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001531 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001532 rpc = nc_rpc_getschema(identifier, version, format, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001533 if (rpc == NULL) {
1534 ERROR("mod_netconf: creating rpc request failed");
1535 return (NULL);
1536 }
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001537
Michal Vaskof35ea502016-02-24 10:44:54 +01001538 res = netconf_op(session_key, rpc, 0, &data);
1539 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001540 if (res != NULL) {
1541 (*err) = res;
1542 } else {
1543 (*err) = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001544
1545 if (data) {
1546 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data)->value, 0);
1547
1548 /* it's with the data root node, remove it */
1549 if (anyxml) {
1550 ptr = strchr(anyxml, '>');
1551 ++ptr;
1552
1553 ptr2 = strrchr(anyxml, '<');
1554
1555 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
1556 free(anyxml);
1557 }
1558 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001559 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001560
Michal Vaskof35ea502016-02-24 10:44:54 +01001561 return (model_data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001562}
1563
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001564static char *
1565netconf_get(unsigned int session_key, const char* filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001566{
Michal Vaskof35ea502016-02-24 10:44:54 +01001567 struct nc_rpc* rpc;
1568 char* data_json = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001569 json_object *res = NULL, *data_cjson;
1570 enum json_tokener_error tok_err;
1571 struct session_with_mutex *locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001572 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001573
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001574 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001575 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001576 if (rpc == NULL) {
1577 ERROR("mod_netconf: creating rpc request failed");
1578 return (NULL);
1579 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001580
Michal Vaskof35ea502016-02-24 10:44:54 +01001581 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001582 nc_rpc_free(rpc);
1583 if (res != NULL) {
1584 (*err) = res;
1585 } else {
1586 (*err) = NULL;
1587 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001588
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001589 if (data) {
1590 for (locked_session = netconf_sessions_list;
1591 locked_session && (locked_session->session_key != session_key);
1592 locked_session = locked_session->next);
1593 /* won't fail */
1594
Michal Vaskof35ea502016-02-24 10:44:54 +01001595 /* print JSON data */
1596 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001597 ERROR("Printing JSON <get> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001598 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001599 return NULL;
1600 }
1601
1602 /* parse JSON data into cjson */
1603 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001604 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001605 if (!data_cjson) {
1606 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1607 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001608 lyd_free_withsiblings(data);
1609 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001610 return NULL;
1611 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001612 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001613
1614 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001615 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001616 node_add_metadata_recursive(sibling, NULL, data_cjson);
1617 lyd_free(sibling);
1618 }
1619
Michal Vaskof35ea502016-02-24 10:44:54 +01001620 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001621 json_object_put(data_cjson);
1622 pthread_mutex_unlock(&json_lock);
1623 }
1624
Michal Vaskof35ea502016-02-24 10:44:54 +01001625 return data_json;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001626}
1627
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001628static json_object *
1629netconf_copyconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, const char *config,
1630 const char *uri_src, const char *uri_trg)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001631{
Michal Vaskof35ea502016-02-24 10:44:54 +01001632 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001633 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001634
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001635 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001636 rpc = nc_rpc_copy(target, uri_trg, source, (config ? config : uri_src), 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001637 if (rpc == NULL) {
1638 ERROR("mod_netconf: creating rpc request failed");
1639 return create_error_reply("Internal: Creating rpc request failed");
1640 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001641
Michal Vaskof35ea502016-02-24 10:44:54 +01001642 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001643 nc_rpc_free(rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001644
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001645 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001646}
Radek Krejci035bf4e2012-07-25 10:59:09 +02001647
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001648static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001649netconf_editconfig(unsigned int session_key, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP defop,
1650 NC_RPC_EDIT_ERROPT erropt, NC_RPC_EDIT_TESTOPT testopt, const char *config_or_url)
Radek Krejci62ab34b2012-07-26 13:42:05 +02001651{
Michal Vaskof35ea502016-02-24 10:44:54 +01001652 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001653 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001654
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001655 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001656 rpc = nc_rpc_edit(target, defop, testopt, erropt, config_or_url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001657 if (rpc == NULL) {
1658 ERROR("mod_netconf: creating rpc request failed");
1659 return create_error_reply("Internal: Creating rpc request failed");
1660 }
Radek Krejci62ab34b2012-07-26 13:42:05 +02001661
Michal Vaskof35ea502016-02-24 10:44:54 +01001662 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001663 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001664
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001665 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001666}
1667
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001668static json_object *
1669netconf_killsession(unsigned int session_key, const char *sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001670{
Michal Vaskof35ea502016-02-24 10:44:54 +01001671 struct nc_rpc *rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001672 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001673
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001674 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001675 rpc = nc_rpc_kill(atoi(sid));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001676 if (rpc == NULL) {
1677 ERROR("mod_netconf: creating rpc request failed");
1678 return create_error_reply("Internal: Creating rpc request failed");
1679 }
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001680
Michal Vaskof35ea502016-02-24 10:44:54 +01001681 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001682 nc_rpc_free(rpc);
1683 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001684}
1685
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001686static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001687netconf_onlytargetop(unsigned int session_key, NC_DATASTORE target, struct nc_rpc *(*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +02001688{
Michal Vaskof35ea502016-02-24 10:44:54 +01001689 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001690 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +02001691
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001692 /* create requests */
1693 rpc = op_func(target);
1694 if (rpc == NULL) {
1695 ERROR("mod_netconf: creating rpc request failed");
1696 return create_error_reply("Internal: Creating rpc request failed");
1697 }
Radek Krejci2f318372012-07-26 14:22:35 +02001698
Michal Vaskof35ea502016-02-24 10:44:54 +01001699 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001700 nc_rpc_free (rpc);
1701 return res;
Radek Krejci2f318372012-07-26 14:22:35 +02001702}
1703
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001704static json_object *
1705netconf_deleteconfig(unsigned int session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001706{
Michal Vaskof35ea502016-02-24 10:44:54 +01001707 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001708 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001709 rpc = nc_rpc_delete(target, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001710 if (rpc == NULL) {
1711 ERROR("mod_netconf: creating rpc request failed");
1712 return create_error_reply("Internal: Creating rpc request failed");
1713 }
Tomas Cejka404d37e2013-04-13 02:31:35 +02001714
Michal Vaskof35ea502016-02-24 10:44:54 +01001715 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001716 nc_rpc_free (rpc);
1717 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +02001718}
1719
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001720static json_object *
1721netconf_lock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001722{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001723 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001724}
1725
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001726static json_object *
1727netconf_unlock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001728{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001729 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001730}
1731
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001732static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001733netconf_generic(unsigned int session_key, const char *xml_content, struct lyd_node **data)
Radek Krejci80c10d92012-07-30 08:38:50 +02001734{
Michal Vaskof35ea502016-02-24 10:44:54 +01001735 struct nc_rpc* rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001736 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +02001737
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001738 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001739 rpc = nc_rpc_generic_xml(xml_content, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001740 if (rpc == NULL) {
1741 ERROR("mod_netconf: creating rpc request failed");
1742 return create_error_reply("Internal: Creating rpc request failed");
1743 }
Radek Krejci80c10d92012-07-30 08:38:50 +02001744
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001745 /* get session where send the RPC */
Michal Vaskof35ea502016-02-24 10:44:54 +01001746 res = netconf_op(session_key, rpc, 0, data);
1747 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001748 return res;
1749}
1750
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001751static int
Michal Vaskof35ea502016-02-24 10:44:54 +01001752node_add_metadata(const struct lys_node *node, const struct lys_module *module, json_object *parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001753{
1754 struct lys_module *cur_module;
1755 json_object *meta_obj;
1756 char *obj_name;
1757
Michal Vaskoa45770b2015-11-23 15:49:41 +01001758 if (node->nodetype == LYS_INPUT) {
1759 /* silently skipped */
1760 return 0;
1761 }
1762
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001763 cur_module = node->module;
1764 if (cur_module->type) {
1765 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1766 }
1767 if (cur_module == module) {
1768 asprintf(&obj_name, "$@%s", node->name);
1769 } else {
1770 asprintf(&obj_name, "$@%s:%s", cur_module->name, node->name);
1771 }
1772
1773 /* in (leaf-)lists the metadata could have already been added */
Michal Vaskoa45770b2015-11-23 15:49:41 +01001774 if ((node->nodetype & (LYS_LEAFLIST | LYS_LIST)) && (json_object_object_get_ex(parent, obj_name, NULL) == TRUE)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001775 free(obj_name);
1776 return 1;
1777 }
1778
1779 meta_obj = json_object_new_object();
1780
1781 switch (node->nodetype) {
1782 case LYS_CONTAINER:
1783 node_metadata_container((struct lys_node_container *)node, meta_obj);
1784 break;
Michal Vasko3fda9a92015-11-23 10:10:57 +01001785 case LYS_CHOICE:
1786 node_metadata_choice((struct lys_node_choice *)node, meta_obj);
1787 break;
1788 case LYS_LEAF:
1789 node_metadata_leaf((struct lys_node_leaf *)node, meta_obj);
1790 break;
1791 case LYS_LEAFLIST:
1792 node_metadata_leaflist((struct lys_node_leaflist *)node, meta_obj);
1793 break;
1794 case LYS_LIST:
1795 node_metadata_list((struct lys_node_list *)node, meta_obj);
1796 break;
1797 case LYS_ANYXML:
1798 node_metadata_anyxml((struct lys_node_anyxml *)node, meta_obj);
1799 break;
1800 case LYS_CASE:
1801 node_metadata_case((struct lys_node_case *)node, meta_obj);
1802 break;
Michal Vaskoa45770b2015-11-23 15:49:41 +01001803 case LYS_RPC:
1804 node_metadata_rpc((struct lys_node_rpc *)node, meta_obj);
1805 break;
1806 default: /* LYS_OUTPUT */
Michal Vasko3fda9a92015-11-23 10:10:57 +01001807 ERROR("Internal: unuxpected nodetype (%s:%d)", __FILE__, __LINE__);
1808 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001809 }
1810
1811 /* just a precaution */
1812 if (json_object_get_type(parent) != json_type_object) {
1813 ERROR("Internal: wrong JSON type (%s:%d)", __FILE__, __LINE__);
1814 free(obj_name);
1815 return 1;
1816 }
1817
1818 json_object_object_add(parent, obj_name, meta_obj);
1819 free(obj_name);
1820 return 0;
1821}
1822
1823static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001824node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module, json_object *data_json_parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001825{
1826 struct lys_module *cur_module;
1827 struct lys_node *list_schema;
1828 struct lyd_node *child, *list_item;
1829 json_object *child_json, *list_child_json;
1830 char *child_name;
1831 int list_idx;
1832
Michal Vaskoa45770b2015-11-23 15:49:41 +01001833 if (data_tree->schema->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1834 return;
1835 }
1836
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001837 /* add data_tree metadata */
1838 if (node_add_metadata(data_tree->schema, module, data_json_parent)) {
1839 return;
1840 }
1841
1842 /* get data_tree module */
1843 cur_module = data_tree->schema->module;
1844 if (cur_module->type) {
1845 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1846 }
1847
1848 if (!(data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
1849 /* print correct data_tree JSON name */
1850 if (cur_module == module) {
1851 asprintf(&child_name, "%s", data_tree->schema->name);
1852 } else {
1853 asprintf(&child_name, "%s:%s", cur_module->name, data_tree->schema->name);
1854 }
1855
1856 /* go down in JSON object */
1857 if (json_object_object_get_ex(data_json_parent, child_name, &child_json) == FALSE) {
1858 ERROR("Internal: failed to get JSON object \"%s\".", child_name);
1859 free(child_name);
1860 return;
1861 }
1862 free(child_name);
1863
1864 if (data_tree->schema->nodetype == LYS_LIST) {
1865 if (json_object_get_type(child_json) != json_type_array) {
1866 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1867 return;
1868 }
1869 /* go down in data tree for every item, we process them all now, skip later
1870 * (metadata duplicate will be detected at the beginning of this function) */
1871 list_idx = 0;
1872 list_schema = data_tree->schema;
1873
1874 LY_TREE_FOR(data_tree, list_item) {
1875 /* another list member */
1876 if (list_item->schema == list_schema) {
1877 list_child_json = json_object_array_get_idx(child_json, list_idx);
1878 if (!list_child_json) {
1879 ERROR("Internal: list \"%s\" idx out-of-bounds", list_schema->name);
1880 return;
1881 }
1882 LY_TREE_FOR(list_item->child, child) {
1883 node_add_metadata_recursive(child, cur_module, list_child_json);
1884 }
1885
1886 ++list_idx;
1887 }
1888 }
1889 } else {
1890 if (json_object_get_type(child_json) != json_type_object) {
1891 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1892 return;
1893 }
1894 /* go down in data tree */
1895 LY_TREE_FOR(data_tree->child, child) {
1896 node_add_metadata_recursive(child, cur_module, child_json);
1897 }
1898 }
1899 }
1900}
1901
1902static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001903node_add_model_metadata(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001904{
1905 json_object *obj;
1906 char *str;
1907
1908 obj = json_object_new_object();
1909 node_metadata_model(module, obj);
1910 asprintf(&str, "$@@%s", module->name);
1911 json_object_object_add(parent, str, obj);
1912 free(str);
1913}
1914
1915static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001916node_add_children_with_metadata_recursive(const struct lys_node *node, const struct lys_module *module, json_object *parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001917{
Michal Vaskof35ea502016-02-24 10:44:54 +01001918 const struct lys_module *cur_module;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001919 struct lys_node *child;
1920 json_object *node_json;
1921 char *json_name;
1922
Michal Vaskoa45770b2015-11-23 15:49:41 +01001923 if (node->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1924 return;
1925 }
1926
1927 if (node->nodetype & LYS_USES) {
1928 cur_module = module;
1929 node_json = parent;
1930 goto children;
1931 }
1932
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001933 /* add node metadata */
1934 if (node_add_metadata(node, module, parent)) {
1935 ERROR("Internal: metadata duplicate for \"%s\".", node->name);
1936 return;
1937 }
1938
Michal Vaskoa45770b2015-11-23 15:49:41 +01001939 /* no other metadata */
1940 if (!node->child) {
1941 return;
1942 }
1943
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001944 /* get node module */
1945 cur_module = node->module;
1946 if (cur_module->type) {
1947 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1948 }
1949
1950 /* create JSON object for child metadata */
1951 node_json = json_object_new_object();
1952 if (cur_module == module) {
1953 json_object_object_add(parent, node->name, node_json);
1954 } else {
1955 asprintf(&json_name, "%s:%s", cur_module->name, node->name);
1956 json_object_object_add(parent, json_name, node_json);
1957 free(json_name);
1958 }
1959
Michal Vaskoa45770b2015-11-23 15:49:41 +01001960children:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001961 LY_TREE_FOR(node->child, child) {
1962 node_add_children_with_metadata_recursive(child, cur_module, node_json);
1963 }
1964}
1965
1966static json_object *
1967libyang_query(unsigned int session_key, const char *filter, int load_children)
1968{
Michal Vaskof35ea502016-02-24 10:44:54 +01001969 const struct lys_node *node;
1970 const struct lys_module *module = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001971 struct session_with_mutex *locked_session;
1972 json_object *ret = NULL, *data;
1973
1974 locked_session = session_get_locked(session_key, &ret);
1975 if (!locked_session) {
1976 ERROR("Locking failed or session not found.");
1977 goto finish;
1978 }
1979
Michal Vaskof35ea502016-02-24 10:44:54 +01001980 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001981
Michal Vaskoa45770b2015-11-23 15:49:41 +01001982 if (filter[0] == '/') {
Michal Vaskof35ea502016-02-24 10:44:54 +01001983 node = ly_ctx_get_node(nc_session_get_ctx(locked_session->session), filter);
Michal Vaskoa45770b2015-11-23 15:49:41 +01001984 if (!node) {
1985 ret = create_error_reply("Failed to resolve XPath filter node.");
1986 goto finish;
1987 }
1988 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01001989 module = ly_ctx_get_module(nc_session_get_ctx(locked_session->session), filter, NULL);
Michal Vaskoa45770b2015-11-23 15:49:41 +01001990 if (!module) {
1991 ret = create_error_reply("Failed to find model.");
1992 goto finish;
1993 }
1994 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001995
Michal Vaskoa45770b2015-11-23 15:49:41 +01001996 pthread_mutex_lock(&json_lock);
1997 data = json_object_new_object();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001998
Michal Vaskoa45770b2015-11-23 15:49:41 +01001999 if (module) {
2000 node_add_model_metadata(module, data);
2001 if (load_children) {
2002 LY_TREE_FOR(module->data, node) {
2003 node_add_children_with_metadata_recursive(node, NULL, data);
2004 }
2005 }
2006 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002007 if (load_children) {
2008 node_add_children_with_metadata_recursive(node, NULL, data);
2009 } else {
2010 node_add_metadata(node, NULL, data);
2011 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002012 }
2013
Michal Vaskoa45770b2015-11-23 15:49:41 +01002014 pthread_mutex_unlock(&json_lock);
2015 ret = create_data_reply(json_object_to_json_string(data));
2016 json_object_put(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002017
2018finish:
Michal Vaskoa45770b2015-11-23 15:49:41 +01002019 session_unlock(locked_session);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002020 return ret;
2021}
2022
2023static json_object *
2024libyang_merge(unsigned int session_key, const char *config)
2025{
2026 struct lyd_node *data_tree = NULL, *sibling;
2027 struct session_with_mutex *locked_session;
2028 json_object *ret = NULL, *data_json = NULL;
2029 enum json_tokener_error err = 0;
2030
2031 locked_session = session_get_locked(session_key, &ret);
2032 if (!locked_session) {
2033 ERROR("Locking failed or session not found.");
2034 goto finish;
2035 }
2036
Michal Vaskof35ea502016-02-24 10:44:54 +01002037 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002038
Michal Vaskof35ea502016-02-24 10:44:54 +01002039 data_tree = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_STRICT);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002040 if (!data_tree) {
2041 ERROR("Creating data tree failed.");
2042 ret = create_error_reply("Failed to create data tree from JSON config.");
2043 session_unlock(locked_session);
2044 goto finish;
2045 }
2046
2047 session_unlock(locked_session);
2048
2049 pthread_mutex_lock(&json_lock);
2050 data_json = json_tokener_parse_verbose(config, &err);
2051 if (!data_json) {
2052 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(err));
2053 pthread_mutex_unlock(&json_lock);
2054 ret = create_error_reply(json_tokener_error_desc(err));
2055 goto finish;
2056 }
2057
2058 /* go simultaneously through both trees and add metadata */
2059 LY_TREE_FOR(data_tree, sibling) {
2060 node_add_metadata_recursive(sibling, NULL, data_json);
2061 }
2062 pthread_mutex_unlock(&json_lock);
2063 ret = create_data_reply(json_object_to_json_string(data_json));
2064
2065finish:
2066 LY_TREE_FOR(data_tree, sibling) {
2067 lyd_free(sibling);
2068 }
2069 json_object_put(data_json);
2070 return ret;
Radek Krejci80c10d92012-07-30 08:38:50 +02002071}
2072
Tomas Cejka0a4bba82013-04-19 11:51:28 +02002073/**
2074 * @}
2075 *//* netconf_operations */
2076
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002077void
2078clb_print(NC_VERB_LEVEL level, const char *msg)
Radek Krejci469aab82012-07-22 18:42:20 +02002079{
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002080#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +02002081 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
2082
2083#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002084 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002085
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002086 switch (level) {
2087 FOREACH(CASE);
2088 case NC_VERB_VERBOSE:
2089 case NC_VERB_DEBUG:
2090 DEBUG("DEBUG: %s", msg);
2091 break;
2092 }
2093 if (level == NC_VERB_ERROR) {
2094 /* return global error */
2095 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
2096 NULL /* severity */, NULL /* apptag */,
2097 NULL /* path */, msg, NULL /* attribute */,
2098 NULL /* element */, NULL /* ns */, NULL /* sid */);
2099 }
Radek Krejci469aab82012-07-22 18:42:20 +02002100}
2101
Tomas Cejka64b87482013-06-03 16:30:53 +02002102/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02002103 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02002104 * Caller should free message memory.
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002105 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02002106 * \return pointer to message
2107 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002108char *
2109get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02002110{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002111 /* read json in chunked framing */
2112 unsigned int buffer_size = 0;
2113 ssize_t buffer_len = 0;
2114 char *buffer = NULL;
2115 char c;
2116 ssize_t ret;
2117 int i, chunk_len;
2118 char chunk_len_str[12];
Tomas Cejka64b87482013-06-03 16:30:53 +02002119
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002120 while (1) {
2121 /* read chunk length */
2122 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
2123 if (buffer != NULL) {
2124 free (buffer);
2125 buffer = NULL;
2126 }
2127 break;
2128 }
2129 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
2130 if (buffer != NULL) {
2131 free (buffer);
2132 buffer = NULL;
2133 }
2134 break;
2135 }
2136 i=0;
2137 memset (chunk_len_str, 0, 12);
2138 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
2139 if (i==0 && c == '#') {
2140 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
2141 /* end but invalid */
2142 if (buffer != NULL) {
2143 free (buffer);
2144 buffer = NULL;
2145 }
2146 }
2147 /* end of message, double-loop break */
2148 goto msg_complete;
2149 }
2150 chunk_len_str[i++] = c;
2151 if (i==11) {
2152 ERROR("Message is too long, buffer for length is not big enought!!!!");
2153 break;
2154 }
2155 }
2156 if (c != '\n') {
2157 if (buffer != NULL) {
2158 free (buffer);
2159 buffer = NULL;
2160 }
2161 break;
2162 }
2163 chunk_len_str[i] = 0;
2164 if ((chunk_len = atoi (chunk_len_str)) == 0) {
2165 if (buffer != NULL) {
2166 free (buffer);
2167 buffer = NULL;
2168 }
2169 break;
2170 }
2171 buffer_size += chunk_len+1;
2172 buffer = realloc (buffer, sizeof(char)*buffer_size);
2173 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
2174 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
2175 if (buffer != NULL) {
2176 free (buffer);
2177 buffer = NULL;
2178 }
2179 break;
2180 }
2181 buffer_len += ret;
2182 }
Tomas Cejka64b87482013-06-03 16:30:53 +02002183msg_complete:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002184 return buffer;
Tomas Cejka64b87482013-06-03 16:30:53 +02002185}
2186
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002187NC_DATASTORE
2188parse_datastore(const char *ds)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002189{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002190 if (strcmp(ds, "running") == 0) {
2191 return NC_DATASTORE_RUNNING;
2192 } else if (strcmp(ds, "startup") == 0) {
2193 return NC_DATASTORE_STARTUP;
2194 } else if (strcmp(ds, "candidate") == 0) {
2195 return NC_DATASTORE_CANDIDATE;
2196 } else if (strcmp(ds, "url") == 0) {
2197 return NC_DATASTORE_URL;
2198 } else if (strcmp(ds, "config") == 0) {
2199 return NC_DATASTORE_CONFIG;
2200 }
2201 return -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002202}
2203
Michal Vaskof35ea502016-02-24 10:44:54 +01002204NC_RPC_EDIT_TESTOPT
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002205parse_testopt(const char *t)
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002206{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002207 if (strcmp(t, "notset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002208 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002209 } else if (strcmp(t, "testset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002210 return NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002211 } else if (strcmp(t, "set") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002212 return NC_RPC_EDIT_TESTOPT_SET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002213 } else if (strcmp(t, "test") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002214 return NC_RPC_EDIT_TESTOPT_TEST;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002215 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002216 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002217}
2218
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002219json_object *
2220create_error_reply(const char *errmess)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002221{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002222 json_object *reply, *array;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002223
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002224 pthread_mutex_lock(&json_lock);
2225 reply = json_object_new_object();
2226 array = json_object_new_array();
2227 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2228 json_object_array_add(array, json_object_new_string(errmess));
2229 json_object_object_add(reply, "errors", array);
2230 pthread_mutex_unlock(&json_lock);
2231
2232 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002233}
2234
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002235json_object *
2236create_data_reply(const char *data)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002237{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002238 pthread_mutex_lock(&json_lock);
2239 json_object *reply = json_object_new_object();
2240 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
2241 json_object_object_add(reply, "data", json_object_new_string(data));
2242 pthread_mutex_unlock(&json_lock);
2243 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002244}
2245
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002246json_object *
2247create_ok_reply(void)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002248{
Michal Vasko365dc4c2016-03-17 10:03:58 +01002249 json_object *reply;
2250
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002251 pthread_mutex_lock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002252 reply = json_object_new_object();
2253 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2254 pthread_mutex_unlock(&json_lock);
2255 return reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002256}
2257
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002258json_object *
2259create_replies(void)
Tomas Cejka09629492014-07-10 15:58:06 +02002260{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002261 json_object *replies;
2262
2263 pthread_mutex_lock(&json_lock);
2264 replies = json_object_new_object();
2265 pthread_mutex_unlock(&json_lock);
2266
2267 return replies;
Tomas Cejka09629492014-07-10 15:58:06 +02002268}
2269
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002270void
2271add_reply(json_object *replies, json_object *reply, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002272{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002273 char *str;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002274
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002275 asprintf(&str, "%u", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002276
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002277 pthread_mutex_lock(&json_lock);
2278 json_object_object_add(replies, str, reply);
2279 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002280
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002281 free(str);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002282}
2283
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002284char *
2285get_param_string(json_object *data, const char *name)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002286{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002287 json_object *js_tmp = NULL;
2288 char *res = NULL;
2289 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
2290 res = strdup(json_object_get_string(js_tmp));
2291 }
2292 return res;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002293}
2294
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002295json_object *
2296handle_op_connect(json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002297{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002298 char *host = NULL;
2299 char *port = NULL;
2300 char *user = NULL;
2301 char *pass = NULL;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002302 char *privkey = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002303 json_object *reply = NULL;
2304 unsigned int session_key = 0;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002305
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002306 DEBUG("Request: connect");
2307 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002308
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002309 host = get_param_string(request, "host");
2310 port = get_param_string(request, "port");
2311 user = get_param_string(request, "user");
2312 pass = get_param_string(request, "pass");
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002313 privkey = get_param_string(request, "privatekey");
Tomas Cejkad5b53772013-06-08 23:01:07 +02002314
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002315 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002316
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002317 if (host == NULL) {
2318 host = "localhost";
2319 }
2320
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002321 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002322 if (user == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002323 ERROR("Cannot connect - insufficient input.");
2324 session_key = 0;
2325 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002326 session_key = netconf_connect(host, port, user, pass, privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002327 DEBUG("Session key: %u", session_key);
2328 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002329
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002330 GETSPEC_ERR_REPLY
2331
2332 pthread_mutex_lock(&json_lock);
2333 if (session_key == 0) {
2334 /* negative reply */
2335 if (err_reply == NULL) {
2336 reply = json_object_new_object();
2337 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2338 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
2339 ERROR("Connection failed.");
2340 } else {
2341 /* use filled err_reply from libnetconf's callback */
2342 reply = err_reply;
2343 ERROR("Connect - error from libnetconf's callback.");
2344 }
2345 } else {
2346 /* positive reply */
2347 reply = json_object_new_object();
2348 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2349 json_object_object_add(reply, "session", json_object_new_int(session_key));
2350 }
2351 memset(pass, 0, strlen(pass));
2352 pthread_mutex_unlock(&json_lock);
2353 CHECK_AND_FREE(host);
2354 CHECK_AND_FREE(user);
2355 CHECK_AND_FREE(port);
2356 CHECK_AND_FREE(pass);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002357 CHECK_AND_FREE(privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002358 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002359}
2360
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002361json_object *
2362handle_op_disconnect(json_object *UNUSED(request), unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002363{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002364 json_object *reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002365
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002366 DEBUG("Request: disconnect (session %u)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002367
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002368 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
2369 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
2370 } else {
2371 reply = create_ok_reply();
2372 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002373
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002374 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002375}
2376
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002377json_object *
2378handle_op_get(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002379{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002380 char *filter = NULL;
2381 char *data = NULL;
2382 json_object *reply = NULL, *obj;
2383 int strict;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002384
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002385 DEBUG("Request: get (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002386
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002387 pthread_mutex_lock(&json_lock);
2388 filter = get_param_string(request, "filter");
2389 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2390 pthread_mutex_unlock(&json_lock);
2391 reply = create_error_reply("Missing strict parameter.");
2392 return reply;
2393 }
2394 strict = json_object_get_boolean(obj);
2395 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002396
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002397 if ((data = netconf_get(session_key, filter, strict, &reply)) == NULL) {
2398 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
2399 } else {
2400 reply = create_data_reply(data);
2401 free(data);
2402 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002403
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002404 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002405}
2406
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002407json_object *
2408handle_op_getconfig(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002409{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002410 NC_DATASTORE ds_type_s = -1;
2411 char *filter = NULL;
2412 char *data = NULL;
2413 char *source = NULL;
2414 json_object *reply = NULL, *obj;
2415 int strict;
Tomas Cejkab4d05872014-02-14 22:44:38 +01002416
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002417 DEBUG("Request: get-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002418
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002419 pthread_mutex_lock(&json_lock);
2420 filter = get_param_string(request, "filter");
2421 source = get_param_string(request, "source");
2422 if (source != NULL) {
2423 ds_type_s = parse_datastore(source);
2424 }
2425 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2426 pthread_mutex_unlock(&json_lock);
2427 reply = create_error_reply("Missing strict parameter.");
2428 return reply;
2429 }
2430 strict = json_object_get_boolean(obj);
2431 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002432
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002433 if ((int)ds_type_s == -1) {
2434 reply = create_error_reply("Invalid source repository type requested.");
2435 goto finalize;
2436 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002437
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002438 if ((data = netconf_getconfig(session_key, ds_type_s, filter, strict, &reply)) == NULL) {
2439 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
2440 } else {
2441 reply = create_data_reply(data);
2442 free(data);
2443 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002444
Tomas Cejka09629492014-07-10 15:58:06 +02002445finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002446 CHECK_AND_FREE(filter);
2447 CHECK_AND_FREE(source);
2448 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002449}
2450
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002451json_object *
2452handle_op_editconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002453{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002454 NC_DATASTORE ds_type_t = -1;
Michal Vaskof35ea502016-02-24 10:44:54 +01002455 NC_RPC_EDIT_DFLTOP defop_type = 0;
2456 NC_RPC_EDIT_ERROPT erropt_type = 0;
2457 NC_RPC_EDIT_TESTOPT testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002458 char *defop = NULL;
2459 char *erropt = NULL;
2460 char *config = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002461 char *target = NULL;
2462 char *testopt = NULL;
2463 char *urisource = NULL;
2464 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002465 struct lyd_node *content;
2466 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002467
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002468 DEBUG("Request: edit-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002469
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002470 pthread_mutex_lock(&json_lock);
2471 /* get parameters */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002472 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2473 pthread_mutex_unlock(&json_lock);
2474 reply = create_error_reply("Missing configs parameter.");
2475 goto finalize;
2476 }
2477 obj = json_object_array_get_idx(configs, idx);
2478 config = strdup(json_object_get_string(obj));
Tomas Cejkad5b53772013-06-08 23:01:07 +02002479
Michal Vaskof35ea502016-02-24 10:44:54 +01002480 target = get_param_string(request, "target");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002481 defop = get_param_string(request, "default-operation");
2482 erropt = get_param_string(request, "error-option");
2483 urisource = get_param_string(request, "uri-source");
2484 testopt = get_param_string(request, "test-option");
2485 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002486
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002487 if (target != NULL) {
2488 ds_type_t = parse_datastore(target);
2489 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002490
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002491 if (defop != NULL) {
2492 if (strcmp(defop, "merge") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002493 defop_type = NC_RPC_EDIT_DFLTOP_MERGE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002494 } else if (strcmp(defop, "replace") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002495 defop_type = NC_RPC_EDIT_DFLTOP_REPLACE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002496 } else if (strcmp(defop, "none") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002497 defop_type = NC_RPC_EDIT_DFLTOP_NONE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002498 } else {
2499 reply = create_error_reply("Invalid default-operation parameter.");
2500 goto finalize;
2501 }
2502 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002503 defop_type = NC_RPC_EDIT_DFLTOP_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002504 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002505
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002506 if (erropt != NULL) {
2507 if (strcmp(erropt, "continue-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002508 erropt_type = NC_RPC_EDIT_ERROPT_CONTINUE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002509 } else if (strcmp(erropt, "stop-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002510 erropt_type = NC_RPC_EDIT_ERROPT_STOP;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002511 } else if (strcmp(erropt, "rollback-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002512 erropt_type = NC_RPC_EDIT_ERROPT_ROLLBACK;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002513 } else {
2514 reply = create_error_reply("Invalid error-option parameter.");
2515 goto finalize;
2516 }
2517 } else {
2518 erropt_type = 0;
2519 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002520
Michal Vaskof35ea502016-02-24 10:44:54 +01002521 if ((config && urisource) || (!config && !urisource)) {
2522 reply = create_error_reply("Invalid config and uri-source data parameters.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002523 goto finalize;
2524 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002525
2526 if (config) {
2527 locked_session = session_get_locked(session_key, NULL);
2528 if (!locked_session) {
2529 ERROR("Unknown session or locking failed.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002530 goto finalize;
2531 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002532
2533 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_EDIT);
2534 session_unlock(locked_session);
2535
2536 free(config);
2537 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2538 lyd_free_withsiblings(content);
2539 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002540 config = urisource;
2541 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002542
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002543 if (testopt != NULL) {
2544 testopt_type = parse_testopt(testopt);
2545 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002546 testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002547 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002548
Michal Vaskof35ea502016-02-24 10:44:54 +01002549 reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002550
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002551 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002552
Tomas Cejka09629492014-07-10 15:58:06 +02002553finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002554 CHECK_AND_FREE(defop);
2555 CHECK_AND_FREE(erropt);
2556 CHECK_AND_FREE(config);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002557 CHECK_AND_FREE(urisource);
2558 CHECK_AND_FREE(target);
2559 CHECK_AND_FREE(testopt);
2560
2561 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002562}
2563
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002564json_object *
2565handle_op_copyconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002566{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002567 NC_DATASTORE ds_type_s = -1;
2568 NC_DATASTORE ds_type_t = -1;
2569 char *config = NULL;
2570 char *target = NULL;
2571 char *source = NULL;
2572 char *uri_src = NULL;
2573 char *uri_trg = NULL;
2574 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002575 struct lyd_node *content;
2576 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002577
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002578 DEBUG("Request: copy-config (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002579
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002580 /* get parameters */
2581 pthread_mutex_lock(&json_lock);
2582 target = get_param_string(request, "target");
2583 source = get_param_string(request, "source");
2584 uri_src = get_param_string(request, "uri-source");
2585 uri_trg = get_param_string(request, "uri-target");
2586 if (!strcmp(source, "config")) {
2587 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2588 pthread_mutex_unlock(&json_lock);
2589 reply = create_error_reply("Missing configs parameter.");
2590 goto finalize;
2591 }
2592 obj = json_object_array_get_idx(configs, idx);
2593 if (!obj) {
2594 pthread_mutex_unlock(&json_lock);
2595 reply = create_error_reply("Configs array parameter shorter than sessions.");
2596 goto finalize;
2597 }
2598 config = strdup(json_object_get_string(obj));
2599 }
2600 pthread_mutex_unlock(&json_lock);
2601
2602 if (target != NULL) {
2603 ds_type_t = parse_datastore(target);
2604 }
2605 if (source != NULL) {
2606 ds_type_s = parse_datastore(source);
2607 }
2608
2609 if ((int)ds_type_s == -1) {
2610 /* invalid source datastore specified */
2611 reply = create_error_reply("Invalid source repository type requested.");
2612 goto finalize;
2613 }
2614
2615 if ((int)ds_type_t == -1) {
2616 /* invalid target datastore specified */
2617 reply = create_error_reply("Invalid target repository type requested.");
2618 goto finalize;
2619 }
2620
2621 if (ds_type_s == NC_DATASTORE_URL) {
2622 if (uri_src == NULL) {
2623 uri_src = "";
2624 }
2625 }
2626 if (ds_type_t == NC_DATASTORE_URL) {
2627 if (uri_trg == NULL) {
2628 uri_trg = "";
2629 }
2630 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002631
2632 if (config) {
2633 locked_session = session_get_locked(session_key, NULL);
2634 if (!locked_session) {
2635 ERROR("Unknown session or locking failed.");
2636 goto finalize;
2637 }
2638
2639 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_CONFIG);
2640 session_unlock(locked_session);
2641
2642 free(config);
2643 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2644 lyd_free_withsiblings(content);
2645 }
2646
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002647 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
2648
2649 CHECK_ERR_SET_REPLY
2650
2651finalize:
2652 CHECK_AND_FREE(config);
2653 CHECK_AND_FREE(target);
2654 CHECK_AND_FREE(source);
2655 CHECK_AND_FREE(uri_src);
2656 CHECK_AND_FREE(uri_trg);
2657
2658 return reply;
2659}
2660
2661json_object *
2662handle_op_deleteconfig(json_object *request, unsigned int session_key)
2663{
2664 json_object *reply;
2665 NC_DATASTORE ds_type = -1;
2666 char *target, *url;
2667
2668 DEBUG("Request: delete-config (session %u)", session_key);
2669
2670 pthread_mutex_lock(&json_lock);
2671 target = get_param_string(request, "target");
2672 url = get_param_string(request, "url");
2673 pthread_mutex_unlock(&json_lock);
2674
2675 if (target != NULL) {
2676 ds_type = parse_datastore(target);
2677 }
2678 if ((int)ds_type == -1) {
2679 reply = create_error_reply("Invalid target repository type requested.");
2680 goto finalize;
2681 }
2682 if (ds_type == NC_DATASTORE_URL) {
2683 if (!url) {
2684 url = "";
2685 }
2686 }
2687
2688 reply = netconf_deleteconfig(session_key, ds_type, url);
2689
2690 CHECK_ERR_SET_REPLY
2691 if (reply == NULL) {
2692 reply = create_ok_reply();
2693 }
2694
2695finalize:
2696 CHECK_AND_FREE(target);
2697 CHECK_AND_FREE(url);
2698 return reply;
2699}
2700
2701json_object *
2702handle_op_lock(json_object *request, unsigned int session_key)
2703{
2704 json_object *reply;
2705 NC_DATASTORE ds_type = -1;
2706 char *target;
2707
2708 DEBUG("Request: lock (session %u)", session_key);
2709
2710 pthread_mutex_lock(&json_lock);
2711 target = get_param_string(request, "target");
2712 pthread_mutex_unlock(&json_lock);
2713
2714 if (target != NULL) {
2715 ds_type = parse_datastore(target);
2716 }
2717 if ((int)ds_type == -1) {
2718 reply = create_error_reply("Invalid target repository type requested.");
2719 goto finalize;
2720 }
2721
2722 reply = netconf_lock(session_key, ds_type);
2723
2724 CHECK_ERR_SET_REPLY
2725 if (reply == NULL) {
2726 reply = create_ok_reply();
2727 }
2728
2729finalize:
2730 CHECK_AND_FREE(target);
2731 return reply;
2732}
2733
2734json_object *
2735handle_op_unlock(json_object *request, unsigned int session_key)
2736{
2737 json_object *reply;
2738 NC_DATASTORE ds_type = -1;
2739 char *target;
2740
2741 DEBUG("Request: unlock (session %u)", session_key);
2742
2743 pthread_mutex_lock(&json_lock);
2744 target = get_param_string(request, "target");
2745 pthread_mutex_unlock(&json_lock);
2746
2747 if (target != NULL) {
2748 ds_type = parse_datastore(target);
2749 }
2750 if ((int)ds_type == -1) {
2751 reply = create_error_reply("Invalid target repository type requested.");
2752 goto finalize;
2753 }
2754
2755 reply = netconf_unlock(session_key, ds_type);
2756
2757 CHECK_ERR_SET_REPLY
2758 if (reply == NULL) {
2759 reply = create_ok_reply();
2760 }
2761
2762finalize:
2763 CHECK_AND_FREE(target);
2764 return reply;
2765}
2766
2767json_object *
2768handle_op_kill(json_object *request, unsigned int session_key)
2769{
2770 json_object *reply = NULL;
2771 char *sid = NULL;
2772
2773 DEBUG("Request: kill-session (session %u)", session_key);
2774
2775 pthread_mutex_lock(&json_lock);
2776 sid = get_param_string(request, "session-id");
2777 pthread_mutex_unlock(&json_lock);
2778
2779 if (sid == NULL) {
2780 reply = create_error_reply("Missing session-id parameter.");
2781 goto finalize;
2782 }
2783
2784 reply = netconf_killsession(session_key, sid);
2785
2786 CHECK_ERR_SET_REPLY
2787
2788finalize:
2789 CHECK_AND_FREE(sid);
2790 return reply;
2791}
2792
2793json_object *
2794handle_op_info(json_object *UNUSED(request), unsigned int session_key)
2795{
2796 json_object *reply = NULL;
2797 struct session_with_mutex *locked_session = NULL;
2798 DEBUG("Request: get info about session %u", session_key);
2799
2800 DEBUG("LOCK wrlock %s", __func__);
2801 if (pthread_rwlock_rdlock(&session_lock) != 0) {
2802 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2803 }
2804
2805 for (locked_session = netconf_sessions_list;
2806 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002807 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002808 if (locked_session != NULL) {
2809 DEBUG("LOCK mutex %s", __func__);
2810 pthread_mutex_lock(&locked_session->lock);
2811 DEBUG("UNLOCK wrlock %s", __func__);
2812 if (pthread_rwlock_unlock(&session_lock) != 0) {
2813 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2814 }
2815 if (locked_session->hello_message != NULL) {
2816 reply = locked_session->hello_message;
2817 } else {
2818 reply = create_error_reply("Invalid session identifier.");
2819 }
2820 DEBUG("UNLOCK mutex %s", __func__);
2821 pthread_mutex_unlock(&locked_session->lock);
2822 } else {
2823 DEBUG("UNLOCK wrlock %s", __func__);
2824 if (pthread_rwlock_unlock(&session_lock) != 0) {
2825 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2826 }
2827 reply = create_error_reply("Invalid session identifier.");
2828 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002829
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002830 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002831}
2832
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002833json_object *
2834handle_op_generic(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002835{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002836 json_object *reply = NULL, *contents, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002837 char *content = NULL, *str;
2838 struct lyd_node *data = NULL, *node_content;
2839 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002840
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002841 DEBUG("Request: generic request (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002842
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002843 pthread_mutex_lock(&json_lock);
2844 if (json_object_object_get_ex(request, "contents", &contents) == FALSE) {
2845 pthread_mutex_unlock(&json_lock);
2846 reply = create_error_reply("Missing contents parameter.");
2847 goto finalize;
2848 }
2849 obj = json_object_array_get_idx(contents, idx);
2850 if (!obj) {
2851 pthread_mutex_unlock(&json_lock);
2852 reply = create_error_reply("Contents array parameter shorter than sessions.");
2853 goto finalize;
2854 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002855 content = strdup(json_object_get_string(obj));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002856 pthread_mutex_unlock(&json_lock);
2857
Michal Vaskof35ea502016-02-24 10:44:54 +01002858 locked_session = session_get_locked(session_key, NULL);
2859 if (!locked_session) {
2860 ERROR("Unknown session or locking failed.");
2861 goto finalize;
2862 }
2863
2864 node_content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), content, LYD_JSON, LYD_OPT_RPC);
2865 session_unlock(locked_session);
2866
2867 free(content);
2868 lyd_print_mem(&content, node_content, LYD_XML, LYP_WITHSIBLINGS);
2869 lyd_free_withsiblings(node_content);
2870
2871 reply = netconf_generic(session_key, content, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002872 if (reply == NULL) {
2873 GETSPEC_ERR_REPLY
2874 if (err_reply != NULL) {
2875 /* use filled err_reply from libnetconf's callback */
2876 reply = err_reply;
2877 }
2878 } else {
2879 if (data == NULL) {
2880 pthread_mutex_lock(&json_lock);
2881 reply = json_object_new_object();
2882 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2883 pthread_mutex_unlock(&json_lock);
2884 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002885 lyd_print_mem(&str, data, LYD_JSON, LYP_WITHSIBLINGS);
2886 lyd_free_withsiblings(data);
2887 reply = create_data_reply(str);
2888 free(str);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002889 }
2890 }
2891
2892finalize:
Michal Vaskof35ea502016-02-24 10:44:54 +01002893 CHECK_AND_FREE(content);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002894 return reply;
2895}
2896
2897json_object *
2898handle_op_getschema(json_object *request, unsigned int session_key)
2899{
2900 char *data = NULL;
2901 char *identifier = NULL;
2902 char *version = NULL;
2903 char *format = NULL;
2904 json_object *reply = NULL;
2905
2906 DEBUG("Request: get-schema (session %u)", session_key);
2907
2908 pthread_mutex_lock(&json_lock);
2909 identifier = get_param_string(request, "identifier");
2910 version = get_param_string(request, "version");
2911 format = get_param_string(request, "format");
2912 pthread_mutex_unlock(&json_lock);
2913
2914 if (identifier == NULL) {
2915 reply = create_error_reply("No identifier for get-schema supplied.");
2916 goto finalize;
2917 }
2918
2919 DEBUG("get-schema(version: %s, format: %s)", version, format);
2920 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
2921 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
2922 } else {
2923 reply = create_data_reply(data);
2924 free(data);
2925 }
2926
2927finalize:
2928 CHECK_AND_FREE(identifier);
2929 CHECK_AND_FREE(version);
2930 CHECK_AND_FREE(format);
2931 return reply;
2932}
2933
2934json_object *
2935handle_op_reloadhello(json_object *UNUSED(request), unsigned int session_key)
2936{
2937 struct nc_session *temp_session = NULL;
2938 struct session_with_mutex * locked_session = NULL;
2939 json_object *reply = NULL;
2940
2941 DEBUG("Request: reload hello (session %u)", session_key);
2942
2943 DEBUG("LOCK wrlock %s", __func__);
2944 if (pthread_rwlock_wrlock(&session_lock) != 0) {
2945 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2946 return NULL;
2947 }
2948
2949 for (locked_session = netconf_sessions_list;
2950 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002951 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002952 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
2953 DEBUG("LOCK mutex %s", __func__);
2954 pthread_mutex_lock(&locked_session->lock);
2955 DEBUG("creating temporary NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01002956 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002957 if (temp_session != NULL) {
2958 prepare_status_message(locked_session, temp_session);
2959 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01002960 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002961 temp_session = NULL;
2962 } else {
2963 DEBUG("Reload hello failed due to channel establishment");
2964 reply = create_error_reply("Reload was unsuccessful, connection failed.");
2965 }
2966 DEBUG("UNLOCK mutex %s", __func__);
2967 pthread_mutex_unlock(&locked_session->lock);
2968 DEBUG("UNLOCK wrlock %s", __func__);
2969 if (pthread_rwlock_unlock(&session_lock) != 0) {
2970 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2971 }
2972 } else {
2973 DEBUG("UNLOCK wrlock %s", __func__);
2974 if (pthread_rwlock_unlock(&session_lock) != 0) {
2975 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2976 }
2977 reply = create_error_reply("Invalid session identifier.");
2978 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002979
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002980 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
2981 reply = locked_session->hello_message;
2982 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002983
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002984 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002985}
2986
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002987void
Michal Vaskof35ea502016-02-24 10:44:54 +01002988notification_history(struct nc_session *session, const struct nc_notif *notif)
Tomas Cejka6b886e02013-07-05 09:53:17 +02002989{
Michal Vaskof35ea502016-02-24 10:44:54 +01002990 time_t eventtime;
2991 char *content;
2992 (void)session;
2993
2994 eventtime = nc_datetime2time(notif->datetime);
2995
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002996 json_object *notif_history_array = (json_object *)pthread_getspecific(notif_history_key);
2997 if (notif_history_array == NULL) {
2998 ERROR("No list of notification history found.");
2999 return;
3000 }
3001 DEBUG("Got notification from history %lu.", (long unsigned)eventtime);
3002 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01003003 json_object *notif_obj = json_object_new_object();
3004 if (notif_obj == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003005 ERROR("Could not allocate memory for notification (json).");
3006 goto failed;
3007 }
Michal Vaskof35ea502016-02-24 10:44:54 +01003008 lyd_print_mem(&content, notif->tree, LYD_JSON, 0);
3009
3010 json_object_object_add(notif_obj, "eventtime", json_object_new_int64(eventtime));
3011 json_object_object_add(notif_obj, "content", json_object_new_string(content));
3012
3013 free(content);
3014
3015 json_object_array_add(notif_history_array, notif_obj);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003016failed:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003017 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003018}
3019
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003020json_object *
3021handle_op_ntfgethistory(json_object *request, unsigned int session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003022{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003023 json_object *reply = NULL;
3024 json_object *js_tmp = NULL;
3025 struct session_with_mutex *locked_session = NULL;
3026 struct nc_session *temp_session = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003027 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003028 time_t start = 0;
3029 time_t stop = 0;
3030 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003031
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003032 DEBUG("Request: get notification history (session %u)", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003033
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003034 pthread_mutex_lock(&json_lock);
3035 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
3036 from = json_object_get_int64(js_tmp);
3037 }
3038 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
3039 to = json_object_get_int64(js_tmp);
3040 }
3041 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02003042
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003043 start = time(NULL) + from;
3044 stop = time(NULL) + to;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003045
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003046 DEBUG("notification history interval %li %li", (long int)from, (long int)to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003047
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003048 DEBUG("LOCK wrlock %s", __func__);
3049 if (pthread_rwlock_rdlock(&session_lock) != 0) {
3050 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3051 reply = create_error_reply("Internal lock failed.");
3052 goto finalize;
3053 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003054
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003055 for (locked_session = netconf_sessions_list;
3056 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003057 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003058 if (locked_session != NULL) {
3059 DEBUG("LOCK mutex %s", __func__);
3060 pthread_mutex_lock(&locked_session->lock);
3061 DEBUG("UNLOCK wrlock %s", __func__);
3062 if (pthread_rwlock_unlock(&session_lock) != 0) {
3063 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3064 }
3065 DEBUG("creating temporal NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01003066 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003067 if (temp_session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003068 rpc = nc_rpc_subscribe(NULL, NULL, nc_time2datetime(start, NULL), nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003069 if (rpc == NULL) {
3070 DEBUG("UNLOCK mutex %s", __func__);
3071 pthread_mutex_unlock(&locked_session->lock);
3072 DEBUG("notifications: creating an rpc request failed.");
3073 reply = create_error_reply("notifications: creating an rpc request failed.");
3074 goto finalize;
3075 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003076
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003077 DEBUG("Send NC subscribe.");
3078 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
3079 json_object *res = netconf_unlocked_op(temp_session, rpc);
3080 if (res != NULL) {
3081 DEBUG("UNLOCK mutex %s", __func__);
3082 pthread_mutex_unlock(&locked_session->lock);
3083 DEBUG("Subscription RPC failed.");
3084 reply = res;
3085 goto finalize;
3086 }
3087 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka6b886e02013-07-05 09:53:17 +02003088
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003089 DEBUG("UNLOCK mutex %s", __func__);
3090 pthread_mutex_unlock(&locked_session->lock);
3091 DEBUG("LOCK mutex %s", __func__);
3092 pthread_mutex_lock(&ntf_history_lock);
3093 pthread_mutex_lock(&json_lock);
3094 json_object *notif_history_array = json_object_new_array();
3095 pthread_mutex_unlock(&json_lock);
3096 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
3097 ERROR("notif_history: cannot set thread-specific hash value.");
3098 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003099
Michal Vaskof35ea502016-02-24 10:44:54 +01003100 nc_recv_notif_dispatch(temp_session, notification_history);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003101
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003102 pthread_mutex_lock(&json_lock);
3103 reply = json_object_new_object();
3104 json_object_object_add(reply, "notifications", notif_history_array);
3105 //json_object_put(notif_history_array);
3106 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003107
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003108 DEBUG("UNLOCK mutex %s", __func__);
3109 pthread_mutex_unlock(&ntf_history_lock);
3110 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01003111 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003112 temp_session = NULL;
3113 } else {
3114 DEBUG("UNLOCK mutex %s", __func__);
3115 pthread_mutex_unlock(&locked_session->lock);
3116 DEBUG("Get history of notification failed due to channel establishment");
3117 reply = create_error_reply("Get history of notification was unsuccessful, connection failed.");
3118 }
3119 } else {
3120 DEBUG("UNLOCK wrlock %s", __func__);
3121 if (pthread_rwlock_unlock(&session_lock) != 0) {
3122 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3123 }
3124 reply = create_error_reply("Invalid session identifier.");
3125 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003126
Tomas Cejka09629492014-07-10 15:58:06 +02003127finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003128 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02003129}
3130
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003131json_object *
3132handle_op_validate(json_object *request, unsigned int session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02003133{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003134 json_object *reply = NULL;
3135 char *target = NULL;
3136 char *url = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003137 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003138 NC_DATASTORE target_ds;
Tomas Cejka4003a702013-10-01 00:02:45 +02003139
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003140 DEBUG("Request: validate datastore (session %u)", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02003141
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003142 pthread_mutex_lock(&json_lock);
3143 target = get_param_string(request, "target");
3144 url = get_param_string(request, "url");
3145 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02003146
3147
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003148 if (target == NULL) {
3149 reply = create_error_reply("Missing target parameter.");
3150 goto finalize;
3151 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003152
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003153 /* validation */
3154 target_ds = parse_datastore(target);
Michal Vaskof35ea502016-02-24 10:44:54 +01003155 rpc = nc_rpc_validate(target_ds, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003156 if (rpc == NULL) {
3157 DEBUG("mod_netconf: creating rpc request failed");
3158 reply = create_error_reply("Creation of RPC request failed.");
3159 goto finalize;
3160 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003161
Michal Vaskof35ea502016-02-24 10:44:54 +01003162 if ((reply = netconf_op(session_key, rpc, 0, NULL)) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003163 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003164
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003165 if (reply == NULL) {
3166 DEBUG("Request: validation ok.");
3167 reply = create_ok_reply();
3168 }
3169 }
3170 nc_rpc_free (rpc);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003171
Tomas Cejka09629492014-07-10 15:58:06 +02003172finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003173 CHECK_AND_FREE(target);
3174 CHECK_AND_FREE(url);
3175 return reply;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003176}
3177
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003178json_object *
3179handle_op_query(json_object *request, unsigned int session_key, int idx)
David Kupka8e60a372012-09-04 09:15:20 +02003180{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003181 json_object *reply = NULL, *filters, *obj;
3182 char *filter = NULL;
3183 int load_children = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003184
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003185 DEBUG("Request: query (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003186
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003187 pthread_mutex_lock(&json_lock);
3188 if (json_object_object_get_ex(request, "filters", &filters) == FALSE) {
3189 pthread_mutex_unlock(&json_lock);
3190 reply = create_error_reply("Missing filters parameter.");
3191 goto finalize;
3192 }
3193 obj = json_object_array_get_idx(filters, idx);
3194 if (!obj) {
3195 pthread_mutex_unlock(&json_lock);
3196 reply = create_error_reply("Filters array parameter shorter than sessions.");
3197 goto finalize;
3198 }
3199 filter = strdup(json_object_get_string(obj));
3200 if (json_object_object_get_ex(request, "load_children", &obj) == TRUE) {
3201 load_children = json_object_get_boolean(obj);
3202 }
3203 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02003204
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003205 reply = libyang_query(session_key, filter, load_children);
David Kupka8e60a372012-09-04 09:15:20 +02003206
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003207 CHECK_ERR_SET_REPLY
3208 if (!reply) {
3209 reply = create_error_reply("Query failed.");
3210 }
David Kupka8e60a372012-09-04 09:15:20 +02003211
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003212finalize:
3213 CHECK_AND_FREE(filter);
3214 return reply;
3215}
David Kupka8e60a372012-09-04 09:15:20 +02003216
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003217json_object *
3218handle_op_merge(json_object *request, unsigned int session_key, int idx)
3219{
3220 json_object *reply = NULL, *configs, *obj;
3221 char *config = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003222 struct lyd_node *content;
3223 struct session_with_mutex *locked_session;
David Kupka8e60a372012-09-04 09:15:20 +02003224
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003225 DEBUG("Request: merge (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003226
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003227 pthread_mutex_lock(&json_lock);
3228 if (json_object_object_get_ex(request, "configurations", &configs) == FALSE) {
3229 pthread_mutex_unlock(&json_lock);
3230 reply = create_error_reply("Missing configurations parameter.");
3231 goto finalize;
3232 }
3233 obj = json_object_array_get_idx(configs, idx);
3234 if (!obj) {
3235 pthread_mutex_unlock(&json_lock);
3236 reply = create_error_reply("Filters array parameter shorter than sessions.");
3237 goto finalize;
3238 }
3239 config = strdup(json_object_get_string(obj));
3240 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02003241
Michal Vaskof35ea502016-02-24 10:44:54 +01003242 locked_session = session_get_locked(session_key, NULL);
3243 if (!locked_session) {
3244 ERROR("Unknown session or locking failed.");
3245 goto finalize;
3246 }
3247
3248 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_DATA);
3249 session_unlock(locked_session);
3250
3251 free(config);
3252 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
3253 lyd_free_withsiblings(content);
3254
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003255 reply = libyang_merge(session_key, config);
David Kupka8e60a372012-09-04 09:15:20 +02003256
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003257 CHECK_ERR_SET_REPLY
3258 if (!reply) {
3259 reply = create_error_reply("Merge failed.");
3260 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003261
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003262finalize:
3263 CHECK_AND_FREE(config);
3264 return reply;
3265}
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003266
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003267void *
3268thread_routine(void *arg)
3269{
3270 void *retval = NULL;
3271 struct pollfd fds;
3272 json_object *request = NULL, *replies = NULL, *reply, *sessions = NULL;
3273 json_object *js_tmp = NULL;
Michal Vaskodf280a22016-03-17 09:19:53 +01003274 int operation = (-1), count, i, sent;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003275 int status = 0;
3276 const char *msgtext;
3277 unsigned int session_key = 0;
3278 char *chunked_out_msg = NULL;
3279 int client = ((struct pass_to_thread *)arg)->client;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003280
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003281 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02003282
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003283 /* init thread specific err_reply memory */
3284 create_err_reply_p();
David Kupka8e60a372012-09-04 09:15:20 +02003285
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003286 while (!isterminated) {
3287 fds.fd = client;
3288 fds.events = POLLIN;
3289 fds.revents = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003290
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003291 status = poll(&fds, 1, 1000);
Tomas Cejkad5b53772013-06-08 23:01:07 +02003292
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003293 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
3294 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
3295 continue;
3296 } else if (status < 0) {
3297 /* 0: poll time outed
3298 * close socket and ignore this request from the client, it can try it again
3299 * -1: poll failed
3300 * something wrong happend, close this socket and wait for another request
3301 */
3302 close(client);
3303 break;
3304 }
3305 /* status > 0 */
David Kupka8e60a372012-09-04 09:15:20 +02003306
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003307 /* check the status of the socket */
David Kupka8e60a372012-09-04 09:15:20 +02003308
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003309 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
3310 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
3311 /* close client's socket (it's probably already closed by client */
3312 close(client);
3313 break;
3314 }
Tomas Cejka09629492014-07-10 15:58:06 +02003315
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003316 DEBUG("Get framed message...");
3317 buffer = get_framed_message(client);
Tomas Cejka09629492014-07-10 15:58:06 +02003318
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003319 DEBUG("Check read buffer.");
3320 if (buffer != NULL) {
3321 enum json_tokener_error jerr;
3322 pthread_mutex_lock(&json_lock);
3323 request = json_tokener_parse_verbose(buffer, &jerr);
3324 if (jerr != json_tokener_success) {
3325 ERROR("JSON parsing error");
3326 pthread_mutex_unlock(&json_lock);
3327 continue;
3328 }
3329
3330 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
3331 operation = json_object_get_int(js_tmp);
3332 }
3333 pthread_mutex_unlock(&json_lock);
3334 if (operation == -1) {
3335 replies = create_replies();
3336 add_reply(replies, create_error_reply("Missing operation type from frontend."), 0);
3337 goto send_reply;
3338 }
3339
3340 if ((operation < 4) || ((operation > 19) && (operation < 100)) || (operation > 101)) {
3341 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
3342 replies = create_replies();
3343 add_reply(replies, create_error_reply("Operation not supported."), 0);
3344 goto send_reply;
3345 }
3346
3347 DEBUG("operation %d", operation);
3348
3349 /* null global JSON error-reply */
3350 clean_err_reply();
3351
3352 /* clean replies envelope */
3353 if (replies != NULL) {
3354 pthread_mutex_lock(&json_lock);
3355 json_object_put(replies);
3356 pthread_mutex_unlock(&json_lock);
3357 }
3358 replies = create_replies();
3359
3360 if (operation == MSG_CONNECT) {
3361 count = 1;
3362 } else {
3363 pthread_mutex_lock(&json_lock);
3364 if (json_object_object_get_ex(request, "sessions", &sessions) == FALSE) {
Michal Vasko642cad02016-03-17 12:31:18 +01003365 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003366 add_reply(replies, create_error_reply("Operation missing \"sessions\" arg"), 0);
3367 goto send_reply;
3368 }
3369 count = json_object_array_length(sessions);
3370 pthread_mutex_unlock(&json_lock);
3371 }
3372
3373 for (i = 0; i < count; ++i) {
3374 if (operation != MSG_CONNECT) {
3375 js_tmp = json_object_array_get_idx(sessions, i);
3376 session_key = json_object_get_int(js_tmp);
3377 }
3378
3379 /* process required operation */
Michal Vasko977bad92016-03-15 16:20:51 +01003380 reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003381 switch (operation) {
3382 case MSG_CONNECT:
3383 reply = handle_op_connect(request);
3384 break;
3385 case MSG_DISCONNECT:
3386 reply = handle_op_disconnect(request, session_key);
3387 break;
3388 case MSG_GET:
3389 reply = handle_op_get(request, session_key);
3390 break;
3391 case MSG_GETCONFIG:
3392 reply = handle_op_getconfig(request, session_key);
3393 break;
3394 case MSG_EDITCONFIG:
3395 reply = handle_op_editconfig(request, session_key, i);
3396 break;
3397 case MSG_COPYCONFIG:
3398 reply = handle_op_copyconfig(request, session_key, i);
3399 break;
3400 case MSG_DELETECONFIG:
3401 reply = handle_op_deleteconfig(request, session_key);
3402 break;
3403 case MSG_LOCK:
3404 reply = handle_op_lock(request, session_key);
3405 break;
3406 case MSG_UNLOCK:
3407 reply = handle_op_unlock(request, session_key);
3408 break;
3409 case MSG_KILL:
3410 reply = handle_op_kill(request, session_key);
3411 break;
3412 case MSG_INFO:
3413 reply = handle_op_info(request, session_key);
3414 break;
3415 case MSG_GENERIC:
3416 reply = handle_op_generic(request, session_key, i);
3417 break;
3418 case MSG_GETSCHEMA:
3419 reply = handle_op_getschema(request, session_key);
3420 break;
3421 case MSG_RELOADHELLO:
3422 reply = handle_op_reloadhello(request, session_key);
3423 break;
3424 case MSG_NTF_GETHISTORY:
3425 reply = handle_op_ntfgethistory(request, session_key);
3426 break;
3427 case MSG_VALIDATE:
3428 reply = handle_op_validate(request, session_key);
3429 break;
3430 case SCH_QUERY:
3431 reply = handle_op_query(request, session_key, i);
3432 break;
3433 case SCH_MERGE:
3434 reply = handle_op_merge(request, session_key, i);
3435 break;
3436 }
3437
3438 add_reply(replies, reply, session_key);
3439 }
3440
3441 /* free parameters */
3442 operation = (-1);
3443
3444 DEBUG("Clean request json object.");
3445 if (request != NULL) {
3446 pthread_mutex_lock(&json_lock);
3447 json_object_put(request);
3448 pthread_mutex_unlock(&json_lock);
Michal Vasko365dc4c2016-03-17 10:03:58 +01003449 request = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003450 }
3451 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02003452
3453send_reply:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003454 /* send reply to caller */
3455 if (replies) {
3456 pthread_mutex_lock(&json_lock);
3457 msgtext = json_object_to_json_string(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003458 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003459 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
3460 if (buffer != NULL) {
3461 free(buffer);
3462 buffer = NULL;
3463 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003464 break;
3465 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003466
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003467 DEBUG("Send framed reply json object.");
Michal Vaskodf280a22016-03-17 09:19:53 +01003468 i = 0;
3469 sent = 0;
3470 count = strlen(chunked_out_msg) + 1;
3471 while (count && ((i = send(client, chunked_out_msg + sent, count, 0)) != -1)) {
3472 sent += i;
3473 count -= i;
3474 }
3475 if (i == -1) {
3476 ERROR("Sending message failed (%s).", strerror(errno));
3477 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003478 DEBUG("Clean reply json object.");
3479 pthread_mutex_lock(&json_lock);
3480 json_object_put(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003481 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003482 replies = NULL;
3483 DEBUG("Clean message buffer.");
3484 CHECK_AND_FREE(chunked_out_msg);
3485 chunked_out_msg = NULL;
3486 if (buffer) {
3487 free(buffer);
3488 buffer = NULL;
3489 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003490 clean_err_reply();
3491 } else {
3492 ERROR("Reply is NULL, shouldn't be...");
3493 continue;
3494 }
3495 }
3496 }
3497 free(arg);
3498 free_err_reply();
Michal Vaskod0205992016-03-17 10:04:49 +01003499 nc_thread_destroy();
David Kupka8e60a372012-09-04 09:15:20 +02003500
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003501 return retval;
David Kupka8e60a372012-09-04 09:15:20 +02003502}
3503
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003504/**
3505 * \brief Close all open NETCONF sessions.
3506 *
3507 * During termination of mod_netconf, it is useful to close all remaining
3508 * sessions. This function iterates over the list of sessions and close them
3509 * all.
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003510 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003511static void
3512close_all_nc_sessions(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003513{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003514 struct session_with_mutex *locked_session, *next_session;
3515 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003516
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003517 /* get exclusive access to sessions_list (conns) */
3518 DEBUG("LOCK wrlock %s", __func__);
3519 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
3520 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3521 return;
3522 }
3523 for (next_session = netconf_sessions_list; next_session;) {
3524 locked_session = next_session;
3525 next_session = locked_session->next;
Tomas Cejka47387fd2013-06-10 20:37:46 +02003526
Michal Vaskoc3146782015-11-04 14:46:41 +01003527 /* close_and_free_session handles locking on its own */
Michal Vaskof35ea502016-02-24 10:44:54 +01003528 DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003529 close_and_free_session(locked_session);
3530 }
3531 netconf_sessions_list = NULL;
3532
3533 /* get exclusive access to sessions_list (conns) */
3534 DEBUG("UNLOCK wrlock %s", __func__);
3535 if (pthread_rwlock_unlock (&session_lock) != 0) {
3536 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3537 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003538}
3539
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003540static void
3541check_timeout_and_close(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003542{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003543 struct nc_session *ns = NULL;
3544 struct session_with_mutex *locked_session = NULL;
3545 time_t current_time = time(NULL);
3546 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003547
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003548 /* get exclusive access to sessions_list (conns) */
3549 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
3550 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3551 return;
3552 }
3553 for (locked_session = netconf_sessions_list; locked_session; locked_session = locked_session->next) {
3554 ns = locked_session->session;
3555 if (ns == NULL) {
3556 continue;
3557 }
3558 pthread_mutex_lock(&locked_session->lock);
3559 if ((current_time - locked_session->last_activity) > ACTIVITY_TIMEOUT) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003560 DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session));
Tomas Cejka47387fd2013-06-10 20:37:46 +02003561
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003562 /* close_and_free_session handles locking on its own */
3563 close_and_free_session(locked_session);
3564 } else {
3565 pthread_mutex_unlock(&locked_session->lock);
3566 }
3567 }
3568 /* get exclusive access to sessions_list (conns) */
3569 if (pthread_rwlock_unlock(&session_lock) != 0) {
3570 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3571 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003572}
3573
3574
3575/**
Radek Krejcif23850c2012-07-23 16:14:17 +02003576 * This is actually implementation of NETCONF client
3577 * - requests are received from UNIX socket in the predefined format
3578 * - results are replied through the same way
Michal Vaskoc3146782015-11-04 14:46:41 +01003579 * - the daemon run as a separate process
Radek Krejcif23850c2012-07-23 16:14:17 +02003580 *
3581 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003582static void
3583forked_proc(void)
Radek Krejci469aab82012-07-22 18:42:20 +02003584{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003585 struct timeval tv;
3586 struct sockaddr_un local, remote;
3587 int lsock, client, ret, i, pthread_count = 0;
3588 unsigned int olds = 0, timediff = 0;
3589 socklen_t len;
3590 struct pass_to_thread *arg;
3591 pthread_t *ptids = calloc(1, sizeof(pthread_t));
3592 struct timespec maxtime;
3593 pthread_rwlockattr_t lock_attrs;
3594 #ifdef WITH_NOTIFICATIONS
3595 char use_notifications = 0;
3596 #endif
David Kupka8e60a372012-09-04 09:15:20 +02003597
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003598 /* wait at most 5 seconds for every thread to terminate */
3599 maxtime.tv_sec = 5;
3600 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02003601
Tomas Cejka04e08f42014-03-27 19:52:34 +01003602#ifdef HAVE_UNIXD_SETUP_CHILD
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003603 /* change uid and gid of process for security reasons */
3604 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01003605#else
3606# ifdef SU_GROUP
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003607 if (strlen(SU_GROUP) > 0) {
3608 struct group *g = getgrnam(SU_GROUP);
3609 if (g == NULL) {
3610 ERROR("GID (%s) was not found.", SU_GROUP);
3611 return;
3612 }
3613 if (setgid(g->gr_gid) != 0) {
3614 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
3615 return;
3616 }
3617 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003618# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003619 DEBUG("no SU_GROUP");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003620# endif
3621# ifdef SU_USER
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003622 if (strlen(SU_USER) > 0) {
3623 struct passwd *p = getpwnam(SU_USER);
3624 if (p == NULL) {
3625 ERROR("UID (%s) was not found.", SU_USER);
3626 return;
3627 }
3628 if (setuid(p->pw_uid) != 0) {
3629 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
3630 return;
3631 }
3632 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003633# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003634 DEBUG("no SU_USER");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003635# endif
3636#endif
Radek Krejci469aab82012-07-22 18:42:20 +02003637
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003638 /* try to remove if exists */
3639 unlink(sockname);
Tomas Cejka04e08f42014-03-27 19:52:34 +01003640
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003641 /* create listening UNIX socket to accept incoming connections */
3642 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
3643 ERROR("Creating socket failed (%s)", strerror(errno));
3644 goto error_exit;
3645 }
Radek Krejci469aab82012-07-22 18:42:20 +02003646
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003647 local.sun_family = AF_UNIX;
3648 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
3649 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
Radek Krejci469aab82012-07-22 18:42:20 +02003650
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003651 if (bind(lsock, (struct sockaddr *)&local, len) == -1) {
3652 if (errno == EADDRINUSE) {
3653 ERROR("mod_netconf socket address already in use");
3654 goto error_exit;
3655 }
3656 ERROR("Binding socket failed (%s)", strerror(errno));
3657 goto error_exit;
3658 }
Radek Krejci469aab82012-07-22 18:42:20 +02003659
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003660 if (listen(lsock, MAX_SOCKET_CL) == -1) {
3661 ERROR("Setting up listen socket failed (%s)", strerror(errno));
3662 goto error_exit;
3663 }
3664 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02003665
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003666 uid_t user = -1;
3667 if (strlen(CHOWN_USER) > 0) {
3668 struct passwd *p = getpwnam(CHOWN_USER);
3669 if (p != NULL) {
3670 user = p->pw_uid;
3671 }
3672 }
3673 gid_t group = -1;
3674 if (strlen(CHOWN_GROUP) > 0) {
3675 struct group *g = getgrnam(CHOWN_GROUP);
3676 if (g != NULL) {
3677 group = g->gr_gid;
3678 }
3679 }
3680 if (chown(sockname, user, group) == -1) {
3681 ERROR("Chown on socket file failed (%s).", strerror(errno));
3682 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003683
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003684 /* prepare internal lists */
Tomas Cejkaba21b382013-04-13 02:37:32 +02003685
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003686 #ifdef WITH_NOTIFICATIONS
3687 if (notification_init() == -1) {
3688 ERROR("libwebsockets initialization failed");
3689 use_notifications = 0;
3690 } else {
3691 use_notifications = 1;
3692 }
3693 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003694
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003695 /* setup libnetconf's callbacks */
Michal Vaskod0205992016-03-17 10:04:49 +01003696 nc_client_init();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003697 nc_verbosity(NC_VERB_DEBUG);
Michal Vaskof35ea502016-02-24 10:44:54 +01003698 nc_set_print_clb(clb_print);
3699 nc_client_ssh_set_auth_hostkey_check_clb(netconf_callback_ssh_hostkey_check);
3700 nc_client_ssh_set_auth_interactive_clb(netconf_callback_sshauth_interactive);
3701 nc_client_ssh_set_auth_password_clb(netconf_callback_sshauth_password);
3702 nc_client_ssh_set_auth_privkey_passphrase_clb(netconf_callback_sshauth_passphrase);
Radek Krejci469aab82012-07-22 18:42:20 +02003703
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003704 /* disable publickey authentication */
Michal Vaskof35ea502016-02-24 10:44:54 +01003705 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1);
Radek Krejci469aab82012-07-22 18:42:20 +02003706
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003707 /* create mutex protecting session list */
3708 pthread_rwlockattr_init(&lock_attrs);
3709 /* rwlock is shared only with threads in this process */
3710 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
3711 /* create rw lock */
3712 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
3713 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
3714 goto error_exit;
3715 }
3716 pthread_mutex_init(&ntf_history_lock, NULL);
3717 pthread_mutex_init(&json_lock, NULL);
3718 DEBUG("Initialization of notification history.");
3719 if (pthread_key_create(&notif_history_key, NULL) != 0) {
3720 ERROR("Initialization of notification history failed.");
3721 }
3722 if (pthread_key_create(&err_reply_key, NULL) != 0) {
3723 ERROR("Initialization of reply key failed.");
3724 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02003725
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003726 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
3727 while (isterminated == 0) {
3728 gettimeofday(&tv, NULL);
3729 timediff = (unsigned int)tv.tv_sec - olds;
3730 #ifdef WITH_NOTIFICATIONS
3731 if (use_notifications == 1) {
3732 notification_handle();
3733 }
3734 #endif
3735 if (timediff > ACTIVITY_CHECK_INTERVAL) {
3736 check_timeout_and_close();
3737 }
Radek Krejci469aab82012-07-22 18:42:20 +02003738
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003739 /* open incoming connection if any */
3740 len = sizeof(remote);
3741 client = accept(lsock, (struct sockaddr *) &remote, &len);
3742 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
3743 usleep(SLEEP_TIME * 1000);
3744 continue;
3745 } else if (client == -1 && (errno == EINTR)) {
3746 continue;
3747 } else if (client == -1) {
3748 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
3749 continue;
3750 }
Radek Krejci469aab82012-07-22 18:42:20 +02003751
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003752 /* set client's socket as non-blocking */
3753 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02003754
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003755 arg = malloc(sizeof(struct pass_to_thread));
3756 arg->client = client;
3757 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02003758
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003759 /* start new thread. It will serve this particular request and then terminate */
3760 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void *)arg)) != 0) {
3761 ERROR("Creating POSIX thread failed: %d\n", ret);
3762 } else {
3763 DEBUG("Thread %lu created", ptids[pthread_count]);
3764 pthread_count++;
3765 ptids = realloc (ptids, sizeof(pthread_t) * (pthread_count+1));
3766 ptids[pthread_count] = 0;
3767 }
Radek Krejci469aab82012-07-22 18:42:20 +02003768
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003769 /* check if some thread already terminated, free some resources by joining it */
3770 for (i = 0; i < pthread_count; i++) {
3771 if (pthread_tryjoin_np(ptids[i], (void **)&arg) == 0) {
3772 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
3773 pthread_count--;
3774 if (pthread_count > 0) {
3775 /* place last Thread ID on the place of joined one */
3776 ptids[i] = ptids[pthread_count];
3777 }
3778 }
3779 }
3780 DEBUG("Running %d threads", pthread_count);
3781 }
Radek Krejci469aab82012-07-22 18:42:20 +02003782
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003783 DEBUG("mod_netconf terminating...");
3784 /* join all threads */
3785 for (i = 0; i < pthread_count; i++) {
3786 pthread_timedjoin_np(ptids[i], (void **)&arg, &maxtime);
3787 }
Radek Krejci469aab82012-07-22 18:42:20 +02003788
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003789 #ifdef WITH_NOTIFICATIONS
3790 notification_close();
3791 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003792
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003793 /* close all NETCONF sessions */
3794 close_all_nc_sessions();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003795
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003796 /* destroy rwlock */
3797 pthread_rwlock_destroy(&session_lock);
3798 pthread_rwlockattr_destroy(&lock_attrs);
David Kupka8e60a372012-09-04 09:15:20 +02003799
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003800 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02003801
Michal Vaskod0205992016-03-17 10:04:49 +01003802 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003803 free(ptids);
3804 close(lsock);
3805 exit(0);
3806 return;
3807
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003808error_exit:
Michal Vaskod0205992016-03-17 10:04:49 +01003809 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003810 close(lsock);
3811 free(ptids);
3812 return;
Radek Krejci469aab82012-07-22 18:42:20 +02003813}
3814
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003815int
3816main(int argc, char **argv)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003817{
Michal Vaskoc3146782015-11-04 14:46:41 +01003818 struct sigaction action;
3819 sigset_t block_mask;
Michal Vaskoa53ef882015-11-24 11:02:01 +01003820 int daemonize = 0, i;
Michal Vaskoc3146782015-11-04 14:46:41 +01003821
Michal Vaskoa53ef882015-11-24 11:02:01 +01003822 if (argc > 3) {
3823 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3824 return 1;
3825 }
3826
3827 sockname = SOCKET_FILENAME;
3828 for (i = 1; i < argc; ++i) {
3829 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
3830 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3831 return 0;
3832 } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) {
3833 daemonize = 1;
3834 } else {
3835 sockname = argv[i];
3836 }
3837 }
3838
3839 if (daemonize && (daemon(0, 0) == -1)) {
3840 ERROR("daemon() failed (%s)", strerror(errno));
3841 return 1;
Michal Vaskoc3146782015-11-04 14:46:41 +01003842 }
3843
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003844 sigfillset(&block_mask);
Michal Vaskoc3146782015-11-04 14:46:41 +01003845 action.sa_handler = signal_handler;
3846 action.sa_mask = block_mask;
3847 action.sa_flags = 0;
3848 sigaction(SIGINT, &action, NULL);
3849 sigaction(SIGTERM, &action, NULL);
3850
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003851 forked_proc();
3852 DEBUG("Terminated");
3853 return 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003854}