blob: 3059c78aeffb9ef69e320ca96a2398e9c44e0393 [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
Michal Vasko30aa3822015-11-25 10:41:54 +01002 * \file netopeerguid.c
3 * \brief NetopeerGUI daemon
Radek Krejci469aab82012-07-22 18:42:20 +02004 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoa53ef882015-11-24 11:02:01 +01006 * \author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci469aab82012-07-22 18:42:20 +02007 * \date 2011
8 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01009 * \date 2013
Michal Vaskoa53ef882015-11-24 11:02:01 +010010 * \date 2015
Radek Krejci469aab82012-07-22 18:42:20 +020011 */
12/*
Michal Vaskoa53ef882015-11-24 11:02:01 +010013 * Copyright (C) 2011-2015 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020014 *
15 * LICENSE TERMS
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in
24 * the documentation and/or other materials provided with the
25 * distribution.
26 * 3. Neither the name of the Company nor the names of its contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * ALTERNATIVELY, provided that this notice is retained in full, this
31 * product may be distributed under the terms of the GNU General Public
32 * License (GPL) version 2 or later, in which case the provisions
33 * of the GPL apply INSTEAD OF those given above.
34 *
35 * This software is provided ``as is'', and any express or implied
36 * warranties, including, but not limited to, the implied warranties of
37 * merchantability and fitness for a particular purpose are disclaimed.
38 * In no event shall the company or contributors be liable for any
39 * direct, indirect, incidental, special, exemplary, or consequential
40 * damages (including, but not limited to, procurement of substitute
41 * goods or services; loss of use, data, or profits; or business
42 * interruption) however caused and on any theory of liability, whether
43 * in contract, strict liability, or tort (including negligence or
44 * otherwise) arising in any way out of the use of this software, even
45 * if advised of the possibility of such damage.
46 *
47 */
Michal Vaskoc3146782015-11-04 14:46:41 +010048#define _GNU_SOURCE
Radek Krejci469aab82012-07-22 18:42:20 +020049
Radek Krejci7b4ddd02012-07-30 08:09:58 +020050#include <unistd.h>
51#include <poll.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010052#include <time.h>
Michal Vaskob69dde22016-03-09 11:42:20 +010053#include <fcntl.h>
Radek Krejci469aab82012-07-22 18:42:20 +020054#include <sys/types.h>
55#include <sys/socket.h>
56#include <sys/un.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010057#include <sys/stat.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010058#include <pwd.h>
Michal Vaskoefdd49f2016-04-06 11:13:27 +020059#include <syslog.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010060#include <errno.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010061#include <limits.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010062#include <grp.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010063#include <signal.h>
David Kupka8e60a372012-09-04 09:15:20 +020064#include <pthread.h>
65#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020066
Michal Vaskof35ea502016-02-24 10:44:54 +010067#include <nc_client.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;
Michal Vaskoefdd49f2016-04-06 11:13:27 +0200117int daemonize;
Radek Krejci469aab82012-07-22 18:42:20 +0200118
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100119json_object *create_ok_reply(void);
120json_object *create_data_reply(const char *data);
121static char *netconf_getschema(unsigned int session_key, const char *identifier, const char *version,
122 const char *format, json_object **err);
Michal Vaskof35ea502016-02-24 10:44:54 +0100123static void node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module,
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100124 json_object *data_json_parent);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100125static void node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100126
127static void
128signal_handler(int sign)
Radek Krejci469aab82012-07-22 18:42:20 +0200129{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100130 switch (sign) {
131 case SIGINT:
132 case SIGTERM:
133 isterminated = 1;
134 break;
135 }
Radek Krejci469aab82012-07-22 18:42:20 +0200136}
137
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100138int
139netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session))
Radek Krejci469aab82012-07-22 18:42:20 +0200140{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100141 /* always approve */
142 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200143}
144
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100145char *
Michal Vaskof35ea502016-02-24 10:44:54 +0100146netconf_callback_sshauth_passphrase(const char *UNUSED(priv_key_file))
Radek Krejci469aab82012-07-22 18:42:20 +0200147{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100148 char *buf;
149 buf = strdup(password);
150 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200151}
152
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100153char *
154netconf_callback_sshauth_password(const char *UNUSED(username), const char *UNUSED(hostname))
Radek Krejci469aab82012-07-22 18:42:20 +0200155{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100156 char *buf;
157 buf = strdup(password);
158 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200159}
160
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100161char *
162netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction),
163 const char *UNUSED(prompt), int UNUSED(echo))
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200164{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100165 char *buf;
166 buf = strdup(password);
167 return (buf);
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200168}
169
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100170void
Michal Vasko56c02b22016-04-06 09:59:09 +0200171netconf_callback_error_process(const char *message)
Radek Krejcic11fd862012-07-26 12:41:21 +0200172{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100173 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
174 if (err_reply_p == NULL) {
175 ERROR("Error message was not allocated. %s", __func__);
176 return;
177 }
178 json_object *err_reply = *err_reply_p;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100179
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100180 json_object *array = NULL;
181 if (err_reply == NULL) {
182 ERROR("error calback: empty error list");
183 pthread_mutex_lock(&json_lock);
184 err_reply = json_object_new_object();
185 array = json_object_new_array();
186 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
187 json_object_object_add(err_reply, "errors", array);
188 if (message != NULL) {
189 json_object_array_add(array, json_object_new_string(message));
190 }
191 pthread_mutex_unlock(&json_lock);
192 (*err_reply_p) = err_reply;
193 } else {
194 ERROR("error calback: nonempty error list");
195 pthread_mutex_lock(&json_lock);
196 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
197 if (message != NULL) {
198 json_object_array_add(array, json_object_new_string(message));
199 }
200 }
201 pthread_mutex_unlock(&json_lock);
202 }
203 pthread_setspecific(err_reply_key, err_reply_p);
204 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200205}
206
Tomas Cejka47387fd2013-06-10 20:37:46 +0200207/**
208 * should be used in locked area
209 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100210void
211prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200212{
Michal Vaskof35ea502016-02-24 10:44:54 +0100213 int i;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100214 json_object *json_obj = NULL;
215 json_object *js_tmp = NULL;
216 char *old_sid = NULL;
217 const char *j_old_sid = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +0100218 char str_port[6];
219 const char **cpblts;
220 struct lyd_node *yanglib, *module, *node;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200221
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100222 if (s == NULL) {
223 ERROR("No session given.");
224 return;
225 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200226
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100227 pthread_mutex_lock(&json_lock);
228 if (s->hello_message != NULL) {
229 ERROR("clean previous hello message");
230 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
231 j_old_sid = json_object_get_string(js_tmp);
232 if (j_old_sid != NULL) {
233 old_sid = strdup(j_old_sid);
234 }
235 }
236 json_object_put(s->hello_message);
237 s->hello_message = NULL;
238 }
239 s->hello_message = json_object_new_object();
240 if (session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100241 if (!old_sid) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100242 /* we don't have old sid */
Michal Vaskof35ea502016-02-24 10:44:54 +0100243 asprintf(&old_sid, "%u", nc_session_get_id(session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100244 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100245 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
246 free(old_sid);
247 old_sid = NULL;
248
249 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) ? "1.1":"1.0")));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100250 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
Michal Vaskof35ea502016-02-24 10:44:54 +0100251 sprintf(str_port, "%u", nc_session_get_port(session));
252 json_object_object_add(s->hello_message, "port", json_object_new_string(str_port));
253 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_username(session)));
254 cpblts = nc_session_get_cpblts(session);
255 if (cpblts) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100256 json_obj = json_object_new_array();
Michal Vaskof35ea502016-02-24 10:44:54 +0100257 for (i = 0; cpblts[i]; ++i) {
258 json_object_array_add(json_obj, json_object_new_string(cpblts[i]));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100259 }
260 json_object_object_add(s->hello_message, "capabilities", json_obj);
261 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100262
263 yanglib = ly_ctx_info(nc_session_get_ctx(session));
264 if (yanglib) {
265 json_obj = json_object_new_array();
266 LY_TREE_FOR(yanglib->child, module) {
267 if (!strcmp(module->schema->name, "module")) {
268 LY_TREE_FOR(module->child, node) {
269 if (!strcmp(node->schema->name, "name")) {
270 json_object_array_add(json_obj, json_object_new_string(((struct lyd_node_leaf_list *)node)->value_str));
271 break;
272 }
273 }
274 }
275 }
276 json_object_object_add(s->hello_message, "models", json_obj);
277
Michal Vaskoc04900a2016-03-15 16:20:24 +0100278 lyd_free(yanglib);
Michal Vaskof35ea502016-02-24 10:44:54 +0100279 }
280
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100281 DEBUG("%s", json_object_to_json_string(s->hello_message));
282 } else {
283 ERROR("Session was not given.");
284 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
285 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
286 }
287 DEBUG("Status info from hello message prepared");
288 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200289}
290
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100291void
292create_err_reply_p()
Tomas Cejka442258e2014-04-01 18:17:18 +0200293{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100294 json_object **err_reply = calloc(1, sizeof(json_object **));
295 if (err_reply == NULL) {
296 ERROR("Allocation of err_reply storage failed!");
297 return;
298 }
299 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
300 ERROR("cannot set thread-specific value.");
301 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200302}
303
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100304void
305clean_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200306{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100307 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
308 if (err_reply != NULL) {
309 if (*err_reply != NULL) {
310 pthread_mutex_lock(&json_lock);
311 json_object_put(*err_reply);
312 pthread_mutex_unlock(&json_lock);
313 }
314 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
315 ERROR("Cannot set thread-specific hash value.");
316 }
317 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200318}
319
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100320void
321free_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200322{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100323 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
324 if (err_reply != NULL) {
325 if (*err_reply != NULL) {
326 pthread_mutex_lock(&json_lock);
327 json_object_put(*err_reply);
328 pthread_mutex_unlock(&json_lock);
329 }
330 free(err_reply);
331 err_reply = NULL;
332 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
333 ERROR("Cannot set thread-specific hash value.");
334 }
335 }
336}
337
338static struct session_with_mutex *
339session_get_locked(unsigned int session_key, json_object **err)
340{
341 struct session_with_mutex *locked_session;
342
343 /* get non-exclusive (read) access to sessions_list (conns) */
344 DEBUG("LOCK wrlock %s", __func__);
345 if (pthread_rwlock_rdlock(&session_lock) != 0) {
346 if (*err) {
347 *err = create_error_reply("Locking failed.");
348 }
349 return NULL;
350 }
351 /* get session where send the RPC */
352 for (locked_session = netconf_sessions_list;
353 locked_session && (locked_session->session_key != session_key);
354 locked_session = locked_session->next);
355 if (!locked_session) {
356 if (*err) {
357 *err = create_error_reply("Session not found.");
358 }
359 return NULL;
360 }
361
362 /* get exclusive access to session */
363 DEBUG("LOCK mutex %s", __func__);
364 if (pthread_mutex_lock(&locked_session->lock) != 0) {
365 if (*err) {
366 *err = create_error_reply("Locking failed.");
367 }
368 goto wrlock_fail;
369 }
370 return locked_session;
371
372wrlock_fail:
373 DEBUG("UNLOCK wrlock %s", __func__);
374 pthread_rwlock_unlock(&session_lock);
375 return NULL;
376}
377
378static void
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100379session_user_activity(const char *username)
380{
381 struct session_with_mutex *sess;
382
383 for (sess = netconf_sessions_list; sess; sess = sess->next) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100384 if (!strcmp(nc_session_get_username(sess->session), username)) {
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100385 sess->last_activity = time(NULL);
386 }
387 }
388}
389
390static void
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100391session_unlock(struct session_with_mutex *locked_session)
392{
393 DEBUG("UNLOCK mutex %s", __func__);
394 pthread_mutex_unlock(&locked_session->lock);
395 DEBUG("UNLOCK wrlock %s", __func__);
396 pthread_rwlock_unlock(&session_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +0200397}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200398
Michal Vasko3fda9a92015-11-23 10:10:57 +0100399static void
400node_metadata_text(const char *text, const char *name, json_object *parent)
401{
402 json_object *obj;
403
404 if (!text) {
405 return;
406 }
407
408 obj = json_object_new_string(text);
409 json_object_object_add(parent, name, obj);
410}
411
412static void
413node_metadata_restr(struct lys_restr *restr, const char *name, json_object *parent)
414{
415 json_object *obj;
416
417 if (!restr) {
418 return;
419 }
420
421 obj = json_object_new_string(restr->expr);
422 json_object_object_add(parent, name, obj);
423}
424
425static void
426node_metadata_must(uint8_t must_size, struct lys_restr *must, json_object *parent)
427{
428 uint8_t i;
429 json_object *array, *obj;
430
431 if (!must_size || !must) {
432 return;
433 }
434
435 array = json_object_new_array();
436
437 for (i = 0; i < must_size; ++i) {
438 obj = json_object_new_string(must[i].expr);
439 json_object_array_add(array, obj);
440 }
441
442 json_object_object_add(parent, "must", array);
443}
444
445static void
446node_metadata_basic(struct lys_node *node, json_object *parent)
447{
448 json_object *obj;
449
450 /* description */
451 node_metadata_text(node->dsc, "description", parent);
452
453 /* reference */
454 node_metadata_text(node->ref, "reference", parent);
455
456 /* config */
457 if (node->flags & LYS_CONFIG_R) {
458 obj = json_object_new_boolean(0);
459 } else {
460 obj = json_object_new_boolean(1);
461 }
462 json_object_object_add(parent, "config", obj);
463
464 /* status */
465 if (node->flags & LYS_STATUS_DEPRC) {
466 obj = json_object_new_string("deprecated");
467 } else if (node->flags & LYS_STATUS_OBSLT) {
468 obj = json_object_new_string("obsolete");
469 } else {
470 obj = json_object_new_string("current");
471 }
472 json_object_object_add(parent, "status", obj);
473
474 /* mandatory */
475 if (node->flags & LYS_MAND_TRUE) {
476 obj = json_object_new_boolean(1);
477 } else {
478 obj = json_object_new_boolean(0);
479 }
480 json_object_object_add(parent, "mandatory", obj);
481
482 /* NACM extensions */
483 if (node->nacm) {
484 if (node->nacm & LYS_NACM_DENYW) {
485 obj = json_object_new_string("default-deny-write");
486 } else {
487 obj = json_object_new_string("default-deny-all");
488 }
489 json_object_object_add(parent, "ext", obj);
490 }
491}
492
493static void
494node_metadata_when(struct lys_when *when, json_object *parent)
495{
496 json_object *obj;
497
498 if (!when) {
499 return;
500 }
501
502 obj = json_object_new_string(when->cond);
503 json_object_object_add(parent, "when", obj);
504}
505
506static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100507node_metadata_children_recursive(struct lys_node *node, json_object **child_array, json_object **choice_array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100508{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100509 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100510 struct lys_node *child;
511
512 if (!node->child) {
513 return;
514 }
515
516 LY_TREE_FOR(node->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100517 if (child->nodetype == LYS_USES) {
518 node_metadata_children_recursive(child, child_array, choice_array);
519 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100520 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100521 if (!*child_array) {
522 *child_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100523 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100524 json_object_array_add(*child_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100525 } else if (child->nodetype == LYS_CHOICE) {
526 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100527 if (!*choice_array) {
528 *choice_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100529 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100530 json_object_array_add(*choice_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100531 }
532 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100533}
534
535static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100536node_metadata_cases_recursive(struct lys_node_choice *choice, json_object *array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100537{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100538 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100539 struct lys_node *child;
540
541 if (!choice->child) {
542 return;
543 }
544
Michal Vasko3fda9a92015-11-23 10:10:57 +0100545 LY_TREE_FOR(choice->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100546 if (child->nodetype == LYS_USES) {
547 node_metadata_cases_recursive((struct lys_node_choice *)child, array);
548 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100549 obj = json_object_new_string(child->name);
550 json_object_array_add(array, obj);
551 }
552 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100553}
554
555static void
556node_metadata_min_max(uint32_t min, uint32_t max, json_object *parent)
557{
558 json_object *obj;
559
560 if (min) {
561 obj = json_object_new_int(min);
562 json_object_object_add(parent, "min-elements", obj);
563 }
564
565 if (max) {
566 obj = json_object_new_int(max);
567 json_object_object_add(parent, "max-elements", obj);
568 }
569}
570
571static void
572node_metadata_ident_recursive(struct lys_ident *ident, json_object *array)
573{
574 struct lys_ident_der *cur;
575 json_object *obj;
576
577 if (!ident) {
578 return;
579 }
580
581 obj = json_object_new_string(ident->name);
582 json_object_array_add(array, obj);
583
584 for (cur = ident->der; cur; cur = cur->next) {
585 node_metadata_ident_recursive(cur->ident, array);
586 }
587}
588
589static void
590node_metadata_type(struct lys_type *type, struct lys_module *module, json_object *parent)
591{
592 json_object *obj, *array, *item;
593 char *str;
594 int i;
595
596 /* built-in YANG type */
597 if (!type->der->module) {
598 switch (type->base) {
599 case LY_TYPE_BINARY:
600 node_metadata_text("binary", "type", parent);
601 node_metadata_restr(type->info.binary.length, "length", parent);
602 break;
603 case LY_TYPE_BITS:
604 node_metadata_text("bits", "type", parent);
605
606 array = json_object_new_array();
607 for (i = 0; i < type->info.bits.count; ++i) {
608 item = json_object_new_object();
609 obj = json_object_new_string(type->info.bits.bit[i].name);
610 json_object_object_add(item, "name", obj);
611 obj = json_object_new_int(type->info.bits.bit[i].pos);
612 json_object_object_add(item, "position", obj);
613 json_object_array_add(array, item);
614 }
615 json_object_object_add(parent, "bits", array);
616 break;
617 case LY_TYPE_BOOL:
618 node_metadata_text("bool", "type", parent);
619 break;
620 case LY_TYPE_DEC64:
621 node_metadata_text("decimal64", "type", parent);
622 node_metadata_restr(type->info.dec64.range, "range", parent);
623 obj = json_object_new_int(type->info.dec64.dig);
624 json_object_object_add(parent, "fraction-digits", obj);
625 break;
626 case LY_TYPE_EMPTY:
627 node_metadata_text("empty", "type", parent);
628 break;
629 case LY_TYPE_ENUM:
630 node_metadata_text("enumeration", "type", parent);
631
632 array = json_object_new_array();
633 for (i = 0; i < type->info.enums.count; ++i) {
634 obj = json_object_new_string(type->info.enums.enm[i].name);
635 json_object_array_add(array, obj);
636 }
637 json_object_object_add(parent, "enumval", array);
638 break;
639 case LY_TYPE_IDENT:
640 node_metadata_text("identityref", "type", parent);
641
642 array = json_object_new_array();
643 node_metadata_ident_recursive(type->info.ident.ref, array);
644 json_object_object_add(parent, "identityval", array);
645 break;
646 case LY_TYPE_INST:
647 node_metadata_text("instance-identifier", "type", parent);
648 if (type->info.inst.req == -1) {
649 obj = json_object_new_boolean(0);
650 } else {
651 obj = json_object_new_boolean(1);
652 }
653 json_object_object_add(parent, "require-instance", obj);
654 break;
655 case LY_TYPE_LEAFREF:
656 node_metadata_text("leafref", "type", parent);
657 node_metadata_text(type->info.lref.path, "path", parent);
658 break;
659 case LY_TYPE_STRING:
660 node_metadata_text("string", "type", parent);
661 node_metadata_restr(type->info.str.length, "length", parent);
662 if (type->info.str.pat_count) {
663 array = json_object_new_array();
664 for (i = 0; i < type->info.str.pat_count; ++i) {
665 obj = json_object_new_string(type->info.str.patterns[i].expr);
666 json_object_array_add(array, obj);
667 }
668 json_object_object_add(parent, "pattern", array);
669 }
670 break;
671 case LY_TYPE_UNION:
672 node_metadata_text("union", "type", parent);
673 array = json_object_new_array();
674 for (i = 0; i < type->info.uni.count; ++i) {
675 obj = json_object_new_object();
676 node_metadata_type(&type->info.uni.types[i], module, obj);
677 json_object_array_add(array, obj);
678 }
679 json_object_object_add(parent, "types", array);
680 break;
681 case LY_TYPE_INT8:
682 node_metadata_text("int8", "type", parent);
683 node_metadata_restr(type->info.num.range, "range", parent);
684 break;
685 case LY_TYPE_UINT8:
686 node_metadata_text("uint8", "type", parent);
687 node_metadata_restr(type->info.num.range, "range", parent);
688 break;
689 case LY_TYPE_INT16:
690 node_metadata_text("int16", "type", parent);
691 node_metadata_restr(type->info.num.range, "range", parent);
692 break;
693 case LY_TYPE_UINT16:
694 node_metadata_text("uint16", "type", parent);
695 node_metadata_restr(type->info.num.range, "range", parent);
696 break;
697 case LY_TYPE_INT32:
698 node_metadata_text("int32", "type", parent);
699 node_metadata_restr(type->info.num.range, "range", parent);
700 break;
701 case LY_TYPE_UINT32:
702 node_metadata_text("uint32", "type", parent);
703 node_metadata_restr(type->info.num.range, "range", parent);
704 break;
705 case LY_TYPE_INT64:
706 node_metadata_text("int64", "type", parent);
707 node_metadata_restr(type->info.num.range, "range", parent);
708 break;
709 case LY_TYPE_UINT64:
710 node_metadata_text("uint64", "type", parent);
711 node_metadata_restr(type->info.num.range, "range", parent);
712 break;
713 default:
714 ERROR("Internal: unknown type (%s:%d)", __FILE__, __LINE__);
715 break;
716 }
717
718 /* typedef */
719 } else {
720 if (!module || !type->module_name || !strcmp(type->module_name, module->name)) {
721 node_metadata_text(type->der->name, "type", parent);
722 } else {
723 asprintf(&str, "%s:%s", type->module_name, type->der->name);
724 node_metadata_text(str, "type", parent);
725 free(str);
726 }
727 obj = json_object_new_object();
728 node_metadata_typedef(type->der, obj);
729 json_object_object_add(parent, "typedef", obj);
730 }
731}
732
733static void
734node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent)
735{
736 json_object *obj;
737
738 /* description */
739 node_metadata_text(tpdf->dsc, "description", parent);
740
741 /* reference */
742 node_metadata_text(tpdf->ref, "reference", parent);
743
744 /* status */
745 if (tpdf->flags & LYS_STATUS_DEPRC) {
746 obj = json_object_new_string("deprecated");
747 } else if (tpdf->flags & LYS_STATUS_OBSLT) {
748 obj = json_object_new_string("obsolete");
749 } else {
750 obj = json_object_new_string("current");
751 }
752 json_object_object_add(parent, "status", obj);
753
754 /* type */
755 node_metadata_type(&tpdf->type, tpdf->module, parent);
756
757 /* units */
758 node_metadata_text(tpdf->units, "units", parent);
759
760 /* default */
761 node_metadata_text(tpdf->dflt, "default", parent);
762}
763
764static void
765node_metadata_container(struct lys_node_container *cont, json_object *parent)
766{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100767 json_object *obj, *child_array = NULL, *choice_array = NULL;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100768
769 /* element type */
770 obj = json_object_new_string("container");
771 json_object_object_add(parent, "eltype", obj);
772
773 /* shared info */
774 node_metadata_basic((struct lys_node *)cont, parent);
775
776 /* must */
777 node_metadata_must(cont->must_size, cont->must, parent);
778
779 /* presence */
780 node_metadata_text(cont->presence, "presence", parent);
781
782 /* when */
783 node_metadata_when(cont->when, parent);
784
785 /* children & choice */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100786 node_metadata_children_recursive((struct lys_node *)cont, &child_array, &choice_array);
787 if (child_array) {
788 json_object_object_add(parent, "children", child_array);
789 }
790 if (choice_array) {
791 json_object_object_add(parent, "choice", choice_array);
792 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100793}
794
795static void
796node_metadata_choice(struct lys_node_choice *choice, json_object *parent)
797{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100798 json_object *obj, *array;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100799
800 /* element type */
801 obj = json_object_new_string("choice");
802 json_object_object_add(parent, "eltype", obj);
803
804 /* shared info */
805 node_metadata_basic((struct lys_node *)choice, parent);
806
807 /* default */
Michal Vasko5e1c6052016-03-17 15:43:33 +0100808 if (choice->dflt) {
809 node_metadata_text(choice->dflt->name, "default", parent);
810 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100811
812 /* when */
813 node_metadata_when(choice->when, parent);
814
815 /* cases */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100816 if (choice->child) {
817 array = json_object_new_array();
818 node_metadata_cases_recursive(choice, array);
819 json_object_object_add(parent, "cases", array);
820 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100821}
822
823static void
824node_metadata_leaf(struct lys_node_leaf *leaf, json_object *parent)
825{
826 json_object *obj;
827 struct lys_node_list *list;
828 int is_key, i;
829
830 /* element type */
831 obj = json_object_new_string("leaf");
832 json_object_object_add(parent, "eltype", obj);
833
834 /* shared info */
835 node_metadata_basic((struct lys_node *)leaf, parent);
836
837 /* type */
838 node_metadata_type(&leaf->type, leaf->module, parent);
839
840 /* units */
841 node_metadata_text(leaf->units, "units", parent);
842
843 /* default */
844 node_metadata_text(leaf->dflt, "default", parent);
845
846 /* must */
847 node_metadata_must(leaf->must_size, leaf->must, parent);
848
849 /* when */
850 node_metadata_when(leaf->when, parent);
851
852 /* iskey */
853 is_key = 0;
854 list = (struct lys_node_list *)lys_parent((struct lys_node *)leaf);
855 if (list && (list->nodetype == LYS_LIST)) {
856 for (i = 0; i < list->keys_size; ++i) {
857 if (list->keys[i] == leaf) {
858 is_key = 1;
859 break;
860 }
861 }
862 }
863 obj = json_object_new_boolean(is_key);
864 json_object_object_add(parent, "iskey", obj);
865}
866
867static void
868node_metadata_leaflist(struct lys_node_leaflist *llist, json_object *parent)
869{
870 json_object *obj;
871
872 /* element type */
873 obj = json_object_new_string("leaf-list");
874 json_object_object_add(parent, "eltype", obj);
875
876 /* shared info */
877 node_metadata_basic((struct lys_node *)llist, parent);
878
879 /* type */
880 node_metadata_type(&llist->type, llist->module, parent);
881
882 /* units */
883 node_metadata_text(llist->units, "units", parent);
884
885 /* must */
886 node_metadata_must(llist->must_size, llist->must, parent);
887
888 /* when */
889 node_metadata_when(llist->when, parent);
890
891 /* min/max-elements */
892 node_metadata_min_max(llist->min, llist->max, parent);
893}
894
895static void
896node_metadata_list(struct lys_node_list *list, json_object *parent)
897{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100898 json_object *obj, *array, *child_array = NULL, *choice_array = NULL;;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100899 int i;
900 unsigned int j;
901
902 /* element type */
903 obj = json_object_new_string("list");
904 json_object_object_add(parent, "eltype", obj);
905
906 /* shared info */
907 node_metadata_basic((struct lys_node *)list, parent);
908
909 /* must */
910 node_metadata_must(list->must_size, list->must, parent);
911
912 /* when */
913 node_metadata_when(list->when, parent);
914
915 /* min/max-elements */
916 node_metadata_min_max(list->min, list->max, parent);
917
918 /* keys */
919 if (list->keys_size) {
920 array = json_object_new_array();
921 for (i = 0; i < list->keys_size; ++i) {
922 obj = json_object_new_string(list->keys[i]->name);
923 json_object_array_add(array, obj);
924 }
925 json_object_object_add(parent, "keys", array);
926 }
927
928 /* unique */
929 if (list->unique_size) {
930 array = json_object_new_array();
931 for (i = 0; i < list->unique_size; ++i) {
932 for (j = 0; j < list->unique[i].expr_size; ++j) {
933 obj = json_object_new_string(list->unique[i].expr[j]);
934 json_object_array_add(array, obj);
935 }
936 }
937 json_object_object_add(parent, "unique", array);
938 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100939
940 /* children & choice */
941 node_metadata_children_recursive((struct lys_node *)list, &child_array, &choice_array);
942 if (child_array) {
943 json_object_object_add(parent, "children", child_array);
944 }
945 if (choice_array) {
946 json_object_object_add(parent, "choice", choice_array);
947 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100948}
949
950static void
951node_metadata_anyxml(struct lys_node_anyxml *anyxml, json_object *parent)
952{
953 json_object *obj;
954
955 /* element type */
956 obj = json_object_new_string("anyxml");
957 json_object_object_add(parent, "eltype", obj);
958
959 /* shared info */
960 node_metadata_basic((struct lys_node *)anyxml, parent);
961
962 /* must */
963 node_metadata_must(anyxml->must_size, anyxml->must, parent);
964
965 /* when */
966 node_metadata_when(anyxml->when, parent);
967
968}
969
970static void
971node_metadata_case(struct lys_node_case *cas, json_object *parent)
972{
973 json_object *obj;
974
975 /* element type */
976 obj = json_object_new_string("case");
977 json_object_object_add(parent, "eltype", obj);
978
979 /* shared info */
980 node_metadata_basic((struct lys_node *)cas, parent);
981
982 /* when */
983 node_metadata_when(cas->when, parent);
984}
985
Michal Vaskoa45770b2015-11-23 15:49:41 +0100986static void
987node_metadata_rpc(struct lys_node_rpc *rpc, json_object *parent)
988{
989 json_object *obj;
990
991 /* element type */
992 obj = json_object_new_string("rpc");
993 json_object_object_add(parent, "eltype", obj);
994
995 /* description */
996 node_metadata_text(rpc->dsc, "description", parent);
997
998 /* reference */
999 node_metadata_text(rpc->ref, "reference", parent);
1000
1001 /* status */
1002 if (rpc->flags & LYS_STATUS_DEPRC) {
1003 obj = json_object_new_string("deprecated");
1004 } else if (rpc->flags & LYS_STATUS_OBSLT) {
1005 obj = json_object_new_string("obsolete");
1006 } else {
1007 obj = json_object_new_string("current");
1008 }
1009 json_object_object_add(parent, "status", obj);
1010}
1011
1012static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001013node_metadata_model(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001014{
1015 json_object *obj, *array, *item;
Michal Vaskoa4b8cdb2016-03-17 15:44:45 +01001016 const struct lys_node *node;
Michal Vaskoa45770b2015-11-23 15:49:41 +01001017 int i;
1018
1019 /* yang-version */
1020 if (module->version == 2) {
1021 obj = json_object_new_string("1.1");
1022 } else {
1023 obj = json_object_new_string("1.0");
1024 }
1025 json_object_object_add(parent, "yang-version", obj);
1026
1027 /* namespace */
1028 node_metadata_text(module->ns, "namespace", parent);
1029
1030 /* prefix */
1031 node_metadata_text(module->prefix, "prefix", parent);
1032
1033 /* contact */
1034 node_metadata_text(module->contact, "contact", parent);
1035
1036 /* organization */
1037 node_metadata_text(module->org, "organization", parent);
1038
1039 /* revision */
1040 if (module->rev_size) {
1041 node_metadata_text(module->rev[0].date, "revision", parent);
1042 }
1043
1044 /* description */
1045 node_metadata_text(module->dsc, "description", parent);
1046
1047 /* import */
1048 if (module->imp_size) {
1049 array = json_object_new_array();
1050 for (i = 0; i < module->imp_size; ++i) {
1051 item = json_object_new_object();
1052
1053 node_metadata_text(module->imp[i].module->name, "name", item);
1054 node_metadata_text(module->imp[i].prefix, "prefix", item);
1055 if (module->imp[i].rev && module->imp[i].rev[0]) {
1056 node_metadata_text(module->imp[i].rev, "revision", item);
1057 }
1058
1059 json_object_array_add(array, item);
1060 }
1061 json_object_object_add(parent, "imports", array);
1062 }
1063
1064 /* include */
1065 if (module->inc_size) {
1066 array = json_object_new_array();
1067 for (i = 0; i < module->inc_size; ++i) {
1068 item = json_object_new_object();
1069
1070 node_metadata_text(module->inc[i].submodule->name, "name", item);
1071 if (module->inc[i].rev && module->inc[i].rev[0]) {
1072 node_metadata_text(module->inc[i].rev, "revision", item);
1073 }
1074
1075 json_object_array_add(array, item);
1076 }
1077 json_object_object_add(parent, "includes", array);
1078 }
Michal Vaskoa4b8cdb2016-03-17 15:44:45 +01001079
1080 /* top-nodes */
1081 node = NULL;
1082 array = NULL;
1083 while ((node = lys_getnext(node, NULL, module, LYS_GETNEXT_WITHCHOICE))) {
1084 if (node->nodetype & (LYS_RPC | LYS_NOTIF)) {
1085 continue;
1086 }
1087 if (!array) {
1088 array = json_object_new_array();
1089 }
1090 item = json_object_new_string(node->name);
1091 json_object_array_add(array, item);
1092 }
1093 if (array) {
1094 json_object_object_add(parent, "top-nodes", array);
1095 }
Michal Vaskoa45770b2015-11-23 15:49:41 +01001096}
1097
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001098/**
1099 * \defgroup netconf_operations NETCONF operations
1100 * The list of NETCONF operations that mod_netconf supports.
1101 * @{
1102 */
1103
1104/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001105 * \brief Send RPC and wait for reply with timeout.
1106 *
1107 * \param[in] session libnetconf session
1108 * \param[in] rpc prepared RPC message
1109 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
1110 * \param[out] reply reply from the server
Michal Vaskof35ea502016-02-24 10:44:54 +01001111 * \return NC_MSG_WOULDBLOCK or NC_MSG_ERROR.
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001112 * On success, it returns NC_MSG_REPLY.
1113 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001114NC_MSG_TYPE
Michal Vaskof35ea502016-02-24 10:44:54 +01001115netconf_send_recv_timed(struct nc_session *session, struct nc_rpc *rpc, int timeout, int strict, struct nc_reply **reply)
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001116{
Michal Vaskof35ea502016-02-24 10:44:54 +01001117 uint64_t msgid;
1118 NC_MSG_TYPE ret;
1119 ret = nc_send_rpc(session, rpc, timeout, &msgid);
1120 if (ret != NC_MSG_RPC) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001121 return ret;
1122 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001123
1124 while ((ret = nc_recv_reply(session, rpc, msgid, timeout, (strict ? LYD_OPT_STRICT : 0), reply)) == NC_MSG_NOTIF);
1125
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001126 return ret;
1127}
1128
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001129/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001130 * \brief Connect to NETCONF server
1131 *
1132 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
1133 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001134static unsigned int
Michal Vaskof35ea502016-02-24 10:44:54 +01001135netconf_connect(const char *host, const char *port, const char *user, const char *pass, const char *privkey)
Radek Krejci469aab82012-07-22 18:42:20 +02001136{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001137 struct nc_session* session = NULL;
1138 struct session_with_mutex *locked_session, *last_session;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001139 char *pubkey;
Radek Krejci469aab82012-07-22 18:42:20 +02001140
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001141 /* connect to the requested NETCONF server */
1142 password = (char*)pass;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001143 if (privkey) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001144 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 3);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001145 asprintf(&pubkey, "%s.pub", privkey);
Michal Vaskof35ea502016-02-24 10:44:54 +01001146 nc_client_ssh_add_keypair(pubkey, privkey);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001147 free(pubkey);
1148 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001149 nc_client_ssh_set_username(user);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001150 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Michal Vaskof35ea502016-02-24 10:44:54 +01001151 session = nc_connect_ssh(host, (unsigned short)atoi(port), NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001152 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +02001153
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001154 /* if connected successful, add session to the list */
1155 if (session != NULL) {
1156 if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001157 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001158 session = NULL;
1159 free(locked_session);
1160 locked_session = NULL;
1161 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
1162 return 0;
1163 }
1164 locked_session->session = session;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001165 locked_session->hello_message = NULL;
1166 locked_session->closed = 0;
1167 pthread_mutex_init(&locked_session->lock, NULL);
1168 DEBUG("Before session_lock");
1169 /* get exclusive access to sessions_list (conns) */
1170 DEBUG("LOCK wrlock %s", __func__);
1171 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001172 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001173 free(locked_session);
1174 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
1175 return 0;
1176 }
1177 locked_session->ntfc_subscribed = 0;
1178 DEBUG("Add connection to the list");
1179 if (!netconf_sessions_list) {
Michal Vaskoc3146782015-11-04 14:46:41 +01001180 netconf_sessions_list = locked_session;
1181 } else {
1182 for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next);
1183 last_session->next = locked_session;
1184 locked_session->prev = last_session;
1185 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001186 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001187
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001188 /* no need to lock session, noone can read it while we have wrlock */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001189
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001190 /* store information about session from hello message for future usage */
1191 prepare_status_message(locked_session, session);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001192
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001193 DEBUG("NETCONF session established");
1194 locked_session->session_key = session_key_generator;
1195 ++session_key_generator;
1196 if (session_key_generator == UINT_MAX) {
1197 session_key_generator = 1;
1198 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001199
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001200 DEBUG("Before session_unlock");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001201 /* unlock session list */
1202 DEBUG("UNLOCK wrlock %s", __func__);
1203 if (pthread_rwlock_unlock(&session_lock) != 0) {
1204 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1205 }
Radek Krejci469aab82012-07-22 18:42:20 +02001206
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001207 return locked_session->session_key;
1208 }
1209
1210 ERROR("Connection could not be established");
1211 return 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001212}
1213
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001214static int
1215close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +02001216{
Michal Vaskoa53ef882015-11-24 11:02:01 +01001217 int i;
1218
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001219 DEBUG("lock private lock.");
1220 DEBUG("LOCK mutex %s", __func__);
1221 if (pthread_mutex_lock(&locked_session->lock) != 0) {
1222 ERROR("Error while locking rwlock");
1223 }
1224 locked_session->ntfc_subscribed = 0;
1225 locked_session->closed = 1;
1226 if (locked_session->session != NULL) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001227 nc_session_free(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001228 locked_session->session = NULL;
1229 }
1230 DEBUG("session closed.");
1231 DEBUG("unlock private lock.");
1232 DEBUG("UNLOCK mutex %s", __func__);
1233 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
1234 ERROR("Error while locking rwlock");
1235 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001236
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001237 DEBUG("unlock session lock.");
1238 DEBUG("closed session, disabled notif(?), wait 0.5s");
1239 usleep(500000); /* let notification thread stop */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001240
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001241 /* session shouldn't be used by now */
Michal Vaskoa53ef882015-11-24 11:02:01 +01001242 for (i = 0; i < locked_session->notif_count; ++i) {
1243 free(locked_session->notifications[i].content);
1244 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001245 free(locked_session->notifications);
1246 pthread_mutex_destroy(&locked_session->lock);
1247 if (locked_session->hello_message != NULL) {
1248 json_object_put(locked_session->hello_message);
1249 locked_session->hello_message = NULL;
1250 }
1251 locked_session->session = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001252 free(locked_session);
1253 locked_session = NULL;
1254 DEBUG("NETCONF session closed, everything cleared.");
1255 return (EXIT_SUCCESS);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001256}
1257
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001258static int
1259netconf_close(unsigned int session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +02001260{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001261 struct session_with_mutex *locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +02001262
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001263 DEBUG("Session to close: %u", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001264
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001265 /* get exclusive (write) access to sessions_list (conns) */
1266 DEBUG("lock session lock.");
1267 DEBUG("LOCK wrlock %s", __func__);
1268 if (pthread_rwlock_wrlock (&session_lock) != 0) {
1269 ERROR("Error while locking rwlock");
1270 (*reply) = create_error_reply("Internal: Error while locking.");
1271 return EXIT_FAILURE;
1272 }
1273 /* remove session from the active sessions list -> nobody new can now work with session */
1274 for (locked_session = netconf_sessions_list;
1275 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01001276 locked_session = locked_session->next);
1277
1278 if (!locked_session) {
Michal Vasko642cad02016-03-17 12:31:18 +01001279 pthread_rwlock_unlock(&session_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001280 ERROR("Could not find the session %u to close.", session_key);
1281 (*reply) = create_error_reply("Internal: Error while finding a session.");
Michal Vaskoc3146782015-11-04 14:46:41 +01001282 return EXIT_FAILURE;
1283 }
1284
1285 if (!locked_session->prev) {
1286 netconf_sessions_list = netconf_sessions_list->next;
Michal Vaskof0b6fbb2016-03-08 12:04:34 +01001287 if (netconf_sessions_list) {
1288 netconf_sessions_list->prev = NULL;
1289 }
Michal Vaskoc3146782015-11-04 14:46:41 +01001290 } else {
1291 locked_session->prev->next = locked_session->next;
1292 if (locked_session->next) {
1293 locked_session->next->prev = locked_session->prev;
1294 }
1295 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001296
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001297 DEBUG("UNLOCK wrlock %s", __func__);
1298 if (pthread_rwlock_unlock (&session_lock) != 0) {
1299 ERROR("Error while unlocking rwlock");
1300 (*reply) = create_error_reply("Internal: Error while unlocking.");
1301 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001302
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001303 if ((locked_session != NULL) && (locked_session->session != NULL)) {
1304 return close_and_free_session(locked_session);
1305 } else {
1306 ERROR("Unknown session to close");
1307 (*reply) = create_error_reply("Internal: Unkown session to close.");
1308 return (EXIT_FAILURE);
1309 }
1310 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +02001311}
1312
Tomas Cejkac7929632013-10-24 19:25:15 +02001313/**
1314 * Test reply message type and return error message.
1315 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001316 * \param[in] session nc_session internal struct
Michal Vaskoc3146782015-11-04 14:46:41 +01001317 * \param[in] session_key session ID, 0 to disable disconnect on error
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001318 * \param[in] msgt RPC-REPLY message type
Tomas Cejkac7929632013-10-24 19:25:15 +02001319 * \param[out] data
1320 * \return NULL on success
1321 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001322json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001323netconf_test_reply(struct nc_session *session, unsigned int session_key, NC_MSG_TYPE msgt, struct nc_reply *reply, struct lyd_node **data)
Tomas Cejkac7929632013-10-24 19:25:15 +02001324{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001325 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001326
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001327 /* process the result of the operation */
1328 switch (msgt) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001329 case NC_MSG_ERROR:
1330 if (nc_session_get_status(session) != NC_STATUS_RUNNING) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001331 ERROR("mod_netconf: receiving rpc-reply failed");
1332 if (session_key) {
1333 netconf_close(session_key, &err);
1334 }
1335 if (err != NULL) {
1336 return err;
1337 }
1338 return create_error_reply("Internal: Receiving RPC-REPLY failed.");
1339 }
1340 case NC_MSG_NONE:
1341 /* there is error handled by callback */
1342 if (data != NULL) {
1343 free(*data);
1344 (*data) = NULL;
1345 }
1346 return NULL;
1347 case NC_MSG_REPLY:
Michal Vaskof35ea502016-02-24 10:44:54 +01001348 switch (reply->type) {
1349 case NC_RPL_OK:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001350 if ((data != NULL) && (*data != NULL)) {
1351 free(*data);
1352 (*data) = NULL;
1353 }
1354 return create_ok_reply();
Michal Vaskof35ea502016-02-24 10:44:54 +01001355 case NC_RPL_DATA:
1356 if (((*data) = ((struct nc_reply_data *)reply)->data) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001357 ERROR("mod_netconf: no data from reply");
1358 return create_error_reply("Internal: No data from reply received.");
1359 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01001360 ((struct nc_reply_data *)reply)->data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001361 return NULL;
1362 }
1363 break;
Michal Vaskof35ea502016-02-24 10:44:54 +01001364 case NC_RPL_ERROR:
1365 ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001366 if (data != NULL) {
1367 free(*data);
1368 (*data) = NULL;
1369 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001370 return create_error_reply(((struct nc_reply_error *)reply)->err[0].message);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001371 default:
Michal Vaskof35ea502016-02-24 10:44:54 +01001372 ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001373 if (data != NULL) {
1374 free(*data);
1375 (*data) = NULL;
1376 }
1377 return create_error_reply("Unknown type of NETCONF reply.");
1378 }
1379 break;
1380 default:
1381 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
1382 if (data != NULL) {
1383 free(*data);
1384 (*data) = NULL;
1385 }
1386 return create_error_reply("Internal: Unexpected RPC-REPLY message type.");
1387 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001388}
1389
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001390json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001391netconf_unlocked_op(struct nc_session *session, struct nc_rpc *rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001392{
Michal Vaskof35ea502016-02-24 10:44:54 +01001393 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001394 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001395
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001396 /* check requests */
1397 if (rpc == NULL) {
1398 ERROR("mod_netconf: rpc is not created");
1399 return create_error_reply("Internal error: RPC is not created");
1400 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001401
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001402 if (session != NULL) {
1403 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001404 msgt = netconf_send_recv_timed(session, rpc, 50000, 0, &reply);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001405 /* process the result of the operation */
1406 return netconf_test_reply(session, 0, msgt, reply, NULL);
1407 } else {
1408 ERROR("Unknown session to process.");
1409 return create_error_reply("Internal error: Unknown session to process.");
1410 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001411}
1412
Tomas Cejkac7929632013-10-24 19:25:15 +02001413/**
1414 * Perform RPC method that returns data.
1415 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001416 * \param[in] session_id session identifier
1417 * \param[in] rpc RPC message to perform
1418 * \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 +02001419 * \return NULL on success, json object with error otherwise
1420 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001421static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001422netconf_op(unsigned int session_key, struct nc_rpc *rpc, int strict, struct lyd_node **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +02001423{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001424 struct session_with_mutex * locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001425 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001426 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001427 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001428 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +02001429
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001430 /* check requests */
1431 if (rpc == NULL) {
1432 ERROR("mod_netconf: rpc is not created");
1433 res = create_error_reply("Internal: RPC could not be created.");
1434 data = NULL;
1435 goto finished;
1436 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001437
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001438 locked_session = session_get_locked(session_key, &res);
1439 if (!locked_session) {
1440 ERROR("Unknown session or locking failed.");
1441 goto finished;
1442 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001443
Michal Vaskof35ea502016-02-24 10:44:54 +01001444 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejkac7929632013-10-24 19:25:15 +02001445
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001446 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001447 msgt = netconf_send_recv_timed(locked_session->session, rpc, 2000000, strict, &reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001448
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001449 session_unlock(locked_session);
Tomas Cejkac7929632013-10-24 19:25:15 +02001450
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001451 res = netconf_test_reply(locked_session->session, session_key, msgt, reply, &data);
Radek Krejcia332b692012-11-12 16:15:54 +01001452
Tomas Cejkac7929632013-10-24 19:25:15 +02001453finished:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001454 nc_reply_free(reply);
1455 if (received_data != NULL) {
1456 (*received_data) = data;
1457 } else {
1458 if (data != NULL) {
1459 free(data);
1460 data = NULL;
1461 }
1462 }
1463 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001464}
1465
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001466static char *
1467netconf_getconfig(unsigned int session_key, NC_DATASTORE source, const char *filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001468{
Michal Vaskof35ea502016-02-24 10:44:54 +01001469 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001470 struct session_with_mutex *locked_session;
Michal Vasko04245ee2016-03-16 09:32:59 +01001471 json_object *res = NULL, *data_cjson;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001472 enum json_tokener_error tok_err;
Michal Vasko04245ee2016-03-16 09:32:59 +01001473 char *data_json = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001474 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001475
Michal Vaskof35ea502016-02-24 10:44:54 +01001476 /* tell server to show all elements even if they have default values */
1477#ifdef HAVE_WITHDEFAULTS_TAGGED
1478 rpc = nc_rpc_getconfig(source, filter, NC_WD_MODE_ALL_TAG, NC_PARAMTYPE_CONST);
1479#else
1480 rpc = nc_rpc_getconfig(source, filter, 0, NC_PARAMTYPE_CONST);
1481#endif
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001482 if (rpc == NULL) {
1483 ERROR("mod_netconf: creating rpc request failed");
1484 return (NULL);
1485 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001486
Michal Vaskof35ea502016-02-24 10:44:54 +01001487 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001488 nc_rpc_free(rpc);
1489 if (res != NULL) {
1490 (*err) = res;
1491 } else {
1492 (*err) = NULL;
1493 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001494
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001495 if (data) {
1496 for (locked_session = netconf_sessions_list;
1497 locked_session && (locked_session->session_key != session_key);
1498 locked_session = locked_session->next);
1499 /* won't fail */
1500
Michal Vaskof35ea502016-02-24 10:44:54 +01001501 /* print data into JSON */
1502 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001503 ERROR("Printing JSON <get-config> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001504 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001505 return NULL;
1506 }
1507
1508 /* parse JSON data into cjson */
1509 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001510 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001511 if (!data_cjson) {
1512 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1513 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001514 lyd_free_withsiblings(data);
1515 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001516 return NULL;
1517 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001518 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001519
1520 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001521 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001522 node_add_metadata_recursive(sibling, NULL, data_cjson);
1523 lyd_free(sibling);
1524 }
1525
Michal Vaskof35ea502016-02-24 10:44:54 +01001526 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001527 json_object_put(data_cjson);
1528 pthread_mutex_unlock(&json_lock);
1529 }
1530
Michal Vaskof35ea502016-02-24 10:44:54 +01001531 return (data_json);
Radek Krejci8e4632a2012-07-26 13:40:34 +02001532}
1533
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001534static char *
1535netconf_getschema(unsigned int session_key, const char *identifier, const char *version, const char *format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001536{
Michal Vaskof35ea502016-02-24 10:44:54 +01001537 struct nc_rpc *rpc;
1538 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001539 json_object *res = NULL;
Michal Vasko56ec5952016-04-05 14:30:23 +02001540 char *model_data = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001541
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001542 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001543 rpc = nc_rpc_getschema(identifier, version, format, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001544 if (rpc == NULL) {
1545 ERROR("mod_netconf: creating rpc request failed");
1546 return (NULL);
1547 }
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001548
Michal Vaskof35ea502016-02-24 10:44:54 +01001549 res = netconf_op(session_key, rpc, 0, &data);
1550 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001551 if (res != NULL) {
1552 (*err) = res;
1553 } else {
1554 (*err) = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001555
1556 if (data) {
Michal Vasko56ec5952016-04-05 14:30:23 +02001557 if (((struct lyd_node_anyxml *)data)->xml_struct) {
1558 lyxml_print_mem(&model_data, ((struct lyd_node_anyxml *)data)->value.xml, 0);
1559 } else {
1560 model_data = strdup(((struct lyd_node_anyxml *)data)->value.str);
1561 }
1562 if (!model_data) {
1563 ERROR("memory allocation fail (%s:%d)", __FILE__, __LINE__);
Michal Vaskof35ea502016-02-24 10:44:54 +01001564 }
1565 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001566 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001567
Michal Vaskof35ea502016-02-24 10:44:54 +01001568 return (model_data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001569}
1570
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001571static char *
1572netconf_get(unsigned int session_key, const char* filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001573{
Michal Vaskof35ea502016-02-24 10:44:54 +01001574 struct nc_rpc* rpc;
1575 char* data_json = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001576 json_object *res = NULL, *data_cjson;
1577 enum json_tokener_error tok_err;
1578 struct session_with_mutex *locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001579 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001580
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001581 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001582 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001583 if (rpc == NULL) {
1584 ERROR("mod_netconf: creating rpc request failed");
1585 return (NULL);
1586 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001587
Michal Vaskof35ea502016-02-24 10:44:54 +01001588 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001589 nc_rpc_free(rpc);
1590 if (res != NULL) {
1591 (*err) = res;
1592 } else {
1593 (*err) = NULL;
1594 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001595
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001596 if (data) {
1597 for (locked_session = netconf_sessions_list;
1598 locked_session && (locked_session->session_key != session_key);
1599 locked_session = locked_session->next);
1600 /* won't fail */
1601
Michal Vaskof35ea502016-02-24 10:44:54 +01001602 /* print JSON data */
1603 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001604 ERROR("Printing JSON <get> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001605 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001606 return NULL;
1607 }
1608
1609 /* parse JSON data into cjson */
1610 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001611 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001612 if (!data_cjson) {
1613 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1614 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001615 lyd_free_withsiblings(data);
1616 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001617 return NULL;
1618 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001619 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001620
1621 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001622 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001623 node_add_metadata_recursive(sibling, NULL, data_cjson);
1624 lyd_free(sibling);
1625 }
1626
Michal Vaskof35ea502016-02-24 10:44:54 +01001627 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001628 json_object_put(data_cjson);
1629 pthread_mutex_unlock(&json_lock);
1630 }
1631
Michal Vaskof35ea502016-02-24 10:44:54 +01001632 return data_json;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001633}
1634
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001635static json_object *
1636netconf_copyconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, const char *config,
1637 const char *uri_src, const char *uri_trg)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001638{
Michal Vaskof35ea502016-02-24 10:44:54 +01001639 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001640 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001641
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001642 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001643 rpc = nc_rpc_copy(target, uri_trg, source, (config ? config : uri_src), 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001644 if (rpc == NULL) {
1645 ERROR("mod_netconf: creating rpc request failed");
1646 return create_error_reply("Internal: Creating rpc request failed");
1647 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001648
Michal Vaskof35ea502016-02-24 10:44:54 +01001649 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001650 nc_rpc_free(rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001651
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001652 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001653}
Radek Krejci035bf4e2012-07-25 10:59:09 +02001654
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001655static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001656netconf_editconfig(unsigned int session_key, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP defop,
1657 NC_RPC_EDIT_ERROPT erropt, NC_RPC_EDIT_TESTOPT testopt, const char *config_or_url)
Radek Krejci62ab34b2012-07-26 13:42:05 +02001658{
Michal Vaskof35ea502016-02-24 10:44:54 +01001659 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001660 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001661
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001662 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001663 rpc = nc_rpc_edit(target, defop, testopt, erropt, config_or_url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001664 if (rpc == NULL) {
1665 ERROR("mod_netconf: creating rpc request failed");
1666 return create_error_reply("Internal: Creating rpc request failed");
1667 }
Radek Krejci62ab34b2012-07-26 13:42:05 +02001668
Michal Vaskof35ea502016-02-24 10:44:54 +01001669 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001670 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001671
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001672 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001673}
1674
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001675static json_object *
1676netconf_killsession(unsigned int session_key, const char *sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001677{
Michal Vaskof35ea502016-02-24 10:44:54 +01001678 struct nc_rpc *rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001679 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001680
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001681 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001682 rpc = nc_rpc_kill(atoi(sid));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001683 if (rpc == NULL) {
1684 ERROR("mod_netconf: creating rpc request failed");
1685 return create_error_reply("Internal: Creating rpc request failed");
1686 }
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001687
Michal Vaskof35ea502016-02-24 10:44:54 +01001688 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001689 nc_rpc_free(rpc);
1690 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001691}
1692
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001693static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001694netconf_onlytargetop(unsigned int session_key, NC_DATASTORE target, struct nc_rpc *(*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +02001695{
Michal Vaskof35ea502016-02-24 10:44:54 +01001696 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001697 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +02001698
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001699 /* create requests */
1700 rpc = op_func(target);
1701 if (rpc == NULL) {
1702 ERROR("mod_netconf: creating rpc request failed");
1703 return create_error_reply("Internal: Creating rpc request failed");
1704 }
Radek Krejci2f318372012-07-26 14:22:35 +02001705
Michal Vaskof35ea502016-02-24 10:44:54 +01001706 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001707 nc_rpc_free (rpc);
1708 return res;
Radek Krejci2f318372012-07-26 14:22:35 +02001709}
1710
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001711static json_object *
1712netconf_deleteconfig(unsigned int session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001713{
Michal Vaskof35ea502016-02-24 10:44:54 +01001714 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001715 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001716 rpc = nc_rpc_delete(target, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001717 if (rpc == NULL) {
1718 ERROR("mod_netconf: creating rpc request failed");
1719 return create_error_reply("Internal: Creating rpc request failed");
1720 }
Tomas Cejka404d37e2013-04-13 02:31:35 +02001721
Michal Vaskof35ea502016-02-24 10:44:54 +01001722 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001723 nc_rpc_free (rpc);
1724 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +02001725}
1726
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001727static json_object *
1728netconf_lock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001729{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001730 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001731}
1732
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001733static json_object *
1734netconf_unlock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001735{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001736 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001737}
1738
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001739static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001740netconf_generic(unsigned int session_key, const char *xml_content, struct lyd_node **data)
Radek Krejci80c10d92012-07-30 08:38:50 +02001741{
Michal Vaskof35ea502016-02-24 10:44:54 +01001742 struct nc_rpc* rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001743 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +02001744
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001745 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001746 rpc = nc_rpc_generic_xml(xml_content, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001747 if (rpc == NULL) {
1748 ERROR("mod_netconf: creating rpc request failed");
1749 return create_error_reply("Internal: Creating rpc request failed");
1750 }
Radek Krejci80c10d92012-07-30 08:38:50 +02001751
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001752 /* get session where send the RPC */
Michal Vaskof35ea502016-02-24 10:44:54 +01001753 res = netconf_op(session_key, rpc, 0, data);
1754 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001755 return res;
1756}
1757
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001758static int
Michal Vaskof35ea502016-02-24 10:44:54 +01001759node_add_metadata(const struct lys_node *node, const struct lys_module *module, json_object *parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001760{
1761 struct lys_module *cur_module;
1762 json_object *meta_obj;
1763 char *obj_name;
1764
Michal Vaskoa45770b2015-11-23 15:49:41 +01001765 if (node->nodetype == LYS_INPUT) {
1766 /* silently skipped */
1767 return 0;
1768 }
1769
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001770 cur_module = node->module;
1771 if (cur_module->type) {
1772 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1773 }
1774 if (cur_module == module) {
1775 asprintf(&obj_name, "$@%s", node->name);
1776 } else {
1777 asprintf(&obj_name, "$@%s:%s", cur_module->name, node->name);
1778 }
1779
1780 /* in (leaf-)lists the metadata could have already been added */
Michal Vaskoa45770b2015-11-23 15:49:41 +01001781 if ((node->nodetype & (LYS_LEAFLIST | LYS_LIST)) && (json_object_object_get_ex(parent, obj_name, NULL) == TRUE)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001782 free(obj_name);
1783 return 1;
1784 }
1785
1786 meta_obj = json_object_new_object();
1787
1788 switch (node->nodetype) {
1789 case LYS_CONTAINER:
1790 node_metadata_container((struct lys_node_container *)node, meta_obj);
1791 break;
Michal Vasko3fda9a92015-11-23 10:10:57 +01001792 case LYS_CHOICE:
1793 node_metadata_choice((struct lys_node_choice *)node, meta_obj);
1794 break;
1795 case LYS_LEAF:
1796 node_metadata_leaf((struct lys_node_leaf *)node, meta_obj);
1797 break;
1798 case LYS_LEAFLIST:
1799 node_metadata_leaflist((struct lys_node_leaflist *)node, meta_obj);
1800 break;
1801 case LYS_LIST:
1802 node_metadata_list((struct lys_node_list *)node, meta_obj);
1803 break;
1804 case LYS_ANYXML:
1805 node_metadata_anyxml((struct lys_node_anyxml *)node, meta_obj);
1806 break;
1807 case LYS_CASE:
1808 node_metadata_case((struct lys_node_case *)node, meta_obj);
1809 break;
Michal Vaskoa45770b2015-11-23 15:49:41 +01001810 case LYS_RPC:
1811 node_metadata_rpc((struct lys_node_rpc *)node, meta_obj);
1812 break;
1813 default: /* LYS_OUTPUT */
Michal Vasko3fda9a92015-11-23 10:10:57 +01001814 ERROR("Internal: unuxpected nodetype (%s:%d)", __FILE__, __LINE__);
1815 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001816 }
1817
1818 /* just a precaution */
1819 if (json_object_get_type(parent) != json_type_object) {
1820 ERROR("Internal: wrong JSON type (%s:%d)", __FILE__, __LINE__);
1821 free(obj_name);
1822 return 1;
1823 }
1824
1825 json_object_object_add(parent, obj_name, meta_obj);
1826 free(obj_name);
1827 return 0;
1828}
1829
1830static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001831node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module, json_object *data_json_parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001832{
1833 struct lys_module *cur_module;
1834 struct lys_node *list_schema;
1835 struct lyd_node *child, *list_item;
1836 json_object *child_json, *list_child_json;
1837 char *child_name;
1838 int list_idx;
1839
Michal Vaskoa45770b2015-11-23 15:49:41 +01001840 if (data_tree->schema->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1841 return;
1842 }
1843
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001844 /* add data_tree metadata */
1845 if (node_add_metadata(data_tree->schema, module, data_json_parent)) {
1846 return;
1847 }
1848
1849 /* get data_tree module */
1850 cur_module = data_tree->schema->module;
1851 if (cur_module->type) {
1852 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1853 }
1854
1855 if (!(data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
1856 /* print correct data_tree JSON name */
1857 if (cur_module == module) {
1858 asprintf(&child_name, "%s", data_tree->schema->name);
1859 } else {
1860 asprintf(&child_name, "%s:%s", cur_module->name, data_tree->schema->name);
1861 }
1862
1863 /* go down in JSON object */
1864 if (json_object_object_get_ex(data_json_parent, child_name, &child_json) == FALSE) {
1865 ERROR("Internal: failed to get JSON object \"%s\".", child_name);
1866 free(child_name);
1867 return;
1868 }
1869 free(child_name);
1870
1871 if (data_tree->schema->nodetype == LYS_LIST) {
1872 if (json_object_get_type(child_json) != json_type_array) {
1873 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1874 return;
1875 }
1876 /* go down in data tree for every item, we process them all now, skip later
1877 * (metadata duplicate will be detected at the beginning of this function) */
1878 list_idx = 0;
1879 list_schema = data_tree->schema;
1880
1881 LY_TREE_FOR(data_tree, list_item) {
1882 /* another list member */
1883 if (list_item->schema == list_schema) {
1884 list_child_json = json_object_array_get_idx(child_json, list_idx);
1885 if (!list_child_json) {
1886 ERROR("Internal: list \"%s\" idx out-of-bounds", list_schema->name);
1887 return;
1888 }
1889 LY_TREE_FOR(list_item->child, child) {
1890 node_add_metadata_recursive(child, cur_module, list_child_json);
1891 }
1892
1893 ++list_idx;
1894 }
1895 }
1896 } else {
1897 if (json_object_get_type(child_json) != json_type_object) {
1898 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1899 return;
1900 }
1901 /* go down in data tree */
1902 LY_TREE_FOR(data_tree->child, child) {
1903 node_add_metadata_recursive(child, cur_module, child_json);
1904 }
1905 }
1906 }
1907}
1908
1909static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001910node_add_model_metadata(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001911{
1912 json_object *obj;
1913 char *str;
1914
1915 obj = json_object_new_object();
1916 node_metadata_model(module, obj);
1917 asprintf(&str, "$@@%s", module->name);
1918 json_object_object_add(parent, str, obj);
1919 free(str);
1920}
1921
1922static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001923node_add_children_with_metadata_recursive(const struct lys_node *node, const struct lys_module *module, json_object *parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001924{
Michal Vaskof35ea502016-02-24 10:44:54 +01001925 const struct lys_module *cur_module;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001926 struct lys_node *child;
1927 json_object *node_json;
1928 char *json_name;
1929
Michal Vaskoa45770b2015-11-23 15:49:41 +01001930 if (node->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1931 return;
1932 }
1933
1934 if (node->nodetype & LYS_USES) {
1935 cur_module = module;
1936 node_json = parent;
1937 goto children;
1938 }
1939
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001940 /* add node metadata */
1941 if (node_add_metadata(node, module, parent)) {
1942 ERROR("Internal: metadata duplicate for \"%s\".", node->name);
1943 return;
1944 }
1945
Michal Vaskoa45770b2015-11-23 15:49:41 +01001946 /* no other metadata */
1947 if (!node->child) {
1948 return;
1949 }
1950
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001951 /* get node module */
1952 cur_module = node->module;
1953 if (cur_module->type) {
1954 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1955 }
1956
1957 /* create JSON object for child metadata */
1958 node_json = json_object_new_object();
1959 if (cur_module == module) {
1960 json_object_object_add(parent, node->name, node_json);
1961 } else {
1962 asprintf(&json_name, "%s:%s", cur_module->name, node->name);
1963 json_object_object_add(parent, json_name, node_json);
1964 free(json_name);
1965 }
1966
Michal Vaskoa45770b2015-11-23 15:49:41 +01001967children:
Michal Vaskoea2ddd92016-03-17 15:43:58 +01001968 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
1969 LY_TREE_FOR(node->child, child) {
1970 node_add_children_with_metadata_recursive(child, cur_module, node_json);
1971 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001972 }
1973}
1974
1975static json_object *
1976libyang_query(unsigned int session_key, const char *filter, int load_children)
1977{
Michal Vaskof35ea502016-02-24 10:44:54 +01001978 const struct lys_node *node;
1979 const struct lys_module *module = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001980 struct session_with_mutex *locked_session;
1981 json_object *ret = NULL, *data;
1982
1983 locked_session = session_get_locked(session_key, &ret);
1984 if (!locked_session) {
1985 ERROR("Locking failed or session not found.");
1986 goto finish;
1987 }
1988
Michal Vaskof35ea502016-02-24 10:44:54 +01001989 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001990
Michal Vaskoa45770b2015-11-23 15:49:41 +01001991 if (filter[0] == '/') {
Michal Vasko56ec5952016-04-05 14:30:23 +02001992 node = ly_ctx_get_node(nc_session_get_ctx(locked_session->session), NULL, filter);
Michal Vaskoa45770b2015-11-23 15:49:41 +01001993 if (!node) {
1994 ret = create_error_reply("Failed to resolve XPath filter node.");
1995 goto finish;
1996 }
1997 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01001998 module = ly_ctx_get_module(nc_session_get_ctx(locked_session->session), filter, NULL);
Michal Vaskoa45770b2015-11-23 15:49:41 +01001999 if (!module) {
2000 ret = create_error_reply("Failed to find model.");
2001 goto finish;
2002 }
2003 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002004
Michal Vaskoa45770b2015-11-23 15:49:41 +01002005 pthread_mutex_lock(&json_lock);
2006 data = json_object_new_object();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002007
Michal Vaskoa45770b2015-11-23 15:49:41 +01002008 if (module) {
2009 node_add_model_metadata(module, data);
2010 if (load_children) {
2011 LY_TREE_FOR(module->data, node) {
2012 node_add_children_with_metadata_recursive(node, NULL, data);
2013 }
2014 }
2015 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002016 if (load_children) {
2017 node_add_children_with_metadata_recursive(node, NULL, data);
2018 } else {
2019 node_add_metadata(node, NULL, data);
2020 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002021 }
2022
Michal Vaskoa45770b2015-11-23 15:49:41 +01002023 pthread_mutex_unlock(&json_lock);
2024 ret = create_data_reply(json_object_to_json_string(data));
2025 json_object_put(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002026
2027finish:
Michal Vaskoa45770b2015-11-23 15:49:41 +01002028 session_unlock(locked_session);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002029 return ret;
2030}
2031
2032static json_object *
2033libyang_merge(unsigned int session_key, const char *config)
2034{
2035 struct lyd_node *data_tree = NULL, *sibling;
2036 struct session_with_mutex *locked_session;
2037 json_object *ret = NULL, *data_json = NULL;
2038 enum json_tokener_error err = 0;
2039
2040 locked_session = session_get_locked(session_key, &ret);
2041 if (!locked_session) {
2042 ERROR("Locking failed or session not found.");
2043 goto finish;
2044 }
2045
Michal Vaskof35ea502016-02-24 10:44:54 +01002046 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002047
Michal Vaskof35ea502016-02-24 10:44:54 +01002048 data_tree = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_STRICT);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002049 if (!data_tree) {
2050 ERROR("Creating data tree failed.");
2051 ret = create_error_reply("Failed to create data tree from JSON config.");
2052 session_unlock(locked_session);
2053 goto finish;
2054 }
2055
2056 session_unlock(locked_session);
2057
2058 pthread_mutex_lock(&json_lock);
2059 data_json = json_tokener_parse_verbose(config, &err);
2060 if (!data_json) {
2061 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(err));
2062 pthread_mutex_unlock(&json_lock);
2063 ret = create_error_reply(json_tokener_error_desc(err));
2064 goto finish;
2065 }
2066
2067 /* go simultaneously through both trees and add metadata */
2068 LY_TREE_FOR(data_tree, sibling) {
2069 node_add_metadata_recursive(sibling, NULL, data_json);
2070 }
2071 pthread_mutex_unlock(&json_lock);
2072 ret = create_data_reply(json_object_to_json_string(data_json));
2073
2074finish:
2075 LY_TREE_FOR(data_tree, sibling) {
2076 lyd_free(sibling);
2077 }
2078 json_object_put(data_json);
2079 return ret;
Radek Krejci80c10d92012-07-30 08:38:50 +02002080}
2081
Tomas Cejka0a4bba82013-04-19 11:51:28 +02002082/**
2083 * @}
2084 *//* netconf_operations */
2085
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002086void
2087clb_print(NC_VERB_LEVEL level, const char *msg)
Radek Krejci469aab82012-07-22 18:42:20 +02002088{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002089 switch (level) {
Michal Vasko56c02b22016-04-06 09:59:09 +02002090 case NC_VERB_ERROR:
Michal Vaskoefdd49f2016-04-06 11:13:27 +02002091 ERROR("lib ERROR: %s", msg);
Michal Vasko56c02b22016-04-06 09:59:09 +02002092 break;
2093 case NC_VERB_WARNING:
Michal Vaskoefdd49f2016-04-06 11:13:27 +02002094 ERROR("lib WARNING: %s", msg);
Michal Vasko56c02b22016-04-06 09:59:09 +02002095 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002096 case NC_VERB_VERBOSE:
Michal Vaskoefdd49f2016-04-06 11:13:27 +02002097 ERROR("lib VERBOSE: %s", msg);
Michal Vasko56c02b22016-04-06 09:59:09 +02002098 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002099 case NC_VERB_DEBUG:
Michal Vaskoefdd49f2016-04-06 11:13:27 +02002100 DEBUG("lib DEBUG: %s", msg);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002101 break;
2102 }
Michal Vasko56c02b22016-04-06 09:59:09 +02002103
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002104 if (level == NC_VERB_ERROR) {
2105 /* return global error */
Michal Vasko56c02b22016-04-06 09:59:09 +02002106 netconf_callback_error_process(msg);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002107 }
Radek Krejci469aab82012-07-22 18:42:20 +02002108}
2109
Tomas Cejka64b87482013-06-03 16:30:53 +02002110/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02002111 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02002112 * Caller should free message memory.
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002113 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02002114 * \return pointer to message
2115 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002116char *
2117get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02002118{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002119 /* read json in chunked framing */
2120 unsigned int buffer_size = 0;
2121 ssize_t buffer_len = 0;
2122 char *buffer = NULL;
2123 char c;
2124 ssize_t ret;
2125 int i, chunk_len;
2126 char chunk_len_str[12];
Tomas Cejka64b87482013-06-03 16:30:53 +02002127
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002128 while (1) {
2129 /* read chunk length */
2130 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
2131 if (buffer != NULL) {
2132 free (buffer);
2133 buffer = NULL;
2134 }
2135 break;
2136 }
2137 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
2138 if (buffer != NULL) {
2139 free (buffer);
2140 buffer = NULL;
2141 }
2142 break;
2143 }
2144 i=0;
2145 memset (chunk_len_str, 0, 12);
2146 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
2147 if (i==0 && c == '#') {
2148 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
2149 /* end but invalid */
2150 if (buffer != NULL) {
2151 free (buffer);
2152 buffer = NULL;
2153 }
2154 }
2155 /* end of message, double-loop break */
2156 goto msg_complete;
2157 }
2158 chunk_len_str[i++] = c;
2159 if (i==11) {
2160 ERROR("Message is too long, buffer for length is not big enought!!!!");
2161 break;
2162 }
2163 }
2164 if (c != '\n') {
2165 if (buffer != NULL) {
2166 free (buffer);
2167 buffer = NULL;
2168 }
2169 break;
2170 }
2171 chunk_len_str[i] = 0;
2172 if ((chunk_len = atoi (chunk_len_str)) == 0) {
2173 if (buffer != NULL) {
2174 free (buffer);
2175 buffer = NULL;
2176 }
2177 break;
2178 }
2179 buffer_size += chunk_len+1;
2180 buffer = realloc (buffer, sizeof(char)*buffer_size);
2181 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
2182 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
2183 if (buffer != NULL) {
2184 free (buffer);
2185 buffer = NULL;
2186 }
2187 break;
2188 }
2189 buffer_len += ret;
2190 }
Tomas Cejka64b87482013-06-03 16:30:53 +02002191msg_complete:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002192 return buffer;
Tomas Cejka64b87482013-06-03 16:30:53 +02002193}
2194
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002195NC_DATASTORE
2196parse_datastore(const char *ds)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002197{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002198 if (strcmp(ds, "running") == 0) {
2199 return NC_DATASTORE_RUNNING;
2200 } else if (strcmp(ds, "startup") == 0) {
2201 return NC_DATASTORE_STARTUP;
2202 } else if (strcmp(ds, "candidate") == 0) {
2203 return NC_DATASTORE_CANDIDATE;
2204 } else if (strcmp(ds, "url") == 0) {
2205 return NC_DATASTORE_URL;
2206 } else if (strcmp(ds, "config") == 0) {
2207 return NC_DATASTORE_CONFIG;
2208 }
2209 return -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002210}
2211
Michal Vaskof35ea502016-02-24 10:44:54 +01002212NC_RPC_EDIT_TESTOPT
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002213parse_testopt(const char *t)
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002214{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002215 if (strcmp(t, "notset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002216 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002217 } else if (strcmp(t, "testset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002218 return NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002219 } else if (strcmp(t, "set") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002220 return NC_RPC_EDIT_TESTOPT_SET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002221 } else if (strcmp(t, "test") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002222 return NC_RPC_EDIT_TESTOPT_TEST;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002223 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002224 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002225}
2226
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002227json_object *
2228create_error_reply(const char *errmess)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002229{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002230 json_object *reply, *array;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002231
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002232 pthread_mutex_lock(&json_lock);
2233 reply = json_object_new_object();
2234 array = json_object_new_array();
2235 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2236 json_object_array_add(array, json_object_new_string(errmess));
2237 json_object_object_add(reply, "errors", array);
2238 pthread_mutex_unlock(&json_lock);
2239
2240 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002241}
2242
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002243json_object *
2244create_data_reply(const char *data)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002245{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002246 pthread_mutex_lock(&json_lock);
2247 json_object *reply = json_object_new_object();
2248 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
2249 json_object_object_add(reply, "data", json_object_new_string(data));
2250 pthread_mutex_unlock(&json_lock);
2251 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002252}
2253
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002254json_object *
2255create_ok_reply(void)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002256{
Michal Vasko365dc4c2016-03-17 10:03:58 +01002257 json_object *reply;
2258
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002259 pthread_mutex_lock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002260 reply = json_object_new_object();
2261 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2262 pthread_mutex_unlock(&json_lock);
2263 return reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002264}
2265
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002266json_object *
2267create_replies(void)
Tomas Cejka09629492014-07-10 15:58:06 +02002268{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002269 json_object *replies;
2270
2271 pthread_mutex_lock(&json_lock);
2272 replies = json_object_new_object();
2273 pthread_mutex_unlock(&json_lock);
2274
2275 return replies;
Tomas Cejka09629492014-07-10 15:58:06 +02002276}
2277
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002278void
2279add_reply(json_object *replies, json_object *reply, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002280{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002281 char *str;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002282
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002283 asprintf(&str, "%u", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002284
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002285 pthread_mutex_lock(&json_lock);
2286 json_object_object_add(replies, str, reply);
2287 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002288
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002289 free(str);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002290}
2291
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002292char *
2293get_param_string(json_object *data, const char *name)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002294{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002295 json_object *js_tmp = NULL;
2296 char *res = NULL;
2297 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
2298 res = strdup(json_object_get_string(js_tmp));
2299 }
2300 return res;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002301}
2302
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002303json_object *
2304handle_op_connect(json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002305{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002306 char *host = NULL;
2307 char *port = NULL;
2308 char *user = NULL;
2309 char *pass = NULL;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002310 char *privkey = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002311 json_object *reply = NULL;
2312 unsigned int session_key = 0;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002313
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002314 DEBUG("Request: connect");
2315 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002316
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002317 host = get_param_string(request, "host");
2318 port = get_param_string(request, "port");
2319 user = get_param_string(request, "user");
2320 pass = get_param_string(request, "pass");
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002321 privkey = get_param_string(request, "privatekey");
Tomas Cejkad5b53772013-06-08 23:01:07 +02002322
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002323 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002324
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002325 if (host == NULL) {
2326 host = "localhost";
2327 }
2328
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002329 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002330 if (user == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002331 ERROR("Cannot connect - insufficient input.");
2332 session_key = 0;
2333 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002334 session_key = netconf_connect(host, port, user, pass, privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002335 DEBUG("Session key: %u", session_key);
2336 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002337
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002338 GETSPEC_ERR_REPLY
2339
2340 pthread_mutex_lock(&json_lock);
2341 if (session_key == 0) {
2342 /* negative reply */
2343 if (err_reply == NULL) {
2344 reply = json_object_new_object();
2345 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2346 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
2347 ERROR("Connection failed.");
2348 } else {
2349 /* use filled err_reply from libnetconf's callback */
2350 reply = err_reply;
2351 ERROR("Connect - error from libnetconf's callback.");
2352 }
2353 } else {
2354 /* positive reply */
2355 reply = json_object_new_object();
2356 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2357 json_object_object_add(reply, "session", json_object_new_int(session_key));
2358 }
2359 memset(pass, 0, strlen(pass));
2360 pthread_mutex_unlock(&json_lock);
2361 CHECK_AND_FREE(host);
2362 CHECK_AND_FREE(user);
2363 CHECK_AND_FREE(port);
2364 CHECK_AND_FREE(pass);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002365 CHECK_AND_FREE(privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002366 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002367}
2368
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002369json_object *
2370handle_op_disconnect(json_object *UNUSED(request), unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002371{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002372 json_object *reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002373
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002374 DEBUG("Request: disconnect (session %u)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002375
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002376 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
2377 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
2378 } else {
2379 reply = create_ok_reply();
2380 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002381
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002382 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002383}
2384
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002385json_object *
2386handle_op_get(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002387{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002388 char *filter = NULL;
2389 char *data = NULL;
2390 json_object *reply = NULL, *obj;
2391 int strict;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002392
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002393 DEBUG("Request: get (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002394
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002395 pthread_mutex_lock(&json_lock);
2396 filter = get_param_string(request, "filter");
2397 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2398 pthread_mutex_unlock(&json_lock);
2399 reply = create_error_reply("Missing strict parameter.");
2400 return reply;
2401 }
2402 strict = json_object_get_boolean(obj);
2403 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002404
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002405 if ((data = netconf_get(session_key, filter, strict, &reply)) == NULL) {
2406 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
2407 } else {
2408 reply = create_data_reply(data);
2409 free(data);
2410 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002411
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002412 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002413}
2414
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002415json_object *
2416handle_op_getconfig(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002417{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002418 NC_DATASTORE ds_type_s = -1;
2419 char *filter = NULL;
2420 char *data = NULL;
2421 char *source = NULL;
2422 json_object *reply = NULL, *obj;
2423 int strict;
Tomas Cejkab4d05872014-02-14 22:44:38 +01002424
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002425 DEBUG("Request: get-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002426
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002427 pthread_mutex_lock(&json_lock);
2428 filter = get_param_string(request, "filter");
2429 source = get_param_string(request, "source");
2430 if (source != NULL) {
2431 ds_type_s = parse_datastore(source);
2432 }
2433 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2434 pthread_mutex_unlock(&json_lock);
2435 reply = create_error_reply("Missing strict parameter.");
2436 return reply;
2437 }
2438 strict = json_object_get_boolean(obj);
2439 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002440
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002441 if ((int)ds_type_s == -1) {
2442 reply = create_error_reply("Invalid source repository type requested.");
2443 goto finalize;
2444 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002445
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002446 if ((data = netconf_getconfig(session_key, ds_type_s, filter, strict, &reply)) == NULL) {
2447 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
2448 } else {
2449 reply = create_data_reply(data);
2450 free(data);
2451 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002452
Tomas Cejka09629492014-07-10 15:58:06 +02002453finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002454 CHECK_AND_FREE(filter);
2455 CHECK_AND_FREE(source);
2456 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002457}
2458
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002459json_object *
2460handle_op_editconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002461{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002462 NC_DATASTORE ds_type_t = -1;
Michal Vaskof35ea502016-02-24 10:44:54 +01002463 NC_RPC_EDIT_DFLTOP defop_type = 0;
2464 NC_RPC_EDIT_ERROPT erropt_type = 0;
2465 NC_RPC_EDIT_TESTOPT testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002466 char *defop = NULL;
2467 char *erropt = NULL;
2468 char *config = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002469 char *target = NULL;
2470 char *testopt = NULL;
2471 char *urisource = NULL;
2472 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002473 struct lyd_node *content;
2474 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002475
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002476 DEBUG("Request: edit-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002477
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002478 pthread_mutex_lock(&json_lock);
2479 /* get parameters */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002480 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2481 pthread_mutex_unlock(&json_lock);
2482 reply = create_error_reply("Missing configs parameter.");
2483 goto finalize;
2484 }
2485 obj = json_object_array_get_idx(configs, idx);
2486 config = strdup(json_object_get_string(obj));
Tomas Cejkad5b53772013-06-08 23:01:07 +02002487
Michal Vaskof35ea502016-02-24 10:44:54 +01002488 target = get_param_string(request, "target");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002489 defop = get_param_string(request, "default-operation");
2490 erropt = get_param_string(request, "error-option");
2491 urisource = get_param_string(request, "uri-source");
2492 testopt = get_param_string(request, "test-option");
2493 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002494
Michal Vaskob8f5d442016-04-05 14:48:37 +02002495 if (!target) {
2496 ERROR("Missing the target parameter.");
2497 goto finalize;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002498 }
Michal Vaskob8f5d442016-04-05 14:48:37 +02002499 ds_type_t = parse_datastore(target);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002500
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002501 if (defop != NULL) {
2502 if (strcmp(defop, "merge") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002503 defop_type = NC_RPC_EDIT_DFLTOP_MERGE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002504 } else if (strcmp(defop, "replace") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002505 defop_type = NC_RPC_EDIT_DFLTOP_REPLACE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002506 } else if (strcmp(defop, "none") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002507 defop_type = NC_RPC_EDIT_DFLTOP_NONE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002508 } else {
2509 reply = create_error_reply("Invalid default-operation parameter.");
2510 goto finalize;
2511 }
2512 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002513 defop_type = NC_RPC_EDIT_DFLTOP_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002514 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002515
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002516 if (erropt != NULL) {
2517 if (strcmp(erropt, "continue-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002518 erropt_type = NC_RPC_EDIT_ERROPT_CONTINUE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002519 } else if (strcmp(erropt, "stop-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002520 erropt_type = NC_RPC_EDIT_ERROPT_STOP;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002521 } else if (strcmp(erropt, "rollback-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002522 erropt_type = NC_RPC_EDIT_ERROPT_ROLLBACK;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002523 } else {
2524 reply = create_error_reply("Invalid error-option parameter.");
2525 goto finalize;
2526 }
2527 } else {
2528 erropt_type = 0;
2529 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002530
Michal Vaskof35ea502016-02-24 10:44:54 +01002531 if ((config && urisource) || (!config && !urisource)) {
2532 reply = create_error_reply("Invalid config and uri-source data parameters.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002533 goto finalize;
2534 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002535
2536 if (config) {
2537 locked_session = session_get_locked(session_key, NULL);
2538 if (!locked_session) {
2539 ERROR("Unknown session or locking failed.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002540 goto finalize;
2541 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002542
2543 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_EDIT);
2544 session_unlock(locked_session);
2545
Michal Vaskoc7a8f3c2016-04-05 14:53:33 +02002546 if (!content) {
2547 ERROR("Failed to parse edit-config content.");
2548 goto finalize;
2549 }
2550
Michal Vaskof35ea502016-02-24 10:44:54 +01002551 free(config);
Michal Vaskoc7a8f3c2016-04-05 14:53:33 +02002552 config = NULL;
2553
Michal Vaskof35ea502016-02-24 10:44:54 +01002554 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2555 lyd_free_withsiblings(content);
Michal Vaskoc7a8f3c2016-04-05 14:53:33 +02002556 if (!config) {
2557 ERROR("Failed to print edit-config content.");
2558 goto finalize;
2559 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002560 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002561 config = urisource;
2562 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002563
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002564 if (testopt != NULL) {
2565 testopt_type = parse_testopt(testopt);
2566 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002567 testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002568 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002569
Michal Vaskof35ea502016-02-24 10:44:54 +01002570 reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002571
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002572 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002573
Tomas Cejka09629492014-07-10 15:58:06 +02002574finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002575 CHECK_AND_FREE(defop);
2576 CHECK_AND_FREE(erropt);
2577 CHECK_AND_FREE(config);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002578 CHECK_AND_FREE(urisource);
2579 CHECK_AND_FREE(target);
2580 CHECK_AND_FREE(testopt);
2581
2582 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002583}
2584
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002585json_object *
2586handle_op_copyconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002587{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002588 NC_DATASTORE ds_type_s = -1;
2589 NC_DATASTORE ds_type_t = -1;
2590 char *config = NULL;
2591 char *target = NULL;
2592 char *source = NULL;
2593 char *uri_src = NULL;
2594 char *uri_trg = NULL;
2595 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002596 struct lyd_node *content;
2597 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002598
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002599 DEBUG("Request: copy-config (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002600
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002601 /* get parameters */
2602 pthread_mutex_lock(&json_lock);
2603 target = get_param_string(request, "target");
2604 source = get_param_string(request, "source");
2605 uri_src = get_param_string(request, "uri-source");
2606 uri_trg = get_param_string(request, "uri-target");
2607 if (!strcmp(source, "config")) {
2608 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2609 pthread_mutex_unlock(&json_lock);
2610 reply = create_error_reply("Missing configs parameter.");
2611 goto finalize;
2612 }
2613 obj = json_object_array_get_idx(configs, idx);
2614 if (!obj) {
2615 pthread_mutex_unlock(&json_lock);
2616 reply = create_error_reply("Configs array parameter shorter than sessions.");
2617 goto finalize;
2618 }
2619 config = strdup(json_object_get_string(obj));
2620 }
2621 pthread_mutex_unlock(&json_lock);
2622
2623 if (target != NULL) {
2624 ds_type_t = parse_datastore(target);
2625 }
2626 if (source != NULL) {
2627 ds_type_s = parse_datastore(source);
2628 }
2629
2630 if ((int)ds_type_s == -1) {
2631 /* invalid source datastore specified */
2632 reply = create_error_reply("Invalid source repository type requested.");
2633 goto finalize;
2634 }
2635
2636 if ((int)ds_type_t == -1) {
2637 /* invalid target datastore specified */
2638 reply = create_error_reply("Invalid target repository type requested.");
2639 goto finalize;
2640 }
2641
2642 if (ds_type_s == NC_DATASTORE_URL) {
2643 if (uri_src == NULL) {
2644 uri_src = "";
2645 }
2646 }
2647 if (ds_type_t == NC_DATASTORE_URL) {
2648 if (uri_trg == NULL) {
2649 uri_trg = "";
2650 }
2651 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002652
2653 if (config) {
2654 locked_session = session_get_locked(session_key, NULL);
2655 if (!locked_session) {
2656 ERROR("Unknown session or locking failed.");
2657 goto finalize;
2658 }
2659
2660 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_CONFIG);
2661 session_unlock(locked_session);
2662
2663 free(config);
2664 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2665 lyd_free_withsiblings(content);
2666 }
2667
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002668 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
2669
2670 CHECK_ERR_SET_REPLY
2671
2672finalize:
2673 CHECK_AND_FREE(config);
2674 CHECK_AND_FREE(target);
2675 CHECK_AND_FREE(source);
2676 CHECK_AND_FREE(uri_src);
2677 CHECK_AND_FREE(uri_trg);
2678
2679 return reply;
2680}
2681
2682json_object *
2683handle_op_deleteconfig(json_object *request, unsigned int session_key)
2684{
2685 json_object *reply;
2686 NC_DATASTORE ds_type = -1;
2687 char *target, *url;
2688
2689 DEBUG("Request: delete-config (session %u)", session_key);
2690
2691 pthread_mutex_lock(&json_lock);
2692 target = get_param_string(request, "target");
2693 url = get_param_string(request, "url");
2694 pthread_mutex_unlock(&json_lock);
2695
2696 if (target != NULL) {
2697 ds_type = parse_datastore(target);
2698 }
2699 if ((int)ds_type == -1) {
2700 reply = create_error_reply("Invalid target repository type requested.");
2701 goto finalize;
2702 }
2703 if (ds_type == NC_DATASTORE_URL) {
2704 if (!url) {
2705 url = "";
2706 }
2707 }
2708
2709 reply = netconf_deleteconfig(session_key, ds_type, url);
2710
2711 CHECK_ERR_SET_REPLY
2712 if (reply == NULL) {
2713 reply = create_ok_reply();
2714 }
2715
2716finalize:
2717 CHECK_AND_FREE(target);
2718 CHECK_AND_FREE(url);
2719 return reply;
2720}
2721
2722json_object *
2723handle_op_lock(json_object *request, unsigned int session_key)
2724{
2725 json_object *reply;
2726 NC_DATASTORE ds_type = -1;
2727 char *target;
2728
2729 DEBUG("Request: lock (session %u)", session_key);
2730
2731 pthread_mutex_lock(&json_lock);
2732 target = get_param_string(request, "target");
2733 pthread_mutex_unlock(&json_lock);
2734
2735 if (target != NULL) {
2736 ds_type = parse_datastore(target);
2737 }
2738 if ((int)ds_type == -1) {
2739 reply = create_error_reply("Invalid target repository type requested.");
2740 goto finalize;
2741 }
2742
2743 reply = netconf_lock(session_key, ds_type);
2744
2745 CHECK_ERR_SET_REPLY
2746 if (reply == NULL) {
2747 reply = create_ok_reply();
2748 }
2749
2750finalize:
2751 CHECK_AND_FREE(target);
2752 return reply;
2753}
2754
2755json_object *
2756handle_op_unlock(json_object *request, unsigned int session_key)
2757{
2758 json_object *reply;
2759 NC_DATASTORE ds_type = -1;
2760 char *target;
2761
2762 DEBUG("Request: unlock (session %u)", session_key);
2763
2764 pthread_mutex_lock(&json_lock);
2765 target = get_param_string(request, "target");
2766 pthread_mutex_unlock(&json_lock);
2767
2768 if (target != NULL) {
2769 ds_type = parse_datastore(target);
2770 }
2771 if ((int)ds_type == -1) {
2772 reply = create_error_reply("Invalid target repository type requested.");
2773 goto finalize;
2774 }
2775
2776 reply = netconf_unlock(session_key, ds_type);
2777
2778 CHECK_ERR_SET_REPLY
2779 if (reply == NULL) {
2780 reply = create_ok_reply();
2781 }
2782
2783finalize:
2784 CHECK_AND_FREE(target);
2785 return reply;
2786}
2787
2788json_object *
2789handle_op_kill(json_object *request, unsigned int session_key)
2790{
2791 json_object *reply = NULL;
2792 char *sid = NULL;
2793
2794 DEBUG("Request: kill-session (session %u)", session_key);
2795
2796 pthread_mutex_lock(&json_lock);
2797 sid = get_param_string(request, "session-id");
2798 pthread_mutex_unlock(&json_lock);
2799
2800 if (sid == NULL) {
2801 reply = create_error_reply("Missing session-id parameter.");
2802 goto finalize;
2803 }
2804
2805 reply = netconf_killsession(session_key, sid);
2806
2807 CHECK_ERR_SET_REPLY
2808
2809finalize:
2810 CHECK_AND_FREE(sid);
2811 return reply;
2812}
2813
2814json_object *
2815handle_op_info(json_object *UNUSED(request), unsigned int session_key)
2816{
2817 json_object *reply = NULL;
2818 struct session_with_mutex *locked_session = NULL;
2819 DEBUG("Request: get info about session %u", session_key);
2820
2821 DEBUG("LOCK wrlock %s", __func__);
2822 if (pthread_rwlock_rdlock(&session_lock) != 0) {
2823 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2824 }
2825
2826 for (locked_session = netconf_sessions_list;
2827 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002828 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002829 if (locked_session != NULL) {
2830 DEBUG("LOCK mutex %s", __func__);
2831 pthread_mutex_lock(&locked_session->lock);
2832 DEBUG("UNLOCK wrlock %s", __func__);
2833 if (pthread_rwlock_unlock(&session_lock) != 0) {
2834 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2835 }
2836 if (locked_session->hello_message != NULL) {
2837 reply = locked_session->hello_message;
2838 } else {
2839 reply = create_error_reply("Invalid session identifier.");
2840 }
2841 DEBUG("UNLOCK mutex %s", __func__);
2842 pthread_mutex_unlock(&locked_session->lock);
2843 } else {
2844 DEBUG("UNLOCK wrlock %s", __func__);
2845 if (pthread_rwlock_unlock(&session_lock) != 0) {
2846 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2847 }
2848 reply = create_error_reply("Invalid session identifier.");
2849 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002850
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002851 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002852}
2853
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002854json_object *
2855handle_op_generic(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002856{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002857 json_object *reply = NULL, *contents, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002858 char *content = NULL, *str;
2859 struct lyd_node *data = NULL, *node_content;
2860 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002861
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002862 DEBUG("Request: generic request (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002863
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002864 pthread_mutex_lock(&json_lock);
2865 if (json_object_object_get_ex(request, "contents", &contents) == FALSE) {
2866 pthread_mutex_unlock(&json_lock);
2867 reply = create_error_reply("Missing contents parameter.");
2868 goto finalize;
2869 }
2870 obj = json_object_array_get_idx(contents, idx);
2871 if (!obj) {
2872 pthread_mutex_unlock(&json_lock);
2873 reply = create_error_reply("Contents array parameter shorter than sessions.");
2874 goto finalize;
2875 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002876 content = strdup(json_object_get_string(obj));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002877 pthread_mutex_unlock(&json_lock);
2878
Michal Vaskof35ea502016-02-24 10:44:54 +01002879 locked_session = session_get_locked(session_key, NULL);
2880 if (!locked_session) {
2881 ERROR("Unknown session or locking failed.");
2882 goto finalize;
2883 }
2884
2885 node_content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), content, LYD_JSON, LYD_OPT_RPC);
2886 session_unlock(locked_session);
2887
2888 free(content);
2889 lyd_print_mem(&content, node_content, LYD_XML, LYP_WITHSIBLINGS);
2890 lyd_free_withsiblings(node_content);
2891
2892 reply = netconf_generic(session_key, content, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002893 if (reply == NULL) {
2894 GETSPEC_ERR_REPLY
2895 if (err_reply != NULL) {
2896 /* use filled err_reply from libnetconf's callback */
2897 reply = err_reply;
2898 }
2899 } else {
2900 if (data == NULL) {
2901 pthread_mutex_lock(&json_lock);
2902 reply = json_object_new_object();
2903 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2904 pthread_mutex_unlock(&json_lock);
2905 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002906 lyd_print_mem(&str, data, LYD_JSON, LYP_WITHSIBLINGS);
2907 lyd_free_withsiblings(data);
2908 reply = create_data_reply(str);
2909 free(str);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002910 }
2911 }
2912
2913finalize:
Michal Vaskof35ea502016-02-24 10:44:54 +01002914 CHECK_AND_FREE(content);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002915 return reply;
2916}
2917
2918json_object *
2919handle_op_getschema(json_object *request, unsigned int session_key)
2920{
2921 char *data = NULL;
2922 char *identifier = NULL;
2923 char *version = NULL;
2924 char *format = NULL;
2925 json_object *reply = NULL;
2926
2927 DEBUG("Request: get-schema (session %u)", session_key);
2928
2929 pthread_mutex_lock(&json_lock);
2930 identifier = get_param_string(request, "identifier");
2931 version = get_param_string(request, "version");
2932 format = get_param_string(request, "format");
2933 pthread_mutex_unlock(&json_lock);
2934
2935 if (identifier == NULL) {
2936 reply = create_error_reply("No identifier for get-schema supplied.");
2937 goto finalize;
2938 }
2939
2940 DEBUG("get-schema(version: %s, format: %s)", version, format);
2941 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
2942 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
2943 } else {
2944 reply = create_data_reply(data);
2945 free(data);
2946 }
2947
2948finalize:
2949 CHECK_AND_FREE(identifier);
2950 CHECK_AND_FREE(version);
2951 CHECK_AND_FREE(format);
2952 return reply;
2953}
2954
2955json_object *
2956handle_op_reloadhello(json_object *UNUSED(request), unsigned int session_key)
2957{
2958 struct nc_session *temp_session = NULL;
2959 struct session_with_mutex * locked_session = NULL;
2960 json_object *reply = NULL;
2961
2962 DEBUG("Request: reload hello (session %u)", session_key);
2963
2964 DEBUG("LOCK wrlock %s", __func__);
2965 if (pthread_rwlock_wrlock(&session_lock) != 0) {
2966 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2967 return NULL;
2968 }
2969
2970 for (locked_session = netconf_sessions_list;
2971 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002972 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002973 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
2974 DEBUG("LOCK mutex %s", __func__);
2975 pthread_mutex_lock(&locked_session->lock);
2976 DEBUG("creating temporary NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01002977 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002978 if (temp_session != NULL) {
2979 prepare_status_message(locked_session, temp_session);
2980 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01002981 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002982 temp_session = NULL;
2983 } else {
2984 DEBUG("Reload hello failed due to channel establishment");
2985 reply = create_error_reply("Reload was unsuccessful, connection failed.");
2986 }
2987 DEBUG("UNLOCK mutex %s", __func__);
2988 pthread_mutex_unlock(&locked_session->lock);
2989 DEBUG("UNLOCK wrlock %s", __func__);
2990 if (pthread_rwlock_unlock(&session_lock) != 0) {
2991 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2992 }
2993 } else {
2994 DEBUG("UNLOCK wrlock %s", __func__);
2995 if (pthread_rwlock_unlock(&session_lock) != 0) {
2996 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2997 }
2998 reply = create_error_reply("Invalid session identifier.");
2999 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02003000
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003001 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
3002 reply = locked_session->hello_message;
3003 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02003004
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003005 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02003006}
3007
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003008void
Michal Vaskof35ea502016-02-24 10:44:54 +01003009notification_history(struct nc_session *session, const struct nc_notif *notif)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003010{
Michal Vaskof35ea502016-02-24 10:44:54 +01003011 time_t eventtime;
3012 char *content;
3013 (void)session;
3014
3015 eventtime = nc_datetime2time(notif->datetime);
3016
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003017 json_object *notif_history_array = (json_object *)pthread_getspecific(notif_history_key);
3018 if (notif_history_array == NULL) {
3019 ERROR("No list of notification history found.");
3020 return;
3021 }
3022 DEBUG("Got notification from history %lu.", (long unsigned)eventtime);
3023 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01003024 json_object *notif_obj = json_object_new_object();
3025 if (notif_obj == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003026 ERROR("Could not allocate memory for notification (json).");
3027 goto failed;
3028 }
Michal Vaskof35ea502016-02-24 10:44:54 +01003029 lyd_print_mem(&content, notif->tree, LYD_JSON, 0);
3030
3031 json_object_object_add(notif_obj, "eventtime", json_object_new_int64(eventtime));
3032 json_object_object_add(notif_obj, "content", json_object_new_string(content));
3033
3034 free(content);
3035
3036 json_object_array_add(notif_history_array, notif_obj);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003037failed:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003038 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003039}
3040
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003041json_object *
3042handle_op_ntfgethistory(json_object *request, unsigned int session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003043{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003044 json_object *reply = NULL;
3045 json_object *js_tmp = NULL;
3046 struct session_with_mutex *locked_session = NULL;
3047 struct nc_session *temp_session = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003048 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003049 time_t start = 0;
3050 time_t stop = 0;
3051 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003052
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003053 DEBUG("Request: get notification history (session %u)", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003054
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003055 pthread_mutex_lock(&json_lock);
3056 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
3057 from = json_object_get_int64(js_tmp);
3058 }
3059 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
3060 to = json_object_get_int64(js_tmp);
3061 }
3062 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02003063
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003064 start = time(NULL) + from;
3065 stop = time(NULL) + to;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003066
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003067 DEBUG("notification history interval %li %li", (long int)from, (long int)to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003068
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003069 DEBUG("LOCK wrlock %s", __func__);
3070 if (pthread_rwlock_rdlock(&session_lock) != 0) {
3071 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3072 reply = create_error_reply("Internal lock failed.");
3073 goto finalize;
3074 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003075
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003076 for (locked_session = netconf_sessions_list;
3077 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003078 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003079 if (locked_session != NULL) {
3080 DEBUG("LOCK mutex %s", __func__);
3081 pthread_mutex_lock(&locked_session->lock);
3082 DEBUG("UNLOCK wrlock %s", __func__);
3083 if (pthread_rwlock_unlock(&session_lock) != 0) {
3084 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3085 }
3086 DEBUG("creating temporal NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01003087 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003088 if (temp_session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003089 rpc = nc_rpc_subscribe(NULL, NULL, nc_time2datetime(start, NULL), nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003090 if (rpc == NULL) {
3091 DEBUG("UNLOCK mutex %s", __func__);
3092 pthread_mutex_unlock(&locked_session->lock);
3093 DEBUG("notifications: creating an rpc request failed.");
3094 reply = create_error_reply("notifications: creating an rpc request failed.");
3095 goto finalize;
3096 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003097
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003098 DEBUG("Send NC subscribe.");
3099 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
3100 json_object *res = netconf_unlocked_op(temp_session, rpc);
3101 if (res != NULL) {
3102 DEBUG("UNLOCK mutex %s", __func__);
3103 pthread_mutex_unlock(&locked_session->lock);
3104 DEBUG("Subscription RPC failed.");
3105 reply = res;
3106 goto finalize;
3107 }
3108 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka6b886e02013-07-05 09:53:17 +02003109
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003110 DEBUG("UNLOCK mutex %s", __func__);
3111 pthread_mutex_unlock(&locked_session->lock);
3112 DEBUG("LOCK mutex %s", __func__);
3113 pthread_mutex_lock(&ntf_history_lock);
3114 pthread_mutex_lock(&json_lock);
3115 json_object *notif_history_array = json_object_new_array();
3116 pthread_mutex_unlock(&json_lock);
3117 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
3118 ERROR("notif_history: cannot set thread-specific hash value.");
3119 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003120
Michal Vaskof35ea502016-02-24 10:44:54 +01003121 nc_recv_notif_dispatch(temp_session, notification_history);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003122
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003123 pthread_mutex_lock(&json_lock);
3124 reply = json_object_new_object();
3125 json_object_object_add(reply, "notifications", notif_history_array);
3126 //json_object_put(notif_history_array);
3127 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003128
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003129 DEBUG("UNLOCK mutex %s", __func__);
3130 pthread_mutex_unlock(&ntf_history_lock);
3131 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01003132 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003133 temp_session = NULL;
3134 } else {
3135 DEBUG("UNLOCK mutex %s", __func__);
3136 pthread_mutex_unlock(&locked_session->lock);
3137 DEBUG("Get history of notification failed due to channel establishment");
3138 reply = create_error_reply("Get history of notification was unsuccessful, connection failed.");
3139 }
3140 } else {
3141 DEBUG("UNLOCK wrlock %s", __func__);
3142 if (pthread_rwlock_unlock(&session_lock) != 0) {
3143 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3144 }
3145 reply = create_error_reply("Invalid session identifier.");
3146 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003147
Tomas Cejka09629492014-07-10 15:58:06 +02003148finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003149 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02003150}
3151
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003152json_object *
3153handle_op_validate(json_object *request, unsigned int session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02003154{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003155 json_object *reply = NULL;
3156 char *target = NULL;
3157 char *url = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003158 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003159 NC_DATASTORE target_ds;
Tomas Cejka4003a702013-10-01 00:02:45 +02003160
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003161 DEBUG("Request: validate datastore (session %u)", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02003162
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003163 pthread_mutex_lock(&json_lock);
3164 target = get_param_string(request, "target");
3165 url = get_param_string(request, "url");
3166 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02003167
3168
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003169 if (target == NULL) {
3170 reply = create_error_reply("Missing target parameter.");
3171 goto finalize;
3172 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003173
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003174 /* validation */
3175 target_ds = parse_datastore(target);
Michal Vaskof35ea502016-02-24 10:44:54 +01003176 rpc = nc_rpc_validate(target_ds, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003177 if (rpc == NULL) {
3178 DEBUG("mod_netconf: creating rpc request failed");
3179 reply = create_error_reply("Creation of RPC request failed.");
3180 goto finalize;
3181 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003182
Michal Vaskof35ea502016-02-24 10:44:54 +01003183 if ((reply = netconf_op(session_key, rpc, 0, NULL)) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003184 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003185
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003186 if (reply == NULL) {
3187 DEBUG("Request: validation ok.");
3188 reply = create_ok_reply();
3189 }
3190 }
3191 nc_rpc_free (rpc);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003192
Tomas Cejka09629492014-07-10 15:58:06 +02003193finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003194 CHECK_AND_FREE(target);
3195 CHECK_AND_FREE(url);
3196 return reply;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003197}
3198
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003199json_object *
3200handle_op_query(json_object *request, unsigned int session_key, int idx)
David Kupka8e60a372012-09-04 09:15:20 +02003201{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003202 json_object *reply = NULL, *filters, *obj;
3203 char *filter = NULL;
3204 int load_children = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003205
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003206 DEBUG("Request: query (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003207
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003208 pthread_mutex_lock(&json_lock);
3209 if (json_object_object_get_ex(request, "filters", &filters) == FALSE) {
3210 pthread_mutex_unlock(&json_lock);
3211 reply = create_error_reply("Missing filters parameter.");
3212 goto finalize;
3213 }
3214 obj = json_object_array_get_idx(filters, idx);
3215 if (!obj) {
3216 pthread_mutex_unlock(&json_lock);
3217 reply = create_error_reply("Filters array parameter shorter than sessions.");
3218 goto finalize;
3219 }
3220 filter = strdup(json_object_get_string(obj));
3221 if (json_object_object_get_ex(request, "load_children", &obj) == TRUE) {
3222 load_children = json_object_get_boolean(obj);
3223 }
3224 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02003225
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003226 reply = libyang_query(session_key, filter, load_children);
David Kupka8e60a372012-09-04 09:15:20 +02003227
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003228 CHECK_ERR_SET_REPLY
3229 if (!reply) {
3230 reply = create_error_reply("Query failed.");
3231 }
David Kupka8e60a372012-09-04 09:15:20 +02003232
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003233finalize:
3234 CHECK_AND_FREE(filter);
3235 return reply;
3236}
David Kupka8e60a372012-09-04 09:15:20 +02003237
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003238json_object *
3239handle_op_merge(json_object *request, unsigned int session_key, int idx)
3240{
3241 json_object *reply = NULL, *configs, *obj;
3242 char *config = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003243 struct lyd_node *content;
3244 struct session_with_mutex *locked_session;
David Kupka8e60a372012-09-04 09:15:20 +02003245
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003246 DEBUG("Request: merge (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003247
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003248 pthread_mutex_lock(&json_lock);
3249 if (json_object_object_get_ex(request, "configurations", &configs) == FALSE) {
3250 pthread_mutex_unlock(&json_lock);
3251 reply = create_error_reply("Missing configurations parameter.");
3252 goto finalize;
3253 }
3254 obj = json_object_array_get_idx(configs, idx);
3255 if (!obj) {
3256 pthread_mutex_unlock(&json_lock);
3257 reply = create_error_reply("Filters array parameter shorter than sessions.");
3258 goto finalize;
3259 }
3260 config = strdup(json_object_get_string(obj));
3261 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02003262
Michal Vaskof35ea502016-02-24 10:44:54 +01003263 locked_session = session_get_locked(session_key, NULL);
3264 if (!locked_session) {
3265 ERROR("Unknown session or locking failed.");
3266 goto finalize;
3267 }
3268
3269 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_DATA);
3270 session_unlock(locked_session);
3271
3272 free(config);
3273 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
3274 lyd_free_withsiblings(content);
3275
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003276 reply = libyang_merge(session_key, config);
David Kupka8e60a372012-09-04 09:15:20 +02003277
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003278 CHECK_ERR_SET_REPLY
3279 if (!reply) {
3280 reply = create_error_reply("Merge failed.");
3281 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003282
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003283finalize:
3284 CHECK_AND_FREE(config);
3285 return reply;
3286}
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003287
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003288void *
3289thread_routine(void *arg)
3290{
3291 void *retval = NULL;
3292 struct pollfd fds;
3293 json_object *request = NULL, *replies = NULL, *reply, *sessions = NULL;
3294 json_object *js_tmp = NULL;
Michal Vaskodf280a22016-03-17 09:19:53 +01003295 int operation = (-1), count, i, sent;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003296 int status = 0;
3297 const char *msgtext;
3298 unsigned int session_key = 0;
3299 char *chunked_out_msg = NULL;
3300 int client = ((struct pass_to_thread *)arg)->client;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003301
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003302 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02003303
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003304 /* init thread specific err_reply memory */
3305 create_err_reply_p();
David Kupka8e60a372012-09-04 09:15:20 +02003306
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003307 while (!isterminated) {
3308 fds.fd = client;
3309 fds.events = POLLIN;
3310 fds.revents = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003311
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003312 status = poll(&fds, 1, 1000);
Tomas Cejkad5b53772013-06-08 23:01:07 +02003313
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003314 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
3315 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
3316 continue;
3317 } else if (status < 0) {
3318 /* 0: poll time outed
3319 * close socket and ignore this request from the client, it can try it again
3320 * -1: poll failed
3321 * something wrong happend, close this socket and wait for another request
3322 */
3323 close(client);
3324 break;
3325 }
3326 /* status > 0 */
David Kupka8e60a372012-09-04 09:15:20 +02003327
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003328 /* check the status of the socket */
David Kupka8e60a372012-09-04 09:15:20 +02003329
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003330 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
3331 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
3332 /* close client's socket (it's probably already closed by client */
3333 close(client);
3334 break;
3335 }
Tomas Cejka09629492014-07-10 15:58:06 +02003336
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003337 DEBUG("Get framed message...");
3338 buffer = get_framed_message(client);
Tomas Cejka09629492014-07-10 15:58:06 +02003339
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003340 DEBUG("Check read buffer.");
3341 if (buffer != NULL) {
Michal Vaskoe7d1bbc2016-04-05 16:09:29 +02003342 DEBUG("Received message:\n%s\n", buffer);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003343 enum json_tokener_error jerr;
3344 pthread_mutex_lock(&json_lock);
3345 request = json_tokener_parse_verbose(buffer, &jerr);
3346 if (jerr != json_tokener_success) {
3347 ERROR("JSON parsing error");
3348 pthread_mutex_unlock(&json_lock);
3349 continue;
3350 }
3351
3352 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
3353 operation = json_object_get_int(js_tmp);
3354 }
3355 pthread_mutex_unlock(&json_lock);
3356 if (operation == -1) {
3357 replies = create_replies();
3358 add_reply(replies, create_error_reply("Missing operation type from frontend."), 0);
3359 goto send_reply;
3360 }
3361
3362 if ((operation < 4) || ((operation > 19) && (operation < 100)) || (operation > 101)) {
3363 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
3364 replies = create_replies();
3365 add_reply(replies, create_error_reply("Operation not supported."), 0);
3366 goto send_reply;
3367 }
3368
3369 DEBUG("operation %d", operation);
3370
3371 /* null global JSON error-reply */
3372 clean_err_reply();
3373
3374 /* clean replies envelope */
3375 if (replies != NULL) {
3376 pthread_mutex_lock(&json_lock);
3377 json_object_put(replies);
3378 pthread_mutex_unlock(&json_lock);
3379 }
3380 replies = create_replies();
3381
3382 if (operation == MSG_CONNECT) {
3383 count = 1;
3384 } else {
3385 pthread_mutex_lock(&json_lock);
3386 if (json_object_object_get_ex(request, "sessions", &sessions) == FALSE) {
Michal Vasko642cad02016-03-17 12:31:18 +01003387 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003388 add_reply(replies, create_error_reply("Operation missing \"sessions\" arg"), 0);
3389 goto send_reply;
3390 }
3391 count = json_object_array_length(sessions);
3392 pthread_mutex_unlock(&json_lock);
3393 }
3394
3395 for (i = 0; i < count; ++i) {
3396 if (operation != MSG_CONNECT) {
3397 js_tmp = json_object_array_get_idx(sessions, i);
3398 session_key = json_object_get_int(js_tmp);
3399 }
3400
3401 /* process required operation */
Michal Vasko977bad92016-03-15 16:20:51 +01003402 reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003403 switch (operation) {
3404 case MSG_CONNECT:
3405 reply = handle_op_connect(request);
3406 break;
3407 case MSG_DISCONNECT:
3408 reply = handle_op_disconnect(request, session_key);
3409 break;
3410 case MSG_GET:
3411 reply = handle_op_get(request, session_key);
3412 break;
3413 case MSG_GETCONFIG:
3414 reply = handle_op_getconfig(request, session_key);
3415 break;
3416 case MSG_EDITCONFIG:
3417 reply = handle_op_editconfig(request, session_key, i);
3418 break;
3419 case MSG_COPYCONFIG:
3420 reply = handle_op_copyconfig(request, session_key, i);
3421 break;
3422 case MSG_DELETECONFIG:
3423 reply = handle_op_deleteconfig(request, session_key);
3424 break;
3425 case MSG_LOCK:
3426 reply = handle_op_lock(request, session_key);
3427 break;
3428 case MSG_UNLOCK:
3429 reply = handle_op_unlock(request, session_key);
3430 break;
3431 case MSG_KILL:
3432 reply = handle_op_kill(request, session_key);
3433 break;
3434 case MSG_INFO:
3435 reply = handle_op_info(request, session_key);
3436 break;
3437 case MSG_GENERIC:
3438 reply = handle_op_generic(request, session_key, i);
3439 break;
3440 case MSG_GETSCHEMA:
3441 reply = handle_op_getschema(request, session_key);
3442 break;
3443 case MSG_RELOADHELLO:
3444 reply = handle_op_reloadhello(request, session_key);
3445 break;
3446 case MSG_NTF_GETHISTORY:
3447 reply = handle_op_ntfgethistory(request, session_key);
3448 break;
3449 case MSG_VALIDATE:
3450 reply = handle_op_validate(request, session_key);
3451 break;
3452 case SCH_QUERY:
3453 reply = handle_op_query(request, session_key, i);
3454 break;
3455 case SCH_MERGE:
3456 reply = handle_op_merge(request, session_key, i);
3457 break;
3458 }
3459
3460 add_reply(replies, reply, session_key);
3461 }
3462
3463 /* free parameters */
3464 operation = (-1);
3465
3466 DEBUG("Clean request json object.");
3467 if (request != NULL) {
3468 pthread_mutex_lock(&json_lock);
3469 json_object_put(request);
3470 pthread_mutex_unlock(&json_lock);
Michal Vasko365dc4c2016-03-17 10:03:58 +01003471 request = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003472 }
3473 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02003474
3475send_reply:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003476 /* send reply to caller */
3477 if (replies) {
3478 pthread_mutex_lock(&json_lock);
3479 msgtext = json_object_to_json_string(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003480 pthread_mutex_unlock(&json_lock);
Michal Vaskof849e4e2016-04-06 10:00:08 +02003481 DEBUG("Sending message:\n%s\n", msgtext);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003482 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
3483 if (buffer != NULL) {
3484 free(buffer);
3485 buffer = NULL;
3486 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003487 break;
3488 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003489
Michal Vaskodf280a22016-03-17 09:19:53 +01003490 i = 0;
3491 sent = 0;
3492 count = strlen(chunked_out_msg) + 1;
3493 while (count && ((i = send(client, chunked_out_msg + sent, count, 0)) != -1)) {
3494 sent += i;
3495 count -= i;
3496 }
3497 if (i == -1) {
3498 ERROR("Sending message failed (%s).", strerror(errno));
3499 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003500 DEBUG("Clean reply json object.");
3501 pthread_mutex_lock(&json_lock);
3502 json_object_put(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003503 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003504 replies = NULL;
3505 DEBUG("Clean message buffer.");
3506 CHECK_AND_FREE(chunked_out_msg);
3507 chunked_out_msg = NULL;
3508 if (buffer) {
3509 free(buffer);
3510 buffer = NULL;
3511 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003512 clean_err_reply();
3513 } else {
3514 ERROR("Reply is NULL, shouldn't be...");
3515 continue;
3516 }
3517 }
3518 }
3519 free(arg);
3520 free_err_reply();
Michal Vaskod0205992016-03-17 10:04:49 +01003521 nc_thread_destroy();
David Kupka8e60a372012-09-04 09:15:20 +02003522
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003523 return retval;
David Kupka8e60a372012-09-04 09:15:20 +02003524}
3525
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003526/**
3527 * \brief Close all open NETCONF sessions.
3528 *
3529 * During termination of mod_netconf, it is useful to close all remaining
3530 * sessions. This function iterates over the list of sessions and close them
3531 * all.
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003532 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003533static void
3534close_all_nc_sessions(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003535{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003536 struct session_with_mutex *locked_session, *next_session;
3537 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003538
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003539 /* get exclusive access to sessions_list (conns) */
3540 DEBUG("LOCK wrlock %s", __func__);
3541 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
3542 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3543 return;
3544 }
3545 for (next_session = netconf_sessions_list; next_session;) {
3546 locked_session = next_session;
3547 next_session = locked_session->next;
Tomas Cejka47387fd2013-06-10 20:37:46 +02003548
Michal Vaskoc3146782015-11-04 14:46:41 +01003549 /* close_and_free_session handles locking on its own */
Michal Vaskof35ea502016-02-24 10:44:54 +01003550 DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003551 close_and_free_session(locked_session);
3552 }
3553 netconf_sessions_list = NULL;
3554
3555 /* get exclusive access to sessions_list (conns) */
3556 DEBUG("UNLOCK wrlock %s", __func__);
3557 if (pthread_rwlock_unlock (&session_lock) != 0) {
3558 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3559 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003560}
3561
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003562static void
3563check_timeout_and_close(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003564{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003565 struct nc_session *ns = NULL;
3566 struct session_with_mutex *locked_session = NULL;
3567 time_t current_time = time(NULL);
3568 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003569
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003570 /* get exclusive access to sessions_list (conns) */
3571 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
3572 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3573 return;
3574 }
3575 for (locked_session = netconf_sessions_list; locked_session; locked_session = locked_session->next) {
3576 ns = locked_session->session;
3577 if (ns == NULL) {
3578 continue;
3579 }
3580 pthread_mutex_lock(&locked_session->lock);
3581 if ((current_time - locked_session->last_activity) > ACTIVITY_TIMEOUT) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003582 DEBUG("Closing NETCONF session %u (SID %u).", locked_session->session_key, nc_session_get_id(locked_session->session));
Tomas Cejka47387fd2013-06-10 20:37:46 +02003583
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003584 /* close_and_free_session handles locking on its own */
3585 close_and_free_session(locked_session);
3586 } else {
3587 pthread_mutex_unlock(&locked_session->lock);
3588 }
3589 }
3590 /* get exclusive access to sessions_list (conns) */
3591 if (pthread_rwlock_unlock(&session_lock) != 0) {
3592 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3593 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003594}
3595
3596
3597/**
Radek Krejcif23850c2012-07-23 16:14:17 +02003598 * This is actually implementation of NETCONF client
3599 * - requests are received from UNIX socket in the predefined format
3600 * - results are replied through the same way
Michal Vaskoc3146782015-11-04 14:46:41 +01003601 * - the daemon run as a separate process
Radek Krejcif23850c2012-07-23 16:14:17 +02003602 *
3603 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003604static void
3605forked_proc(void)
Radek Krejci469aab82012-07-22 18:42:20 +02003606{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003607 struct timeval tv;
3608 struct sockaddr_un local, remote;
3609 int lsock, client, ret, i, pthread_count = 0;
3610 unsigned int olds = 0, timediff = 0;
3611 socklen_t len;
3612 struct pass_to_thread *arg;
3613 pthread_t *ptids = calloc(1, sizeof(pthread_t));
3614 struct timespec maxtime;
3615 pthread_rwlockattr_t lock_attrs;
3616 #ifdef WITH_NOTIFICATIONS
3617 char use_notifications = 0;
3618 #endif
David Kupka8e60a372012-09-04 09:15:20 +02003619
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003620 /* wait at most 5 seconds for every thread to terminate */
3621 maxtime.tv_sec = 5;
3622 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02003623
Tomas Cejka04e08f42014-03-27 19:52:34 +01003624#ifdef HAVE_UNIXD_SETUP_CHILD
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003625 /* change uid and gid of process for security reasons */
3626 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01003627#else
3628# ifdef SU_GROUP
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003629 if (strlen(SU_GROUP) > 0) {
3630 struct group *g = getgrnam(SU_GROUP);
3631 if (g == NULL) {
3632 ERROR("GID (%s) was not found.", SU_GROUP);
3633 return;
3634 }
3635 if (setgid(g->gr_gid) != 0) {
3636 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
3637 return;
3638 }
3639 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003640# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003641 DEBUG("no SU_GROUP");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003642# endif
3643# ifdef SU_USER
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003644 if (strlen(SU_USER) > 0) {
3645 struct passwd *p = getpwnam(SU_USER);
3646 if (p == NULL) {
3647 ERROR("UID (%s) was not found.", SU_USER);
3648 return;
3649 }
3650 if (setuid(p->pw_uid) != 0) {
3651 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
3652 return;
3653 }
3654 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003655# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003656 DEBUG("no SU_USER");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003657# endif
3658#endif
Radek Krejci469aab82012-07-22 18:42:20 +02003659
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003660 /* try to remove if exists */
3661 unlink(sockname);
Tomas Cejka04e08f42014-03-27 19:52:34 +01003662
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003663 /* create listening UNIX socket to accept incoming connections */
3664 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
3665 ERROR("Creating socket failed (%s)", strerror(errno));
3666 goto error_exit;
3667 }
Radek Krejci469aab82012-07-22 18:42:20 +02003668
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003669 local.sun_family = AF_UNIX;
3670 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
3671 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
Radek Krejci469aab82012-07-22 18:42:20 +02003672
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003673 if (bind(lsock, (struct sockaddr *)&local, len) == -1) {
3674 if (errno == EADDRINUSE) {
3675 ERROR("mod_netconf socket address already in use");
3676 goto error_exit;
3677 }
3678 ERROR("Binding socket failed (%s)", strerror(errno));
3679 goto error_exit;
3680 }
Radek Krejci469aab82012-07-22 18:42:20 +02003681
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003682 if (listen(lsock, MAX_SOCKET_CL) == -1) {
3683 ERROR("Setting up listen socket failed (%s)", strerror(errno));
3684 goto error_exit;
3685 }
3686 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02003687
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003688 uid_t user = -1;
3689 if (strlen(CHOWN_USER) > 0) {
3690 struct passwd *p = getpwnam(CHOWN_USER);
3691 if (p != NULL) {
3692 user = p->pw_uid;
3693 }
3694 }
3695 gid_t group = -1;
3696 if (strlen(CHOWN_GROUP) > 0) {
3697 struct group *g = getgrnam(CHOWN_GROUP);
3698 if (g != NULL) {
3699 group = g->gr_gid;
3700 }
3701 }
3702 if (chown(sockname, user, group) == -1) {
3703 ERROR("Chown on socket file failed (%s).", strerror(errno));
3704 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003705
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003706 /* prepare internal lists */
Tomas Cejkaba21b382013-04-13 02:37:32 +02003707
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003708 #ifdef WITH_NOTIFICATIONS
3709 if (notification_init() == -1) {
3710 ERROR("libwebsockets initialization failed");
3711 use_notifications = 0;
3712 } else {
3713 use_notifications = 1;
3714 }
3715 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003716
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003717 /* setup libnetconf's callbacks */
Michal Vaskod0205992016-03-17 10:04:49 +01003718 nc_client_init();
Michal Vaskodedf91b2016-04-06 10:00:31 +02003719 nc_verbosity(NC_VERB_VERBOSE);
Michal Vaskof35ea502016-02-24 10:44:54 +01003720 nc_set_print_clb(clb_print);
3721 nc_client_ssh_set_auth_hostkey_check_clb(netconf_callback_ssh_hostkey_check);
3722 nc_client_ssh_set_auth_interactive_clb(netconf_callback_sshauth_interactive);
3723 nc_client_ssh_set_auth_password_clb(netconf_callback_sshauth_password);
3724 nc_client_ssh_set_auth_privkey_passphrase_clb(netconf_callback_sshauth_passphrase);
Radek Krejci469aab82012-07-22 18:42:20 +02003725
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003726 /* disable publickey authentication */
Michal Vaskof35ea502016-02-24 10:44:54 +01003727 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1);
Radek Krejci469aab82012-07-22 18:42:20 +02003728
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003729 /* create mutex protecting session list */
3730 pthread_rwlockattr_init(&lock_attrs);
3731 /* rwlock is shared only with threads in this process */
3732 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
3733 /* create rw lock */
3734 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
3735 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
3736 goto error_exit;
3737 }
3738 pthread_mutex_init(&ntf_history_lock, NULL);
3739 pthread_mutex_init(&json_lock, NULL);
3740 DEBUG("Initialization of notification history.");
3741 if (pthread_key_create(&notif_history_key, NULL) != 0) {
3742 ERROR("Initialization of notification history failed.");
3743 }
3744 if (pthread_key_create(&err_reply_key, NULL) != 0) {
3745 ERROR("Initialization of reply key failed.");
3746 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02003747
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003748 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
3749 while (isterminated == 0) {
3750 gettimeofday(&tv, NULL);
3751 timediff = (unsigned int)tv.tv_sec - olds;
3752 #ifdef WITH_NOTIFICATIONS
3753 if (use_notifications == 1) {
3754 notification_handle();
3755 }
3756 #endif
3757 if (timediff > ACTIVITY_CHECK_INTERVAL) {
3758 check_timeout_and_close();
3759 }
Radek Krejci469aab82012-07-22 18:42:20 +02003760
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003761 /* open incoming connection if any */
3762 len = sizeof(remote);
3763 client = accept(lsock, (struct sockaddr *) &remote, &len);
3764 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
3765 usleep(SLEEP_TIME * 1000);
3766 continue;
3767 } else if (client == -1 && (errno == EINTR)) {
3768 continue;
3769 } else if (client == -1) {
3770 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
3771 continue;
3772 }
Radek Krejci469aab82012-07-22 18:42:20 +02003773
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003774 /* set client's socket as non-blocking */
3775 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02003776
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003777 arg = malloc(sizeof(struct pass_to_thread));
3778 arg->client = client;
3779 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02003780
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003781 /* start new thread. It will serve this particular request and then terminate */
3782 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void *)arg)) != 0) {
3783 ERROR("Creating POSIX thread failed: %d\n", ret);
3784 } else {
3785 DEBUG("Thread %lu created", ptids[pthread_count]);
3786 pthread_count++;
3787 ptids = realloc (ptids, sizeof(pthread_t) * (pthread_count+1));
3788 ptids[pthread_count] = 0;
3789 }
Radek Krejci469aab82012-07-22 18:42:20 +02003790
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003791 /* check if some thread already terminated, free some resources by joining it */
3792 for (i = 0; i < pthread_count; i++) {
3793 if (pthread_tryjoin_np(ptids[i], (void **)&arg) == 0) {
3794 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
3795 pthread_count--;
3796 if (pthread_count > 0) {
3797 /* place last Thread ID on the place of joined one */
3798 ptids[i] = ptids[pthread_count];
3799 }
3800 }
3801 }
3802 DEBUG("Running %d threads", pthread_count);
3803 }
Radek Krejci469aab82012-07-22 18:42:20 +02003804
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003805 DEBUG("mod_netconf terminating...");
3806 /* join all threads */
3807 for (i = 0; i < pthread_count; i++) {
3808 pthread_timedjoin_np(ptids[i], (void **)&arg, &maxtime);
3809 }
Radek Krejci469aab82012-07-22 18:42:20 +02003810
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003811 #ifdef WITH_NOTIFICATIONS
3812 notification_close();
3813 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003814
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003815 /* close all NETCONF sessions */
3816 close_all_nc_sessions();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003817
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003818 /* destroy rwlock */
3819 pthread_rwlock_destroy(&session_lock);
3820 pthread_rwlockattr_destroy(&lock_attrs);
David Kupka8e60a372012-09-04 09:15:20 +02003821
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003822 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02003823
Michal Vaskod0205992016-03-17 10:04:49 +01003824 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003825 free(ptids);
3826 close(lsock);
3827 exit(0);
3828 return;
3829
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003830error_exit:
Michal Vaskod0205992016-03-17 10:04:49 +01003831 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003832 close(lsock);
3833 free(ptids);
3834 return;
Radek Krejci469aab82012-07-22 18:42:20 +02003835}
3836
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003837int
3838main(int argc, char **argv)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003839{
Michal Vaskoc3146782015-11-04 14:46:41 +01003840 struct sigaction action;
3841 sigset_t block_mask;
Michal Vaskoefdd49f2016-04-06 11:13:27 +02003842 int i;
Michal Vaskoc3146782015-11-04 14:46:41 +01003843
Michal Vaskoa53ef882015-11-24 11:02:01 +01003844 if (argc > 3) {
3845 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3846 return 1;
3847 }
3848
3849 sockname = SOCKET_FILENAME;
3850 for (i = 1; i < argc; ++i) {
3851 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
3852 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3853 return 0;
3854 } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) {
3855 daemonize = 1;
3856 } else {
3857 sockname = argv[i];
3858 }
3859 }
3860
Michal Vaskoefdd49f2016-04-06 11:13:27 +02003861 if (daemonize) {
3862 if (daemon(0, 0) == -1) {
3863 ERROR("daemon() failed (%s)", strerror(errno));
3864 return 1;
3865 }
3866 openlog("netopeerguid", LOG_PID, LOG_DAEMON);
Michal Vaskoc3146782015-11-04 14:46:41 +01003867 }
3868
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003869 sigfillset(&block_mask);
Michal Vaskoc3146782015-11-04 14:46:41 +01003870 action.sa_handler = signal_handler;
3871 action.sa_mask = block_mask;
3872 action.sa_flags = 0;
3873 sigaction(SIGINT, &action, NULL);
3874 sigaction(SIGTERM, &action, NULL);
3875
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003876 forked_proc();
3877 DEBUG("Terminated");
Michal Vaskoefdd49f2016-04-06 11:13:27 +02003878 if (daemonize) {
3879 closelog();
3880 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003881 return 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003882}