blob: 21aaac3231e6a6816400a55fd982f62006ea3b27 [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>
Radek Krejci469aab82012-07-22 18:42:20 +020053#include <sys/types.h>
54#include <sys/socket.h>
55#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010056#include <sys/fcntl.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
Radek Krejci469aab82012-07-22 18:42:20 +020066#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020067#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020068
Tomas Cejka04e08f42014-03-27 19:52:34 +010069#include "../config.h"
70
Tomas Cejkad340dbf2013-03-24 20:36:57 +010071#ifdef WITH_NOTIFICATIONS
Michal Vaskoa53ef882015-11-24 11:02:01 +010072#include "notification_server.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010073#endif
74
Tomas Cejka94da2c52013-01-08 18:20:30 +010075#include "message_type.h"
Michal Vaskoa53ef882015-11-24 11:02:01 +010076#include "netopeerguid.h"
Radek Krejci469aab82012-07-22 18:42:20 +020077
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010078#define SCHEMA_DIR "/tmp/yang_models"
Radek Krejci469aab82012-07-22 18:42:20 +020079#define MAX_PROCS 5
Michal Vaskoa53ef882015-11-24 11:02:01 +010080#define SOCKET_FILENAME "/var/run/netopeerguid.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020081#define MAX_SOCKET_CL 10
82#define BUFFER_SIZE 4096
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010083#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
84#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020085
Michal Vasko7732dee2015-11-05 10:22:15 +010086/* sleep in master process for non-blocking socket reading, in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020087#define SLEEP_TIME 200
88
89#ifndef offsetof
90#define offsetof(type, member) ((size_t) ((type *) 0)->member)
91#endif
92
Tomas Cejka027f3bc2012-11-10 20:28:36 +010093/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020094struct timeval timeout = { 1, 0 };
95
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010096#define NCWITHDEFAULTS NCWD_MODE_NOTSET
Tomas Cejka5064c232013-01-17 09:30:58 +010097
Radek Krejci469aab82012-07-22 18:42:20 +020098#define MSG_OK 0
99#define MSG_OPEN 1
100#define MSG_DATA 2
101#define MSG_CLOSE 3
102#define MSG_ERROR 4
103#define MSG_UNKNOWN 5
104
Tomas Cejka47387fd2013-06-10 20:37:46 +0200105pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200106pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100107pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100108pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
109
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100110unsigned int session_key_generator = 1;
Michal Vaskoc3146782015-11-04 14:46:41 +0100111struct session_with_mutex *netconf_sessions_list = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100112static const char *sockname;
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200113static pthread_key_t notif_history_key;
Tomas Cejka442258e2014-04-01 18:17:18 +0200114pthread_key_t err_reply_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200115volatile int isterminated = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200116static char* password;
117
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100118json_object *create_ok_reply(void);
119json_object *create_data_reply(const char *data);
120static char *netconf_getschema(unsigned int session_key, const char *identifier, const char *version,
121 const char *format, json_object **err);
122static void node_add_metadata_recursive(struct lyd_node *data_tree, struct lys_module *module,
123 json_object *data_json_parent);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100124static void node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100125
126static void
127signal_handler(int sign)
Radek Krejci469aab82012-07-22 18:42:20 +0200128{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100129 switch (sign) {
130 case SIGINT:
131 case SIGTERM:
132 isterminated = 1;
133 break;
134 }
Radek Krejci469aab82012-07-22 18:42:20 +0200135}
136
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100137int
138netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session))
Radek Krejci469aab82012-07-22 18:42:20 +0200139{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100140 /* always approve */
141 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200142}
143
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100144char *
145netconf_callback_sshauth_passphrase(const char *UNUSED(username), const char *UNUSED(hostname), const char *UNUSED(priv_key_file))
Radek Krejci469aab82012-07-22 18:42:20 +0200146{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100147 char *buf;
148 buf = strdup(password);
149 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200150}
151
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100152char *
153netconf_callback_sshauth_password(const char *UNUSED(username), const char *UNUSED(hostname))
Radek Krejci469aab82012-07-22 18:42:20 +0200154{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100155 char *buf;
156 buf = strdup(password);
157 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200158}
159
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100160char *
161netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction),
162 const char *UNUSED(prompt), int UNUSED(echo))
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200163{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100164 char *buf;
165 buf = strdup(password);
166 return (buf);
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200167}
168
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100169void
170netconf_callback_error_process(const char *UNUSED(tag),
171 const char *UNUSED(type),
172 const char *UNUSED(severity),
173 const char *UNUSED(apptag),
174 const char *UNUSED(path),
175 const char *message,
176 const char *UNUSED(attribute),
177 const char *UNUSED(element),
178 const char *UNUSED(ns),
179 const char *UNUSED(sid))
Radek Krejcic11fd862012-07-26 12:41:21 +0200180{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100181 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
182 if (err_reply_p == NULL) {
183 ERROR("Error message was not allocated. %s", __func__);
184 return;
185 }
186 json_object *err_reply = *err_reply_p;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100187
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100188 json_object *array = NULL;
189 if (err_reply == NULL) {
190 ERROR("error calback: empty error list");
191 pthread_mutex_lock(&json_lock);
192 err_reply = json_object_new_object();
193 array = json_object_new_array();
194 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
195 json_object_object_add(err_reply, "errors", array);
196 if (message != NULL) {
197 json_object_array_add(array, json_object_new_string(message));
198 }
199 pthread_mutex_unlock(&json_lock);
200 (*err_reply_p) = err_reply;
201 } else {
202 ERROR("error calback: nonempty error list");
203 pthread_mutex_lock(&json_lock);
204 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
205 if (message != NULL) {
206 json_object_array_add(array, json_object_new_string(message));
207 }
208 }
209 pthread_mutex_unlock(&json_lock);
210 }
211 pthread_setspecific(err_reply_key, err_reply_p);
212 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200213}
214
Tomas Cejka47387fd2013-06-10 20:37:46 +0200215/**
216 * should be used in locked area
217 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100218void
219prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200220{
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;
225 const char *cpbltstr;
226 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200227
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100228 if (s == NULL) {
229 ERROR("No session given.");
230 return;
231 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200232
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100233 pthread_mutex_lock(&json_lock);
234 if (s->hello_message != NULL) {
235 ERROR("clean previous hello message");
236 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
237 j_old_sid = json_object_get_string(js_tmp);
238 if (j_old_sid != NULL) {
239 old_sid = strdup(j_old_sid);
240 }
241 }
242 json_object_put(s->hello_message);
243 s->hello_message = NULL;
244 }
245 s->hello_message = json_object_new_object();
246 if (session != NULL) {
247 if (old_sid != NULL) {
248 /* use previous sid */
249 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
250 free(old_sid);
251 old_sid = NULL;
252 } else {
253 /* we don't have old sid */
254 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
255 }
256 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
257 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
258 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
259 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
260 cpblts = nc_session_get_cpblts (session);
261 if (cpblts != NULL) {
262 json_obj = json_object_new_array();
263 nc_cpblts_iter_start (cpblts);
264 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
265 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
266 }
267 json_object_object_add(s->hello_message, "capabilities", json_obj);
268 }
269 DEBUG("%s", json_object_to_json_string(s->hello_message));
270 } else {
271 ERROR("Session was not given.");
272 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
273 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
274 }
275 DEBUG("Status info from hello message prepared");
276 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200277
278}
279
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100280void
281create_err_reply_p()
Tomas Cejka442258e2014-04-01 18:17:18 +0200282{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100283 json_object **err_reply = calloc(1, sizeof(json_object **));
284 if (err_reply == NULL) {
285 ERROR("Allocation of err_reply storage failed!");
286 return;
287 }
288 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
289 ERROR("cannot set thread-specific value.");
290 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200291}
292
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100293void
294clean_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200295{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100296 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
297 if (err_reply != NULL) {
298 if (*err_reply != NULL) {
299 pthread_mutex_lock(&json_lock);
300 json_object_put(*err_reply);
301 pthread_mutex_unlock(&json_lock);
302 }
303 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
304 ERROR("Cannot set thread-specific hash value.");
305 }
306 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200307}
308
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100309void
310free_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200311{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100312 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
313 if (err_reply != NULL) {
314 if (*err_reply != NULL) {
315 pthread_mutex_lock(&json_lock);
316 json_object_put(*err_reply);
317 pthread_mutex_unlock(&json_lock);
318 }
319 free(err_reply);
320 err_reply = NULL;
321 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
322 ERROR("Cannot set thread-specific hash value.");
323 }
324 }
325}
326
327static struct session_with_mutex *
328session_get_locked(unsigned int session_key, json_object **err)
329{
330 struct session_with_mutex *locked_session;
331
332 /* get non-exclusive (read) access to sessions_list (conns) */
333 DEBUG("LOCK wrlock %s", __func__);
334 if (pthread_rwlock_rdlock(&session_lock) != 0) {
335 if (*err) {
336 *err = create_error_reply("Locking failed.");
337 }
338 return NULL;
339 }
340 /* get session where send the RPC */
341 for (locked_session = netconf_sessions_list;
342 locked_session && (locked_session->session_key != session_key);
343 locked_session = locked_session->next);
344 if (!locked_session) {
345 if (*err) {
346 *err = create_error_reply("Session not found.");
347 }
348 return NULL;
349 }
350
351 /* get exclusive access to session */
352 DEBUG("LOCK mutex %s", __func__);
353 if (pthread_mutex_lock(&locked_session->lock) != 0) {
354 if (*err) {
355 *err = create_error_reply("Locking failed.");
356 }
357 goto wrlock_fail;
358 }
359 return locked_session;
360
361wrlock_fail:
362 DEBUG("UNLOCK wrlock %s", __func__);
363 pthread_rwlock_unlock(&session_lock);
364 return NULL;
365}
366
367static void
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100368session_user_activity(const char *username)
369{
370 struct session_with_mutex *sess;
371
372 for (sess = netconf_sessions_list; sess; sess = sess->next) {
373 if (!strcmp(nc_session_get_user(sess->session), username)) {
374 sess->last_activity = time(NULL);
375 }
376 }
377}
378
379static void
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100380session_unlock(struct session_with_mutex *locked_session)
381{
382 DEBUG("UNLOCK mutex %s", __func__);
383 pthread_mutex_unlock(&locked_session->lock);
384 DEBUG("UNLOCK wrlock %s", __func__);
385 pthread_rwlock_unlock(&session_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +0200386}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200387
Michal Vasko3fda9a92015-11-23 10:10:57 +0100388static void
389node_metadata_text(const char *text, const char *name, json_object *parent)
390{
391 json_object *obj;
392
393 if (!text) {
394 return;
395 }
396
397 obj = json_object_new_string(text);
398 json_object_object_add(parent, name, obj);
399}
400
401static void
402node_metadata_restr(struct lys_restr *restr, const char *name, json_object *parent)
403{
404 json_object *obj;
405
406 if (!restr) {
407 return;
408 }
409
410 obj = json_object_new_string(restr->expr);
411 json_object_object_add(parent, name, obj);
412}
413
414static void
415node_metadata_must(uint8_t must_size, struct lys_restr *must, json_object *parent)
416{
417 uint8_t i;
418 json_object *array, *obj;
419
420 if (!must_size || !must) {
421 return;
422 }
423
424 array = json_object_new_array();
425
426 for (i = 0; i < must_size; ++i) {
427 obj = json_object_new_string(must[i].expr);
428 json_object_array_add(array, obj);
429 }
430
431 json_object_object_add(parent, "must", array);
432}
433
434static void
435node_metadata_basic(struct lys_node *node, json_object *parent)
436{
437 json_object *obj;
438
439 /* description */
440 node_metadata_text(node->dsc, "description", parent);
441
442 /* reference */
443 node_metadata_text(node->ref, "reference", parent);
444
445 /* config */
446 if (node->flags & LYS_CONFIG_R) {
447 obj = json_object_new_boolean(0);
448 } else {
449 obj = json_object_new_boolean(1);
450 }
451 json_object_object_add(parent, "config", obj);
452
453 /* status */
454 if (node->flags & LYS_STATUS_DEPRC) {
455 obj = json_object_new_string("deprecated");
456 } else if (node->flags & LYS_STATUS_OBSLT) {
457 obj = json_object_new_string("obsolete");
458 } else {
459 obj = json_object_new_string("current");
460 }
461 json_object_object_add(parent, "status", obj);
462
463 /* mandatory */
464 if (node->flags & LYS_MAND_TRUE) {
465 obj = json_object_new_boolean(1);
466 } else {
467 obj = json_object_new_boolean(0);
468 }
469 json_object_object_add(parent, "mandatory", obj);
470
471 /* NACM extensions */
472 if (node->nacm) {
473 if (node->nacm & LYS_NACM_DENYW) {
474 obj = json_object_new_string("default-deny-write");
475 } else {
476 obj = json_object_new_string("default-deny-all");
477 }
478 json_object_object_add(parent, "ext", obj);
479 }
480}
481
482static void
483node_metadata_when(struct lys_when *when, json_object *parent)
484{
485 json_object *obj;
486
487 if (!when) {
488 return;
489 }
490
491 obj = json_object_new_string(when->cond);
492 json_object_object_add(parent, "when", obj);
493}
494
495static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100496node_metadata_children_recursive(struct lys_node *node, json_object **child_array, json_object **choice_array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100497{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100498 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100499 struct lys_node *child;
500
501 if (!node->child) {
502 return;
503 }
504
505 LY_TREE_FOR(node->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100506 if (child->nodetype == LYS_USES) {
507 node_metadata_children_recursive(child, child_array, choice_array);
508 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100509 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100510 if (!*child_array) {
511 *child_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100512 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100513 json_object_array_add(*child_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100514 } else if (child->nodetype == LYS_CHOICE) {
515 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100516 if (!*choice_array) {
517 *choice_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100518 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100519 json_object_array_add(*choice_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100520 }
521 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100522}
523
524static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100525node_metadata_cases_recursive(struct lys_node_choice *choice, json_object *array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100526{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100527 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100528 struct lys_node *child;
529
530 if (!choice->child) {
531 return;
532 }
533
Michal Vasko3fda9a92015-11-23 10:10:57 +0100534 LY_TREE_FOR(choice->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100535 if (child->nodetype == LYS_USES) {
536 node_metadata_cases_recursive((struct lys_node_choice *)child, array);
537 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100538 obj = json_object_new_string(child->name);
539 json_object_array_add(array, obj);
540 }
541 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100542}
543
544static void
545node_metadata_min_max(uint32_t min, uint32_t max, json_object *parent)
546{
547 json_object *obj;
548
549 if (min) {
550 obj = json_object_new_int(min);
551 json_object_object_add(parent, "min-elements", obj);
552 }
553
554 if (max) {
555 obj = json_object_new_int(max);
556 json_object_object_add(parent, "max-elements", obj);
557 }
558}
559
560static void
561node_metadata_ident_recursive(struct lys_ident *ident, json_object *array)
562{
563 struct lys_ident_der *cur;
564 json_object *obj;
565
566 if (!ident) {
567 return;
568 }
569
570 obj = json_object_new_string(ident->name);
571 json_object_array_add(array, obj);
572
573 for (cur = ident->der; cur; cur = cur->next) {
574 node_metadata_ident_recursive(cur->ident, array);
575 }
576}
577
578static void
579node_metadata_type(struct lys_type *type, struct lys_module *module, json_object *parent)
580{
581 json_object *obj, *array, *item;
582 char *str;
583 int i;
584
585 /* built-in YANG type */
586 if (!type->der->module) {
587 switch (type->base) {
588 case LY_TYPE_BINARY:
589 node_metadata_text("binary", "type", parent);
590 node_metadata_restr(type->info.binary.length, "length", parent);
591 break;
592 case LY_TYPE_BITS:
593 node_metadata_text("bits", "type", parent);
594
595 array = json_object_new_array();
596 for (i = 0; i < type->info.bits.count; ++i) {
597 item = json_object_new_object();
598 obj = json_object_new_string(type->info.bits.bit[i].name);
599 json_object_object_add(item, "name", obj);
600 obj = json_object_new_int(type->info.bits.bit[i].pos);
601 json_object_object_add(item, "position", obj);
602 json_object_array_add(array, item);
603 }
604 json_object_object_add(parent, "bits", array);
605 break;
606 case LY_TYPE_BOOL:
607 node_metadata_text("bool", "type", parent);
608 break;
609 case LY_TYPE_DEC64:
610 node_metadata_text("decimal64", "type", parent);
611 node_metadata_restr(type->info.dec64.range, "range", parent);
612 obj = json_object_new_int(type->info.dec64.dig);
613 json_object_object_add(parent, "fraction-digits", obj);
614 break;
615 case LY_TYPE_EMPTY:
616 node_metadata_text("empty", "type", parent);
617 break;
618 case LY_TYPE_ENUM:
619 node_metadata_text("enumeration", "type", parent);
620
621 array = json_object_new_array();
622 for (i = 0; i < type->info.enums.count; ++i) {
623 obj = json_object_new_string(type->info.enums.enm[i].name);
624 json_object_array_add(array, obj);
625 }
626 json_object_object_add(parent, "enumval", array);
627 break;
628 case LY_TYPE_IDENT:
629 node_metadata_text("identityref", "type", parent);
630
631 array = json_object_new_array();
632 node_metadata_ident_recursive(type->info.ident.ref, array);
633 json_object_object_add(parent, "identityval", array);
634 break;
635 case LY_TYPE_INST:
636 node_metadata_text("instance-identifier", "type", parent);
637 if (type->info.inst.req == -1) {
638 obj = json_object_new_boolean(0);
639 } else {
640 obj = json_object_new_boolean(1);
641 }
642 json_object_object_add(parent, "require-instance", obj);
643 break;
644 case LY_TYPE_LEAFREF:
645 node_metadata_text("leafref", "type", parent);
646 node_metadata_text(type->info.lref.path, "path", parent);
647 break;
648 case LY_TYPE_STRING:
649 node_metadata_text("string", "type", parent);
650 node_metadata_restr(type->info.str.length, "length", parent);
651 if (type->info.str.pat_count) {
652 array = json_object_new_array();
653 for (i = 0; i < type->info.str.pat_count; ++i) {
654 obj = json_object_new_string(type->info.str.patterns[i].expr);
655 json_object_array_add(array, obj);
656 }
657 json_object_object_add(parent, "pattern", array);
658 }
659 break;
660 case LY_TYPE_UNION:
661 node_metadata_text("union", "type", parent);
662 array = json_object_new_array();
663 for (i = 0; i < type->info.uni.count; ++i) {
664 obj = json_object_new_object();
665 node_metadata_type(&type->info.uni.types[i], module, obj);
666 json_object_array_add(array, obj);
667 }
668 json_object_object_add(parent, "types", array);
669 break;
670 case LY_TYPE_INT8:
671 node_metadata_text("int8", "type", parent);
672 node_metadata_restr(type->info.num.range, "range", parent);
673 break;
674 case LY_TYPE_UINT8:
675 node_metadata_text("uint8", "type", parent);
676 node_metadata_restr(type->info.num.range, "range", parent);
677 break;
678 case LY_TYPE_INT16:
679 node_metadata_text("int16", "type", parent);
680 node_metadata_restr(type->info.num.range, "range", parent);
681 break;
682 case LY_TYPE_UINT16:
683 node_metadata_text("uint16", "type", parent);
684 node_metadata_restr(type->info.num.range, "range", parent);
685 break;
686 case LY_TYPE_INT32:
687 node_metadata_text("int32", "type", parent);
688 node_metadata_restr(type->info.num.range, "range", parent);
689 break;
690 case LY_TYPE_UINT32:
691 node_metadata_text("uint32", "type", parent);
692 node_metadata_restr(type->info.num.range, "range", parent);
693 break;
694 case LY_TYPE_INT64:
695 node_metadata_text("int64", "type", parent);
696 node_metadata_restr(type->info.num.range, "range", parent);
697 break;
698 case LY_TYPE_UINT64:
699 node_metadata_text("uint64", "type", parent);
700 node_metadata_restr(type->info.num.range, "range", parent);
701 break;
702 default:
703 ERROR("Internal: unknown type (%s:%d)", __FILE__, __LINE__);
704 break;
705 }
706
707 /* typedef */
708 } else {
709 if (!module || !type->module_name || !strcmp(type->module_name, module->name)) {
710 node_metadata_text(type->der->name, "type", parent);
711 } else {
712 asprintf(&str, "%s:%s", type->module_name, type->der->name);
713 node_metadata_text(str, "type", parent);
714 free(str);
715 }
716 obj = json_object_new_object();
717 node_metadata_typedef(type->der, obj);
718 json_object_object_add(parent, "typedef", obj);
719 }
720}
721
722static void
723node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent)
724{
725 json_object *obj;
726
727 /* description */
728 node_metadata_text(tpdf->dsc, "description", parent);
729
730 /* reference */
731 node_metadata_text(tpdf->ref, "reference", parent);
732
733 /* status */
734 if (tpdf->flags & LYS_STATUS_DEPRC) {
735 obj = json_object_new_string("deprecated");
736 } else if (tpdf->flags & LYS_STATUS_OBSLT) {
737 obj = json_object_new_string("obsolete");
738 } else {
739 obj = json_object_new_string("current");
740 }
741 json_object_object_add(parent, "status", obj);
742
743 /* type */
744 node_metadata_type(&tpdf->type, tpdf->module, parent);
745
746 /* units */
747 node_metadata_text(tpdf->units, "units", parent);
748
749 /* default */
750 node_metadata_text(tpdf->dflt, "default", parent);
751}
752
753static void
754node_metadata_container(struct lys_node_container *cont, json_object *parent)
755{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100756 json_object *obj, *child_array = NULL, *choice_array = NULL;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100757
758 /* element type */
759 obj = json_object_new_string("container");
760 json_object_object_add(parent, "eltype", obj);
761
762 /* shared info */
763 node_metadata_basic((struct lys_node *)cont, parent);
764
765 /* must */
766 node_metadata_must(cont->must_size, cont->must, parent);
767
768 /* presence */
769 node_metadata_text(cont->presence, "presence", parent);
770
771 /* when */
772 node_metadata_when(cont->when, parent);
773
774 /* children & choice */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100775 node_metadata_children_recursive((struct lys_node *)cont, &child_array, &choice_array);
776 if (child_array) {
777 json_object_object_add(parent, "children", child_array);
778 }
779 if (choice_array) {
780 json_object_object_add(parent, "choice", choice_array);
781 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100782}
783
784static void
785node_metadata_choice(struct lys_node_choice *choice, json_object *parent)
786{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100787 json_object *obj, *array;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100788
789 /* element type */
790 obj = json_object_new_string("choice");
791 json_object_object_add(parent, "eltype", obj);
792
793 /* shared info */
794 node_metadata_basic((struct lys_node *)choice, parent);
795
796 /* default */
797 node_metadata_text(choice->dflt->name, "default", parent);
798
799 /* when */
800 node_metadata_when(choice->when, parent);
801
802 /* cases */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100803 if (choice->child) {
804 array = json_object_new_array();
805 node_metadata_cases_recursive(choice, array);
806 json_object_object_add(parent, "cases", array);
807 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100808}
809
810static void
811node_metadata_leaf(struct lys_node_leaf *leaf, json_object *parent)
812{
813 json_object *obj;
814 struct lys_node_list *list;
815 int is_key, i;
816
817 /* element type */
818 obj = json_object_new_string("leaf");
819 json_object_object_add(parent, "eltype", obj);
820
821 /* shared info */
822 node_metadata_basic((struct lys_node *)leaf, parent);
823
824 /* type */
825 node_metadata_type(&leaf->type, leaf->module, parent);
826
827 /* units */
828 node_metadata_text(leaf->units, "units", parent);
829
830 /* default */
831 node_metadata_text(leaf->dflt, "default", parent);
832
833 /* must */
834 node_metadata_must(leaf->must_size, leaf->must, parent);
835
836 /* when */
837 node_metadata_when(leaf->when, parent);
838
839 /* iskey */
840 is_key = 0;
841 list = (struct lys_node_list *)lys_parent((struct lys_node *)leaf);
842 if (list && (list->nodetype == LYS_LIST)) {
843 for (i = 0; i < list->keys_size; ++i) {
844 if (list->keys[i] == leaf) {
845 is_key = 1;
846 break;
847 }
848 }
849 }
850 obj = json_object_new_boolean(is_key);
851 json_object_object_add(parent, "iskey", obj);
852}
853
854static void
855node_metadata_leaflist(struct lys_node_leaflist *llist, json_object *parent)
856{
857 json_object *obj;
858
859 /* element type */
860 obj = json_object_new_string("leaf-list");
861 json_object_object_add(parent, "eltype", obj);
862
863 /* shared info */
864 node_metadata_basic((struct lys_node *)llist, parent);
865
866 /* type */
867 node_metadata_type(&llist->type, llist->module, parent);
868
869 /* units */
870 node_metadata_text(llist->units, "units", parent);
871
872 /* must */
873 node_metadata_must(llist->must_size, llist->must, parent);
874
875 /* when */
876 node_metadata_when(llist->when, parent);
877
878 /* min/max-elements */
879 node_metadata_min_max(llist->min, llist->max, parent);
880}
881
882static void
883node_metadata_list(struct lys_node_list *list, json_object *parent)
884{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100885 json_object *obj, *array, *child_array = NULL, *choice_array = NULL;;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100886 int i;
887 unsigned int j;
888
889 /* element type */
890 obj = json_object_new_string("list");
891 json_object_object_add(parent, "eltype", obj);
892
893 /* shared info */
894 node_metadata_basic((struct lys_node *)list, parent);
895
896 /* must */
897 node_metadata_must(list->must_size, list->must, parent);
898
899 /* when */
900 node_metadata_when(list->when, parent);
901
902 /* min/max-elements */
903 node_metadata_min_max(list->min, list->max, parent);
904
905 /* keys */
906 if (list->keys_size) {
907 array = json_object_new_array();
908 for (i = 0; i < list->keys_size; ++i) {
909 obj = json_object_new_string(list->keys[i]->name);
910 json_object_array_add(array, obj);
911 }
912 json_object_object_add(parent, "keys", array);
913 }
914
915 /* unique */
916 if (list->unique_size) {
917 array = json_object_new_array();
918 for (i = 0; i < list->unique_size; ++i) {
919 for (j = 0; j < list->unique[i].expr_size; ++j) {
920 obj = json_object_new_string(list->unique[i].expr[j]);
921 json_object_array_add(array, obj);
922 }
923 }
924 json_object_object_add(parent, "unique", array);
925 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100926
927 /* children & choice */
928 node_metadata_children_recursive((struct lys_node *)list, &child_array, &choice_array);
929 if (child_array) {
930 json_object_object_add(parent, "children", child_array);
931 }
932 if (choice_array) {
933 json_object_object_add(parent, "choice", choice_array);
934 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100935}
936
937static void
938node_metadata_anyxml(struct lys_node_anyxml *anyxml, json_object *parent)
939{
940 json_object *obj;
941
942 /* element type */
943 obj = json_object_new_string("anyxml");
944 json_object_object_add(parent, "eltype", obj);
945
946 /* shared info */
947 node_metadata_basic((struct lys_node *)anyxml, parent);
948
949 /* must */
950 node_metadata_must(anyxml->must_size, anyxml->must, parent);
951
952 /* when */
953 node_metadata_when(anyxml->when, parent);
954
955}
956
957static void
958node_metadata_case(struct lys_node_case *cas, json_object *parent)
959{
960 json_object *obj;
961
962 /* element type */
963 obj = json_object_new_string("case");
964 json_object_object_add(parent, "eltype", obj);
965
966 /* shared info */
967 node_metadata_basic((struct lys_node *)cas, parent);
968
969 /* when */
970 node_metadata_when(cas->when, parent);
971}
972
Michal Vaskoa45770b2015-11-23 15:49:41 +0100973static void
974node_metadata_rpc(struct lys_node_rpc *rpc, json_object *parent)
975{
976 json_object *obj;
977
978 /* element type */
979 obj = json_object_new_string("rpc");
980 json_object_object_add(parent, "eltype", obj);
981
982 /* description */
983 node_metadata_text(rpc->dsc, "description", parent);
984
985 /* reference */
986 node_metadata_text(rpc->ref, "reference", parent);
987
988 /* status */
989 if (rpc->flags & LYS_STATUS_DEPRC) {
990 obj = json_object_new_string("deprecated");
991 } else if (rpc->flags & LYS_STATUS_OBSLT) {
992 obj = json_object_new_string("obsolete");
993 } else {
994 obj = json_object_new_string("current");
995 }
996 json_object_object_add(parent, "status", obj);
997}
998
999static void
1000node_metadata_model(struct lys_module *module, json_object *parent)
1001{
1002 json_object *obj, *array, *item;
1003 int i;
1004
1005 /* yang-version */
1006 if (module->version == 2) {
1007 obj = json_object_new_string("1.1");
1008 } else {
1009 obj = json_object_new_string("1.0");
1010 }
1011 json_object_object_add(parent, "yang-version", obj);
1012
1013 /* namespace */
1014 node_metadata_text(module->ns, "namespace", parent);
1015
1016 /* prefix */
1017 node_metadata_text(module->prefix, "prefix", parent);
1018
1019 /* contact */
1020 node_metadata_text(module->contact, "contact", parent);
1021
1022 /* organization */
1023 node_metadata_text(module->org, "organization", parent);
1024
1025 /* revision */
1026 if (module->rev_size) {
1027 node_metadata_text(module->rev[0].date, "revision", parent);
1028 }
1029
1030 /* description */
1031 node_metadata_text(module->dsc, "description", parent);
1032
1033 /* import */
1034 if (module->imp_size) {
1035 array = json_object_new_array();
1036 for (i = 0; i < module->imp_size; ++i) {
1037 item = json_object_new_object();
1038
1039 node_metadata_text(module->imp[i].module->name, "name", item);
1040 node_metadata_text(module->imp[i].prefix, "prefix", item);
1041 if (module->imp[i].rev && module->imp[i].rev[0]) {
1042 node_metadata_text(module->imp[i].rev, "revision", item);
1043 }
1044
1045 json_object_array_add(array, item);
1046 }
1047 json_object_object_add(parent, "imports", array);
1048 }
1049
1050 /* include */
1051 if (module->inc_size) {
1052 array = json_object_new_array();
1053 for (i = 0; i < module->inc_size; ++i) {
1054 item = json_object_new_object();
1055
1056 node_metadata_text(module->inc[i].submodule->name, "name", item);
1057 if (module->inc[i].rev && module->inc[i].rev[0]) {
1058 node_metadata_text(module->inc[i].rev, "revision", item);
1059 }
1060
1061 json_object_array_add(array, item);
1062 }
1063 json_object_object_add(parent, "includes", array);
1064 }
1065}
1066
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001067/**
1068 * \defgroup netconf_operations NETCONF operations
1069 * The list of NETCONF operations that mod_netconf supports.
1070 * @{
1071 */
1072
1073/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001074 * \brief Send RPC and wait for reply with timeout.
1075 *
1076 * \param[in] session libnetconf session
1077 * \param[in] rpc prepared RPC message
1078 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
1079 * \param[out] reply reply from the server
1080 * \return Value from nc_session_recv_reply() or NC_MSG_UNKNOWN when send_rpc() fails.
1081 * On success, it returns NC_MSG_REPLY.
1082 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001083NC_MSG_TYPE
1084netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc, int timeout, nc_reply **reply)
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001085{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001086 const nc_msgid msgid = NULL;
1087 NC_MSG_TYPE ret = NC_MSG_UNKNOWN;
1088 msgid = nc_session_send_rpc(session, rpc);
1089 if (msgid == NULL) {
1090 return ret;
1091 }
1092 do {
1093 ret = nc_session_recv_reply(session, timeout, reply);
1094 if (ret == NC_MSG_HELLO) {
1095 ERROR("<hello> received instead reply, it will be lost.");
1096 nc_reply_free(*reply);
1097 }
1098 if (ret == NC_MSG_WOULDBLOCK) {
1099 ERROR("Timeout for receiving RPC reply expired.");
1100 break;
1101 }
1102 } while (ret == NC_MSG_HELLO || ret == NC_MSG_NOTIFICATION);
1103 return ret;
1104}
1105
1106static int
1107ctx_download_module(struct session_with_mutex *session, const char *model_name, const char *revision, const char *schema_dir)
1108{
1109 json_object *err = NULL;
1110 char *model_data = NULL, *model_path;
1111 size_t length;
1112 FILE *file;
1113
1114 DEBUG("UNLOCK rwlock %s", __func__);
1115 if (pthread_rwlock_unlock(&session_lock) != 0) {
1116 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1117 return 1;
1118 }
1119
1120 model_data = netconf_getschema(session->session_key, model_name, revision, "yin", &err);
1121
1122 DEBUG("LOCK rwlock %s", __func__);
1123 if (pthread_rwlock_wrlock(&session_lock) != 0) {
1124 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
1125 return 1;
1126 }
1127
1128 if (!model_data) {
1129 if (err) {
1130 json_object_put(err);
1131 }
1132 ERROR("Failed to get-schema of \"%s\".", model_name);
1133 return 1;
1134 }
1135
1136 if (revision) {
1137 asprintf(&model_path, "%s/%s@%s.yin", schema_dir, model_name, revision);
1138 } else {
1139 asprintf(&model_path, "%s/%s.yin", schema_dir, model_name);
1140 }
1141
1142 file = fopen(model_path, "w");
1143 if (!file) {
1144 ERROR("Failed to open \"%s\" for writing (%s).", model_path, strerror(errno));
1145 free(model_data);
1146 free(model_path);
1147 return 1;
1148 }
1149 free(model_path);
1150
1151 length = strlen(model_data);
1152 if (fwrite(model_data, 1, length, file) < length) {
1153 ERROR("Failed to store the model \"%s\".", model_name);
1154 free(model_data);
1155 fclose(file);
1156 return 1;
1157 }
1158
1159 free(model_data);
1160 fclose(file);
1161 return 0;
1162}
1163
1164static void
1165ctx_enable_features(struct lys_module *module, const char *cpblt)
1166{
1167 char *ptr, *ptr2, *features = NULL;
1168
1169 /* parse features */
1170 ptr = strstr(cpblt, "features=");
1171 if (ptr) {
1172 ptr += 9;
1173 ptr2 = strchr(ptr, '&');
1174 if (!ptr2) {
1175 ptr2 = ptr + strlen(ptr);
1176 }
1177 features = strndup(ptr, ptr2 - ptr);
1178 }
1179
1180 /* enable features */
1181 if (features) {
1182 /* basically manual strtok_r (to avoid macro) */
1183 ptr2 = features;
1184 for (ptr = features; *ptr; ++ptr) {
1185 if (*ptr == ',') {
1186 *ptr = '\0';
1187 /* remember last feature */
1188 ptr2 = ptr + 1;
1189 }
1190 }
1191
1192 ptr = features;
1193 lys_features_enable(module, ptr);
1194 while (ptr != ptr2) {
1195 ptr += strlen(ptr) + 1;
1196 lys_features_enable(module, ptr);
1197 }
1198
1199 free(features);
1200 }
1201}
1202
1203static void
1204ctx_enable_capabs(struct lys_module *ietfnc, json_object *cpb_array)
1205{
1206 json_object *item;
1207 int i;
1208 const char *capab;
1209
1210 /* set supported capabilities from ietf-netconf */
1211 for (i = 0; i < json_object_array_length(cpb_array); ++i) {
1212 item = json_object_array_get_idx(cpb_array, i);
1213 capab = json_object_get_string(item);
1214
1215 if (!strncmp(capab, "urn:ietf:params:netconf:capability:", 35)) {
1216 if (!strncmp(capab, "writable-running", 16)) {
1217 lys_features_enable(ietfnc, "writable-running");
1218 } else if (!strncmp(capab, "candidate", 9)) {
1219 lys_features_enable(ietfnc, "candidate");
1220 } else if (!strcmp(capab, "confirmed-commit:1.1")) {
1221 lys_features_enable(ietfnc, "confirmed-commit");
1222 } else if (!strncmp(capab, "rollback-on-error", 17)) {
1223 lys_features_enable(ietfnc, "rollback-on-error");
1224 } else if (!strcmp(capab, "validate:1.1")) {
1225 lys_features_enable(ietfnc, "validate");
1226 } else if (!strncmp(capab, "startup", 7)) {
1227 lys_features_enable(ietfnc, "startup");
1228 } else if (!strncmp(capab, "url", 3)) {
1229 lys_features_enable(ietfnc, "url");
1230 } else if (!strncmp(capab, "xpath", 5)) {
1231 lys_features_enable(ietfnc, "xpath");
1232 }
1233 }
1234 }
1235}
1236
1237static int
1238prepare_context(struct session_with_mutex *session)
1239{
1240 struct lys_module *module;
1241 json_object *array, *item;
1242 char *ptr, *ptr2;
1243 char *model_name = NULL, *revision = NULL;
1244 const char *capab;
1245 int i, get_schema_support;
1246
1247 if (json_object_object_get_ex(session->hello_message, "capabilities", &array) == FALSE) {
1248 return 1;
1249 }
1250
1251 get_schema_support = 0;
1252 for (i = 0; i < json_object_array_length(array); ++i) {
1253 item = json_object_array_get_idx(array, i);
1254 capab = json_object_get_string(item);
1255
1256 if (!strncmp(capab, "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) {
1257 get_schema_support = 1;
1258 break;
1259 }
1260 }
1261
1262 if (get_schema_support) {
1263 errno = 0;
1264 if (eaccess(SCHEMA_DIR, W_OK)) {
1265 if (errno == ENOENT) {
1266 if (mkdir(SCHEMA_DIR, 00755)) {
1267 ERROR("Failed to create temp model dir \"%s\" (%s).", SCHEMA_DIR, strerror(errno));
1268 return 1;
1269 }
1270 } else {
1271 ERROR("Unable to write to temp model dir \"%s\" (%s).", SCHEMA_DIR, strerror(errno));
1272 return 1;
1273 }
1274 }
1275
1276 session->ctx = ly_ctx_new(SCHEMA_DIR);
1277 } else {
Michal Vaskoa53ef882015-11-24 11:02:01 +01001278 /* TODO try to load models from a local directory */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001279 session->ctx = ly_ctx_new(NULL);
1280 }
1281
1282loop:
1283 /* download all the models first or load them directly */
1284 for (i = 0; i < json_object_array_length(array); ++i) {
1285 item = json_object_array_get_idx(array, i);
1286 capab = json_object_get_string(item);
1287 if (!strncmp(capab, "urn:ietf:params:netconf:capability", 34)
1288 || !strncmp(capab, "urn:ietf:params:netconf:base", 28)) {
1289 continue;
1290 }
1291
1292 /* get module */
1293 ptr = strstr(capab, "module=");
1294 if (!ptr) {
1295 ERROR("Unknown capability \"%s\" could not be parsed.", capab);
1296 continue;
1297 }
1298 ptr += 7;
1299 ptr2 = strchr(ptr, '&');
1300 if (!ptr2) {
1301 ptr2 = ptr + strlen(ptr);
1302 }
1303 model_name = strndup(ptr, ptr2 - ptr);
1304
1305 /* get revision */
1306 ptr = strstr(capab, "revision=");
1307 if (ptr) {
1308 ptr += 9;
1309 ptr2 = strchr(ptr, '&');
1310 if (!ptr2) {
1311 ptr2 = ptr + strlen(ptr);
1312 }
1313 revision = strndup(ptr, ptr2 - ptr);
1314 }
1315
1316 if (get_schema_support) {
1317 ctx_download_module(session, model_name, revision, SCHEMA_DIR);
1318 } else {
1319 module = ly_ctx_get_module(session->ctx, model_name, revision);
1320 if (!module) {
1321 module = ly_ctx_load_module(session->ctx, NULL, model_name, revision);
1322 if (module) {
1323 if (!strcmp(module->name, "ietf-netconf")) {
1324 ctx_enable_capabs(module, array);
1325 } else {
1326 ctx_enable_features(module, capab);
1327 }
1328 }
1329 }
1330 }
1331
1332 free(model_name);
1333 free(revision);
1334 revision = NULL;
1335 }
1336
1337 if (get_schema_support) {
1338 /* we have downloaded all the models, load them now */
1339 get_schema_support = 0;
1340 goto loop;
1341 }
1342
1343 return 0;
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001344}
1345
1346/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001347 * \brief Connect to NETCONF server
1348 *
1349 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
1350 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001351static unsigned int
1352netconf_connect(const char *host, const char *port, const char *user, const char *pass, struct nc_cpblts *cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +02001353{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001354 struct nc_session* session = NULL;
1355 struct session_with_mutex *locked_session, *last_session;
Radek Krejci469aab82012-07-22 18:42:20 +02001356
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001357 /* connect to the requested NETCONF server */
1358 password = (char*)pass;
1359 DEBUG("prepare to connect %s@%s:%s", user, host, port);
1360 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
1361 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +02001362
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001363 /* if connected successful, add session to the list */
1364 if (session != NULL) {
1365 if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
1366 nc_session_free(session);
1367 session = NULL;
1368 free(locked_session);
1369 locked_session = NULL;
1370 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
1371 return 0;
1372 }
1373 locked_session->session = session;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001374 locked_session->hello_message = NULL;
1375 locked_session->closed = 0;
1376 pthread_mutex_init(&locked_session->lock, NULL);
1377 DEBUG("Before session_lock");
1378 /* get exclusive access to sessions_list (conns) */
1379 DEBUG("LOCK wrlock %s", __func__);
1380 if (pthread_rwlock_wrlock(&session_lock) != 0) {
1381 nc_session_free(session);
1382 free(locked_session);
1383 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
1384 return 0;
1385 }
1386 locked_session->ntfc_subscribed = 0;
1387 DEBUG("Add connection to the list");
1388 if (!netconf_sessions_list) {
Michal Vaskoc3146782015-11-04 14:46:41 +01001389 netconf_sessions_list = locked_session;
1390 } else {
1391 for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next);
1392 last_session->next = locked_session;
1393 locked_session->prev = last_session;
1394 }
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001395 session_user_activity(nc_session_get_user(locked_session->session));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001396
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001397 /* no need to lock session, noone can read it while we have wrlock */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001398
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001399 /* store information about session from hello message for future usage */
1400 prepare_status_message(locked_session, session);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001401
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001402 /* create context from the hello message cpabilities */
1403 if (prepare_context(locked_session)) {
1404 nc_session_free(session);
1405 free(locked_session);
1406 DEBUG("UNLOCK wrlock %s", __func__);
1407 pthread_rwlock_unlock(&session_lock);
1408 ERROR("Failed to prepare context");
1409 return 0;
1410 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001411
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001412 DEBUG("NETCONF session established");
1413 locked_session->session_key = session_key_generator;
1414 ++session_key_generator;
1415 if (session_key_generator == UINT_MAX) {
1416 session_key_generator = 1;
1417 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001418
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001419 DEBUG("Before session_unlock");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001420 /* unlock session list */
1421 DEBUG("UNLOCK wrlock %s", __func__);
1422 if (pthread_rwlock_unlock(&session_lock) != 0) {
1423 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1424 }
Radek Krejci469aab82012-07-22 18:42:20 +02001425
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001426 return locked_session->session_key;
1427 }
1428
1429 ERROR("Connection could not be established");
1430 return 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001431}
1432
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001433static int
1434close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +02001435{
Michal Vaskoa53ef882015-11-24 11:02:01 +01001436 int i;
1437
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001438 DEBUG("lock private lock.");
1439 DEBUG("LOCK mutex %s", __func__);
1440 if (pthread_mutex_lock(&locked_session->lock) != 0) {
1441 ERROR("Error while locking rwlock");
1442 }
1443 locked_session->ntfc_subscribed = 0;
1444 locked_session->closed = 1;
1445 if (locked_session->session != NULL) {
1446 nc_session_free(locked_session->session);
1447 locked_session->session = NULL;
1448 }
1449 DEBUG("session closed.");
1450 DEBUG("unlock private lock.");
1451 DEBUG("UNLOCK mutex %s", __func__);
1452 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
1453 ERROR("Error while locking rwlock");
1454 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001455
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001456 DEBUG("unlock session lock.");
1457 DEBUG("closed session, disabled notif(?), wait 0.5s");
1458 usleep(500000); /* let notification thread stop */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001459
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001460 /* session shouldn't be used by now */
Michal Vaskoa53ef882015-11-24 11:02:01 +01001461 for (i = 0; i < locked_session->notif_count; ++i) {
1462 free(locked_session->notifications[i].content);
1463 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001464 free(locked_session->notifications);
1465 pthread_mutex_destroy(&locked_session->lock);
1466 if (locked_session->hello_message != NULL) {
1467 json_object_put(locked_session->hello_message);
1468 locked_session->hello_message = NULL;
1469 }
1470 locked_session->session = NULL;
1471 ly_ctx_destroy(locked_session->ctx);
1472 free(locked_session);
1473 locked_session = NULL;
1474 DEBUG("NETCONF session closed, everything cleared.");
1475 return (EXIT_SUCCESS);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001476}
1477
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001478static int
1479netconf_close(unsigned int session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +02001480{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001481 struct session_with_mutex *locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +02001482
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001483 DEBUG("Session to close: %u", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001484
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001485 /* get exclusive (write) access to sessions_list (conns) */
1486 DEBUG("lock session lock.");
1487 DEBUG("LOCK wrlock %s", __func__);
1488 if (pthread_rwlock_wrlock (&session_lock) != 0) {
1489 ERROR("Error while locking rwlock");
1490 (*reply) = create_error_reply("Internal: Error while locking.");
1491 return EXIT_FAILURE;
1492 }
1493 /* remove session from the active sessions list -> nobody new can now work with session */
1494 for (locked_session = netconf_sessions_list;
1495 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01001496 locked_session = locked_session->next);
1497
1498 if (!locked_session) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001499 ERROR("Could not find the session %u to close.", session_key);
1500 (*reply) = create_error_reply("Internal: Error while finding a session.");
Michal Vaskoc3146782015-11-04 14:46:41 +01001501 return EXIT_FAILURE;
1502 }
1503
1504 if (!locked_session->prev) {
1505 netconf_sessions_list = netconf_sessions_list->next;
1506 netconf_sessions_list->prev = NULL;
1507 } else {
1508 locked_session->prev->next = locked_session->next;
1509 if (locked_session->next) {
1510 locked_session->next->prev = locked_session->prev;
1511 }
1512 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001513
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001514 DEBUG("UNLOCK wrlock %s", __func__);
1515 if (pthread_rwlock_unlock (&session_lock) != 0) {
1516 ERROR("Error while unlocking rwlock");
1517 (*reply) = create_error_reply("Internal: Error while unlocking.");
1518 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001519
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001520 if ((locked_session != NULL) && (locked_session->session != NULL)) {
1521 return close_and_free_session(locked_session);
1522 } else {
1523 ERROR("Unknown session to close");
1524 (*reply) = create_error_reply("Internal: Unkown session to close.");
1525 return (EXIT_FAILURE);
1526 }
1527 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +02001528}
1529
Tomas Cejkac7929632013-10-24 19:25:15 +02001530/**
1531 * Test reply message type and return error message.
1532 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001533 * \param[in] session nc_session internal struct
Michal Vaskoc3146782015-11-04 14:46:41 +01001534 * \param[in] session_key session ID, 0 to disable disconnect on error
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001535 * \param[in] msgt RPC-REPLY message type
Tomas Cejkac7929632013-10-24 19:25:15 +02001536 * \param[out] data
1537 * \return NULL on success
1538 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001539json_object *
1540netconf_test_reply(struct nc_session *session, unsigned int session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data)
Tomas Cejkac7929632013-10-24 19:25:15 +02001541{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001542 NC_REPLY_TYPE replyt;
1543 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001544
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001545 /* process the result of the operation */
1546 switch (msgt) {
1547 case NC_MSG_UNKNOWN:
1548 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
1549 ERROR("mod_netconf: receiving rpc-reply failed");
1550 if (session_key) {
1551 netconf_close(session_key, &err);
1552 }
1553 if (err != NULL) {
1554 return err;
1555 }
1556 return create_error_reply("Internal: Receiving RPC-REPLY failed.");
1557 }
1558 case NC_MSG_NONE:
1559 /* there is error handled by callback */
1560 if (data != NULL) {
1561 free(*data);
1562 (*data) = NULL;
1563 }
1564 return NULL;
1565 case NC_MSG_REPLY:
1566 switch (replyt = nc_reply_get_type(reply)) {
1567 case NC_REPLY_OK:
1568 if ((data != NULL) && (*data != NULL)) {
1569 free(*data);
1570 (*data) = NULL;
1571 }
1572 return create_ok_reply();
1573 case NC_REPLY_DATA:
1574 if (((*data) = nc_reply_get_data(reply)) == NULL) {
1575 ERROR("mod_netconf: no data from reply");
1576 return create_error_reply("Internal: No data from reply received.");
1577 } else {
1578 return NULL;
1579 }
1580 break;
1581 case NC_REPLY_ERROR:
1582 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
1583 if (data != NULL) {
1584 free(*data);
1585 (*data) = NULL;
1586 }
1587 return create_error_reply(nc_reply_get_errormsg(reply));
1588 default:
1589 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
1590 if (data != NULL) {
1591 free(*data);
1592 (*data) = NULL;
1593 }
1594 return create_error_reply("Unknown type of NETCONF reply.");
1595 }
1596 break;
1597 default:
1598 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
1599 if (data != NULL) {
1600 free(*data);
1601 (*data) = NULL;
1602 }
1603 return create_error_reply("Internal: Unexpected RPC-REPLY message type.");
1604 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001605}
1606
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001607json_object *
1608netconf_unlocked_op(struct nc_session *session, nc_rpc *rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001609{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001610 nc_reply* reply = NULL;
1611 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001612
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001613 /* check requests */
1614 if (rpc == NULL) {
1615 ERROR("mod_netconf: rpc is not created");
1616 return create_error_reply("Internal error: RPC is not created");
1617 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001618
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001619 if (session != NULL) {
1620 /* send the request and get the reply */
1621 msgt = netconf_send_recv_timed(session, rpc, 50000, &reply);
1622 /* process the result of the operation */
1623 return netconf_test_reply(session, 0, msgt, reply, NULL);
1624 } else {
1625 ERROR("Unknown session to process.");
1626 return create_error_reply("Internal error: Unknown session to process.");
1627 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001628}
1629
Tomas Cejkac7929632013-10-24 19:25:15 +02001630/**
1631 * Perform RPC method that returns data.
1632 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001633 * \param[in] session_id session identifier
1634 * \param[in] rpc RPC message to perform
1635 * \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 +02001636 * \return NULL on success, json object with error otherwise
1637 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001638static json_object *
1639netconf_op(unsigned int session_key, nc_rpc *rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +02001640{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001641 struct session_with_mutex * locked_session;
1642 nc_reply* reply = NULL;
1643 json_object *res = NULL;
1644 char *data = NULL;
1645 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +02001646
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001647 /* check requests */
1648 if (rpc == NULL) {
1649 ERROR("mod_netconf: rpc is not created");
1650 res = create_error_reply("Internal: RPC could not be created.");
1651 data = NULL;
1652 goto finished;
1653 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001654
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001655 locked_session = session_get_locked(session_key, &res);
1656 if (!locked_session) {
1657 ERROR("Unknown session or locking failed.");
1658 goto finished;
1659 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001660
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001661 session_user_activity(nc_session_get_user(locked_session->session));
Tomas Cejkac7929632013-10-24 19:25:15 +02001662
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001663 /* send the request and get the reply */
1664 msgt = netconf_send_recv_timed(locked_session->session, rpc, 2000000, &reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001665
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001666 session_unlock(locked_session);
Tomas Cejkac7929632013-10-24 19:25:15 +02001667
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001668 res = netconf_test_reply(locked_session->session, session_key, msgt, reply, &data);
Radek Krejcia332b692012-11-12 16:15:54 +01001669
Tomas Cejkac7929632013-10-24 19:25:15 +02001670finished:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001671 nc_reply_free(reply);
1672 if (received_data != NULL) {
1673 (*received_data) = data;
1674 } else {
1675 if (data != NULL) {
1676 free(data);
1677 data = NULL;
1678 }
1679 }
1680 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001681}
1682
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001683static char *
1684netconf_getconfig(unsigned int session_key, NC_DATASTORE source, const char *filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001685{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001686 nc_rpc* rpc;
1687 struct nc_filter *f = NULL;
1688 struct session_with_mutex *locked_session;
1689 char* data = NULL, *data_xml;
1690 json_object *res = NULL, *data_cjson;
1691 enum json_tokener_error tok_err;
1692 struct lyd_node *node, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001693
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001694 /* create filter if set */
1695 if (filter != NULL) {
1696 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
1697 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001698
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001699 /* create requests */
1700 rpc = nc_rpc_getconfig(source, f);
1701 nc_filter_free(f);
1702 if (rpc == NULL) {
1703 ERROR("mod_netconf: creating rpc request failed");
1704 return (NULL);
1705 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001706
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001707 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +01001708#ifdef HAVE_WITHDEFAULTS_TAGGED
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001709 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED))
Tomas Cejkae8bd27c2014-03-27 15:16:31 +01001710#else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001711 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET))
1712 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL))
Tomas Cejkae8bd27c2014-03-27 15:16:31 +01001713#endif
Michal Vaskoc3146782015-11-04 14:46:41 +01001714 {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001715 ERROR("mod_netconf: setting withdefaults failed");
1716 }
Tomas Cejka94674662013-09-13 15:55:24 +02001717
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001718 res = netconf_op(session_key, rpc, &data);
1719 nc_rpc_free(rpc);
1720 if (res != NULL) {
1721 (*err) = res;
1722 } else {
1723 (*err) = NULL;
1724 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001725
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001726 if (data) {
1727 for (locked_session = netconf_sessions_list;
1728 locked_session && (locked_session->session_key != session_key);
1729 locked_session = locked_session->next);
1730 /* won't fail */
1731
1732 asprintf(&data_xml, "<get-config>%s</get-config>", data);
1733 node = lyd_parse(locked_session->ctx, data_xml, LYD_XML, LYD_OPT_GETCONFIG | (strict ? LYD_OPT_STRICT : 0));
1734 free(data_xml);
1735 free(data);
1736 if (!node) {
1737 ERROR("Parsing <get-config> data failed.");
1738 return NULL;
1739 }
1740
1741 /* replace XML data with JSON data */
1742 if (lyd_print_mem(&data, node, LYD_JSON)) {
1743 ERROR("Printing JSON <get-config> data failed.");
1744 LY_TREE_FOR(node, sibling) {
1745 lyd_free(sibling);
1746 }
1747 return NULL;
1748 }
1749
1750 /* parse JSON data into cjson */
1751 pthread_mutex_lock(&json_lock);
1752 data_cjson = json_tokener_parse_verbose(data, &tok_err);
1753 if (!data_cjson) {
1754 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1755 pthread_mutex_unlock(&json_lock);
1756 LY_TREE_FOR(node, sibling) {
1757 lyd_free(sibling);
1758 }
1759 free(data);
1760 return NULL;
1761 }
1762 free(data);
1763
1764 /* go simultaneously through both trees and add metadata */
1765 LY_TREE_FOR_SAFE(node, next, sibling) {
1766 node_add_metadata_recursive(sibling, NULL, data_cjson);
1767 lyd_free(sibling);
1768 }
1769
1770 data = strdup(json_object_to_json_string_ext(data_cjson, 0));
1771 json_object_put(data_cjson);
1772 pthread_mutex_unlock(&json_lock);
1773 }
1774
1775 return (data);
Radek Krejci8e4632a2012-07-26 13:40:34 +02001776}
1777
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001778static char *
1779netconf_getschema(unsigned int session_key, const char *identifier, const char *version, const char *format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001780{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001781 nc_rpc* rpc;
1782 char* data = NULL;
1783 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001784
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001785 /* create requests */
1786 rpc = nc_rpc_getschema(identifier, version, format);
1787 if (rpc == NULL) {
1788 ERROR("mod_netconf: creating rpc request failed");
1789 return (NULL);
1790 }
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001791
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001792 res = netconf_op(session_key, rpc, &data);
1793 nc_rpc_free (rpc);
1794 if (res != NULL) {
1795 (*err) = res;
1796 } else {
1797 (*err) = NULL;
1798 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001799
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001800 return (data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001801}
1802
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001803static char *
1804netconf_get(unsigned int session_key, const char* filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001805{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001806 nc_rpc* rpc;
1807 struct nc_filter *f = NULL;
1808 char* data = NULL, *data_xml;
1809 json_object *res = NULL, *data_cjson;
1810 enum json_tokener_error tok_err;
1811 struct session_with_mutex *locked_session;
1812 struct lyd_node *node, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001813
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001814 /* create filter if set */
1815 if (filter != NULL) {
1816 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
1817 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001818
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001819 /* create requests */
1820 rpc = nc_rpc_get(f);
1821 nc_filter_free(f);
1822 if (rpc == NULL) {
1823 ERROR("mod_netconf: creating rpc request failed");
1824 return (NULL);
1825 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001826
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001827 /* tell server to show all elements even if they have default values */
1828 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
1829 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
1830 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
1831 ERROR("mod_netconf: setting withdefaults failed");
1832 }
Tomas Cejka94674662013-09-13 15:55:24 +02001833
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001834 res = netconf_op(session_key, rpc, &data);
1835 nc_rpc_free(rpc);
1836 if (res != NULL) {
1837 (*err) = res;
1838 } else {
1839 (*err) = NULL;
1840 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001841
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001842 if (data) {
1843 for (locked_session = netconf_sessions_list;
1844 locked_session && (locked_session->session_key != session_key);
1845 locked_session = locked_session->next);
1846 /* won't fail */
1847
1848 asprintf(&data_xml, "<get>%s</get>", data);
1849 node = lyd_parse(locked_session->ctx, data_xml, LYD_XML, LYD_OPT_GET | (strict ? LYD_OPT_STRICT : 0));
1850 free(data_xml);
1851 free(data);
1852 if (!node) {
1853 ERROR("Parsing <get> data failed.");
1854 return NULL;
1855 }
1856
1857 /* replace XML data with JSON data */
1858 if (lyd_print_mem(&data, node, LYD_JSON)) {
1859 ERROR("Printing JSON <get> data failed.");
1860 LY_TREE_FOR(node, sibling) {
1861 lyd_free(sibling);
1862 }
1863 return NULL;
1864 }
1865
1866 /* parse JSON data into cjson */
1867 pthread_mutex_lock(&json_lock);
1868 data_cjson = json_tokener_parse_verbose(data, &tok_err);
1869 if (!data_cjson) {
1870 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1871 pthread_mutex_unlock(&json_lock);
1872 LY_TREE_FOR(node, sibling) {
1873 lyd_free(sibling);
1874 }
1875 free(data);
1876 return NULL;
1877 }
1878 free(data);
1879
1880 /* go simultaneously through both trees and add metadata */
1881 LY_TREE_FOR_SAFE(node, next, sibling) {
1882 node_add_metadata_recursive(sibling, NULL, data_cjson);
1883 lyd_free(sibling);
1884 }
1885
1886 data = strdup(json_object_to_json_string_ext(data_cjson, 0));
1887 json_object_put(data_cjson);
1888 pthread_mutex_unlock(&json_lock);
1889 }
1890
1891 return data;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001892}
1893
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001894static json_object *
1895netconf_copyconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, const char *config,
1896 const char *uri_src, const char *uri_trg)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001897{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001898 nc_rpc* rpc;
1899 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001900
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001901 /* create requests */
1902 if (source == NC_DATASTORE_CONFIG) {
1903 if (target == NC_DATASTORE_URL) {
1904 /* config, url */
1905 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
1906 } else {
1907 /* config, datastore */
1908 rpc = nc_rpc_copyconfig(source, target, config);
1909 }
1910 } else if (source == NC_DATASTORE_URL) {
1911 if (target == NC_DATASTORE_URL) {
1912 /* url, url */
1913 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
1914 } else {
1915 /* url, datastore */
1916 rpc = nc_rpc_copyconfig(source, target, uri_src);
1917 }
1918 } else {
1919 if (target == NC_DATASTORE_URL) {
1920 /* datastore, url */
1921 rpc = nc_rpc_copyconfig(source, target, uri_trg);
1922 } else {
1923 /* datastore, datastore */
1924 rpc = nc_rpc_copyconfig(source, target);
1925 }
1926 }
1927 if (rpc == NULL) {
1928 ERROR("mod_netconf: creating rpc request failed");
1929 return create_error_reply("Internal: Creating rpc request failed");
1930 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001931
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001932 res = netconf_op(session_key, rpc, NULL);
1933 nc_rpc_free(rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001934
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001935 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001936}
Radek Krejci035bf4e2012-07-25 10:59:09 +02001937
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001938static json_object *
1939netconf_editconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop,
1940 NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char *config_or_url)
Radek Krejci62ab34b2012-07-26 13:42:05 +02001941{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001942 nc_rpc* rpc;
1943 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001944
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001945 /* create requests */
1946 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
1947 if (rpc == NULL) {
1948 ERROR("mod_netconf: creating rpc request failed");
1949 return create_error_reply("Internal: Creating rpc request failed");
1950 }
Radek Krejci62ab34b2012-07-26 13:42:05 +02001951
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001952 res = netconf_op(session_key, rpc, NULL);
1953 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001954
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001955 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001956}
1957
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001958static json_object *
1959netconf_killsession(unsigned int session_key, const char *sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001960{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001961 nc_rpc *rpc;
1962 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001963
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001964 /* create requests */
1965 rpc = nc_rpc_killsession(sid);
1966 if (rpc == NULL) {
1967 ERROR("mod_netconf: creating rpc request failed");
1968 return create_error_reply("Internal: Creating rpc request failed");
1969 }
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001970
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001971 res = netconf_op(session_key, rpc, NULL);
1972 nc_rpc_free(rpc);
1973 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001974}
1975
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001976static json_object *
1977netconf_onlytargetop(unsigned int session_key, NC_DATASTORE target, nc_rpc *(*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +02001978{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001979 nc_rpc* rpc;
1980 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +02001981
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001982 /* create requests */
1983 rpc = op_func(target);
1984 if (rpc == NULL) {
1985 ERROR("mod_netconf: creating rpc request failed");
1986 return create_error_reply("Internal: Creating rpc request failed");
1987 }
Radek Krejci2f318372012-07-26 14:22:35 +02001988
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001989 res = netconf_op(session_key, rpc, NULL);
1990 nc_rpc_free (rpc);
1991 return res;
Radek Krejci2f318372012-07-26 14:22:35 +02001992}
1993
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001994static json_object *
1995netconf_deleteconfig(unsigned int session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001996{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001997 nc_rpc *rpc = NULL;
1998 json_object *res = NULL;
1999 if (target != NC_DATASTORE_URL) {
2000 rpc = nc_rpc_deleteconfig(target);
2001 } else {
2002 rpc = nc_rpc_deleteconfig(target, url);
2003 }
2004 if (rpc == NULL) {
2005 ERROR("mod_netconf: creating rpc request failed");
2006 return create_error_reply("Internal: Creating rpc request failed");
2007 }
Tomas Cejka404d37e2013-04-13 02:31:35 +02002008
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002009 res = netconf_op(session_key, rpc, NULL);
2010 nc_rpc_free (rpc);
2011 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +02002012}
2013
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002014static json_object *
2015netconf_lock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02002016{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002017 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02002018}
2019
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002020static json_object *
2021netconf_unlock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02002022{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002023 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02002024}
2025
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002026static json_object *
2027netconf_generic(unsigned int session_key, const char *content, char **data)
Radek Krejci80c10d92012-07-30 08:38:50 +02002028{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002029 nc_rpc* rpc = NULL;
2030 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +02002031
Michal Vaskoa53ef882015-11-24 11:02:01 +01002032 assert(!data || !*data);
2033
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002034 /* create requests */
2035 rpc = nc_rpc_generic(content);
2036 if (rpc == NULL) {
2037 ERROR("mod_netconf: creating rpc request failed");
2038 return create_error_reply("Internal: Creating rpc request failed");
2039 }
Radek Krejci80c10d92012-07-30 08:38:50 +02002040
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002041 /* get session where send the RPC */
2042 res = netconf_op(session_key, rpc, data);
2043 nc_rpc_free (rpc);
2044 return res;
2045}
2046
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002047static int
2048node_add_metadata(struct lys_node *node, struct lys_module *module, json_object *parent)
2049{
2050 struct lys_module *cur_module;
2051 json_object *meta_obj;
2052 char *obj_name;
2053
Michal Vaskoa45770b2015-11-23 15:49:41 +01002054 if (node->nodetype == LYS_INPUT) {
2055 /* silently skipped */
2056 return 0;
2057 }
2058
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002059 cur_module = node->module;
2060 if (cur_module->type) {
2061 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
2062 }
2063 if (cur_module == module) {
2064 asprintf(&obj_name, "$@%s", node->name);
2065 } else {
2066 asprintf(&obj_name, "$@%s:%s", cur_module->name, node->name);
2067 }
2068
2069 /* in (leaf-)lists the metadata could have already been added */
Michal Vaskoa45770b2015-11-23 15:49:41 +01002070 if ((node->nodetype & (LYS_LEAFLIST | LYS_LIST)) && (json_object_object_get_ex(parent, obj_name, NULL) == TRUE)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002071 free(obj_name);
2072 return 1;
2073 }
2074
2075 meta_obj = json_object_new_object();
2076
2077 switch (node->nodetype) {
2078 case LYS_CONTAINER:
2079 node_metadata_container((struct lys_node_container *)node, meta_obj);
2080 break;
Michal Vasko3fda9a92015-11-23 10:10:57 +01002081 case LYS_CHOICE:
2082 node_metadata_choice((struct lys_node_choice *)node, meta_obj);
2083 break;
2084 case LYS_LEAF:
2085 node_metadata_leaf((struct lys_node_leaf *)node, meta_obj);
2086 break;
2087 case LYS_LEAFLIST:
2088 node_metadata_leaflist((struct lys_node_leaflist *)node, meta_obj);
2089 break;
2090 case LYS_LIST:
2091 node_metadata_list((struct lys_node_list *)node, meta_obj);
2092 break;
2093 case LYS_ANYXML:
2094 node_metadata_anyxml((struct lys_node_anyxml *)node, meta_obj);
2095 break;
2096 case LYS_CASE:
2097 node_metadata_case((struct lys_node_case *)node, meta_obj);
2098 break;
Michal Vaskoa45770b2015-11-23 15:49:41 +01002099 case LYS_RPC:
2100 node_metadata_rpc((struct lys_node_rpc *)node, meta_obj);
2101 break;
2102 default: /* LYS_OUTPUT */
Michal Vasko3fda9a92015-11-23 10:10:57 +01002103 ERROR("Internal: unuxpected nodetype (%s:%d)", __FILE__, __LINE__);
2104 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002105 }
2106
2107 /* just a precaution */
2108 if (json_object_get_type(parent) != json_type_object) {
2109 ERROR("Internal: wrong JSON type (%s:%d)", __FILE__, __LINE__);
2110 free(obj_name);
2111 return 1;
2112 }
2113
2114 json_object_object_add(parent, obj_name, meta_obj);
2115 free(obj_name);
2116 return 0;
2117}
2118
2119static void
2120node_add_metadata_recursive(struct lyd_node *data_tree, struct lys_module *module, json_object *data_json_parent)
2121{
2122 struct lys_module *cur_module;
2123 struct lys_node *list_schema;
2124 struct lyd_node *child, *list_item;
2125 json_object *child_json, *list_child_json;
2126 char *child_name;
2127 int list_idx;
2128
Michal Vaskoa45770b2015-11-23 15:49:41 +01002129 if (data_tree->schema->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
2130 return;
2131 }
2132
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002133 /* add data_tree metadata */
2134 if (node_add_metadata(data_tree->schema, module, data_json_parent)) {
2135 return;
2136 }
2137
2138 /* get data_tree module */
2139 cur_module = data_tree->schema->module;
2140 if (cur_module->type) {
2141 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
2142 }
2143
2144 if (!(data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
2145 /* print correct data_tree JSON name */
2146 if (cur_module == module) {
2147 asprintf(&child_name, "%s", data_tree->schema->name);
2148 } else {
2149 asprintf(&child_name, "%s:%s", cur_module->name, data_tree->schema->name);
2150 }
2151
2152 /* go down in JSON object */
2153 if (json_object_object_get_ex(data_json_parent, child_name, &child_json) == FALSE) {
2154 ERROR("Internal: failed to get JSON object \"%s\".", child_name);
2155 free(child_name);
2156 return;
2157 }
2158 free(child_name);
2159
2160 if (data_tree->schema->nodetype == LYS_LIST) {
2161 if (json_object_get_type(child_json) != json_type_array) {
2162 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
2163 return;
2164 }
2165 /* go down in data tree for every item, we process them all now, skip later
2166 * (metadata duplicate will be detected at the beginning of this function) */
2167 list_idx = 0;
2168 list_schema = data_tree->schema;
2169
2170 LY_TREE_FOR(data_tree, list_item) {
2171 /* another list member */
2172 if (list_item->schema == list_schema) {
2173 list_child_json = json_object_array_get_idx(child_json, list_idx);
2174 if (!list_child_json) {
2175 ERROR("Internal: list \"%s\" idx out-of-bounds", list_schema->name);
2176 return;
2177 }
2178 LY_TREE_FOR(list_item->child, child) {
2179 node_add_metadata_recursive(child, cur_module, list_child_json);
2180 }
2181
2182 ++list_idx;
2183 }
2184 }
2185 } else {
2186 if (json_object_get_type(child_json) != json_type_object) {
2187 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
2188 return;
2189 }
2190 /* go down in data tree */
2191 LY_TREE_FOR(data_tree->child, child) {
2192 node_add_metadata_recursive(child, cur_module, child_json);
2193 }
2194 }
2195 }
2196}
2197
2198static void
Michal Vaskoa45770b2015-11-23 15:49:41 +01002199node_add_model_metadata(struct lys_module *module, json_object *parent)
2200{
2201 json_object *obj;
2202 char *str;
2203
2204 obj = json_object_new_object();
2205 node_metadata_model(module, obj);
2206 asprintf(&str, "$@@%s", module->name);
2207 json_object_object_add(parent, str, obj);
2208 free(str);
2209}
2210
2211static void
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002212node_add_children_with_metadata_recursive(struct lys_node *node, struct lys_module *module, json_object *parent)
2213{
2214 struct lys_module *cur_module;
2215 struct lys_node *child;
2216 json_object *node_json;
2217 char *json_name;
2218
Michal Vaskoa45770b2015-11-23 15:49:41 +01002219 if (node->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
2220 return;
2221 }
2222
2223 if (node->nodetype & LYS_USES) {
2224 cur_module = module;
2225 node_json = parent;
2226 goto children;
2227 }
2228
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002229 /* add node metadata */
2230 if (node_add_metadata(node, module, parent)) {
2231 ERROR("Internal: metadata duplicate for \"%s\".", node->name);
2232 return;
2233 }
2234
Michal Vaskoa45770b2015-11-23 15:49:41 +01002235 /* no other metadata */
2236 if (!node->child) {
2237 return;
2238 }
2239
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002240 /* get node module */
2241 cur_module = node->module;
2242 if (cur_module->type) {
2243 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
2244 }
2245
2246 /* create JSON object for child metadata */
2247 node_json = json_object_new_object();
2248 if (cur_module == module) {
2249 json_object_object_add(parent, node->name, node_json);
2250 } else {
2251 asprintf(&json_name, "%s:%s", cur_module->name, node->name);
2252 json_object_object_add(parent, json_name, node_json);
2253 free(json_name);
2254 }
2255
Michal Vaskoa45770b2015-11-23 15:49:41 +01002256children:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002257 LY_TREE_FOR(node->child, child) {
2258 node_add_children_with_metadata_recursive(child, cur_module, node_json);
2259 }
2260}
2261
2262static json_object *
2263libyang_query(unsigned int session_key, const char *filter, int load_children)
2264{
2265 struct lys_node *node;
Michal Vaskoa45770b2015-11-23 15:49:41 +01002266 struct lys_module *module = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002267 struct session_with_mutex *locked_session;
2268 json_object *ret = NULL, *data;
2269
2270 locked_session = session_get_locked(session_key, &ret);
2271 if (!locked_session) {
2272 ERROR("Locking failed or session not found.");
2273 goto finish;
2274 }
2275
Michal Vaskoe32bcba2015-11-24 09:05:51 +01002276 session_user_activity(nc_session_get_user(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002277
Michal Vaskoa45770b2015-11-23 15:49:41 +01002278 if (filter[0] == '/') {
2279 node = ly_ctx_get_node(locked_session->ctx, filter);
2280 if (!node) {
2281 ret = create_error_reply("Failed to resolve XPath filter node.");
2282 goto finish;
2283 }
2284 } else {
2285 module = ly_ctx_get_module(locked_session->ctx, filter, NULL);
2286 if (!module) {
2287 ret = create_error_reply("Failed to find model.");
2288 goto finish;
2289 }
2290 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002291
Michal Vaskoa45770b2015-11-23 15:49:41 +01002292 pthread_mutex_lock(&json_lock);
2293 data = json_object_new_object();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002294
Michal Vaskoa45770b2015-11-23 15:49:41 +01002295 if (module) {
2296 node_add_model_metadata(module, data);
2297 if (load_children) {
2298 LY_TREE_FOR(module->data, node) {
2299 node_add_children_with_metadata_recursive(node, NULL, data);
2300 }
2301 }
2302 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002303 if (load_children) {
2304 node_add_children_with_metadata_recursive(node, NULL, data);
2305 } else {
2306 node_add_metadata(node, NULL, data);
2307 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002308 }
2309
Michal Vaskoa45770b2015-11-23 15:49:41 +01002310 pthread_mutex_unlock(&json_lock);
2311 ret = create_data_reply(json_object_to_json_string(data));
2312 json_object_put(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002313
2314finish:
Michal Vaskoa45770b2015-11-23 15:49:41 +01002315 session_unlock(locked_session);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002316 return ret;
2317}
2318
2319static json_object *
2320libyang_merge(unsigned int session_key, const char *config)
2321{
2322 struct lyd_node *data_tree = NULL, *sibling;
2323 struct session_with_mutex *locked_session;
2324 json_object *ret = NULL, *data_json = NULL;
2325 enum json_tokener_error err = 0;
2326
2327 locked_session = session_get_locked(session_key, &ret);
2328 if (!locked_session) {
2329 ERROR("Locking failed or session not found.");
2330 goto finish;
2331 }
2332
Michal Vaskoe32bcba2015-11-24 09:05:51 +01002333 session_user_activity(nc_session_get_user(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002334
2335 data_tree = lyd_parse(locked_session->ctx, config, LYD_JSON, LYD_OPT_STRICT);
2336 if (!data_tree) {
2337 ERROR("Creating data tree failed.");
2338 ret = create_error_reply("Failed to create data tree from JSON config.");
2339 session_unlock(locked_session);
2340 goto finish;
2341 }
2342
2343 session_unlock(locked_session);
2344
2345 pthread_mutex_lock(&json_lock);
2346 data_json = json_tokener_parse_verbose(config, &err);
2347 if (!data_json) {
2348 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(err));
2349 pthread_mutex_unlock(&json_lock);
2350 ret = create_error_reply(json_tokener_error_desc(err));
2351 goto finish;
2352 }
2353
2354 /* go simultaneously through both trees and add metadata */
2355 LY_TREE_FOR(data_tree, sibling) {
2356 node_add_metadata_recursive(sibling, NULL, data_json);
2357 }
2358 pthread_mutex_unlock(&json_lock);
2359 ret = create_data_reply(json_object_to_json_string(data_json));
2360
2361finish:
2362 LY_TREE_FOR(data_tree, sibling) {
2363 lyd_free(sibling);
2364 }
2365 json_object_put(data_json);
2366 return ret;
Radek Krejci80c10d92012-07-30 08:38:50 +02002367}
2368
Tomas Cejka0a4bba82013-04-19 11:51:28 +02002369/**
2370 * @}
2371 *//* netconf_operations */
2372
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002373void
2374clb_print(NC_VERB_LEVEL level, const char *msg)
Radek Krejci469aab82012-07-22 18:42:20 +02002375{
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002376#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +02002377 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
2378
2379#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002380 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002381
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002382 switch (level) {
2383 FOREACH(CASE);
2384 case NC_VERB_VERBOSE:
2385 case NC_VERB_DEBUG:
2386 DEBUG("DEBUG: %s", msg);
2387 break;
2388 }
2389 if (level == NC_VERB_ERROR) {
2390 /* return global error */
2391 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
2392 NULL /* severity */, NULL /* apptag */,
2393 NULL /* path */, msg, NULL /* attribute */,
2394 NULL /* element */, NULL /* ns */, NULL /* sid */);
2395 }
Radek Krejci469aab82012-07-22 18:42:20 +02002396}
2397
Tomas Cejka64b87482013-06-03 16:30:53 +02002398/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02002399 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02002400 * Caller should free message memory.
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002401 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02002402 * \return pointer to message
2403 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002404char *
2405get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02002406{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002407 /* read json in chunked framing */
2408 unsigned int buffer_size = 0;
2409 ssize_t buffer_len = 0;
2410 char *buffer = NULL;
2411 char c;
2412 ssize_t ret;
2413 int i, chunk_len;
2414 char chunk_len_str[12];
Tomas Cejka64b87482013-06-03 16:30:53 +02002415
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002416 while (1) {
2417 /* read chunk length */
2418 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
2419 if (buffer != NULL) {
2420 free (buffer);
2421 buffer = NULL;
2422 }
2423 break;
2424 }
2425 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
2426 if (buffer != NULL) {
2427 free (buffer);
2428 buffer = NULL;
2429 }
2430 break;
2431 }
2432 i=0;
2433 memset (chunk_len_str, 0, 12);
2434 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
2435 if (i==0 && c == '#') {
2436 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
2437 /* end but invalid */
2438 if (buffer != NULL) {
2439 free (buffer);
2440 buffer = NULL;
2441 }
2442 }
2443 /* end of message, double-loop break */
2444 goto msg_complete;
2445 }
2446 chunk_len_str[i++] = c;
2447 if (i==11) {
2448 ERROR("Message is too long, buffer for length is not big enought!!!!");
2449 break;
2450 }
2451 }
2452 if (c != '\n') {
2453 if (buffer != NULL) {
2454 free (buffer);
2455 buffer = NULL;
2456 }
2457 break;
2458 }
2459 chunk_len_str[i] = 0;
2460 if ((chunk_len = atoi (chunk_len_str)) == 0) {
2461 if (buffer != NULL) {
2462 free (buffer);
2463 buffer = NULL;
2464 }
2465 break;
2466 }
2467 buffer_size += chunk_len+1;
2468 buffer = realloc (buffer, sizeof(char)*buffer_size);
2469 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
2470 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
2471 if (buffer != NULL) {
2472 free (buffer);
2473 buffer = NULL;
2474 }
2475 break;
2476 }
2477 buffer_len += ret;
2478 }
Tomas Cejka64b87482013-06-03 16:30:53 +02002479msg_complete:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002480 return buffer;
Tomas Cejka64b87482013-06-03 16:30:53 +02002481}
2482
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002483NC_DATASTORE
2484parse_datastore(const char *ds)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002485{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002486 if (strcmp(ds, "running") == 0) {
2487 return NC_DATASTORE_RUNNING;
2488 } else if (strcmp(ds, "startup") == 0) {
2489 return NC_DATASTORE_STARTUP;
2490 } else if (strcmp(ds, "candidate") == 0) {
2491 return NC_DATASTORE_CANDIDATE;
2492 } else if (strcmp(ds, "url") == 0) {
2493 return NC_DATASTORE_URL;
2494 } else if (strcmp(ds, "config") == 0) {
2495 return NC_DATASTORE_CONFIG;
2496 }
2497 return -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002498}
2499
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002500NC_EDIT_TESTOPT_TYPE
2501parse_testopt(const char *t)
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002502{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002503 if (strcmp(t, "notset") == 0) {
2504 return NC_EDIT_TESTOPT_NOTSET;
2505 } else if (strcmp(t, "testset") == 0) {
2506 return NC_EDIT_TESTOPT_TESTSET;
2507 } else if (strcmp(t, "set") == 0) {
2508 return NC_EDIT_TESTOPT_SET;
2509 } else if (strcmp(t, "test") == 0) {
2510 return NC_EDIT_TESTOPT_TEST;
2511 }
2512 return NC_EDIT_TESTOPT_ERROR;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002513}
2514
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002515json_object *
2516create_error_reply(const char *errmess)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002517{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002518 json_object *reply, *array;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002519
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002520 pthread_mutex_lock(&json_lock);
2521 reply = json_object_new_object();
2522 array = json_object_new_array();
2523 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2524 json_object_array_add(array, json_object_new_string(errmess));
2525 json_object_object_add(reply, "errors", array);
2526 pthread_mutex_unlock(&json_lock);
2527
2528 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002529}
2530
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002531json_object *
2532create_data_reply(const char *data)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002533{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002534 pthread_mutex_lock(&json_lock);
2535 json_object *reply = json_object_new_object();
2536 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
2537 json_object_object_add(reply, "data", json_object_new_string(data));
2538 pthread_mutex_unlock(&json_lock);
2539 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002540}
2541
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002542json_object *
2543create_ok_reply(void)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002544{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002545 pthread_mutex_lock(&json_lock);
2546 json_object *reply = json_object_new_object();
2547 reply = json_object_new_object();
2548 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2549 pthread_mutex_unlock(&json_lock);
2550 return reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002551}
2552
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002553json_object *
2554create_replies(void)
Tomas Cejka09629492014-07-10 15:58:06 +02002555{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002556 json_object *replies;
2557
2558 pthread_mutex_lock(&json_lock);
2559 replies = json_object_new_object();
2560 pthread_mutex_unlock(&json_lock);
2561
2562 return replies;
Tomas Cejka09629492014-07-10 15:58:06 +02002563}
2564
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002565void
2566add_reply(json_object *replies, json_object *reply, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002567{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002568 char *str;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002569
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002570 asprintf(&str, "%u", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002571
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002572 pthread_mutex_lock(&json_lock);
2573 json_object_object_add(replies, str, reply);
2574 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002575
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002576 free(str);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002577}
2578
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002579char *
2580get_param_string(json_object *data, const char *name)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002581{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002582 json_object *js_tmp = NULL;
2583 char *res = NULL;
2584 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
2585 res = strdup(json_object_get_string(js_tmp));
2586 }
2587 return res;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002588}
2589
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002590json_object *
2591handle_op_connect(json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002592{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002593 char *host = NULL;
2594 char *port = NULL;
2595 char *user = NULL;
2596 char *pass = NULL;
2597 json_object *reply = NULL;
2598 unsigned int session_key = 0;
2599 struct nc_cpblts* cpblts = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002600
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002601 DEBUG("Request: connect");
2602 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002603
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002604 host = get_param_string(request, "host");
2605 port = get_param_string(request, "port");
2606 user = get_param_string(request, "user");
2607 pass = get_param_string(request, "pass");
Tomas Cejkad5b53772013-06-08 23:01:07 +02002608
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002609 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002610
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002611 DEBUG("host: %s, port: %s, user: %s", host, port, user);
2612 if ((host == NULL) || (user == NULL)) {
2613 ERROR("Cannot connect - insufficient input.");
2614 session_key = 0;
2615 } else {
2616 session_key = netconf_connect(host, port, user, pass, cpblts);
2617 DEBUG("Session key: %u", session_key);
2618 }
2619 if (cpblts != NULL) {
2620 nc_cpblts_free(cpblts);
2621 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002622
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002623 GETSPEC_ERR_REPLY
2624
2625 pthread_mutex_lock(&json_lock);
2626 if (session_key == 0) {
2627 /* negative reply */
2628 if (err_reply == NULL) {
2629 reply = json_object_new_object();
2630 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2631 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
2632 ERROR("Connection failed.");
2633 } else {
2634 /* use filled err_reply from libnetconf's callback */
2635 reply = err_reply;
2636 ERROR("Connect - error from libnetconf's callback.");
2637 }
2638 } else {
2639 /* positive reply */
2640 reply = json_object_new_object();
2641 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2642 json_object_object_add(reply, "session", json_object_new_int(session_key));
2643 }
2644 memset(pass, 0, strlen(pass));
2645 pthread_mutex_unlock(&json_lock);
2646 CHECK_AND_FREE(host);
2647 CHECK_AND_FREE(user);
2648 CHECK_AND_FREE(port);
2649 CHECK_AND_FREE(pass);
2650 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002651}
2652
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002653json_object *
2654handle_op_disconnect(json_object *UNUSED(request), unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002655{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002656 json_object *reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002657
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002658 DEBUG("Request: disconnect (session %u)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002659
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002660 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
2661 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
2662 } else {
2663 reply = create_ok_reply();
2664 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002665
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002666 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002667}
2668
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002669json_object *
2670handle_op_get(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002671{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002672 char *filter = NULL;
2673 char *data = NULL;
2674 json_object *reply = NULL, *obj;
2675 int strict;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002676
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002677 DEBUG("Request: get (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002678
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002679 pthread_mutex_lock(&json_lock);
2680 filter = get_param_string(request, "filter");
2681 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2682 pthread_mutex_unlock(&json_lock);
2683 reply = create_error_reply("Missing strict parameter.");
2684 return reply;
2685 }
2686 strict = json_object_get_boolean(obj);
2687 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002688
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002689 if ((data = netconf_get(session_key, filter, strict, &reply)) == NULL) {
2690 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
2691 } else {
2692 reply = create_data_reply(data);
2693 free(data);
2694 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002695
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002696 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002697}
2698
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002699json_object *
2700handle_op_getconfig(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002701{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002702 NC_DATASTORE ds_type_s = -1;
2703 char *filter = NULL;
2704 char *data = NULL;
2705 char *source = NULL;
2706 json_object *reply = NULL, *obj;
2707 int strict;
Tomas Cejkab4d05872014-02-14 22:44:38 +01002708
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002709 DEBUG("Request: get-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002710
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002711 pthread_mutex_lock(&json_lock);
2712 filter = get_param_string(request, "filter");
2713 source = get_param_string(request, "source");
2714 if (source != NULL) {
2715 ds_type_s = parse_datastore(source);
2716 }
2717 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2718 pthread_mutex_unlock(&json_lock);
2719 reply = create_error_reply("Missing strict parameter.");
2720 return reply;
2721 }
2722 strict = json_object_get_boolean(obj);
2723 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002724
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002725 if ((int)ds_type_s == -1) {
2726 reply = create_error_reply("Invalid source repository type requested.");
2727 goto finalize;
2728 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002729
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002730 if ((data = netconf_getconfig(session_key, ds_type_s, filter, strict, &reply)) == NULL) {
2731 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
2732 } else {
2733 reply = create_data_reply(data);
2734 free(data);
2735 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002736
Tomas Cejka09629492014-07-10 15:58:06 +02002737finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002738 CHECK_AND_FREE(filter);
2739 CHECK_AND_FREE(source);
2740 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002741}
2742
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002743json_object *
2744handle_op_editconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002745{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002746 NC_DATASTORE ds_type_s = -1;
2747 NC_DATASTORE ds_type_t = -1;
2748 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
2749 NC_EDIT_ERROPT_TYPE erropt_type = 0;
2750 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
2751 char *defop = NULL;
2752 char *erropt = NULL;
2753 char *config = NULL;
2754 char *source = NULL;
2755 char *target = NULL;
2756 char *testopt = NULL;
2757 char *urisource = NULL;
2758 json_object *reply = NULL, *configs, *obj;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002759
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002760 DEBUG("Request: edit-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002761
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002762 pthread_mutex_lock(&json_lock);
2763 /* get parameters */
2764 target = get_param_string(request, "target");
2765 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2766 pthread_mutex_unlock(&json_lock);
2767 reply = create_error_reply("Missing configs parameter.");
2768 goto finalize;
2769 }
2770 obj = json_object_array_get_idx(configs, idx);
2771 config = strdup(json_object_get_string(obj));
Tomas Cejkad5b53772013-06-08 23:01:07 +02002772
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002773 source = get_param_string(request, "source");
2774 defop = get_param_string(request, "default-operation");
2775 erropt = get_param_string(request, "error-option");
2776 urisource = get_param_string(request, "uri-source");
2777 testopt = get_param_string(request, "test-option");
2778 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002779
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002780 if (target != NULL) {
2781 ds_type_t = parse_datastore(target);
2782 }
2783 if (source != NULL) {
2784 ds_type_s = parse_datastore(source);
2785 } else {
2786 /* source is optional, default value is config */
2787 ds_type_s = NC_DATASTORE_CONFIG;
2788 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002789
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002790 if (defop != NULL) {
2791 if (strcmp(defop, "merge") == 0) {
2792 defop_type = NC_EDIT_DEFOP_MERGE;
2793 } else if (strcmp(defop, "replace") == 0) {
2794 defop_type = NC_EDIT_DEFOP_REPLACE;
2795 } else if (strcmp(defop, "none") == 0) {
2796 defop_type = NC_EDIT_DEFOP_NONE;
2797 } else {
2798 reply = create_error_reply("Invalid default-operation parameter.");
2799 goto finalize;
2800 }
2801 } else {
2802 defop_type = NC_EDIT_DEFOP_NOTSET;
2803 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002804
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002805 if (erropt != NULL) {
2806 if (strcmp(erropt, "continue-on-error") == 0) {
2807 erropt_type = NC_EDIT_ERROPT_CONT;
2808 } else if (strcmp(erropt, "stop-on-error") == 0) {
2809 erropt_type = NC_EDIT_ERROPT_STOP;
2810 } else if (strcmp(erropt, "rollback-on-error") == 0) {
2811 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
2812 } else {
2813 reply = create_error_reply("Invalid error-option parameter.");
2814 goto finalize;
2815 }
2816 } else {
2817 erropt_type = 0;
2818 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002819
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002820 if ((int)ds_type_t == -1) {
2821 reply = create_error_reply("Invalid target repository type requested.");
2822 goto finalize;
2823 }
2824 if (ds_type_s == NC_DATASTORE_CONFIG) {
2825 if (config == NULL) {
2826 reply = create_error_reply("Invalid config data parameter.");
2827 goto finalize;
2828 }
2829 } else if (ds_type_s == NC_DATASTORE_URL){
2830 if (urisource == NULL) {
2831 reply = create_error_reply("Invalid uri-source parameter.");
2832 goto finalize;
2833 }
2834 config = urisource;
2835 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002836
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002837 if (testopt != NULL) {
2838 testopt_type = parse_testopt(testopt);
2839 } else {
2840 testopt_type = NC_EDIT_TESTOPT_TESTSET;
2841 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002842
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002843 reply = netconf_editconfig(session_key, ds_type_s, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002844
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002845 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002846
Tomas Cejka09629492014-07-10 15:58:06 +02002847finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002848 CHECK_AND_FREE(defop);
2849 CHECK_AND_FREE(erropt);
2850 CHECK_AND_FREE(config);
2851 CHECK_AND_FREE(source);
2852 CHECK_AND_FREE(urisource);
2853 CHECK_AND_FREE(target);
2854 CHECK_AND_FREE(testopt);
2855
2856 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002857}
2858
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002859json_object *
2860handle_op_copyconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002861{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002862 NC_DATASTORE ds_type_s = -1;
2863 NC_DATASTORE ds_type_t = -1;
2864 char *config = NULL;
2865 char *target = NULL;
2866 char *source = NULL;
2867 char *uri_src = NULL;
2868 char *uri_trg = NULL;
2869 json_object *reply = NULL, *configs, *obj;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002870
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002871 DEBUG("Request: copy-config (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002872
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002873 /* get parameters */
2874 pthread_mutex_lock(&json_lock);
2875 target = get_param_string(request, "target");
2876 source = get_param_string(request, "source");
2877 uri_src = get_param_string(request, "uri-source");
2878 uri_trg = get_param_string(request, "uri-target");
2879 if (!strcmp(source, "config")) {
2880 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2881 pthread_mutex_unlock(&json_lock);
2882 reply = create_error_reply("Missing configs parameter.");
2883 goto finalize;
2884 }
2885 obj = json_object_array_get_idx(configs, idx);
2886 if (!obj) {
2887 pthread_mutex_unlock(&json_lock);
2888 reply = create_error_reply("Configs array parameter shorter than sessions.");
2889 goto finalize;
2890 }
2891 config = strdup(json_object_get_string(obj));
2892 }
2893 pthread_mutex_unlock(&json_lock);
2894
2895 if (target != NULL) {
2896 ds_type_t = parse_datastore(target);
2897 }
2898 if (source != NULL) {
2899 ds_type_s = parse_datastore(source);
2900 }
2901
2902 if ((int)ds_type_s == -1) {
2903 /* invalid source datastore specified */
2904 reply = create_error_reply("Invalid source repository type requested.");
2905 goto finalize;
2906 }
2907
2908 if ((int)ds_type_t == -1) {
2909 /* invalid target datastore specified */
2910 reply = create_error_reply("Invalid target repository type requested.");
2911 goto finalize;
2912 }
2913
2914 if (ds_type_s == NC_DATASTORE_URL) {
2915 if (uri_src == NULL) {
2916 uri_src = "";
2917 }
2918 }
2919 if (ds_type_t == NC_DATASTORE_URL) {
2920 if (uri_trg == NULL) {
2921 uri_trg = "";
2922 }
2923 }
2924 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
2925
2926 CHECK_ERR_SET_REPLY
2927
2928finalize:
2929 CHECK_AND_FREE(config);
2930 CHECK_AND_FREE(target);
2931 CHECK_AND_FREE(source);
2932 CHECK_AND_FREE(uri_src);
2933 CHECK_AND_FREE(uri_trg);
2934
2935 return reply;
2936}
2937
2938json_object *
2939handle_op_deleteconfig(json_object *request, unsigned int session_key)
2940{
2941 json_object *reply;
2942 NC_DATASTORE ds_type = -1;
2943 char *target, *url;
2944
2945 DEBUG("Request: delete-config (session %u)", session_key);
2946
2947 pthread_mutex_lock(&json_lock);
2948 target = get_param_string(request, "target");
2949 url = get_param_string(request, "url");
2950 pthread_mutex_unlock(&json_lock);
2951
2952 if (target != NULL) {
2953 ds_type = parse_datastore(target);
2954 }
2955 if ((int)ds_type == -1) {
2956 reply = create_error_reply("Invalid target repository type requested.");
2957 goto finalize;
2958 }
2959 if (ds_type == NC_DATASTORE_URL) {
2960 if (!url) {
2961 url = "";
2962 }
2963 }
2964
2965 reply = netconf_deleteconfig(session_key, ds_type, url);
2966
2967 CHECK_ERR_SET_REPLY
2968 if (reply == NULL) {
2969 reply = create_ok_reply();
2970 }
2971
2972finalize:
2973 CHECK_AND_FREE(target);
2974 CHECK_AND_FREE(url);
2975 return reply;
2976}
2977
2978json_object *
2979handle_op_lock(json_object *request, unsigned int session_key)
2980{
2981 json_object *reply;
2982 NC_DATASTORE ds_type = -1;
2983 char *target;
2984
2985 DEBUG("Request: lock (session %u)", session_key);
2986
2987 pthread_mutex_lock(&json_lock);
2988 target = get_param_string(request, "target");
2989 pthread_mutex_unlock(&json_lock);
2990
2991 if (target != NULL) {
2992 ds_type = parse_datastore(target);
2993 }
2994 if ((int)ds_type == -1) {
2995 reply = create_error_reply("Invalid target repository type requested.");
2996 goto finalize;
2997 }
2998
2999 reply = netconf_lock(session_key, ds_type);
3000
3001 CHECK_ERR_SET_REPLY
3002 if (reply == NULL) {
3003 reply = create_ok_reply();
3004 }
3005
3006finalize:
3007 CHECK_AND_FREE(target);
3008 return reply;
3009}
3010
3011json_object *
3012handle_op_unlock(json_object *request, unsigned int session_key)
3013{
3014 json_object *reply;
3015 NC_DATASTORE ds_type = -1;
3016 char *target;
3017
3018 DEBUG("Request: unlock (session %u)", session_key);
3019
3020 pthread_mutex_lock(&json_lock);
3021 target = get_param_string(request, "target");
3022 pthread_mutex_unlock(&json_lock);
3023
3024 if (target != NULL) {
3025 ds_type = parse_datastore(target);
3026 }
3027 if ((int)ds_type == -1) {
3028 reply = create_error_reply("Invalid target repository type requested.");
3029 goto finalize;
3030 }
3031
3032 reply = netconf_unlock(session_key, ds_type);
3033
3034 CHECK_ERR_SET_REPLY
3035 if (reply == NULL) {
3036 reply = create_ok_reply();
3037 }
3038
3039finalize:
3040 CHECK_AND_FREE(target);
3041 return reply;
3042}
3043
3044json_object *
3045handle_op_kill(json_object *request, unsigned int session_key)
3046{
3047 json_object *reply = NULL;
3048 char *sid = NULL;
3049
3050 DEBUG("Request: kill-session (session %u)", session_key);
3051
3052 pthread_mutex_lock(&json_lock);
3053 sid = get_param_string(request, "session-id");
3054 pthread_mutex_unlock(&json_lock);
3055
3056 if (sid == NULL) {
3057 reply = create_error_reply("Missing session-id parameter.");
3058 goto finalize;
3059 }
3060
3061 reply = netconf_killsession(session_key, sid);
3062
3063 CHECK_ERR_SET_REPLY
3064
3065finalize:
3066 CHECK_AND_FREE(sid);
3067 return reply;
3068}
3069
3070json_object *
3071handle_op_info(json_object *UNUSED(request), unsigned int session_key)
3072{
3073 json_object *reply = NULL;
3074 struct session_with_mutex *locked_session = NULL;
3075 DEBUG("Request: get info about session %u", session_key);
3076
3077 DEBUG("LOCK wrlock %s", __func__);
3078 if (pthread_rwlock_rdlock(&session_lock) != 0) {
3079 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3080 }
3081
3082 for (locked_session = netconf_sessions_list;
3083 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003084 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003085 if (locked_session != NULL) {
3086 DEBUG("LOCK mutex %s", __func__);
3087 pthread_mutex_lock(&locked_session->lock);
3088 DEBUG("UNLOCK wrlock %s", __func__);
3089 if (pthread_rwlock_unlock(&session_lock) != 0) {
3090 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3091 }
3092 if (locked_session->hello_message != NULL) {
3093 reply = locked_session->hello_message;
3094 } else {
3095 reply = create_error_reply("Invalid session identifier.");
3096 }
3097 DEBUG("UNLOCK mutex %s", __func__);
3098 pthread_mutex_unlock(&locked_session->lock);
3099 } else {
3100 DEBUG("UNLOCK wrlock %s", __func__);
3101 if (pthread_rwlock_unlock(&session_lock) != 0) {
3102 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3103 }
3104 reply = create_error_reply("Invalid session identifier.");
3105 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02003106
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003107 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02003108}
3109
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003110json_object *
3111handle_op_generic(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02003112{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003113 json_object *reply = NULL, *contents, *obj;
3114 char *config = NULL;
3115 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02003116
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003117 DEBUG("Request: generic request (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02003118
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003119 pthread_mutex_lock(&json_lock);
3120 if (json_object_object_get_ex(request, "contents", &contents) == FALSE) {
3121 pthread_mutex_unlock(&json_lock);
3122 reply = create_error_reply("Missing contents parameter.");
3123 goto finalize;
3124 }
3125 obj = json_object_array_get_idx(contents, idx);
3126 if (!obj) {
3127 pthread_mutex_unlock(&json_lock);
3128 reply = create_error_reply("Contents array parameter shorter than sessions.");
3129 goto finalize;
3130 }
3131 config = strdup(json_object_get_string(obj));
3132 pthread_mutex_unlock(&json_lock);
3133
3134 reply = netconf_generic(session_key, config, &data);
3135 if (reply == NULL) {
3136 GETSPEC_ERR_REPLY
3137 if (err_reply != NULL) {
3138 /* use filled err_reply from libnetconf's callback */
3139 reply = err_reply;
3140 }
3141 } else {
3142 if (data == NULL) {
3143 pthread_mutex_lock(&json_lock);
3144 reply = json_object_new_object();
3145 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
3146 pthread_mutex_unlock(&json_lock);
3147 } else {
3148 reply = create_data_reply(data);
3149 free(data);
3150 }
3151 }
3152
3153finalize:
3154 CHECK_AND_FREE(config);
3155 return reply;
3156}
3157
3158json_object *
3159handle_op_getschema(json_object *request, unsigned int session_key)
3160{
3161 char *data = NULL;
3162 char *identifier = NULL;
3163 char *version = NULL;
3164 char *format = NULL;
3165 json_object *reply = NULL;
3166
3167 DEBUG("Request: get-schema (session %u)", session_key);
3168
3169 pthread_mutex_lock(&json_lock);
3170 identifier = get_param_string(request, "identifier");
3171 version = get_param_string(request, "version");
3172 format = get_param_string(request, "format");
3173 pthread_mutex_unlock(&json_lock);
3174
3175 if (identifier == NULL) {
3176 reply = create_error_reply("No identifier for get-schema supplied.");
3177 goto finalize;
3178 }
3179
3180 DEBUG("get-schema(version: %s, format: %s)", version, format);
3181 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
3182 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
3183 } else {
3184 reply = create_data_reply(data);
3185 free(data);
3186 }
3187
3188finalize:
3189 CHECK_AND_FREE(identifier);
3190 CHECK_AND_FREE(version);
3191 CHECK_AND_FREE(format);
3192 return reply;
3193}
3194
3195json_object *
3196handle_op_reloadhello(json_object *UNUSED(request), unsigned int session_key)
3197{
3198 struct nc_session *temp_session = NULL;
3199 struct session_with_mutex * locked_session = NULL;
3200 json_object *reply = NULL;
3201
3202 DEBUG("Request: reload hello (session %u)", session_key);
3203
3204 DEBUG("LOCK wrlock %s", __func__);
3205 if (pthread_rwlock_wrlock(&session_lock) != 0) {
3206 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3207 return NULL;
3208 }
3209
3210 for (locked_session = netconf_sessions_list;
3211 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003212 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003213 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
3214 DEBUG("LOCK mutex %s", __func__);
3215 pthread_mutex_lock(&locked_session->lock);
3216 DEBUG("creating temporary NC session.");
3217 temp_session = nc_session_connect_channel(locked_session->session, NULL);
3218 if (temp_session != NULL) {
3219 prepare_status_message(locked_session, temp_session);
3220 DEBUG("closing temporal NC session.");
3221 nc_session_free(temp_session);
3222 temp_session = NULL;
3223 } else {
3224 DEBUG("Reload hello failed due to channel establishment");
3225 reply = create_error_reply("Reload was unsuccessful, connection failed.");
3226 }
3227 DEBUG("UNLOCK mutex %s", __func__);
3228 pthread_mutex_unlock(&locked_session->lock);
3229 DEBUG("UNLOCK wrlock %s", __func__);
3230 if (pthread_rwlock_unlock(&session_lock) != 0) {
3231 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3232 }
3233 } else {
3234 DEBUG("UNLOCK wrlock %s", __func__);
3235 if (pthread_rwlock_unlock(&session_lock) != 0) {
3236 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3237 }
3238 reply = create_error_reply("Invalid session identifier.");
3239 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02003240
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003241 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
3242 reply = locked_session->hello_message;
3243 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02003244
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003245 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02003246}
3247
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003248void
3249notification_history(time_t eventtime, const char *content)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003250{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003251 json_object *notif_history_array = (json_object *)pthread_getspecific(notif_history_key);
3252 if (notif_history_array == NULL) {
3253 ERROR("No list of notification history found.");
3254 return;
3255 }
3256 DEBUG("Got notification from history %lu.", (long unsigned)eventtime);
3257 pthread_mutex_lock(&json_lock);
3258 json_object *notif = json_object_new_object();
3259 if (notif == NULL) {
3260 ERROR("Could not allocate memory for notification (json).");
3261 goto failed;
3262 }
3263 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
3264 json_object_object_add(notif, "content", json_object_new_string(content));
3265 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003266failed:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003267 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003268}
3269
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003270json_object *
3271handle_op_ntfgethistory(json_object *request, unsigned int session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003272{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003273 json_object *reply = NULL;
3274 json_object *js_tmp = NULL;
3275 struct session_with_mutex *locked_session = NULL;
3276 struct nc_session *temp_session = NULL;
3277 nc_rpc *rpc = NULL;
3278 time_t start = 0;
3279 time_t stop = 0;
3280 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003281
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003282 DEBUG("Request: get notification history (session %u)", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003283
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003284 pthread_mutex_lock(&json_lock);
3285 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
3286 from = json_object_get_int64(js_tmp);
3287 }
3288 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
3289 to = json_object_get_int64(js_tmp);
3290 }
3291 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02003292
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003293 start = time(NULL) + from;
3294 stop = time(NULL) + to;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003295
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003296 DEBUG("notification history interval %li %li", (long int)from, (long int)to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003297
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003298 DEBUG("LOCK wrlock %s", __func__);
3299 if (pthread_rwlock_rdlock(&session_lock) != 0) {
3300 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3301 reply = create_error_reply("Internal lock failed.");
3302 goto finalize;
3303 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003304
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003305 for (locked_session = netconf_sessions_list;
3306 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003307 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003308 if (locked_session != NULL) {
3309 DEBUG("LOCK mutex %s", __func__);
3310 pthread_mutex_lock(&locked_session->lock);
3311 DEBUG("UNLOCK wrlock %s", __func__);
3312 if (pthread_rwlock_unlock(&session_lock) != 0) {
3313 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3314 }
3315 DEBUG("creating temporal NC session.");
3316 temp_session = nc_session_connect_channel(locked_session->session, NULL);
3317 if (temp_session != NULL) {
3318 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
3319 if (rpc == NULL) {
3320 DEBUG("UNLOCK mutex %s", __func__);
3321 pthread_mutex_unlock(&locked_session->lock);
3322 DEBUG("notifications: creating an rpc request failed.");
3323 reply = create_error_reply("notifications: creating an rpc request failed.");
3324 goto finalize;
3325 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003326
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003327 DEBUG("Send NC subscribe.");
3328 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
3329 json_object *res = netconf_unlocked_op(temp_session, rpc);
3330 if (res != NULL) {
3331 DEBUG("UNLOCK mutex %s", __func__);
3332 pthread_mutex_unlock(&locked_session->lock);
3333 DEBUG("Subscription RPC failed.");
3334 reply = res;
3335 goto finalize;
3336 }
3337 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka6b886e02013-07-05 09:53:17 +02003338
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003339 DEBUG("UNLOCK mutex %s", __func__);
3340 pthread_mutex_unlock(&locked_session->lock);
3341 DEBUG("LOCK mutex %s", __func__);
3342 pthread_mutex_lock(&ntf_history_lock);
3343 pthread_mutex_lock(&json_lock);
3344 json_object *notif_history_array = json_object_new_array();
3345 pthread_mutex_unlock(&json_lock);
3346 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
3347 ERROR("notif_history: cannot set thread-specific hash value.");
3348 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003349
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003350 ncntf_dispatch_receive(temp_session, notification_history);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003351
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003352 pthread_mutex_lock(&json_lock);
3353 reply = json_object_new_object();
3354 json_object_object_add(reply, "notifications", notif_history_array);
3355 //json_object_put(notif_history_array);
3356 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003357
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003358 DEBUG("UNLOCK mutex %s", __func__);
3359 pthread_mutex_unlock(&ntf_history_lock);
3360 DEBUG("closing temporal NC session.");
3361 nc_session_free(temp_session);
3362 temp_session = NULL;
3363 } else {
3364 DEBUG("UNLOCK mutex %s", __func__);
3365 pthread_mutex_unlock(&locked_session->lock);
3366 DEBUG("Get history of notification failed due to channel establishment");
3367 reply = create_error_reply("Get history of notification was unsuccessful, connection failed.");
3368 }
3369 } else {
3370 DEBUG("UNLOCK wrlock %s", __func__);
3371 if (pthread_rwlock_unlock(&session_lock) != 0) {
3372 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3373 }
3374 reply = create_error_reply("Invalid session identifier.");
3375 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003376
Tomas Cejka09629492014-07-10 15:58:06 +02003377finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003378 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02003379}
3380
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003381json_object *
3382handle_op_validate(json_object *request, unsigned int session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02003383{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003384 json_object *reply = NULL;
3385 char *target = NULL;
3386 char *url = NULL;
3387 nc_rpc *rpc = NULL;
3388 NC_DATASTORE target_ds;
Tomas Cejka4003a702013-10-01 00:02:45 +02003389
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003390 DEBUG("Request: validate datastore (session %u)", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02003391
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003392 pthread_mutex_lock(&json_lock);
3393 target = get_param_string(request, "target");
3394 url = get_param_string(request, "url");
3395 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02003396
3397
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003398 if (target == NULL) {
3399 reply = create_error_reply("Missing target parameter.");
3400 goto finalize;
3401 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003402
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003403 /* validation */
3404 target_ds = parse_datastore(target);
3405 if (target_ds == NC_DATASTORE_URL) {
3406 if (url != NULL) {
3407 rpc = nc_rpc_validate(target_ds, url);
3408 }
3409 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
3410 || (target_ds == NC_DATASTORE_CANDIDATE)) {
3411 rpc = nc_rpc_validate(target_ds);
3412 }
3413 if (rpc == NULL) {
3414 DEBUG("mod_netconf: creating rpc request failed");
3415 reply = create_error_reply("Creation of RPC request failed.");
3416 goto finalize;
3417 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003418
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003419 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
3420 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003421
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003422 if (reply == NULL) {
3423 DEBUG("Request: validation ok.");
3424 reply = create_ok_reply();
3425 }
3426 }
3427 nc_rpc_free (rpc);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003428
Tomas Cejka09629492014-07-10 15:58:06 +02003429finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003430 CHECK_AND_FREE(target);
3431 CHECK_AND_FREE(url);
3432 return reply;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003433}
3434
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003435json_object *
3436handle_op_query(json_object *request, unsigned int session_key, int idx)
David Kupka8e60a372012-09-04 09:15:20 +02003437{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003438 json_object *reply = NULL, *filters, *obj;
3439 char *filter = NULL;
3440 int load_children = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003441
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003442 DEBUG("Request: query (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003443
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003444 pthread_mutex_lock(&json_lock);
3445 if (json_object_object_get_ex(request, "filters", &filters) == FALSE) {
3446 pthread_mutex_unlock(&json_lock);
3447 reply = create_error_reply("Missing filters parameter.");
3448 goto finalize;
3449 }
3450 obj = json_object_array_get_idx(filters, idx);
3451 if (!obj) {
3452 pthread_mutex_unlock(&json_lock);
3453 reply = create_error_reply("Filters array parameter shorter than sessions.");
3454 goto finalize;
3455 }
3456 filter = strdup(json_object_get_string(obj));
3457 if (json_object_object_get_ex(request, "load_children", &obj) == TRUE) {
3458 load_children = json_object_get_boolean(obj);
3459 }
3460 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02003461
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003462 reply = libyang_query(session_key, filter, load_children);
David Kupka8e60a372012-09-04 09:15:20 +02003463
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003464 CHECK_ERR_SET_REPLY
3465 if (!reply) {
3466 reply = create_error_reply("Query failed.");
3467 }
David Kupka8e60a372012-09-04 09:15:20 +02003468
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003469finalize:
3470 CHECK_AND_FREE(filter);
3471 return reply;
3472}
David Kupka8e60a372012-09-04 09:15:20 +02003473
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003474json_object *
3475handle_op_merge(json_object *request, unsigned int session_key, int idx)
3476{
3477 json_object *reply = NULL, *configs, *obj;
3478 char *config = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02003479
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003480 DEBUG("Request: merge (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003481
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003482 pthread_mutex_lock(&json_lock);
3483 if (json_object_object_get_ex(request, "configurations", &configs) == FALSE) {
3484 pthread_mutex_unlock(&json_lock);
3485 reply = create_error_reply("Missing configurations parameter.");
3486 goto finalize;
3487 }
3488 obj = json_object_array_get_idx(configs, idx);
3489 if (!obj) {
3490 pthread_mutex_unlock(&json_lock);
3491 reply = create_error_reply("Filters array parameter shorter than sessions.");
3492 goto finalize;
3493 }
3494 config = strdup(json_object_get_string(obj));
3495 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02003496
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003497 reply = libyang_merge(session_key, config);
David Kupka8e60a372012-09-04 09:15:20 +02003498
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003499 CHECK_ERR_SET_REPLY
3500 if (!reply) {
3501 reply = create_error_reply("Merge failed.");
3502 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003503
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003504finalize:
3505 CHECK_AND_FREE(config);
3506 return reply;
3507}
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003508
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003509void *
3510thread_routine(void *arg)
3511{
3512 void *retval = NULL;
3513 struct pollfd fds;
3514 json_object *request = NULL, *replies = NULL, *reply, *sessions = NULL;
3515 json_object *js_tmp = NULL;
3516 int operation = (-1), count, i;
3517 int status = 0;
3518 const char *msgtext;
3519 unsigned int session_key = 0;
3520 char *chunked_out_msg = NULL;
3521 int client = ((struct pass_to_thread *)arg)->client;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003522
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003523 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02003524
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003525 /* init thread specific err_reply memory */
3526 create_err_reply_p();
David Kupka8e60a372012-09-04 09:15:20 +02003527
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003528 while (!isterminated) {
3529 fds.fd = client;
3530 fds.events = POLLIN;
3531 fds.revents = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003532
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003533 status = poll(&fds, 1, 1000);
Tomas Cejkad5b53772013-06-08 23:01:07 +02003534
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003535 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
3536 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
3537 continue;
3538 } else if (status < 0) {
3539 /* 0: poll time outed
3540 * close socket and ignore this request from the client, it can try it again
3541 * -1: poll failed
3542 * something wrong happend, close this socket and wait for another request
3543 */
3544 close(client);
3545 break;
3546 }
3547 /* status > 0 */
David Kupka8e60a372012-09-04 09:15:20 +02003548
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003549 /* check the status of the socket */
David Kupka8e60a372012-09-04 09:15:20 +02003550
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003551 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
3552 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
3553 /* close client's socket (it's probably already closed by client */
3554 close(client);
3555 break;
3556 }
Tomas Cejka09629492014-07-10 15:58:06 +02003557
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003558 DEBUG("Get framed message...");
3559 buffer = get_framed_message(client);
Tomas Cejka09629492014-07-10 15:58:06 +02003560
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003561 DEBUG("Check read buffer.");
3562 if (buffer != NULL) {
3563 enum json_tokener_error jerr;
3564 pthread_mutex_lock(&json_lock);
3565 request = json_tokener_parse_verbose(buffer, &jerr);
3566 if (jerr != json_tokener_success) {
3567 ERROR("JSON parsing error");
3568 pthread_mutex_unlock(&json_lock);
3569 continue;
3570 }
3571
3572 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
3573 operation = json_object_get_int(js_tmp);
3574 }
3575 pthread_mutex_unlock(&json_lock);
3576 if (operation == -1) {
3577 replies = create_replies();
3578 add_reply(replies, create_error_reply("Missing operation type from frontend."), 0);
3579 goto send_reply;
3580 }
3581
3582 if ((operation < 4) || ((operation > 19) && (operation < 100)) || (operation > 101)) {
3583 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
3584 replies = create_replies();
3585 add_reply(replies, create_error_reply("Operation not supported."), 0);
3586 goto send_reply;
3587 }
3588
3589 DEBUG("operation %d", operation);
3590
3591 /* null global JSON error-reply */
3592 clean_err_reply();
3593
3594 /* clean replies envelope */
3595 if (replies != NULL) {
3596 pthread_mutex_lock(&json_lock);
3597 json_object_put(replies);
3598 pthread_mutex_unlock(&json_lock);
3599 }
3600 replies = create_replies();
3601
3602 if (operation == MSG_CONNECT) {
3603 count = 1;
3604 } else {
3605 pthread_mutex_lock(&json_lock);
3606 if (json_object_object_get_ex(request, "sessions", &sessions) == FALSE) {
3607 add_reply(replies, create_error_reply("Operation missing \"sessions\" arg"), 0);
3608 goto send_reply;
3609 }
3610 count = json_object_array_length(sessions);
3611 pthread_mutex_unlock(&json_lock);
3612 }
3613
3614 for (i = 0; i < count; ++i) {
3615 if (operation != MSG_CONNECT) {
3616 js_tmp = json_object_array_get_idx(sessions, i);
3617 session_key = json_object_get_int(js_tmp);
3618 }
3619
3620 /* process required operation */
3621 switch (operation) {
3622 case MSG_CONNECT:
3623 reply = handle_op_connect(request);
3624 break;
3625 case MSG_DISCONNECT:
3626 reply = handle_op_disconnect(request, session_key);
3627 break;
3628 case MSG_GET:
3629 reply = handle_op_get(request, session_key);
3630 break;
3631 case MSG_GETCONFIG:
3632 reply = handle_op_getconfig(request, session_key);
3633 break;
3634 case MSG_EDITCONFIG:
3635 reply = handle_op_editconfig(request, session_key, i);
3636 break;
3637 case MSG_COPYCONFIG:
3638 reply = handle_op_copyconfig(request, session_key, i);
3639 break;
3640 case MSG_DELETECONFIG:
3641 reply = handle_op_deleteconfig(request, session_key);
3642 break;
3643 case MSG_LOCK:
3644 reply = handle_op_lock(request, session_key);
3645 break;
3646 case MSG_UNLOCK:
3647 reply = handle_op_unlock(request, session_key);
3648 break;
3649 case MSG_KILL:
3650 reply = handle_op_kill(request, session_key);
3651 break;
3652 case MSG_INFO:
3653 reply = handle_op_info(request, session_key);
3654 break;
3655 case MSG_GENERIC:
3656 reply = handle_op_generic(request, session_key, i);
3657 break;
3658 case MSG_GETSCHEMA:
3659 reply = handle_op_getschema(request, session_key);
3660 break;
3661 case MSG_RELOADHELLO:
3662 reply = handle_op_reloadhello(request, session_key);
3663 break;
3664 case MSG_NTF_GETHISTORY:
3665 reply = handle_op_ntfgethistory(request, session_key);
3666 break;
3667 case MSG_VALIDATE:
3668 reply = handle_op_validate(request, session_key);
3669 break;
3670 case SCH_QUERY:
3671 reply = handle_op_query(request, session_key, i);
3672 break;
3673 case SCH_MERGE:
3674 reply = handle_op_merge(request, session_key, i);
3675 break;
3676 }
3677
3678 add_reply(replies, reply, session_key);
3679 }
3680
3681 /* free parameters */
3682 operation = (-1);
3683
3684 DEBUG("Clean request json object.");
3685 if (request != NULL) {
3686 pthread_mutex_lock(&json_lock);
3687 json_object_put(request);
3688 pthread_mutex_unlock(&json_lock);
3689 }
3690 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02003691
3692send_reply:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003693 /* send reply to caller */
3694 if (replies) {
3695 pthread_mutex_lock(&json_lock);
3696 msgtext = json_object_to_json_string(replies);
3697 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
3698 if (buffer != NULL) {
3699 free(buffer);
3700 buffer = NULL;
3701 }
3702 pthread_mutex_unlock(&json_lock);
3703 break;
3704 }
3705 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003706
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003707 DEBUG("Send framed reply json object.");
3708 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
3709 DEBUG("Clean reply json object.");
3710 pthread_mutex_lock(&json_lock);
3711 json_object_put(replies);
3712 replies = NULL;
3713 DEBUG("Clean message buffer.");
3714 CHECK_AND_FREE(chunked_out_msg);
3715 chunked_out_msg = NULL;
3716 if (buffer) {
3717 free(buffer);
3718 buffer = NULL;
3719 }
3720 pthread_mutex_unlock(&json_lock);
3721 clean_err_reply();
3722 } else {
3723 ERROR("Reply is NULL, shouldn't be...");
3724 continue;
3725 }
3726 }
3727 }
3728 free(arg);
3729 free_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02003730
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003731 return retval;
David Kupka8e60a372012-09-04 09:15:20 +02003732}
3733
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003734/**
3735 * \brief Close all open NETCONF sessions.
3736 *
3737 * During termination of mod_netconf, it is useful to close all remaining
3738 * sessions. This function iterates over the list of sessions and close them
3739 * all.
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003740 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003741static void
3742close_all_nc_sessions(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003743{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003744 struct session_with_mutex *locked_session, *next_session;
3745 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003746
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003747 /* get exclusive access to sessions_list (conns) */
3748 DEBUG("LOCK wrlock %s", __func__);
3749 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
3750 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3751 return;
3752 }
3753 for (next_session = netconf_sessions_list; next_session;) {
3754 locked_session = next_session;
3755 next_session = locked_session->next;
Tomas Cejka47387fd2013-06-10 20:37:46 +02003756
Michal Vaskoc3146782015-11-04 14:46:41 +01003757 /* close_and_free_session handles locking on its own */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003758 DEBUG("Closing NETCONF session %u (SID %s).", locked_session->session_key, nc_session_get_id(locked_session->session));
3759 close_and_free_session(locked_session);
3760 }
3761 netconf_sessions_list = NULL;
3762
3763 /* get exclusive access to sessions_list (conns) */
3764 DEBUG("UNLOCK wrlock %s", __func__);
3765 if (pthread_rwlock_unlock (&session_lock) != 0) {
3766 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3767 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003768}
3769
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003770static void
3771check_timeout_and_close(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003772{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003773 struct nc_session *ns = NULL;
3774 struct session_with_mutex *locked_session = NULL;
3775 time_t current_time = time(NULL);
3776 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003777
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003778 /* get exclusive access to sessions_list (conns) */
3779 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
3780 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3781 return;
3782 }
3783 for (locked_session = netconf_sessions_list; locked_session; locked_session = locked_session->next) {
3784 ns = locked_session->session;
3785 if (ns == NULL) {
3786 continue;
3787 }
3788 pthread_mutex_lock(&locked_session->lock);
3789 if ((current_time - locked_session->last_activity) > ACTIVITY_TIMEOUT) {
3790 DEBUG("Closing NETCONF session %u (SID %s).", locked_session->session_key, nc_session_get_id(locked_session->session));
Tomas Cejka47387fd2013-06-10 20:37:46 +02003791
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003792 /* close_and_free_session handles locking on its own */
3793 close_and_free_session(locked_session);
3794 } else {
3795 pthread_mutex_unlock(&locked_session->lock);
3796 }
3797 }
3798 /* get exclusive access to sessions_list (conns) */
3799 if (pthread_rwlock_unlock(&session_lock) != 0) {
3800 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3801 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003802}
3803
3804
3805/**
Radek Krejcif23850c2012-07-23 16:14:17 +02003806 * This is actually implementation of NETCONF client
3807 * - requests are received from UNIX socket in the predefined format
3808 * - results are replied through the same way
Michal Vaskoc3146782015-11-04 14:46:41 +01003809 * - the daemon run as a separate process
Radek Krejcif23850c2012-07-23 16:14:17 +02003810 *
3811 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003812static void
3813forked_proc(void)
Radek Krejci469aab82012-07-22 18:42:20 +02003814{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003815 struct timeval tv;
3816 struct sockaddr_un local, remote;
3817 int lsock, client, ret, i, pthread_count = 0;
3818 unsigned int olds = 0, timediff = 0;
3819 socklen_t len;
3820 struct pass_to_thread *arg;
3821 pthread_t *ptids = calloc(1, sizeof(pthread_t));
3822 struct timespec maxtime;
3823 pthread_rwlockattr_t lock_attrs;
3824 #ifdef WITH_NOTIFICATIONS
3825 char use_notifications = 0;
3826 #endif
David Kupka8e60a372012-09-04 09:15:20 +02003827
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003828 /* wait at most 5 seconds for every thread to terminate */
3829 maxtime.tv_sec = 5;
3830 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02003831
Tomas Cejka04e08f42014-03-27 19:52:34 +01003832#ifdef HAVE_UNIXD_SETUP_CHILD
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003833 /* change uid and gid of process for security reasons */
3834 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01003835#else
3836# ifdef SU_GROUP
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003837 if (strlen(SU_GROUP) > 0) {
3838 struct group *g = getgrnam(SU_GROUP);
3839 if (g == NULL) {
3840 ERROR("GID (%s) was not found.", SU_GROUP);
3841 return;
3842 }
3843 if (setgid(g->gr_gid) != 0) {
3844 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
3845 return;
3846 }
3847 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003848# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003849 DEBUG("no SU_GROUP");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003850# endif
3851# ifdef SU_USER
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003852 if (strlen(SU_USER) > 0) {
3853 struct passwd *p = getpwnam(SU_USER);
3854 if (p == NULL) {
3855 ERROR("UID (%s) was not found.", SU_USER);
3856 return;
3857 }
3858 if (setuid(p->pw_uid) != 0) {
3859 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
3860 return;
3861 }
3862 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003863# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003864 DEBUG("no SU_USER");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003865# endif
3866#endif
Radek Krejci469aab82012-07-22 18:42:20 +02003867
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003868 /* try to remove if exists */
3869 unlink(sockname);
Tomas Cejka04e08f42014-03-27 19:52:34 +01003870
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003871 /* create listening UNIX socket to accept incoming connections */
3872 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
3873 ERROR("Creating socket failed (%s)", strerror(errno));
3874 goto error_exit;
3875 }
Radek Krejci469aab82012-07-22 18:42:20 +02003876
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003877 local.sun_family = AF_UNIX;
3878 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
3879 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
Radek Krejci469aab82012-07-22 18:42:20 +02003880
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003881 if (bind(lsock, (struct sockaddr *)&local, len) == -1) {
3882 if (errno == EADDRINUSE) {
3883 ERROR("mod_netconf socket address already in use");
3884 goto error_exit;
3885 }
3886 ERROR("Binding socket failed (%s)", strerror(errno));
3887 goto error_exit;
3888 }
Radek Krejci469aab82012-07-22 18:42:20 +02003889
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003890 if (listen(lsock, MAX_SOCKET_CL) == -1) {
3891 ERROR("Setting up listen socket failed (%s)", strerror(errno));
3892 goto error_exit;
3893 }
3894 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02003895
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003896 uid_t user = -1;
3897 if (strlen(CHOWN_USER) > 0) {
3898 struct passwd *p = getpwnam(CHOWN_USER);
3899 if (p != NULL) {
3900 user = p->pw_uid;
3901 }
3902 }
3903 gid_t group = -1;
3904 if (strlen(CHOWN_GROUP) > 0) {
3905 struct group *g = getgrnam(CHOWN_GROUP);
3906 if (g != NULL) {
3907 group = g->gr_gid;
3908 }
3909 }
3910 if (chown(sockname, user, group) == -1) {
3911 ERROR("Chown on socket file failed (%s).", strerror(errno));
3912 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003913
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003914 /* prepare internal lists */
Tomas Cejkaba21b382013-04-13 02:37:32 +02003915
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003916 #ifdef WITH_NOTIFICATIONS
3917 if (notification_init() == -1) {
3918 ERROR("libwebsockets initialization failed");
3919 use_notifications = 0;
3920 } else {
3921 use_notifications = 1;
3922 }
3923 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003924
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003925 /* setup libnetconf's callbacks */
3926 nc_verbosity(NC_VERB_DEBUG);
3927 nc_callback_print(clb_print);
3928 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
3929 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
3930 nc_callback_sshauth_password(netconf_callback_sshauth_password);
3931 nc_callback_sshauth_passphrase(netconf_callback_sshauth_passphrase);
3932 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02003933
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003934 /* disable publickey authentication */
3935 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
Radek Krejci469aab82012-07-22 18:42:20 +02003936
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003937 /* create mutex protecting session list */
3938 pthread_rwlockattr_init(&lock_attrs);
3939 /* rwlock is shared only with threads in this process */
3940 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
3941 /* create rw lock */
3942 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
3943 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
3944 goto error_exit;
3945 }
3946 pthread_mutex_init(&ntf_history_lock, NULL);
3947 pthread_mutex_init(&json_lock, NULL);
3948 DEBUG("Initialization of notification history.");
3949 if (pthread_key_create(&notif_history_key, NULL) != 0) {
3950 ERROR("Initialization of notification history failed.");
3951 }
3952 if (pthread_key_create(&err_reply_key, NULL) != 0) {
3953 ERROR("Initialization of reply key failed.");
3954 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02003955
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003956 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
3957 while (isterminated == 0) {
3958 gettimeofday(&tv, NULL);
3959 timediff = (unsigned int)tv.tv_sec - olds;
3960 #ifdef WITH_NOTIFICATIONS
3961 if (use_notifications == 1) {
3962 notification_handle();
3963 }
3964 #endif
3965 if (timediff > ACTIVITY_CHECK_INTERVAL) {
3966 check_timeout_and_close();
3967 }
Radek Krejci469aab82012-07-22 18:42:20 +02003968
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003969 /* open incoming connection if any */
3970 len = sizeof(remote);
3971 client = accept(lsock, (struct sockaddr *) &remote, &len);
3972 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
3973 usleep(SLEEP_TIME * 1000);
3974 continue;
3975 } else if (client == -1 && (errno == EINTR)) {
3976 continue;
3977 } else if (client == -1) {
3978 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
3979 continue;
3980 }
Radek Krejci469aab82012-07-22 18:42:20 +02003981
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003982 /* set client's socket as non-blocking */
3983 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02003984
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003985 arg = malloc(sizeof(struct pass_to_thread));
3986 arg->client = client;
3987 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02003988
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003989 /* start new thread. It will serve this particular request and then terminate */
3990 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void *)arg)) != 0) {
3991 ERROR("Creating POSIX thread failed: %d\n", ret);
3992 } else {
3993 DEBUG("Thread %lu created", ptids[pthread_count]);
3994 pthread_count++;
3995 ptids = realloc (ptids, sizeof(pthread_t) * (pthread_count+1));
3996 ptids[pthread_count] = 0;
3997 }
Radek Krejci469aab82012-07-22 18:42:20 +02003998
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003999 /* check if some thread already terminated, free some resources by joining it */
4000 for (i = 0; i < pthread_count; i++) {
4001 if (pthread_tryjoin_np(ptids[i], (void **)&arg) == 0) {
4002 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
4003 pthread_count--;
4004 if (pthread_count > 0) {
4005 /* place last Thread ID on the place of joined one */
4006 ptids[i] = ptids[pthread_count];
4007 }
4008 }
4009 }
4010 DEBUG("Running %d threads", pthread_count);
4011 }
Radek Krejci469aab82012-07-22 18:42:20 +02004012
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004013 DEBUG("mod_netconf terminating...");
4014 /* join all threads */
4015 for (i = 0; i < pthread_count; i++) {
4016 pthread_timedjoin_np(ptids[i], (void **)&arg, &maxtime);
4017 }
Radek Krejci469aab82012-07-22 18:42:20 +02004018
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004019 #ifdef WITH_NOTIFICATIONS
4020 notification_close();
4021 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01004022
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004023 /* close all NETCONF sessions */
4024 close_all_nc_sessions();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02004025
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004026 /* destroy rwlock */
4027 pthread_rwlock_destroy(&session_lock);
4028 pthread_rwlockattr_destroy(&lock_attrs);
David Kupka8e60a372012-09-04 09:15:20 +02004029
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004030 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02004031
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004032 free(ptids);
4033 close(lsock);
4034 exit(0);
4035 return;
4036
Tomas Cejkaef531ee2013-11-12 16:07:00 +01004037error_exit:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004038 close(lsock);
4039 free(ptids);
4040 return;
Radek Krejci469aab82012-07-22 18:42:20 +02004041}
4042
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004043int
4044main(int argc, char **argv)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01004045{
Michal Vaskoc3146782015-11-04 14:46:41 +01004046 struct sigaction action;
4047 sigset_t block_mask;
Michal Vaskoa53ef882015-11-24 11:02:01 +01004048 int daemonize = 0, i;
Michal Vaskoc3146782015-11-04 14:46:41 +01004049
Michal Vaskoa53ef882015-11-24 11:02:01 +01004050 if (argc > 3) {
4051 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
4052 return 1;
4053 }
4054
4055 sockname = SOCKET_FILENAME;
4056 for (i = 1; i < argc; ++i) {
4057 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
4058 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
4059 return 0;
4060 } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) {
4061 daemonize = 1;
4062 } else {
4063 sockname = argv[i];
4064 }
4065 }
4066
4067 if (daemonize && (daemon(0, 0) == -1)) {
4068 ERROR("daemon() failed (%s)", strerror(errno));
4069 return 1;
Michal Vaskoc3146782015-11-04 14:46:41 +01004070 }
4071
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004072 sigfillset(&block_mask);
Michal Vaskoc3146782015-11-04 14:46:41 +01004073 action.sa_handler = signal_handler;
4074 action.sa_mask = block_mask;
4075 action.sa_flags = 0;
4076 sigaction(SIGINT, &action, NULL);
4077 sigaction(SIGTERM, &action, NULL);
4078
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01004079 forked_proc();
4080 DEBUG("Terminated");
4081 return 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01004082}