blob: a5197dd3b45750ab21a3e004465e96a0cb6c180d [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
Michal Vasko30aa3822015-11-25 10:41:54 +01002 * \file netopeerguid.c
3 * \brief NetopeerGUI daemon
Radek Krejci469aab82012-07-22 18:42:20 +02004 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoa53ef882015-11-24 11:02:01 +01006 * \author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci469aab82012-07-22 18:42:20 +02007 * \date 2011
8 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01009 * \date 2013
Michal Vaskoa53ef882015-11-24 11:02:01 +010010 * \date 2015
Radek Krejci469aab82012-07-22 18:42:20 +020011 */
12/*
Michal Vaskoa53ef882015-11-24 11:02:01 +010013 * Copyright (C) 2011-2015 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020014 *
15 * LICENSE TERMS
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in
24 * the documentation and/or other materials provided with the
25 * distribution.
26 * 3. Neither the name of the Company nor the names of its contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * ALTERNATIVELY, provided that this notice is retained in full, this
31 * product may be distributed under the terms of the GNU General Public
32 * License (GPL) version 2 or later, in which case the provisions
33 * of the GPL apply INSTEAD OF those given above.
34 *
35 * This software is provided ``as is'', and any express or implied
36 * warranties, including, but not limited to, the implied warranties of
37 * merchantability and fitness for a particular purpose are disclaimed.
38 * In no event shall the company or contributors be liable for any
39 * direct, indirect, incidental, special, exemplary, or consequential
40 * damages (including, but not limited to, procurement of substitute
41 * goods or services; loss of use, data, or profits; or business
42 * interruption) however caused and on any theory of liability, whether
43 * in contract, strict liability, or tort (including negligence or
44 * otherwise) arising in any way out of the use of this software, even
45 * if advised of the possibility of such damage.
46 *
47 */
Michal Vaskoc3146782015-11-04 14:46:41 +010048#define _GNU_SOURCE
Radek Krejci469aab82012-07-22 18:42:20 +020049
Radek Krejci7b4ddd02012-07-30 08:09:58 +020050#include <unistd.h>
51#include <poll.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010052#include <time.h>
Michal Vaskob69dde22016-03-09 11:42:20 +010053#include <fcntl.h>
Radek Krejci469aab82012-07-22 18:42:20 +020054#include <sys/types.h>
55#include <sys/socket.h>
56#include <sys/un.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010057#include <sys/stat.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010058#include <pwd.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010059#include <errno.h>
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010060#include <limits.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010061#include <grp.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010062#include <signal.h>
David Kupka8e60a372012-09-04 09:15:20 +020063#include <pthread.h>
64#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020065
Michal Vaskof35ea502016-02-24 10:44:54 +010066#include <nc_client.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020067
Tomas Cejka04e08f42014-03-27 19:52:34 +010068#include "../config.h"
69
Tomas Cejkad340dbf2013-03-24 20:36:57 +010070#ifdef WITH_NOTIFICATIONS
Michal Vaskoa53ef882015-11-24 11:02:01 +010071#include "notification_server.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010072#endif
73
Tomas Cejka94da2c52013-01-08 18:20:30 +010074#include "message_type.h"
Michal Vaskoa53ef882015-11-24 11:02:01 +010075#include "netopeerguid.h"
Radek Krejci469aab82012-07-22 18:42:20 +020076
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010077#define SCHEMA_DIR "/tmp/yang_models"
Radek Krejci469aab82012-07-22 18:42:20 +020078#define MAX_PROCS 5
Michal Vaskoa53ef882015-11-24 11:02:01 +010079#define SOCKET_FILENAME "/var/run/netopeerguid.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020080#define MAX_SOCKET_CL 10
81#define BUFFER_SIZE 4096
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010082#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
83#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020084
Michal Vasko7732dee2015-11-05 10:22:15 +010085/* sleep in master process for non-blocking socket reading, in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020086#define SLEEP_TIME 200
87
88#ifndef offsetof
89#define offsetof(type, member) ((size_t) ((type *) 0)->member)
90#endif
91
Tomas Cejka027f3bc2012-11-10 20:28:36 +010092/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020093struct timeval timeout = { 1, 0 };
94
Michal Vaskoda0ab5c2015-11-13 13:24:51 +010095#define NCWITHDEFAULTS NCWD_MODE_NOTSET
Tomas Cejka5064c232013-01-17 09:30:58 +010096
Radek Krejci469aab82012-07-22 18:42:20 +020097#define MSG_OK 0
98#define MSG_OPEN 1
99#define MSG_DATA 2
100#define MSG_CLOSE 3
101#define MSG_ERROR 4
102#define MSG_UNKNOWN 5
103
Tomas Cejka47387fd2013-06-10 20:37:46 +0200104pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200105pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100106pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100107pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
108
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100109unsigned int session_key_generator = 1;
Michal Vaskoc3146782015-11-04 14:46:41 +0100110struct session_with_mutex *netconf_sessions_list = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100111static const char *sockname;
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200112static pthread_key_t notif_history_key;
Tomas Cejka442258e2014-04-01 18:17:18 +0200113pthread_key_t err_reply_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200114volatile int isterminated = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200115static char* password;
116
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100117json_object *create_ok_reply(void);
118json_object *create_data_reply(const char *data);
119static char *netconf_getschema(unsigned int session_key, const char *identifier, const char *version,
120 const char *format, json_object **err);
Michal Vaskof35ea502016-02-24 10:44:54 +0100121static void node_add_metadata_recursive(struct lyd_node *data_tree, const struct lys_module *module,
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100122 json_object *data_json_parent);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100123static void node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100124
125static void
126signal_handler(int sign)
Radek Krejci469aab82012-07-22 18:42:20 +0200127{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100128 switch (sign) {
129 case SIGINT:
130 case SIGTERM:
131 isterminated = 1;
132 break;
133 }
Radek Krejci469aab82012-07-22 18:42:20 +0200134}
135
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100136int
137netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session))
Radek Krejci469aab82012-07-22 18:42:20 +0200138{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100139 /* always approve */
140 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200141}
142
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100143char *
Michal Vaskof35ea502016-02-24 10:44:54 +0100144netconf_callback_sshauth_passphrase(const char *UNUSED(priv_key_file))
Radek Krejci469aab82012-07-22 18:42:20 +0200145{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100146 char *buf;
147 buf = strdup(password);
148 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200149}
150
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100151char *
152netconf_callback_sshauth_password(const char *UNUSED(username), const char *UNUSED(hostname))
Radek Krejci469aab82012-07-22 18:42:20 +0200153{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100154 char *buf;
155 buf = strdup(password);
156 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200157}
158
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100159char *
160netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction),
161 const char *UNUSED(prompt), int UNUSED(echo))
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200162{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100163 char *buf;
164 buf = strdup(password);
165 return (buf);
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200166}
167
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100168void
169netconf_callback_error_process(const char *UNUSED(tag),
170 const char *UNUSED(type),
171 const char *UNUSED(severity),
172 const char *UNUSED(apptag),
173 const char *UNUSED(path),
174 const char *message,
175 const char *UNUSED(attribute),
176 const char *UNUSED(element),
177 const char *UNUSED(ns),
178 const char *UNUSED(sid))
Radek Krejcic11fd862012-07-26 12:41:21 +0200179{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100180 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
181 if (err_reply_p == NULL) {
182 ERROR("Error message was not allocated. %s", __func__);
183 return;
184 }
185 json_object *err_reply = *err_reply_p;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100186
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100187 json_object *array = NULL;
188 if (err_reply == NULL) {
189 ERROR("error calback: empty error list");
190 pthread_mutex_lock(&json_lock);
191 err_reply = json_object_new_object();
192 array = json_object_new_array();
193 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
194 json_object_object_add(err_reply, "errors", array);
195 if (message != NULL) {
196 json_object_array_add(array, json_object_new_string(message));
197 }
198 pthread_mutex_unlock(&json_lock);
199 (*err_reply_p) = err_reply;
200 } else {
201 ERROR("error calback: nonempty error list");
202 pthread_mutex_lock(&json_lock);
203 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
204 if (message != NULL) {
205 json_object_array_add(array, json_object_new_string(message));
206 }
207 }
208 pthread_mutex_unlock(&json_lock);
209 }
210 pthread_setspecific(err_reply_key, err_reply_p);
211 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200212}
213
Tomas Cejka47387fd2013-06-10 20:37:46 +0200214/**
215 * should be used in locked area
216 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100217void
218prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200219{
Michal Vaskof35ea502016-02-24 10:44:54 +0100220 int i;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100221 json_object *json_obj = NULL;
222 json_object *js_tmp = NULL;
223 char *old_sid = NULL;
224 const char *j_old_sid = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +0100225 char str_port[6];
226 const char **cpblts;
227 struct lyd_node *yanglib, *module, *node;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200228
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100229 if (s == NULL) {
230 ERROR("No session given.");
231 return;
232 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200233
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100234 pthread_mutex_lock(&json_lock);
235 if (s->hello_message != NULL) {
236 ERROR("clean previous hello message");
237 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
238 j_old_sid = json_object_get_string(js_tmp);
239 if (j_old_sid != NULL) {
240 old_sid = strdup(j_old_sid);
241 }
242 }
243 json_object_put(s->hello_message);
244 s->hello_message = NULL;
245 }
246 s->hello_message = json_object_new_object();
247 if (session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100248 if (!old_sid) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100249 /* we don't have old sid */
Michal Vaskof35ea502016-02-24 10:44:54 +0100250 asprintf(&old_sid, "%u", nc_session_get_id(session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100251 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100252 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
253 free(old_sid);
254 old_sid = NULL;
255
256 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) ? "1.1":"1.0")));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100257 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
Michal Vaskof35ea502016-02-24 10:44:54 +0100258 sprintf(str_port, "%u", nc_session_get_port(session));
259 json_object_object_add(s->hello_message, "port", json_object_new_string(str_port));
260 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_username(session)));
261 cpblts = nc_session_get_cpblts(session);
262 if (cpblts) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100263 json_obj = json_object_new_array();
Michal Vaskof35ea502016-02-24 10:44:54 +0100264 for (i = 0; cpblts[i]; ++i) {
265 json_object_array_add(json_obj, json_object_new_string(cpblts[i]));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100266 }
267 json_object_object_add(s->hello_message, "capabilities", json_obj);
268 }
Michal Vaskof35ea502016-02-24 10:44:54 +0100269
270 yanglib = ly_ctx_info(nc_session_get_ctx(session));
271 if (yanglib) {
272 json_obj = json_object_new_array();
273 LY_TREE_FOR(yanglib->child, module) {
274 if (!strcmp(module->schema->name, "module")) {
275 LY_TREE_FOR(module->child, node) {
276 if (!strcmp(node->schema->name, "name")) {
277 json_object_array_add(json_obj, json_object_new_string(((struct lyd_node_leaf_list *)node)->value_str));
278 break;
279 }
280 }
281 }
282 }
283 json_object_object_add(s->hello_message, "models", json_obj);
284
Michal Vaskoc04900a2016-03-15 16:20:24 +0100285 lyd_free(yanglib);
Michal Vaskof35ea502016-02-24 10:44:54 +0100286 }
287
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100288 DEBUG("%s", json_object_to_json_string(s->hello_message));
289 } else {
290 ERROR("Session was not given.");
291 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
292 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
293 }
294 DEBUG("Status info from hello message prepared");
295 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200296}
297
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100298void
299create_err_reply_p()
Tomas Cejka442258e2014-04-01 18:17:18 +0200300{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100301 json_object **err_reply = calloc(1, sizeof(json_object **));
302 if (err_reply == NULL) {
303 ERROR("Allocation of err_reply storage failed!");
304 return;
305 }
306 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
307 ERROR("cannot set thread-specific value.");
308 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200309}
310
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100311void
312clean_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200313{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100314 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
315 if (err_reply != NULL) {
316 if (*err_reply != NULL) {
317 pthread_mutex_lock(&json_lock);
318 json_object_put(*err_reply);
319 pthread_mutex_unlock(&json_lock);
320 }
321 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
322 ERROR("Cannot set thread-specific hash value.");
323 }
324 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200325}
326
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100327void
328free_err_reply()
Tomas Cejka442258e2014-04-01 18:17:18 +0200329{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100330 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
331 if (err_reply != NULL) {
332 if (*err_reply != NULL) {
333 pthread_mutex_lock(&json_lock);
334 json_object_put(*err_reply);
335 pthread_mutex_unlock(&json_lock);
336 }
337 free(err_reply);
338 err_reply = NULL;
339 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
340 ERROR("Cannot set thread-specific hash value.");
341 }
342 }
343}
344
345static struct session_with_mutex *
346session_get_locked(unsigned int session_key, json_object **err)
347{
348 struct session_with_mutex *locked_session;
349
350 /* get non-exclusive (read) access to sessions_list (conns) */
351 DEBUG("LOCK wrlock %s", __func__);
352 if (pthread_rwlock_rdlock(&session_lock) != 0) {
353 if (*err) {
354 *err = create_error_reply("Locking failed.");
355 }
356 return NULL;
357 }
358 /* get session where send the RPC */
359 for (locked_session = netconf_sessions_list;
360 locked_session && (locked_session->session_key != session_key);
361 locked_session = locked_session->next);
362 if (!locked_session) {
363 if (*err) {
364 *err = create_error_reply("Session not found.");
365 }
366 return NULL;
367 }
368
369 /* get exclusive access to session */
370 DEBUG("LOCK mutex %s", __func__);
371 if (pthread_mutex_lock(&locked_session->lock) != 0) {
372 if (*err) {
373 *err = create_error_reply("Locking failed.");
374 }
375 goto wrlock_fail;
376 }
377 return locked_session;
378
379wrlock_fail:
380 DEBUG("UNLOCK wrlock %s", __func__);
381 pthread_rwlock_unlock(&session_lock);
382 return NULL;
383}
384
385static void
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100386session_user_activity(const char *username)
387{
388 struct session_with_mutex *sess;
389
390 for (sess = netconf_sessions_list; sess; sess = sess->next) {
Michal Vaskof35ea502016-02-24 10:44:54 +0100391 if (!strcmp(nc_session_get_username(sess->session), username)) {
Michal Vaskoe32bcba2015-11-24 09:05:51 +0100392 sess->last_activity = time(NULL);
393 }
394 }
395}
396
397static void
Michal Vaskoda0ab5c2015-11-13 13:24:51 +0100398session_unlock(struct session_with_mutex *locked_session)
399{
400 DEBUG("UNLOCK mutex %s", __func__);
401 pthread_mutex_unlock(&locked_session->lock);
402 DEBUG("UNLOCK wrlock %s", __func__);
403 pthread_rwlock_unlock(&session_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +0200404}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200405
Michal Vasko3fda9a92015-11-23 10:10:57 +0100406static void
407node_metadata_text(const char *text, const char *name, json_object *parent)
408{
409 json_object *obj;
410
411 if (!text) {
412 return;
413 }
414
415 obj = json_object_new_string(text);
416 json_object_object_add(parent, name, obj);
417}
418
419static void
420node_metadata_restr(struct lys_restr *restr, const char *name, json_object *parent)
421{
422 json_object *obj;
423
424 if (!restr) {
425 return;
426 }
427
428 obj = json_object_new_string(restr->expr);
429 json_object_object_add(parent, name, obj);
430}
431
432static void
433node_metadata_must(uint8_t must_size, struct lys_restr *must, json_object *parent)
434{
435 uint8_t i;
436 json_object *array, *obj;
437
438 if (!must_size || !must) {
439 return;
440 }
441
442 array = json_object_new_array();
443
444 for (i = 0; i < must_size; ++i) {
445 obj = json_object_new_string(must[i].expr);
446 json_object_array_add(array, obj);
447 }
448
449 json_object_object_add(parent, "must", array);
450}
451
452static void
453node_metadata_basic(struct lys_node *node, json_object *parent)
454{
455 json_object *obj;
456
457 /* description */
458 node_metadata_text(node->dsc, "description", parent);
459
460 /* reference */
461 node_metadata_text(node->ref, "reference", parent);
462
463 /* config */
464 if (node->flags & LYS_CONFIG_R) {
465 obj = json_object_new_boolean(0);
466 } else {
467 obj = json_object_new_boolean(1);
468 }
469 json_object_object_add(parent, "config", obj);
470
471 /* status */
472 if (node->flags & LYS_STATUS_DEPRC) {
473 obj = json_object_new_string("deprecated");
474 } else if (node->flags & LYS_STATUS_OBSLT) {
475 obj = json_object_new_string("obsolete");
476 } else {
477 obj = json_object_new_string("current");
478 }
479 json_object_object_add(parent, "status", obj);
480
481 /* mandatory */
482 if (node->flags & LYS_MAND_TRUE) {
483 obj = json_object_new_boolean(1);
484 } else {
485 obj = json_object_new_boolean(0);
486 }
487 json_object_object_add(parent, "mandatory", obj);
488
489 /* NACM extensions */
490 if (node->nacm) {
491 if (node->nacm & LYS_NACM_DENYW) {
492 obj = json_object_new_string("default-deny-write");
493 } else {
494 obj = json_object_new_string("default-deny-all");
495 }
496 json_object_object_add(parent, "ext", obj);
497 }
498}
499
500static void
501node_metadata_when(struct lys_when *when, json_object *parent)
502{
503 json_object *obj;
504
505 if (!when) {
506 return;
507 }
508
509 obj = json_object_new_string(when->cond);
510 json_object_object_add(parent, "when", obj);
511}
512
513static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100514node_metadata_children_recursive(struct lys_node *node, json_object **child_array, json_object **choice_array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100515{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100516 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100517 struct lys_node *child;
518
519 if (!node->child) {
520 return;
521 }
522
523 LY_TREE_FOR(node->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100524 if (child->nodetype == LYS_USES) {
525 node_metadata_children_recursive(child, child_array, choice_array);
526 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100527 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100528 if (!*child_array) {
529 *child_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100530 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100531 json_object_array_add(*child_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100532 } else if (child->nodetype == LYS_CHOICE) {
533 obj = json_object_new_string(child->name);
Michal Vaskoa45770b2015-11-23 15:49:41 +0100534 if (!*choice_array) {
535 *choice_array = json_object_new_array();
Michal Vasko3fda9a92015-11-23 10:10:57 +0100536 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100537 json_object_array_add(*choice_array, obj);
Michal Vasko3fda9a92015-11-23 10:10:57 +0100538 }
539 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100540}
541
542static void
Michal Vaskoa45770b2015-11-23 15:49:41 +0100543node_metadata_cases_recursive(struct lys_node_choice *choice, json_object *array)
Michal Vasko3fda9a92015-11-23 10:10:57 +0100544{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100545 json_object *obj;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100546 struct lys_node *child;
547
548 if (!choice->child) {
549 return;
550 }
551
Michal Vasko3fda9a92015-11-23 10:10:57 +0100552 LY_TREE_FOR(choice->child, child) {
Michal Vaskoa45770b2015-11-23 15:49:41 +0100553 if (child->nodetype == LYS_USES) {
554 node_metadata_cases_recursive((struct lys_node_choice *)child, array);
555 } else if (child->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE)) {
Michal Vasko3fda9a92015-11-23 10:10:57 +0100556 obj = json_object_new_string(child->name);
557 json_object_array_add(array, obj);
558 }
559 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100560}
561
562static void
563node_metadata_min_max(uint32_t min, uint32_t max, json_object *parent)
564{
565 json_object *obj;
566
567 if (min) {
568 obj = json_object_new_int(min);
569 json_object_object_add(parent, "min-elements", obj);
570 }
571
572 if (max) {
573 obj = json_object_new_int(max);
574 json_object_object_add(parent, "max-elements", obj);
575 }
576}
577
578static void
579node_metadata_ident_recursive(struct lys_ident *ident, json_object *array)
580{
581 struct lys_ident_der *cur;
582 json_object *obj;
583
584 if (!ident) {
585 return;
586 }
587
588 obj = json_object_new_string(ident->name);
589 json_object_array_add(array, obj);
590
591 for (cur = ident->der; cur; cur = cur->next) {
592 node_metadata_ident_recursive(cur->ident, array);
593 }
594}
595
596static void
597node_metadata_type(struct lys_type *type, struct lys_module *module, json_object *parent)
598{
599 json_object *obj, *array, *item;
600 char *str;
601 int i;
602
603 /* built-in YANG type */
604 if (!type->der->module) {
605 switch (type->base) {
606 case LY_TYPE_BINARY:
607 node_metadata_text("binary", "type", parent);
608 node_metadata_restr(type->info.binary.length, "length", parent);
609 break;
610 case LY_TYPE_BITS:
611 node_metadata_text("bits", "type", parent);
612
613 array = json_object_new_array();
614 for (i = 0; i < type->info.bits.count; ++i) {
615 item = json_object_new_object();
616 obj = json_object_new_string(type->info.bits.bit[i].name);
617 json_object_object_add(item, "name", obj);
618 obj = json_object_new_int(type->info.bits.bit[i].pos);
619 json_object_object_add(item, "position", obj);
620 json_object_array_add(array, item);
621 }
622 json_object_object_add(parent, "bits", array);
623 break;
624 case LY_TYPE_BOOL:
625 node_metadata_text("bool", "type", parent);
626 break;
627 case LY_TYPE_DEC64:
628 node_metadata_text("decimal64", "type", parent);
629 node_metadata_restr(type->info.dec64.range, "range", parent);
630 obj = json_object_new_int(type->info.dec64.dig);
631 json_object_object_add(parent, "fraction-digits", obj);
632 break;
633 case LY_TYPE_EMPTY:
634 node_metadata_text("empty", "type", parent);
635 break;
636 case LY_TYPE_ENUM:
637 node_metadata_text("enumeration", "type", parent);
638
639 array = json_object_new_array();
640 for (i = 0; i < type->info.enums.count; ++i) {
641 obj = json_object_new_string(type->info.enums.enm[i].name);
642 json_object_array_add(array, obj);
643 }
644 json_object_object_add(parent, "enumval", array);
645 break;
646 case LY_TYPE_IDENT:
647 node_metadata_text("identityref", "type", parent);
648
649 array = json_object_new_array();
650 node_metadata_ident_recursive(type->info.ident.ref, array);
651 json_object_object_add(parent, "identityval", array);
652 break;
653 case LY_TYPE_INST:
654 node_metadata_text("instance-identifier", "type", parent);
655 if (type->info.inst.req == -1) {
656 obj = json_object_new_boolean(0);
657 } else {
658 obj = json_object_new_boolean(1);
659 }
660 json_object_object_add(parent, "require-instance", obj);
661 break;
662 case LY_TYPE_LEAFREF:
663 node_metadata_text("leafref", "type", parent);
664 node_metadata_text(type->info.lref.path, "path", parent);
665 break;
666 case LY_TYPE_STRING:
667 node_metadata_text("string", "type", parent);
668 node_metadata_restr(type->info.str.length, "length", parent);
669 if (type->info.str.pat_count) {
670 array = json_object_new_array();
671 for (i = 0; i < type->info.str.pat_count; ++i) {
672 obj = json_object_new_string(type->info.str.patterns[i].expr);
673 json_object_array_add(array, obj);
674 }
675 json_object_object_add(parent, "pattern", array);
676 }
677 break;
678 case LY_TYPE_UNION:
679 node_metadata_text("union", "type", parent);
680 array = json_object_new_array();
681 for (i = 0; i < type->info.uni.count; ++i) {
682 obj = json_object_new_object();
683 node_metadata_type(&type->info.uni.types[i], module, obj);
684 json_object_array_add(array, obj);
685 }
686 json_object_object_add(parent, "types", array);
687 break;
688 case LY_TYPE_INT8:
689 node_metadata_text("int8", "type", parent);
690 node_metadata_restr(type->info.num.range, "range", parent);
691 break;
692 case LY_TYPE_UINT8:
693 node_metadata_text("uint8", "type", parent);
694 node_metadata_restr(type->info.num.range, "range", parent);
695 break;
696 case LY_TYPE_INT16:
697 node_metadata_text("int16", "type", parent);
698 node_metadata_restr(type->info.num.range, "range", parent);
699 break;
700 case LY_TYPE_UINT16:
701 node_metadata_text("uint16", "type", parent);
702 node_metadata_restr(type->info.num.range, "range", parent);
703 break;
704 case LY_TYPE_INT32:
705 node_metadata_text("int32", "type", parent);
706 node_metadata_restr(type->info.num.range, "range", parent);
707 break;
708 case LY_TYPE_UINT32:
709 node_metadata_text("uint32", "type", parent);
710 node_metadata_restr(type->info.num.range, "range", parent);
711 break;
712 case LY_TYPE_INT64:
713 node_metadata_text("int64", "type", parent);
714 node_metadata_restr(type->info.num.range, "range", parent);
715 break;
716 case LY_TYPE_UINT64:
717 node_metadata_text("uint64", "type", parent);
718 node_metadata_restr(type->info.num.range, "range", parent);
719 break;
720 default:
721 ERROR("Internal: unknown type (%s:%d)", __FILE__, __LINE__);
722 break;
723 }
724
725 /* typedef */
726 } else {
727 if (!module || !type->module_name || !strcmp(type->module_name, module->name)) {
728 node_metadata_text(type->der->name, "type", parent);
729 } else {
730 asprintf(&str, "%s:%s", type->module_name, type->der->name);
731 node_metadata_text(str, "type", parent);
732 free(str);
733 }
734 obj = json_object_new_object();
735 node_metadata_typedef(type->der, obj);
736 json_object_object_add(parent, "typedef", obj);
737 }
738}
739
740static void
741node_metadata_typedef(struct lys_tpdf *tpdf, json_object *parent)
742{
743 json_object *obj;
744
745 /* description */
746 node_metadata_text(tpdf->dsc, "description", parent);
747
748 /* reference */
749 node_metadata_text(tpdf->ref, "reference", parent);
750
751 /* status */
752 if (tpdf->flags & LYS_STATUS_DEPRC) {
753 obj = json_object_new_string("deprecated");
754 } else if (tpdf->flags & LYS_STATUS_OBSLT) {
755 obj = json_object_new_string("obsolete");
756 } else {
757 obj = json_object_new_string("current");
758 }
759 json_object_object_add(parent, "status", obj);
760
761 /* type */
762 node_metadata_type(&tpdf->type, tpdf->module, parent);
763
764 /* units */
765 node_metadata_text(tpdf->units, "units", parent);
766
767 /* default */
768 node_metadata_text(tpdf->dflt, "default", parent);
769}
770
771static void
772node_metadata_container(struct lys_node_container *cont, json_object *parent)
773{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100774 json_object *obj, *child_array = NULL, *choice_array = NULL;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100775
776 /* element type */
777 obj = json_object_new_string("container");
778 json_object_object_add(parent, "eltype", obj);
779
780 /* shared info */
781 node_metadata_basic((struct lys_node *)cont, parent);
782
783 /* must */
784 node_metadata_must(cont->must_size, cont->must, parent);
785
786 /* presence */
787 node_metadata_text(cont->presence, "presence", parent);
788
789 /* when */
790 node_metadata_when(cont->when, parent);
791
792 /* children & choice */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100793 node_metadata_children_recursive((struct lys_node *)cont, &child_array, &choice_array);
794 if (child_array) {
795 json_object_object_add(parent, "children", child_array);
796 }
797 if (choice_array) {
798 json_object_object_add(parent, "choice", choice_array);
799 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100800}
801
802static void
803node_metadata_choice(struct lys_node_choice *choice, json_object *parent)
804{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100805 json_object *obj, *array;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100806
807 /* element type */
808 obj = json_object_new_string("choice");
809 json_object_object_add(parent, "eltype", obj);
810
811 /* shared info */
812 node_metadata_basic((struct lys_node *)choice, parent);
813
814 /* default */
Michal Vasko5e1c6052016-03-17 15:43:33 +0100815 if (choice->dflt) {
816 node_metadata_text(choice->dflt->name, "default", parent);
817 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100818
819 /* when */
820 node_metadata_when(choice->when, parent);
821
822 /* cases */
Michal Vaskoa45770b2015-11-23 15:49:41 +0100823 if (choice->child) {
824 array = json_object_new_array();
825 node_metadata_cases_recursive(choice, array);
826 json_object_object_add(parent, "cases", array);
827 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100828}
829
830static void
831node_metadata_leaf(struct lys_node_leaf *leaf, json_object *parent)
832{
833 json_object *obj;
834 struct lys_node_list *list;
835 int is_key, i;
836
837 /* element type */
838 obj = json_object_new_string("leaf");
839 json_object_object_add(parent, "eltype", obj);
840
841 /* shared info */
842 node_metadata_basic((struct lys_node *)leaf, parent);
843
844 /* type */
845 node_metadata_type(&leaf->type, leaf->module, parent);
846
847 /* units */
848 node_metadata_text(leaf->units, "units", parent);
849
850 /* default */
851 node_metadata_text(leaf->dflt, "default", parent);
852
853 /* must */
854 node_metadata_must(leaf->must_size, leaf->must, parent);
855
856 /* when */
857 node_metadata_when(leaf->when, parent);
858
859 /* iskey */
860 is_key = 0;
861 list = (struct lys_node_list *)lys_parent((struct lys_node *)leaf);
862 if (list && (list->nodetype == LYS_LIST)) {
863 for (i = 0; i < list->keys_size; ++i) {
864 if (list->keys[i] == leaf) {
865 is_key = 1;
866 break;
867 }
868 }
869 }
870 obj = json_object_new_boolean(is_key);
871 json_object_object_add(parent, "iskey", obj);
872}
873
874static void
875node_metadata_leaflist(struct lys_node_leaflist *llist, json_object *parent)
876{
877 json_object *obj;
878
879 /* element type */
880 obj = json_object_new_string("leaf-list");
881 json_object_object_add(parent, "eltype", obj);
882
883 /* shared info */
884 node_metadata_basic((struct lys_node *)llist, parent);
885
886 /* type */
887 node_metadata_type(&llist->type, llist->module, parent);
888
889 /* units */
890 node_metadata_text(llist->units, "units", parent);
891
892 /* must */
893 node_metadata_must(llist->must_size, llist->must, parent);
894
895 /* when */
896 node_metadata_when(llist->when, parent);
897
898 /* min/max-elements */
899 node_metadata_min_max(llist->min, llist->max, parent);
900}
901
902static void
903node_metadata_list(struct lys_node_list *list, json_object *parent)
904{
Michal Vaskoa45770b2015-11-23 15:49:41 +0100905 json_object *obj, *array, *child_array = NULL, *choice_array = NULL;;
Michal Vasko3fda9a92015-11-23 10:10:57 +0100906 int i;
907 unsigned int j;
908
909 /* element type */
910 obj = json_object_new_string("list");
911 json_object_object_add(parent, "eltype", obj);
912
913 /* shared info */
914 node_metadata_basic((struct lys_node *)list, parent);
915
916 /* must */
917 node_metadata_must(list->must_size, list->must, parent);
918
919 /* when */
920 node_metadata_when(list->when, parent);
921
922 /* min/max-elements */
923 node_metadata_min_max(list->min, list->max, parent);
924
925 /* keys */
926 if (list->keys_size) {
927 array = json_object_new_array();
928 for (i = 0; i < list->keys_size; ++i) {
929 obj = json_object_new_string(list->keys[i]->name);
930 json_object_array_add(array, obj);
931 }
932 json_object_object_add(parent, "keys", array);
933 }
934
935 /* unique */
936 if (list->unique_size) {
937 array = json_object_new_array();
938 for (i = 0; i < list->unique_size; ++i) {
939 for (j = 0; j < list->unique[i].expr_size; ++j) {
940 obj = json_object_new_string(list->unique[i].expr[j]);
941 json_object_array_add(array, obj);
942 }
943 }
944 json_object_object_add(parent, "unique", array);
945 }
Michal Vaskoa45770b2015-11-23 15:49:41 +0100946
947 /* children & choice */
948 node_metadata_children_recursive((struct lys_node *)list, &child_array, &choice_array);
949 if (child_array) {
950 json_object_object_add(parent, "children", child_array);
951 }
952 if (choice_array) {
953 json_object_object_add(parent, "choice", choice_array);
954 }
Michal Vasko3fda9a92015-11-23 10:10:57 +0100955}
956
957static void
958node_metadata_anyxml(struct lys_node_anyxml *anyxml, json_object *parent)
959{
960 json_object *obj;
961
962 /* element type */
963 obj = json_object_new_string("anyxml");
964 json_object_object_add(parent, "eltype", obj);
965
966 /* shared info */
967 node_metadata_basic((struct lys_node *)anyxml, parent);
968
969 /* must */
970 node_metadata_must(anyxml->must_size, anyxml->must, parent);
971
972 /* when */
973 node_metadata_when(anyxml->when, parent);
974
975}
976
977static void
978node_metadata_case(struct lys_node_case *cas, json_object *parent)
979{
980 json_object *obj;
981
982 /* element type */
983 obj = json_object_new_string("case");
984 json_object_object_add(parent, "eltype", obj);
985
986 /* shared info */
987 node_metadata_basic((struct lys_node *)cas, parent);
988
989 /* when */
990 node_metadata_when(cas->when, parent);
991}
992
Michal Vaskoa45770b2015-11-23 15:49:41 +0100993static void
994node_metadata_rpc(struct lys_node_rpc *rpc, json_object *parent)
995{
996 json_object *obj;
997
998 /* element type */
999 obj = json_object_new_string("rpc");
1000 json_object_object_add(parent, "eltype", obj);
1001
1002 /* description */
1003 node_metadata_text(rpc->dsc, "description", parent);
1004
1005 /* reference */
1006 node_metadata_text(rpc->ref, "reference", parent);
1007
1008 /* status */
1009 if (rpc->flags & LYS_STATUS_DEPRC) {
1010 obj = json_object_new_string("deprecated");
1011 } else if (rpc->flags & LYS_STATUS_OBSLT) {
1012 obj = json_object_new_string("obsolete");
1013 } else {
1014 obj = json_object_new_string("current");
1015 }
1016 json_object_object_add(parent, "status", obj);
1017}
1018
1019static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001020node_metadata_model(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001021{
1022 json_object *obj, *array, *item;
Michal Vaskoa4b8cdb2016-03-17 15:44:45 +01001023 const struct lys_node *node;
Michal Vaskoa45770b2015-11-23 15:49:41 +01001024 int i;
1025
1026 /* yang-version */
1027 if (module->version == 2) {
1028 obj = json_object_new_string("1.1");
1029 } else {
1030 obj = json_object_new_string("1.0");
1031 }
1032 json_object_object_add(parent, "yang-version", obj);
1033
1034 /* namespace */
1035 node_metadata_text(module->ns, "namespace", parent);
1036
1037 /* prefix */
1038 node_metadata_text(module->prefix, "prefix", parent);
1039
1040 /* contact */
1041 node_metadata_text(module->contact, "contact", parent);
1042
1043 /* organization */
1044 node_metadata_text(module->org, "organization", parent);
1045
1046 /* revision */
1047 if (module->rev_size) {
1048 node_metadata_text(module->rev[0].date, "revision", parent);
1049 }
1050
1051 /* description */
1052 node_metadata_text(module->dsc, "description", parent);
1053
1054 /* import */
1055 if (module->imp_size) {
1056 array = json_object_new_array();
1057 for (i = 0; i < module->imp_size; ++i) {
1058 item = json_object_new_object();
1059
1060 node_metadata_text(module->imp[i].module->name, "name", item);
1061 node_metadata_text(module->imp[i].prefix, "prefix", item);
1062 if (module->imp[i].rev && module->imp[i].rev[0]) {
1063 node_metadata_text(module->imp[i].rev, "revision", item);
1064 }
1065
1066 json_object_array_add(array, item);
1067 }
1068 json_object_object_add(parent, "imports", array);
1069 }
1070
1071 /* include */
1072 if (module->inc_size) {
1073 array = json_object_new_array();
1074 for (i = 0; i < module->inc_size; ++i) {
1075 item = json_object_new_object();
1076
1077 node_metadata_text(module->inc[i].submodule->name, "name", item);
1078 if (module->inc[i].rev && module->inc[i].rev[0]) {
1079 node_metadata_text(module->inc[i].rev, "revision", item);
1080 }
1081
1082 json_object_array_add(array, item);
1083 }
1084 json_object_object_add(parent, "includes", array);
1085 }
Michal Vaskoa4b8cdb2016-03-17 15:44:45 +01001086
1087 /* top-nodes */
1088 node = NULL;
1089 array = NULL;
1090 while ((node = lys_getnext(node, NULL, module, LYS_GETNEXT_WITHCHOICE))) {
1091 if (node->nodetype & (LYS_RPC | LYS_NOTIF)) {
1092 continue;
1093 }
1094 if (!array) {
1095 array = json_object_new_array();
1096 }
1097 item = json_object_new_string(node->name);
1098 json_object_array_add(array, item);
1099 }
1100 if (array) {
1101 json_object_object_add(parent, "top-nodes", array);
1102 }
Michal Vaskoa45770b2015-11-23 15:49:41 +01001103}
1104
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001105/**
1106 * \defgroup netconf_operations NETCONF operations
1107 * The list of NETCONF operations that mod_netconf supports.
1108 * @{
1109 */
1110
1111/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001112 * \brief Send RPC and wait for reply with timeout.
1113 *
1114 * \param[in] session libnetconf session
1115 * \param[in] rpc prepared RPC message
1116 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
1117 * \param[out] reply reply from the server
Michal Vaskof35ea502016-02-24 10:44:54 +01001118 * \return NC_MSG_WOULDBLOCK or NC_MSG_ERROR.
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001119 * On success, it returns NC_MSG_REPLY.
1120 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001121NC_MSG_TYPE
Michal Vaskof35ea502016-02-24 10:44:54 +01001122netconf_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 +02001123{
Michal Vaskof35ea502016-02-24 10:44:54 +01001124 uint64_t msgid;
1125 NC_MSG_TYPE ret;
1126 ret = nc_send_rpc(session, rpc, timeout, &msgid);
1127 if (ret != NC_MSG_RPC) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001128 return ret;
1129 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001130
1131 while ((ret = nc_recv_reply(session, rpc, msgid, timeout, (strict ? LYD_OPT_STRICT : 0), reply)) == NC_MSG_NOTIF);
1132
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001133 return ret;
1134}
1135
Tomas Cejka8ce138c2015-04-27 23:25:25 +02001136/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +02001137 * \brief Connect to NETCONF server
1138 *
1139 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
1140 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001141static unsigned int
Michal Vaskof35ea502016-02-24 10:44:54 +01001142netconf_connect(const char *host, const char *port, const char *user, const char *pass, const char *privkey)
Radek Krejci469aab82012-07-22 18:42:20 +02001143{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001144 struct nc_session* session = NULL;
1145 struct session_with_mutex *locked_session, *last_session;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001146 char *pubkey;
Radek Krejci469aab82012-07-22 18:42:20 +02001147
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001148 /* connect to the requested NETCONF server */
1149 password = (char*)pass;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001150 if (privkey) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001151 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 3);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001152 asprintf(&pubkey, "%s.pub", privkey);
Michal Vaskof35ea502016-02-24 10:44:54 +01001153 nc_client_ssh_add_keypair(pubkey, privkey);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01001154 free(pubkey);
1155 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001156 nc_client_ssh_set_username(user);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001157 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Michal Vaskof35ea502016-02-24 10:44:54 +01001158 session = nc_connect_ssh(host, (unsigned short)atoi(port), NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001159 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +02001160
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001161 /* if connected successful, add session to the list */
1162 if (session != NULL) {
1163 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 +01001164 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001165 session = NULL;
1166 free(locked_session);
1167 locked_session = NULL;
1168 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
1169 return 0;
1170 }
1171 locked_session->session = session;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001172 locked_session->hello_message = NULL;
1173 locked_session->closed = 0;
1174 pthread_mutex_init(&locked_session->lock, NULL);
1175 DEBUG("Before session_lock");
1176 /* get exclusive access to sessions_list (conns) */
1177 DEBUG("LOCK wrlock %s", __func__);
1178 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001179 nc_session_free(session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001180 free(locked_session);
1181 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
1182 return 0;
1183 }
1184 locked_session->ntfc_subscribed = 0;
1185 DEBUG("Add connection to the list");
1186 if (!netconf_sessions_list) {
Michal Vaskoc3146782015-11-04 14:46:41 +01001187 netconf_sessions_list = locked_session;
1188 } else {
1189 for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next);
1190 last_session->next = locked_session;
1191 locked_session->prev = last_session;
1192 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001193 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001194
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001195 /* no need to lock session, noone can read it while we have wrlock */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001196
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001197 /* store information about session from hello message for future usage */
1198 prepare_status_message(locked_session, session);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001199
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001200 DEBUG("NETCONF session established");
1201 locked_session->session_key = session_key_generator;
1202 ++session_key_generator;
1203 if (session_key_generator == UINT_MAX) {
1204 session_key_generator = 1;
1205 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001206
Michal Vaskoe32bcba2015-11-24 09:05:51 +01001207 DEBUG("Before session_unlock");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001208 /* unlock session list */
1209 DEBUG("UNLOCK wrlock %s", __func__);
1210 if (pthread_rwlock_unlock(&session_lock) != 0) {
1211 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1212 }
Radek Krejci469aab82012-07-22 18:42:20 +02001213
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001214 return locked_session->session_key;
1215 }
1216
1217 ERROR("Connection could not be established");
1218 return 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001219}
1220
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001221static int
1222close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +02001223{
Michal Vaskoa53ef882015-11-24 11:02:01 +01001224 int i;
1225
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001226 DEBUG("lock private lock.");
1227 DEBUG("LOCK mutex %s", __func__);
1228 if (pthread_mutex_lock(&locked_session->lock) != 0) {
1229 ERROR("Error while locking rwlock");
1230 }
1231 locked_session->ntfc_subscribed = 0;
1232 locked_session->closed = 1;
1233 if (locked_session->session != NULL) {
Michal Vaskoa9590052016-03-08 10:12:24 +01001234 nc_session_free(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001235 locked_session->session = NULL;
1236 }
1237 DEBUG("session closed.");
1238 DEBUG("unlock private lock.");
1239 DEBUG("UNLOCK mutex %s", __func__);
1240 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
1241 ERROR("Error while locking rwlock");
1242 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001243
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001244 DEBUG("unlock session lock.");
1245 DEBUG("closed session, disabled notif(?), wait 0.5s");
1246 usleep(500000); /* let notification thread stop */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001247
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001248 /* session shouldn't be used by now */
Michal Vaskoa53ef882015-11-24 11:02:01 +01001249 for (i = 0; i < locked_session->notif_count; ++i) {
1250 free(locked_session->notifications[i].content);
1251 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001252 free(locked_session->notifications);
1253 pthread_mutex_destroy(&locked_session->lock);
1254 if (locked_session->hello_message != NULL) {
1255 json_object_put(locked_session->hello_message);
1256 locked_session->hello_message = NULL;
1257 }
1258 locked_session->session = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001259 free(locked_session);
1260 locked_session = NULL;
1261 DEBUG("NETCONF session closed, everything cleared.");
1262 return (EXIT_SUCCESS);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001263}
1264
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001265static int
1266netconf_close(unsigned int session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +02001267{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001268 struct session_with_mutex *locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +02001269
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001270 DEBUG("Session to close: %u", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001271
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001272 /* get exclusive (write) access to sessions_list (conns) */
1273 DEBUG("lock session lock.");
1274 DEBUG("LOCK wrlock %s", __func__);
1275 if (pthread_rwlock_wrlock (&session_lock) != 0) {
1276 ERROR("Error while locking rwlock");
1277 (*reply) = create_error_reply("Internal: Error while locking.");
1278 return EXIT_FAILURE;
1279 }
1280 /* remove session from the active sessions list -> nobody new can now work with session */
1281 for (locked_session = netconf_sessions_list;
1282 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01001283 locked_session = locked_session->next);
1284
1285 if (!locked_session) {
Michal Vasko642cad02016-03-17 12:31:18 +01001286 pthread_rwlock_unlock(&session_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001287 ERROR("Could not find the session %u to close.", session_key);
1288 (*reply) = create_error_reply("Internal: Error while finding a session.");
Michal Vaskoc3146782015-11-04 14:46:41 +01001289 return EXIT_FAILURE;
1290 }
1291
1292 if (!locked_session->prev) {
1293 netconf_sessions_list = netconf_sessions_list->next;
Michal Vaskof0b6fbb2016-03-08 12:04:34 +01001294 if (netconf_sessions_list) {
1295 netconf_sessions_list->prev = NULL;
1296 }
Michal Vaskoc3146782015-11-04 14:46:41 +01001297 } else {
1298 locked_session->prev->next = locked_session->next;
1299 if (locked_session->next) {
1300 locked_session->next->prev = locked_session->prev;
1301 }
1302 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001303
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001304 DEBUG("UNLOCK wrlock %s", __func__);
1305 if (pthread_rwlock_unlock (&session_lock) != 0) {
1306 ERROR("Error while unlocking rwlock");
1307 (*reply) = create_error_reply("Internal: Error while unlocking.");
1308 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001309
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001310 if ((locked_session != NULL) && (locked_session->session != NULL)) {
1311 return close_and_free_session(locked_session);
1312 } else {
1313 ERROR("Unknown session to close");
1314 (*reply) = create_error_reply("Internal: Unkown session to close.");
1315 return (EXIT_FAILURE);
1316 }
1317 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +02001318}
1319
Tomas Cejkac7929632013-10-24 19:25:15 +02001320/**
1321 * Test reply message type and return error message.
1322 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001323 * \param[in] session nc_session internal struct
Michal Vaskoc3146782015-11-04 14:46:41 +01001324 * \param[in] session_key session ID, 0 to disable disconnect on error
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001325 * \param[in] msgt RPC-REPLY message type
Tomas Cejkac7929632013-10-24 19:25:15 +02001326 * \param[out] data
1327 * \return NULL on success
1328 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001329json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001330netconf_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 +02001331{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001332 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001333
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001334 /* process the result of the operation */
1335 switch (msgt) {
Michal Vaskof35ea502016-02-24 10:44:54 +01001336 case NC_MSG_ERROR:
1337 if (nc_session_get_status(session) != NC_STATUS_RUNNING) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001338 ERROR("mod_netconf: receiving rpc-reply failed");
1339 if (session_key) {
1340 netconf_close(session_key, &err);
1341 }
1342 if (err != NULL) {
1343 return err;
1344 }
1345 return create_error_reply("Internal: Receiving RPC-REPLY failed.");
1346 }
1347 case NC_MSG_NONE:
1348 /* there is error handled by callback */
1349 if (data != NULL) {
1350 free(*data);
1351 (*data) = NULL;
1352 }
1353 return NULL;
1354 case NC_MSG_REPLY:
Michal Vaskof35ea502016-02-24 10:44:54 +01001355 switch (reply->type) {
1356 case NC_RPL_OK:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001357 if ((data != NULL) && (*data != NULL)) {
1358 free(*data);
1359 (*data) = NULL;
1360 }
1361 return create_ok_reply();
Michal Vaskof35ea502016-02-24 10:44:54 +01001362 case NC_RPL_DATA:
1363 if (((*data) = ((struct nc_reply_data *)reply)->data) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001364 ERROR("mod_netconf: no data from reply");
1365 return create_error_reply("Internal: No data from reply received.");
1366 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01001367 ((struct nc_reply_data *)reply)->data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001368 return NULL;
1369 }
1370 break;
Michal Vaskof35ea502016-02-24 10:44:54 +01001371 case NC_RPL_ERROR:
1372 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 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001377 return create_error_reply(((struct nc_reply_error *)reply)->err[0].message);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001378 default:
Michal Vaskof35ea502016-02-24 10:44:54 +01001379 ERROR("mod_netconf: unexpected rpc-reply (%d)", reply->type);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001380 if (data != NULL) {
1381 free(*data);
1382 (*data) = NULL;
1383 }
1384 return create_error_reply("Unknown type of NETCONF reply.");
1385 }
1386 break;
1387 default:
1388 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
1389 if (data != NULL) {
1390 free(*data);
1391 (*data) = NULL;
1392 }
1393 return create_error_reply("Internal: Unexpected RPC-REPLY message type.");
1394 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001395}
1396
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001397json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001398netconf_unlocked_op(struct nc_session *session, struct nc_rpc *rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001399{
Michal Vaskof35ea502016-02-24 10:44:54 +01001400 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001401 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001402
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001403 /* check requests */
1404 if (rpc == NULL) {
1405 ERROR("mod_netconf: rpc is not created");
1406 return create_error_reply("Internal error: RPC is not created");
1407 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001408
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001409 if (session != NULL) {
1410 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001411 msgt = netconf_send_recv_timed(session, rpc, 50000, 0, &reply);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001412 /* process the result of the operation */
1413 return netconf_test_reply(session, 0, msgt, reply, NULL);
1414 } else {
1415 ERROR("Unknown session to process.");
1416 return create_error_reply("Internal error: Unknown session to process.");
1417 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001418}
1419
Tomas Cejkac7929632013-10-24 19:25:15 +02001420/**
1421 * Perform RPC method that returns data.
1422 *
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001423 * \param[in] session_id session identifier
1424 * \param[in] rpc RPC message to perform
1425 * \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 +02001426 * \return NULL on success, json object with error otherwise
1427 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001428static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001429netconf_op(unsigned int session_key, struct nc_rpc *rpc, int strict, struct lyd_node **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +02001430{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001431 struct session_with_mutex * locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001432 struct nc_reply* reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001433 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001434 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001435 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +02001436
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001437 /* check requests */
1438 if (rpc == NULL) {
1439 ERROR("mod_netconf: rpc is not created");
1440 res = create_error_reply("Internal: RPC could not be created.");
1441 data = NULL;
1442 goto finished;
1443 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001444
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001445 locked_session = session_get_locked(session_key, &res);
1446 if (!locked_session) {
1447 ERROR("Unknown session or locking failed.");
1448 goto finished;
1449 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001450
Michal Vaskof35ea502016-02-24 10:44:54 +01001451 session_user_activity(nc_session_get_username(locked_session->session));
Tomas Cejkac7929632013-10-24 19:25:15 +02001452
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001453 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +01001454 msgt = netconf_send_recv_timed(locked_session->session, rpc, 2000000, strict, &reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001455
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001456 session_unlock(locked_session);
Tomas Cejkac7929632013-10-24 19:25:15 +02001457
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001458 res = netconf_test_reply(locked_session->session, session_key, msgt, reply, &data);
Radek Krejcia332b692012-11-12 16:15:54 +01001459
Tomas Cejkac7929632013-10-24 19:25:15 +02001460finished:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001461 nc_reply_free(reply);
1462 if (received_data != NULL) {
1463 (*received_data) = data;
1464 } else {
1465 if (data != NULL) {
1466 free(data);
1467 data = NULL;
1468 }
1469 }
1470 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001471}
1472
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001473static char *
1474netconf_getconfig(unsigned int session_key, NC_DATASTORE source, const char *filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001475{
Michal Vaskof35ea502016-02-24 10:44:54 +01001476 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001477 struct session_with_mutex *locked_session;
Michal Vasko04245ee2016-03-16 09:32:59 +01001478 json_object *res = NULL, *data_cjson;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001479 enum json_tokener_error tok_err;
Michal Vasko04245ee2016-03-16 09:32:59 +01001480 char *data_json = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001481 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001482
Michal Vaskof35ea502016-02-24 10:44:54 +01001483 /* tell server to show all elements even if they have default values */
1484#ifdef HAVE_WITHDEFAULTS_TAGGED
1485 rpc = nc_rpc_getconfig(source, filter, NC_WD_MODE_ALL_TAG, NC_PARAMTYPE_CONST);
1486#else
1487 rpc = nc_rpc_getconfig(source, filter, 0, NC_PARAMTYPE_CONST);
1488#endif
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001489 if (rpc == NULL) {
1490 ERROR("mod_netconf: creating rpc request failed");
1491 return (NULL);
1492 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001493
Michal Vaskof35ea502016-02-24 10:44:54 +01001494 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001495 nc_rpc_free(rpc);
1496 if (res != NULL) {
1497 (*err) = res;
1498 } else {
1499 (*err) = NULL;
1500 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001501
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001502 if (data) {
1503 for (locked_session = netconf_sessions_list;
1504 locked_session && (locked_session->session_key != session_key);
1505 locked_session = locked_session->next);
1506 /* won't fail */
1507
Michal Vaskof35ea502016-02-24 10:44:54 +01001508 /* print data into JSON */
1509 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001510 ERROR("Printing JSON <get-config> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001511 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001512 return NULL;
1513 }
1514
1515 /* parse JSON data into cjson */
1516 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001517 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001518 if (!data_cjson) {
1519 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1520 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001521 lyd_free_withsiblings(data);
1522 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001523 return NULL;
1524 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001525 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001526
1527 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001528 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001529 node_add_metadata_recursive(sibling, NULL, data_cjson);
1530 lyd_free(sibling);
1531 }
1532
Michal Vaskof35ea502016-02-24 10:44:54 +01001533 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001534 json_object_put(data_cjson);
1535 pthread_mutex_unlock(&json_lock);
1536 }
1537
Michal Vaskof35ea502016-02-24 10:44:54 +01001538 return (data_json);
Radek Krejci8e4632a2012-07-26 13:40:34 +02001539}
1540
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001541static char *
1542netconf_getschema(unsigned int session_key, const char *identifier, const char *version, const char *format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001543{
Michal Vaskof35ea502016-02-24 10:44:54 +01001544 struct nc_rpc *rpc;
1545 struct lyd_node *data = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001546 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001547 char *model_data = NULL, *anyxml, *ptr, *ptr2;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001548
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001549 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001550 rpc = nc_rpc_getschema(identifier, version, format, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001551 if (rpc == NULL) {
1552 ERROR("mod_netconf: creating rpc request failed");
1553 return (NULL);
1554 }
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001555
Michal Vaskof35ea502016-02-24 10:44:54 +01001556 res = netconf_op(session_key, rpc, 0, &data);
1557 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001558 if (res != NULL) {
1559 (*err) = res;
1560 } else {
1561 (*err) = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001562
1563 if (data) {
1564 lyxml_print_mem(&anyxml, ((struct lyd_node_anyxml *)data)->value, 0);
1565
1566 /* it's with the data root node, remove it */
1567 if (anyxml) {
1568 ptr = strchr(anyxml, '>');
1569 ++ptr;
1570
1571 ptr2 = strrchr(anyxml, '<');
1572
1573 model_data = strndup(ptr, strlen(ptr) - strlen(ptr2));
1574 free(anyxml);
1575 }
1576 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001577 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001578
Michal Vaskof35ea502016-02-24 10:44:54 +01001579 return (model_data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001580}
1581
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001582static char *
1583netconf_get(unsigned int session_key, const char* filter, int strict, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001584{
Michal Vaskof35ea502016-02-24 10:44:54 +01001585 struct nc_rpc* rpc;
1586 char* data_json = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001587 json_object *res = NULL, *data_cjson;
1588 enum json_tokener_error tok_err;
1589 struct session_with_mutex *locked_session;
Michal Vaskof35ea502016-02-24 10:44:54 +01001590 struct lyd_node *data, *sibling, *next;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001591
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001592 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001593 rpc = nc_rpc_get(filter, 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001594 if (rpc == NULL) {
1595 ERROR("mod_netconf: creating rpc request failed");
1596 return (NULL);
1597 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001598
Michal Vaskof35ea502016-02-24 10:44:54 +01001599 res = netconf_op(session_key, rpc, strict, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001600 nc_rpc_free(rpc);
1601 if (res != NULL) {
1602 (*err) = res;
1603 } else {
1604 (*err) = NULL;
1605 }
Tomas Cejkac7929632013-10-24 19:25:15 +02001606
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001607 if (data) {
1608 for (locked_session = netconf_sessions_list;
1609 locked_session && (locked_session->session_key != session_key);
1610 locked_session = locked_session->next);
1611 /* won't fail */
1612
Michal Vaskof35ea502016-02-24 10:44:54 +01001613 /* print JSON data */
1614 if (lyd_print_mem(&data_json, data, LYD_JSON, LYP_WITHSIBLINGS)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001615 ERROR("Printing JSON <get> data failed.");
Michal Vaskof35ea502016-02-24 10:44:54 +01001616 lyd_free_withsiblings(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001617 return NULL;
1618 }
1619
1620 /* parse JSON data into cjson */
1621 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001622 data_cjson = json_tokener_parse_verbose(data_json, &tok_err);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001623 if (!data_cjson) {
1624 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(tok_err));
1625 pthread_mutex_unlock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01001626 lyd_free_withsiblings(data);
1627 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001628 return NULL;
1629 }
Michal Vaskof35ea502016-02-24 10:44:54 +01001630 free(data_json);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001631
1632 /* go simultaneously through both trees and add metadata */
Michal Vaskof35ea502016-02-24 10:44:54 +01001633 LY_TREE_FOR_SAFE(data, next, sibling) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001634 node_add_metadata_recursive(sibling, NULL, data_cjson);
1635 lyd_free(sibling);
1636 }
1637
Michal Vaskof35ea502016-02-24 10:44:54 +01001638 data_json = strdup(json_object_to_json_string_ext(data_cjson, 0));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001639 json_object_put(data_cjson);
1640 pthread_mutex_unlock(&json_lock);
1641 }
1642
Michal Vaskof35ea502016-02-24 10:44:54 +01001643 return data_json;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001644}
1645
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001646static json_object *
1647netconf_copyconfig(unsigned int session_key, NC_DATASTORE source, NC_DATASTORE target, const char *config,
1648 const char *uri_src, const char *uri_trg)
Radek Krejci8e4632a2012-07-26 13:40:34 +02001649{
Michal Vaskof35ea502016-02-24 10:44:54 +01001650 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001651 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001652
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001653 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001654 rpc = nc_rpc_copy(target, uri_trg, source, (config ? config : uri_src), 0, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001655 if (rpc == NULL) {
1656 ERROR("mod_netconf: creating rpc request failed");
1657 return create_error_reply("Internal: Creating rpc request failed");
1658 }
Radek Krejci8e4632a2012-07-26 13:40:34 +02001659
Michal Vaskof35ea502016-02-24 10:44:54 +01001660 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001661 nc_rpc_free(rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001662
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001663 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +02001664}
Radek Krejci035bf4e2012-07-25 10:59:09 +02001665
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001666static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001667netconf_editconfig(unsigned int session_key, NC_DATASTORE target, NC_RPC_EDIT_DFLTOP defop,
1668 NC_RPC_EDIT_ERROPT erropt, NC_RPC_EDIT_TESTOPT testopt, const char *config_or_url)
Radek Krejci62ab34b2012-07-26 13:42:05 +02001669{
Michal Vaskof35ea502016-02-24 10:44:54 +01001670 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001671 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001672
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001673 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001674 rpc = nc_rpc_edit(target, defop, testopt, erropt, config_or_url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001675 if (rpc == NULL) {
1676 ERROR("mod_netconf: creating rpc request failed");
1677 return create_error_reply("Internal: Creating rpc request failed");
1678 }
Radek Krejci62ab34b2012-07-26 13:42:05 +02001679
Michal Vaskof35ea502016-02-24 10:44:54 +01001680 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001681 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001682
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001683 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +02001684}
1685
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001686static json_object *
1687netconf_killsession(unsigned int session_key, const char *sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001688{
Michal Vaskof35ea502016-02-24 10:44:54 +01001689 struct nc_rpc *rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001690 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001691
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001692 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001693 rpc = nc_rpc_kill(atoi(sid));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001694 if (rpc == NULL) {
1695 ERROR("mod_netconf: creating rpc request failed");
1696 return create_error_reply("Internal: Creating rpc request failed");
1697 }
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001698
Michal Vaskof35ea502016-02-24 10:44:54 +01001699 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001700 nc_rpc_free(rpc);
1701 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +02001702}
1703
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001704static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001705netconf_onlytargetop(unsigned int session_key, NC_DATASTORE target, struct nc_rpc *(*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +02001706{
Michal Vaskof35ea502016-02-24 10:44:54 +01001707 struct nc_rpc* rpc;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001708 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +02001709
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001710 /* create requests */
1711 rpc = op_func(target);
1712 if (rpc == NULL) {
1713 ERROR("mod_netconf: creating rpc request failed");
1714 return create_error_reply("Internal: Creating rpc request failed");
1715 }
Radek Krejci2f318372012-07-26 14:22:35 +02001716
Michal Vaskof35ea502016-02-24 10:44:54 +01001717 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001718 nc_rpc_free (rpc);
1719 return res;
Radek Krejci2f318372012-07-26 14:22:35 +02001720}
1721
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001722static json_object *
1723netconf_deleteconfig(unsigned int session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001724{
Michal Vaskof35ea502016-02-24 10:44:54 +01001725 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001726 json_object *res = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01001727 rpc = nc_rpc_delete(target, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001728 if (rpc == NULL) {
1729 ERROR("mod_netconf: creating rpc request failed");
1730 return create_error_reply("Internal: Creating rpc request failed");
1731 }
Tomas Cejka404d37e2013-04-13 02:31:35 +02001732
Michal Vaskof35ea502016-02-24 10:44:54 +01001733 res = netconf_op(session_key, rpc, 0, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001734 nc_rpc_free (rpc);
1735 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +02001736}
1737
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001738static json_object *
1739netconf_lock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001740{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001741 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001742}
1743
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001744static json_object *
1745netconf_unlock(unsigned int session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +02001746{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001747 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +02001748}
1749
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001750static json_object *
Michal Vaskof35ea502016-02-24 10:44:54 +01001751netconf_generic(unsigned int session_key, const char *xml_content, struct lyd_node **data)
Radek Krejci80c10d92012-07-30 08:38:50 +02001752{
Michal Vaskof35ea502016-02-24 10:44:54 +01001753 struct nc_rpc* rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001754 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +02001755
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001756 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +01001757 rpc = nc_rpc_generic_xml(xml_content, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001758 if (rpc == NULL) {
1759 ERROR("mod_netconf: creating rpc request failed");
1760 return create_error_reply("Internal: Creating rpc request failed");
1761 }
Radek Krejci80c10d92012-07-30 08:38:50 +02001762
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001763 /* get session where send the RPC */
Michal Vaskof35ea502016-02-24 10:44:54 +01001764 res = netconf_op(session_key, rpc, 0, data);
1765 nc_rpc_free(rpc);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001766 return res;
1767}
1768
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001769static int
Michal Vaskof35ea502016-02-24 10:44:54 +01001770node_add_metadata(const struct lys_node *node, const struct lys_module *module, json_object *parent)
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001771{
1772 struct lys_module *cur_module;
1773 json_object *meta_obj;
1774 char *obj_name;
1775
Michal Vaskoa45770b2015-11-23 15:49:41 +01001776 if (node->nodetype == LYS_INPUT) {
1777 /* silently skipped */
1778 return 0;
1779 }
1780
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001781 cur_module = node->module;
1782 if (cur_module->type) {
1783 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1784 }
1785 if (cur_module == module) {
1786 asprintf(&obj_name, "$@%s", node->name);
1787 } else {
1788 asprintf(&obj_name, "$@%s:%s", cur_module->name, node->name);
1789 }
1790
1791 /* in (leaf-)lists the metadata could have already been added */
Michal Vaskoa45770b2015-11-23 15:49:41 +01001792 if ((node->nodetype & (LYS_LEAFLIST | LYS_LIST)) && (json_object_object_get_ex(parent, obj_name, NULL) == TRUE)) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001793 free(obj_name);
1794 return 1;
1795 }
1796
1797 meta_obj = json_object_new_object();
1798
1799 switch (node->nodetype) {
1800 case LYS_CONTAINER:
1801 node_metadata_container((struct lys_node_container *)node, meta_obj);
1802 break;
Michal Vasko3fda9a92015-11-23 10:10:57 +01001803 case LYS_CHOICE:
1804 node_metadata_choice((struct lys_node_choice *)node, meta_obj);
1805 break;
1806 case LYS_LEAF:
1807 node_metadata_leaf((struct lys_node_leaf *)node, meta_obj);
1808 break;
1809 case LYS_LEAFLIST:
1810 node_metadata_leaflist((struct lys_node_leaflist *)node, meta_obj);
1811 break;
1812 case LYS_LIST:
1813 node_metadata_list((struct lys_node_list *)node, meta_obj);
1814 break;
1815 case LYS_ANYXML:
1816 node_metadata_anyxml((struct lys_node_anyxml *)node, meta_obj);
1817 break;
1818 case LYS_CASE:
1819 node_metadata_case((struct lys_node_case *)node, meta_obj);
1820 break;
Michal Vaskoa45770b2015-11-23 15:49:41 +01001821 case LYS_RPC:
1822 node_metadata_rpc((struct lys_node_rpc *)node, meta_obj);
1823 break;
1824 default: /* LYS_OUTPUT */
Michal Vasko3fda9a92015-11-23 10:10:57 +01001825 ERROR("Internal: unuxpected nodetype (%s:%d)", __FILE__, __LINE__);
1826 break;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001827 }
1828
1829 /* just a precaution */
1830 if (json_object_get_type(parent) != json_type_object) {
1831 ERROR("Internal: wrong JSON type (%s:%d)", __FILE__, __LINE__);
1832 free(obj_name);
1833 return 1;
1834 }
1835
1836 json_object_object_add(parent, obj_name, meta_obj);
1837 free(obj_name);
1838 return 0;
1839}
1840
1841static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001842node_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 +01001843{
1844 struct lys_module *cur_module;
1845 struct lys_node *list_schema;
1846 struct lyd_node *child, *list_item;
1847 json_object *child_json, *list_child_json;
1848 char *child_name;
1849 int list_idx;
1850
Michal Vaskoa45770b2015-11-23 15:49:41 +01001851 if (data_tree->schema->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1852 return;
1853 }
1854
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001855 /* add data_tree metadata */
1856 if (node_add_metadata(data_tree->schema, module, data_json_parent)) {
1857 return;
1858 }
1859
1860 /* get data_tree module */
1861 cur_module = data_tree->schema->module;
1862 if (cur_module->type) {
1863 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1864 }
1865
1866 if (!(data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
1867 /* print correct data_tree JSON name */
1868 if (cur_module == module) {
1869 asprintf(&child_name, "%s", data_tree->schema->name);
1870 } else {
1871 asprintf(&child_name, "%s:%s", cur_module->name, data_tree->schema->name);
1872 }
1873
1874 /* go down in JSON object */
1875 if (json_object_object_get_ex(data_json_parent, child_name, &child_json) == FALSE) {
1876 ERROR("Internal: failed to get JSON object \"%s\".", child_name);
1877 free(child_name);
1878 return;
1879 }
1880 free(child_name);
1881
1882 if (data_tree->schema->nodetype == LYS_LIST) {
1883 if (json_object_get_type(child_json) != json_type_array) {
1884 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1885 return;
1886 }
1887 /* go down in data tree for every item, we process them all now, skip later
1888 * (metadata duplicate will be detected at the beginning of this function) */
1889 list_idx = 0;
1890 list_schema = data_tree->schema;
1891
1892 LY_TREE_FOR(data_tree, list_item) {
1893 /* another list member */
1894 if (list_item->schema == list_schema) {
1895 list_child_json = json_object_array_get_idx(child_json, list_idx);
1896 if (!list_child_json) {
1897 ERROR("Internal: list \"%s\" idx out-of-bounds", list_schema->name);
1898 return;
1899 }
1900 LY_TREE_FOR(list_item->child, child) {
1901 node_add_metadata_recursive(child, cur_module, list_child_json);
1902 }
1903
1904 ++list_idx;
1905 }
1906 }
1907 } else {
1908 if (json_object_get_type(child_json) != json_type_object) {
1909 ERROR("Internal: type mismatch (%s:%d)", __FILE__, __LINE__);
1910 return;
1911 }
1912 /* go down in data tree */
1913 LY_TREE_FOR(data_tree->child, child) {
1914 node_add_metadata_recursive(child, cur_module, child_json);
1915 }
1916 }
1917 }
1918}
1919
1920static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001921node_add_model_metadata(const struct lys_module *module, json_object *parent)
Michal Vaskoa45770b2015-11-23 15:49:41 +01001922{
1923 json_object *obj;
1924 char *str;
1925
1926 obj = json_object_new_object();
1927 node_metadata_model(module, obj);
1928 asprintf(&str, "$@@%s", module->name);
1929 json_object_object_add(parent, str, obj);
1930 free(str);
1931}
1932
1933static void
Michal Vaskof35ea502016-02-24 10:44:54 +01001934node_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 +01001935{
Michal Vaskof35ea502016-02-24 10:44:54 +01001936 const struct lys_module *cur_module;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001937 struct lys_node *child;
1938 json_object *node_json;
1939 char *json_name;
1940
Michal Vaskoa45770b2015-11-23 15:49:41 +01001941 if (node->nodetype & (LYS_OUTPUT | LYS_GROUPING)) {
1942 return;
1943 }
1944
1945 if (node->nodetype & LYS_USES) {
1946 cur_module = module;
1947 node_json = parent;
1948 goto children;
1949 }
1950
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001951 /* add node metadata */
1952 if (node_add_metadata(node, module, parent)) {
1953 ERROR("Internal: metadata duplicate for \"%s\".", node->name);
1954 return;
1955 }
1956
Michal Vaskoa45770b2015-11-23 15:49:41 +01001957 /* no other metadata */
1958 if (!node->child) {
1959 return;
1960 }
1961
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001962 /* get node module */
1963 cur_module = node->module;
1964 if (cur_module->type) {
1965 cur_module = ((struct lys_submodule *)cur_module)->belongsto;
1966 }
1967
1968 /* create JSON object for child metadata */
1969 node_json = json_object_new_object();
1970 if (cur_module == module) {
1971 json_object_object_add(parent, node->name, node_json);
1972 } else {
1973 asprintf(&json_name, "%s:%s", cur_module->name, node->name);
1974 json_object_object_add(parent, json_name, node_json);
1975 free(json_name);
1976 }
1977
Michal Vaskoa45770b2015-11-23 15:49:41 +01001978children:
Michal Vaskoea2ddd92016-03-17 15:43:58 +01001979 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) {
1980 LY_TREE_FOR(node->child, child) {
1981 node_add_children_with_metadata_recursive(child, cur_module, node_json);
1982 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001983 }
1984}
1985
1986static json_object *
1987libyang_query(unsigned int session_key, const char *filter, int load_children)
1988{
Michal Vaskof35ea502016-02-24 10:44:54 +01001989 const struct lys_node *node;
1990 const struct lys_module *module = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01001991 struct session_with_mutex *locked_session;
1992 json_object *ret = NULL, *data;
1993
1994 locked_session = session_get_locked(session_key, &ret);
1995 if (!locked_session) {
1996 ERROR("Locking failed or session not found.");
1997 goto finish;
1998 }
1999
Michal Vaskof35ea502016-02-24 10:44:54 +01002000 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002001
Michal Vaskoa45770b2015-11-23 15:49:41 +01002002 if (filter[0] == '/') {
Michal Vaskof35ea502016-02-24 10:44:54 +01002003 node = ly_ctx_get_node(nc_session_get_ctx(locked_session->session), filter);
Michal Vaskoa45770b2015-11-23 15:49:41 +01002004 if (!node) {
2005 ret = create_error_reply("Failed to resolve XPath filter node.");
2006 goto finish;
2007 }
2008 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002009 module = ly_ctx_get_module(nc_session_get_ctx(locked_session->session), filter, NULL);
Michal Vaskoa45770b2015-11-23 15:49:41 +01002010 if (!module) {
2011 ret = create_error_reply("Failed to find model.");
2012 goto finish;
2013 }
2014 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002015
Michal Vaskoa45770b2015-11-23 15:49:41 +01002016 pthread_mutex_lock(&json_lock);
2017 data = json_object_new_object();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002018
Michal Vaskoa45770b2015-11-23 15:49:41 +01002019 if (module) {
2020 node_add_model_metadata(module, data);
2021 if (load_children) {
2022 LY_TREE_FOR(module->data, node) {
2023 node_add_children_with_metadata_recursive(node, NULL, data);
2024 }
2025 }
2026 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002027 if (load_children) {
2028 node_add_children_with_metadata_recursive(node, NULL, data);
2029 } else {
2030 node_add_metadata(node, NULL, data);
2031 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002032 }
2033
Michal Vaskoa45770b2015-11-23 15:49:41 +01002034 pthread_mutex_unlock(&json_lock);
2035 ret = create_data_reply(json_object_to_json_string(data));
2036 json_object_put(data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002037
2038finish:
Michal Vaskoa45770b2015-11-23 15:49:41 +01002039 session_unlock(locked_session);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002040 return ret;
2041}
2042
2043static json_object *
2044libyang_merge(unsigned int session_key, const char *config)
2045{
2046 struct lyd_node *data_tree = NULL, *sibling;
2047 struct session_with_mutex *locked_session;
2048 json_object *ret = NULL, *data_json = NULL;
2049 enum json_tokener_error err = 0;
2050
2051 locked_session = session_get_locked(session_key, &ret);
2052 if (!locked_session) {
2053 ERROR("Locking failed or session not found.");
2054 goto finish;
2055 }
2056
Michal Vaskof35ea502016-02-24 10:44:54 +01002057 session_user_activity(nc_session_get_username(locked_session->session));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002058
Michal Vaskof35ea502016-02-24 10:44:54 +01002059 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 +01002060 if (!data_tree) {
2061 ERROR("Creating data tree failed.");
2062 ret = create_error_reply("Failed to create data tree from JSON config.");
2063 session_unlock(locked_session);
2064 goto finish;
2065 }
2066
2067 session_unlock(locked_session);
2068
2069 pthread_mutex_lock(&json_lock);
2070 data_json = json_tokener_parse_verbose(config, &err);
2071 if (!data_json) {
2072 ERROR("Parsing JSON config failed (%s).", json_tokener_error_desc(err));
2073 pthread_mutex_unlock(&json_lock);
2074 ret = create_error_reply(json_tokener_error_desc(err));
2075 goto finish;
2076 }
2077
2078 /* go simultaneously through both trees and add metadata */
2079 LY_TREE_FOR(data_tree, sibling) {
2080 node_add_metadata_recursive(sibling, NULL, data_json);
2081 }
2082 pthread_mutex_unlock(&json_lock);
2083 ret = create_data_reply(json_object_to_json_string(data_json));
2084
2085finish:
2086 LY_TREE_FOR(data_tree, sibling) {
2087 lyd_free(sibling);
2088 }
2089 json_object_put(data_json);
2090 return ret;
Radek Krejci80c10d92012-07-30 08:38:50 +02002091}
2092
Tomas Cejka0a4bba82013-04-19 11:51:28 +02002093/**
2094 * @}
2095 *//* netconf_operations */
2096
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002097void
2098clb_print(NC_VERB_LEVEL level, const char *msg)
Radek Krejci469aab82012-07-22 18:42:20 +02002099{
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002100#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +02002101 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
2102
2103#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002104 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +02002105
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002106 switch (level) {
2107 FOREACH(CASE);
2108 case NC_VERB_VERBOSE:
2109 case NC_VERB_DEBUG:
2110 DEBUG("DEBUG: %s", msg);
2111 break;
2112 }
2113 if (level == NC_VERB_ERROR) {
2114 /* return global error */
2115 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
2116 NULL /* severity */, NULL /* apptag */,
2117 NULL /* path */, msg, NULL /* attribute */,
2118 NULL /* element */, NULL /* ns */, NULL /* sid */);
2119 }
Radek Krejci469aab82012-07-22 18:42:20 +02002120}
2121
Tomas Cejka64b87482013-06-03 16:30:53 +02002122/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02002123 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02002124 * Caller should free message memory.
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002125 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02002126 * \return pointer to message
2127 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002128char *
2129get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02002130{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002131 /* read json in chunked framing */
2132 unsigned int buffer_size = 0;
2133 ssize_t buffer_len = 0;
2134 char *buffer = NULL;
2135 char c;
2136 ssize_t ret;
2137 int i, chunk_len;
2138 char chunk_len_str[12];
Tomas Cejka64b87482013-06-03 16:30:53 +02002139
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002140 while (1) {
2141 /* read chunk length */
2142 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
2143 if (buffer != NULL) {
2144 free (buffer);
2145 buffer = NULL;
2146 }
2147 break;
2148 }
2149 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
2150 if (buffer != NULL) {
2151 free (buffer);
2152 buffer = NULL;
2153 }
2154 break;
2155 }
2156 i=0;
2157 memset (chunk_len_str, 0, 12);
2158 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
2159 if (i==0 && c == '#') {
2160 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
2161 /* end but invalid */
2162 if (buffer != NULL) {
2163 free (buffer);
2164 buffer = NULL;
2165 }
2166 }
2167 /* end of message, double-loop break */
2168 goto msg_complete;
2169 }
2170 chunk_len_str[i++] = c;
2171 if (i==11) {
2172 ERROR("Message is too long, buffer for length is not big enought!!!!");
2173 break;
2174 }
2175 }
2176 if (c != '\n') {
2177 if (buffer != NULL) {
2178 free (buffer);
2179 buffer = NULL;
2180 }
2181 break;
2182 }
2183 chunk_len_str[i] = 0;
2184 if ((chunk_len = atoi (chunk_len_str)) == 0) {
2185 if (buffer != NULL) {
2186 free (buffer);
2187 buffer = NULL;
2188 }
2189 break;
2190 }
2191 buffer_size += chunk_len+1;
2192 buffer = realloc (buffer, sizeof(char)*buffer_size);
2193 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
2194 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
2195 if (buffer != NULL) {
2196 free (buffer);
2197 buffer = NULL;
2198 }
2199 break;
2200 }
2201 buffer_len += ret;
2202 }
Tomas Cejka64b87482013-06-03 16:30:53 +02002203msg_complete:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002204 return buffer;
Tomas Cejka64b87482013-06-03 16:30:53 +02002205}
2206
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002207NC_DATASTORE
2208parse_datastore(const char *ds)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002209{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002210 if (strcmp(ds, "running") == 0) {
2211 return NC_DATASTORE_RUNNING;
2212 } else if (strcmp(ds, "startup") == 0) {
2213 return NC_DATASTORE_STARTUP;
2214 } else if (strcmp(ds, "candidate") == 0) {
2215 return NC_DATASTORE_CANDIDATE;
2216 } else if (strcmp(ds, "url") == 0) {
2217 return NC_DATASTORE_URL;
2218 } else if (strcmp(ds, "config") == 0) {
2219 return NC_DATASTORE_CONFIG;
2220 }
2221 return -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002222}
2223
Michal Vaskof35ea502016-02-24 10:44:54 +01002224NC_RPC_EDIT_TESTOPT
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002225parse_testopt(const char *t)
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002226{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002227 if (strcmp(t, "notset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002228 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002229 } else if (strcmp(t, "testset") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002230 return NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002231 } else if (strcmp(t, "set") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002232 return NC_RPC_EDIT_TESTOPT_SET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002233 } else if (strcmp(t, "test") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002234 return NC_RPC_EDIT_TESTOPT_TEST;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002235 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002236 return NC_RPC_EDIT_TESTOPT_UNKNOWN;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01002237}
2238
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002239json_object *
2240create_error_reply(const char *errmess)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002241{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002242 json_object *reply, *array;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002243
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002244 pthread_mutex_lock(&json_lock);
2245 reply = json_object_new_object();
2246 array = json_object_new_array();
2247 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2248 json_object_array_add(array, json_object_new_string(errmess));
2249 json_object_object_add(reply, "errors", array);
2250 pthread_mutex_unlock(&json_lock);
2251
2252 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002253}
2254
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002255json_object *
2256create_data_reply(const char *data)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002257{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002258 pthread_mutex_lock(&json_lock);
2259 json_object *reply = json_object_new_object();
2260 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
2261 json_object_object_add(reply, "data", json_object_new_string(data));
2262 pthread_mutex_unlock(&json_lock);
2263 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002264}
2265
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002266json_object *
2267create_ok_reply(void)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002268{
Michal Vasko365dc4c2016-03-17 10:03:58 +01002269 json_object *reply;
2270
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002271 pthread_mutex_lock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002272 reply = json_object_new_object();
2273 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2274 pthread_mutex_unlock(&json_lock);
2275 return reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002276}
2277
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002278json_object *
2279create_replies(void)
Tomas Cejka09629492014-07-10 15:58:06 +02002280{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002281 json_object *replies;
2282
2283 pthread_mutex_lock(&json_lock);
2284 replies = json_object_new_object();
2285 pthread_mutex_unlock(&json_lock);
2286
2287 return replies;
Tomas Cejka09629492014-07-10 15:58:06 +02002288}
2289
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002290void
2291add_reply(json_object *replies, json_object *reply, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002292{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002293 char *str;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002294
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002295 asprintf(&str, "%u", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002296
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002297 pthread_mutex_lock(&json_lock);
2298 json_object_object_add(replies, str, reply);
2299 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002300
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002301 free(str);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002302}
2303
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002304char *
2305get_param_string(json_object *data, const char *name)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002306{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002307 json_object *js_tmp = NULL;
2308 char *res = NULL;
2309 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
2310 res = strdup(json_object_get_string(js_tmp));
2311 }
2312 return res;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002313}
2314
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002315json_object *
2316handle_op_connect(json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002317{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002318 char *host = NULL;
2319 char *port = NULL;
2320 char *user = NULL;
2321 char *pass = NULL;
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002322 char *privkey = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002323 json_object *reply = NULL;
2324 unsigned int session_key = 0;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002325
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002326 DEBUG("Request: connect");
2327 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002328
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002329 host = get_param_string(request, "host");
2330 port = get_param_string(request, "port");
2331 user = get_param_string(request, "user");
2332 pass = get_param_string(request, "pass");
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002333 privkey = get_param_string(request, "privatekey");
Tomas Cejkad5b53772013-06-08 23:01:07 +02002334
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002335 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002336
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002337 if (host == NULL) {
2338 host = "localhost";
2339 }
2340
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002341 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002342 if (user == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002343 ERROR("Cannot connect - insufficient input.");
2344 session_key = 0;
2345 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002346 session_key = netconf_connect(host, port, user, pass, privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002347 DEBUG("Session key: %u", session_key);
2348 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002349
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002350 GETSPEC_ERR_REPLY
2351
2352 pthread_mutex_lock(&json_lock);
2353 if (session_key == 0) {
2354 /* negative reply */
2355 if (err_reply == NULL) {
2356 reply = json_object_new_object();
2357 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
2358 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
2359 ERROR("Connection failed.");
2360 } else {
2361 /* use filled err_reply from libnetconf's callback */
2362 reply = err_reply;
2363 ERROR("Connect - error from libnetconf's callback.");
2364 }
2365 } else {
2366 /* positive reply */
2367 reply = json_object_new_object();
2368 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2369 json_object_object_add(reply, "session", json_object_new_int(session_key));
2370 }
2371 memset(pass, 0, strlen(pass));
2372 pthread_mutex_unlock(&json_lock);
2373 CHECK_AND_FREE(host);
2374 CHECK_AND_FREE(user);
2375 CHECK_AND_FREE(port);
2376 CHECK_AND_FREE(pass);
Michal Vaskoda5ccc82015-11-25 10:42:49 +01002377 CHECK_AND_FREE(privkey);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002378 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002379}
2380
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002381json_object *
2382handle_op_disconnect(json_object *UNUSED(request), unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002383{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002384 json_object *reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002385
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002386 DEBUG("Request: disconnect (session %u)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002387
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002388 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
2389 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
2390 } else {
2391 reply = create_ok_reply();
2392 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002393
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002394 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002395}
2396
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002397json_object *
2398handle_op_get(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002399{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002400 char *filter = NULL;
2401 char *data = NULL;
2402 json_object *reply = NULL, *obj;
2403 int strict;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002404
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002405 DEBUG("Request: get (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002406
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002407 pthread_mutex_lock(&json_lock);
2408 filter = get_param_string(request, "filter");
2409 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2410 pthread_mutex_unlock(&json_lock);
2411 reply = create_error_reply("Missing strict parameter.");
2412 return reply;
2413 }
2414 strict = json_object_get_boolean(obj);
2415 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002416
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002417 if ((data = netconf_get(session_key, filter, strict, &reply)) == NULL) {
2418 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
2419 } else {
2420 reply = create_data_reply(data);
2421 free(data);
2422 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002423
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002424 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002425}
2426
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002427json_object *
2428handle_op_getconfig(json_object *request, unsigned int session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002429{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002430 NC_DATASTORE ds_type_s = -1;
2431 char *filter = NULL;
2432 char *data = NULL;
2433 char *source = NULL;
2434 json_object *reply = NULL, *obj;
2435 int strict;
Tomas Cejkab4d05872014-02-14 22:44:38 +01002436
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002437 DEBUG("Request: get-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002438
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002439 pthread_mutex_lock(&json_lock);
2440 filter = get_param_string(request, "filter");
2441 source = get_param_string(request, "source");
2442 if (source != NULL) {
2443 ds_type_s = parse_datastore(source);
2444 }
2445 if (json_object_object_get_ex(request, "strict", &obj) == FALSE) {
2446 pthread_mutex_unlock(&json_lock);
2447 reply = create_error_reply("Missing strict parameter.");
2448 return reply;
2449 }
2450 strict = json_object_get_boolean(obj);
2451 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002452
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002453 if ((int)ds_type_s == -1) {
2454 reply = create_error_reply("Invalid source repository type requested.");
2455 goto finalize;
2456 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002457
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002458 if ((data = netconf_getconfig(session_key, ds_type_s, filter, strict, &reply)) == NULL) {
2459 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
2460 } else {
2461 reply = create_data_reply(data);
2462 free(data);
2463 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002464
Tomas Cejka09629492014-07-10 15:58:06 +02002465finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002466 CHECK_AND_FREE(filter);
2467 CHECK_AND_FREE(source);
2468 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002469}
2470
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002471json_object *
2472handle_op_editconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002473{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002474 NC_DATASTORE ds_type_t = -1;
Michal Vaskof35ea502016-02-24 10:44:54 +01002475 NC_RPC_EDIT_DFLTOP defop_type = 0;
2476 NC_RPC_EDIT_ERROPT erropt_type = 0;
2477 NC_RPC_EDIT_TESTOPT testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002478 char *defop = NULL;
2479 char *erropt = NULL;
2480 char *config = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002481 char *target = NULL;
2482 char *testopt = NULL;
2483 char *urisource = NULL;
2484 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002485 struct lyd_node *content;
2486 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002487
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002488 DEBUG("Request: edit-config (session %u)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002489
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002490 pthread_mutex_lock(&json_lock);
2491 /* get parameters */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002492 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2493 pthread_mutex_unlock(&json_lock);
2494 reply = create_error_reply("Missing configs parameter.");
2495 goto finalize;
2496 }
2497 obj = json_object_array_get_idx(configs, idx);
2498 config = strdup(json_object_get_string(obj));
Tomas Cejkad5b53772013-06-08 23:01:07 +02002499
Michal Vaskof35ea502016-02-24 10:44:54 +01002500 target = get_param_string(request, "target");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002501 defop = get_param_string(request, "default-operation");
2502 erropt = get_param_string(request, "error-option");
2503 urisource = get_param_string(request, "uri-source");
2504 testopt = get_param_string(request, "test-option");
2505 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002506
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002507 if (target != NULL) {
2508 ds_type_t = parse_datastore(target);
2509 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002510
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002511 if (defop != NULL) {
2512 if (strcmp(defop, "merge") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002513 defop_type = NC_RPC_EDIT_DFLTOP_MERGE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002514 } else if (strcmp(defop, "replace") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002515 defop_type = NC_RPC_EDIT_DFLTOP_REPLACE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002516 } else if (strcmp(defop, "none") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002517 defop_type = NC_RPC_EDIT_DFLTOP_NONE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002518 } else {
2519 reply = create_error_reply("Invalid default-operation parameter.");
2520 goto finalize;
2521 }
2522 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002523 defop_type = NC_RPC_EDIT_DFLTOP_UNKNOWN;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002524 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002525
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002526 if (erropt != NULL) {
2527 if (strcmp(erropt, "continue-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002528 erropt_type = NC_RPC_EDIT_ERROPT_CONTINUE;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002529 } else if (strcmp(erropt, "stop-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002530 erropt_type = NC_RPC_EDIT_ERROPT_STOP;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002531 } else if (strcmp(erropt, "rollback-on-error") == 0) {
Michal Vaskof35ea502016-02-24 10:44:54 +01002532 erropt_type = NC_RPC_EDIT_ERROPT_ROLLBACK;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002533 } else {
2534 reply = create_error_reply("Invalid error-option parameter.");
2535 goto finalize;
2536 }
2537 } else {
2538 erropt_type = 0;
2539 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002540
Michal Vaskof35ea502016-02-24 10:44:54 +01002541 if ((config && urisource) || (!config && !urisource)) {
2542 reply = create_error_reply("Invalid config and uri-source data parameters.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002543 goto finalize;
2544 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002545
2546 if (config) {
2547 locked_session = session_get_locked(session_key, NULL);
2548 if (!locked_session) {
2549 ERROR("Unknown session or locking failed.");
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002550 goto finalize;
2551 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002552
2553 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_EDIT);
2554 session_unlock(locked_session);
2555
2556 free(config);
2557 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2558 lyd_free_withsiblings(content);
2559 } else {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002560 config = urisource;
2561 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002562
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002563 if (testopt != NULL) {
2564 testopt_type = parse_testopt(testopt);
2565 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002566 testopt_type = NC_RPC_EDIT_TESTOPT_TESTSET;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002567 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002568
Michal Vaskof35ea502016-02-24 10:44:54 +01002569 reply = netconf_editconfig(session_key, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002570
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002571 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002572
Tomas Cejka09629492014-07-10 15:58:06 +02002573finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002574 CHECK_AND_FREE(defop);
2575 CHECK_AND_FREE(erropt);
2576 CHECK_AND_FREE(config);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002577 CHECK_AND_FREE(urisource);
2578 CHECK_AND_FREE(target);
2579 CHECK_AND_FREE(testopt);
2580
2581 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002582}
2583
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002584json_object *
2585handle_op_copyconfig(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002586{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002587 NC_DATASTORE ds_type_s = -1;
2588 NC_DATASTORE ds_type_t = -1;
2589 char *config = NULL;
2590 char *target = NULL;
2591 char *source = NULL;
2592 char *uri_src = NULL;
2593 char *uri_trg = NULL;
2594 json_object *reply = NULL, *configs, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002595 struct lyd_node *content;
2596 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002597
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002598 DEBUG("Request: copy-config (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002599
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002600 /* get parameters */
2601 pthread_mutex_lock(&json_lock);
2602 target = get_param_string(request, "target");
2603 source = get_param_string(request, "source");
2604 uri_src = get_param_string(request, "uri-source");
2605 uri_trg = get_param_string(request, "uri-target");
2606 if (!strcmp(source, "config")) {
2607 if (json_object_object_get_ex(request, "configs", &configs) == FALSE) {
2608 pthread_mutex_unlock(&json_lock);
2609 reply = create_error_reply("Missing configs parameter.");
2610 goto finalize;
2611 }
2612 obj = json_object_array_get_idx(configs, idx);
2613 if (!obj) {
2614 pthread_mutex_unlock(&json_lock);
2615 reply = create_error_reply("Configs array parameter shorter than sessions.");
2616 goto finalize;
2617 }
2618 config = strdup(json_object_get_string(obj));
2619 }
2620 pthread_mutex_unlock(&json_lock);
2621
2622 if (target != NULL) {
2623 ds_type_t = parse_datastore(target);
2624 }
2625 if (source != NULL) {
2626 ds_type_s = parse_datastore(source);
2627 }
2628
2629 if ((int)ds_type_s == -1) {
2630 /* invalid source datastore specified */
2631 reply = create_error_reply("Invalid source repository type requested.");
2632 goto finalize;
2633 }
2634
2635 if ((int)ds_type_t == -1) {
2636 /* invalid target datastore specified */
2637 reply = create_error_reply("Invalid target repository type requested.");
2638 goto finalize;
2639 }
2640
2641 if (ds_type_s == NC_DATASTORE_URL) {
2642 if (uri_src == NULL) {
2643 uri_src = "";
2644 }
2645 }
2646 if (ds_type_t == NC_DATASTORE_URL) {
2647 if (uri_trg == NULL) {
2648 uri_trg = "";
2649 }
2650 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002651
2652 if (config) {
2653 locked_session = session_get_locked(session_key, NULL);
2654 if (!locked_session) {
2655 ERROR("Unknown session or locking failed.");
2656 goto finalize;
2657 }
2658
2659 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_CONFIG);
2660 session_unlock(locked_session);
2661
2662 free(config);
2663 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
2664 lyd_free_withsiblings(content);
2665 }
2666
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002667 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
2668
2669 CHECK_ERR_SET_REPLY
2670
2671finalize:
2672 CHECK_AND_FREE(config);
2673 CHECK_AND_FREE(target);
2674 CHECK_AND_FREE(source);
2675 CHECK_AND_FREE(uri_src);
2676 CHECK_AND_FREE(uri_trg);
2677
2678 return reply;
2679}
2680
2681json_object *
2682handle_op_deleteconfig(json_object *request, unsigned int session_key)
2683{
2684 json_object *reply;
2685 NC_DATASTORE ds_type = -1;
2686 char *target, *url;
2687
2688 DEBUG("Request: delete-config (session %u)", session_key);
2689
2690 pthread_mutex_lock(&json_lock);
2691 target = get_param_string(request, "target");
2692 url = get_param_string(request, "url");
2693 pthread_mutex_unlock(&json_lock);
2694
2695 if (target != NULL) {
2696 ds_type = parse_datastore(target);
2697 }
2698 if ((int)ds_type == -1) {
2699 reply = create_error_reply("Invalid target repository type requested.");
2700 goto finalize;
2701 }
2702 if (ds_type == NC_DATASTORE_URL) {
2703 if (!url) {
2704 url = "";
2705 }
2706 }
2707
2708 reply = netconf_deleteconfig(session_key, ds_type, url);
2709
2710 CHECK_ERR_SET_REPLY
2711 if (reply == NULL) {
2712 reply = create_ok_reply();
2713 }
2714
2715finalize:
2716 CHECK_AND_FREE(target);
2717 CHECK_AND_FREE(url);
2718 return reply;
2719}
2720
2721json_object *
2722handle_op_lock(json_object *request, unsigned int session_key)
2723{
2724 json_object *reply;
2725 NC_DATASTORE ds_type = -1;
2726 char *target;
2727
2728 DEBUG("Request: lock (session %u)", session_key);
2729
2730 pthread_mutex_lock(&json_lock);
2731 target = get_param_string(request, "target");
2732 pthread_mutex_unlock(&json_lock);
2733
2734 if (target != NULL) {
2735 ds_type = parse_datastore(target);
2736 }
2737 if ((int)ds_type == -1) {
2738 reply = create_error_reply("Invalid target repository type requested.");
2739 goto finalize;
2740 }
2741
2742 reply = netconf_lock(session_key, ds_type);
2743
2744 CHECK_ERR_SET_REPLY
2745 if (reply == NULL) {
2746 reply = create_ok_reply();
2747 }
2748
2749finalize:
2750 CHECK_AND_FREE(target);
2751 return reply;
2752}
2753
2754json_object *
2755handle_op_unlock(json_object *request, unsigned int session_key)
2756{
2757 json_object *reply;
2758 NC_DATASTORE ds_type = -1;
2759 char *target;
2760
2761 DEBUG("Request: unlock (session %u)", session_key);
2762
2763 pthread_mutex_lock(&json_lock);
2764 target = get_param_string(request, "target");
2765 pthread_mutex_unlock(&json_lock);
2766
2767 if (target != NULL) {
2768 ds_type = parse_datastore(target);
2769 }
2770 if ((int)ds_type == -1) {
2771 reply = create_error_reply("Invalid target repository type requested.");
2772 goto finalize;
2773 }
2774
2775 reply = netconf_unlock(session_key, ds_type);
2776
2777 CHECK_ERR_SET_REPLY
2778 if (reply == NULL) {
2779 reply = create_ok_reply();
2780 }
2781
2782finalize:
2783 CHECK_AND_FREE(target);
2784 return reply;
2785}
2786
2787json_object *
2788handle_op_kill(json_object *request, unsigned int session_key)
2789{
2790 json_object *reply = NULL;
2791 char *sid = NULL;
2792
2793 DEBUG("Request: kill-session (session %u)", session_key);
2794
2795 pthread_mutex_lock(&json_lock);
2796 sid = get_param_string(request, "session-id");
2797 pthread_mutex_unlock(&json_lock);
2798
2799 if (sid == NULL) {
2800 reply = create_error_reply("Missing session-id parameter.");
2801 goto finalize;
2802 }
2803
2804 reply = netconf_killsession(session_key, sid);
2805
2806 CHECK_ERR_SET_REPLY
2807
2808finalize:
2809 CHECK_AND_FREE(sid);
2810 return reply;
2811}
2812
2813json_object *
2814handle_op_info(json_object *UNUSED(request), unsigned int session_key)
2815{
2816 json_object *reply = NULL;
2817 struct session_with_mutex *locked_session = NULL;
2818 DEBUG("Request: get info about session %u", session_key);
2819
2820 DEBUG("LOCK wrlock %s", __func__);
2821 if (pthread_rwlock_rdlock(&session_lock) != 0) {
2822 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2823 }
2824
2825 for (locked_session = netconf_sessions_list;
2826 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002827 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002828 if (locked_session != NULL) {
2829 DEBUG("LOCK mutex %s", __func__);
2830 pthread_mutex_lock(&locked_session->lock);
2831 DEBUG("UNLOCK wrlock %s", __func__);
2832 if (pthread_rwlock_unlock(&session_lock) != 0) {
2833 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2834 }
2835 if (locked_session->hello_message != NULL) {
2836 reply = locked_session->hello_message;
2837 } else {
2838 reply = create_error_reply("Invalid session identifier.");
2839 }
2840 DEBUG("UNLOCK mutex %s", __func__);
2841 pthread_mutex_unlock(&locked_session->lock);
2842 } else {
2843 DEBUG("UNLOCK wrlock %s", __func__);
2844 if (pthread_rwlock_unlock(&session_lock) != 0) {
2845 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2846 }
2847 reply = create_error_reply("Invalid session identifier.");
2848 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002849
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002850 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002851}
2852
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002853json_object *
2854handle_op_generic(json_object *request, unsigned int session_key, int idx)
Tomas Cejkad5b53772013-06-08 23:01:07 +02002855{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002856 json_object *reply = NULL, *contents, *obj;
Michal Vaskof35ea502016-02-24 10:44:54 +01002857 char *content = NULL, *str;
2858 struct lyd_node *data = NULL, *node_content;
2859 struct session_with_mutex *locked_session;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002860
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002861 DEBUG("Request: generic request (session %u)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002862
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002863 pthread_mutex_lock(&json_lock);
2864 if (json_object_object_get_ex(request, "contents", &contents) == FALSE) {
2865 pthread_mutex_unlock(&json_lock);
2866 reply = create_error_reply("Missing contents parameter.");
2867 goto finalize;
2868 }
2869 obj = json_object_array_get_idx(contents, idx);
2870 if (!obj) {
2871 pthread_mutex_unlock(&json_lock);
2872 reply = create_error_reply("Contents array parameter shorter than sessions.");
2873 goto finalize;
2874 }
Michal Vaskof35ea502016-02-24 10:44:54 +01002875 content = strdup(json_object_get_string(obj));
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002876 pthread_mutex_unlock(&json_lock);
2877
Michal Vaskof35ea502016-02-24 10:44:54 +01002878 locked_session = session_get_locked(session_key, NULL);
2879 if (!locked_session) {
2880 ERROR("Unknown session or locking failed.");
2881 goto finalize;
2882 }
2883
2884 node_content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), content, LYD_JSON, LYD_OPT_RPC);
2885 session_unlock(locked_session);
2886
2887 free(content);
2888 lyd_print_mem(&content, node_content, LYD_XML, LYP_WITHSIBLINGS);
2889 lyd_free_withsiblings(node_content);
2890
2891 reply = netconf_generic(session_key, content, &data);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002892 if (reply == NULL) {
2893 GETSPEC_ERR_REPLY
2894 if (err_reply != NULL) {
2895 /* use filled err_reply from libnetconf's callback */
2896 reply = err_reply;
2897 }
2898 } else {
2899 if (data == NULL) {
2900 pthread_mutex_lock(&json_lock);
2901 reply = json_object_new_object();
2902 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
2903 pthread_mutex_unlock(&json_lock);
2904 } else {
Michal Vaskof35ea502016-02-24 10:44:54 +01002905 lyd_print_mem(&str, data, LYD_JSON, LYP_WITHSIBLINGS);
2906 lyd_free_withsiblings(data);
2907 reply = create_data_reply(str);
2908 free(str);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002909 }
2910 }
2911
2912finalize:
Michal Vaskof35ea502016-02-24 10:44:54 +01002913 CHECK_AND_FREE(content);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002914 return reply;
2915}
2916
2917json_object *
2918handle_op_getschema(json_object *request, unsigned int session_key)
2919{
2920 char *data = NULL;
2921 char *identifier = NULL;
2922 char *version = NULL;
2923 char *format = NULL;
2924 json_object *reply = NULL;
2925
2926 DEBUG("Request: get-schema (session %u)", session_key);
2927
2928 pthread_mutex_lock(&json_lock);
2929 identifier = get_param_string(request, "identifier");
2930 version = get_param_string(request, "version");
2931 format = get_param_string(request, "format");
2932 pthread_mutex_unlock(&json_lock);
2933
2934 if (identifier == NULL) {
2935 reply = create_error_reply("No identifier for get-schema supplied.");
2936 goto finalize;
2937 }
2938
2939 DEBUG("get-schema(version: %s, format: %s)", version, format);
2940 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
2941 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
2942 } else {
2943 reply = create_data_reply(data);
2944 free(data);
2945 }
2946
2947finalize:
2948 CHECK_AND_FREE(identifier);
2949 CHECK_AND_FREE(version);
2950 CHECK_AND_FREE(format);
2951 return reply;
2952}
2953
2954json_object *
2955handle_op_reloadhello(json_object *UNUSED(request), unsigned int session_key)
2956{
2957 struct nc_session *temp_session = NULL;
2958 struct session_with_mutex * locked_session = NULL;
2959 json_object *reply = NULL;
2960
2961 DEBUG("Request: reload hello (session %u)", session_key);
2962
2963 DEBUG("LOCK wrlock %s", __func__);
2964 if (pthread_rwlock_wrlock(&session_lock) != 0) {
2965 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2966 return NULL;
2967 }
2968
2969 for (locked_session = netconf_sessions_list;
2970 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01002971 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002972 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
2973 DEBUG("LOCK mutex %s", __func__);
2974 pthread_mutex_lock(&locked_session->lock);
2975 DEBUG("creating temporary NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01002976 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002977 if (temp_session != NULL) {
2978 prepare_status_message(locked_session, temp_session);
2979 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01002980 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01002981 temp_session = NULL;
2982 } else {
2983 DEBUG("Reload hello failed due to channel establishment");
2984 reply = create_error_reply("Reload was unsuccessful, connection failed.");
2985 }
2986 DEBUG("UNLOCK mutex %s", __func__);
2987 pthread_mutex_unlock(&locked_session->lock);
2988 DEBUG("UNLOCK wrlock %s", __func__);
2989 if (pthread_rwlock_unlock(&session_lock) != 0) {
2990 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2991 }
2992 } else {
2993 DEBUG("UNLOCK wrlock %s", __func__);
2994 if (pthread_rwlock_unlock(&session_lock) != 0) {
2995 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
2996 }
2997 reply = create_error_reply("Invalid session identifier.");
2998 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02002999
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003000 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
3001 reply = locked_session->hello_message;
3002 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02003003
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003004 return reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02003005}
3006
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003007void
Michal Vaskof35ea502016-02-24 10:44:54 +01003008notification_history(struct nc_session *session, const struct nc_notif *notif)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003009{
Michal Vaskof35ea502016-02-24 10:44:54 +01003010 time_t eventtime;
3011 char *content;
3012 (void)session;
3013
3014 eventtime = nc_datetime2time(notif->datetime);
3015
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003016 json_object *notif_history_array = (json_object *)pthread_getspecific(notif_history_key);
3017 if (notif_history_array == NULL) {
3018 ERROR("No list of notification history found.");
3019 return;
3020 }
3021 DEBUG("Got notification from history %lu.", (long unsigned)eventtime);
3022 pthread_mutex_lock(&json_lock);
Michal Vaskof35ea502016-02-24 10:44:54 +01003023 json_object *notif_obj = json_object_new_object();
3024 if (notif_obj == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003025 ERROR("Could not allocate memory for notification (json).");
3026 goto failed;
3027 }
Michal Vaskof35ea502016-02-24 10:44:54 +01003028 lyd_print_mem(&content, notif->tree, LYD_JSON, 0);
3029
3030 json_object_object_add(notif_obj, "eventtime", json_object_new_int64(eventtime));
3031 json_object_object_add(notif_obj, "content", json_object_new_string(content));
3032
3033 free(content);
3034
3035 json_object_array_add(notif_history_array, notif_obj);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003036failed:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003037 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003038}
3039
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003040json_object *
3041handle_op_ntfgethistory(json_object *request, unsigned int session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02003042{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003043 json_object *reply = NULL;
3044 json_object *js_tmp = NULL;
3045 struct session_with_mutex *locked_session = NULL;
3046 struct nc_session *temp_session = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003047 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003048 time_t start = 0;
3049 time_t stop = 0;
3050 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003051
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003052 DEBUG("Request: get notification history (session %u)", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003053
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003054 pthread_mutex_lock(&json_lock);
3055 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
3056 from = json_object_get_int64(js_tmp);
3057 }
3058 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
3059 to = json_object_get_int64(js_tmp);
3060 }
3061 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02003062
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003063 start = time(NULL) + from;
3064 stop = time(NULL) + to;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003065
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003066 DEBUG("notification history interval %li %li", (long int)from, (long int)to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003067
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003068 DEBUG("LOCK wrlock %s", __func__);
3069 if (pthread_rwlock_rdlock(&session_lock) != 0) {
3070 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3071 reply = create_error_reply("Internal lock failed.");
3072 goto finalize;
3073 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003074
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003075 for (locked_session = netconf_sessions_list;
3076 locked_session && (locked_session->session_key != session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +01003077 locked_session = locked_session->next);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003078 if (locked_session != NULL) {
3079 DEBUG("LOCK mutex %s", __func__);
3080 pthread_mutex_lock(&locked_session->lock);
3081 DEBUG("UNLOCK wrlock %s", __func__);
3082 if (pthread_rwlock_unlock(&session_lock) != 0) {
3083 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3084 }
3085 DEBUG("creating temporal NC session.");
Michal Vaskof35ea502016-02-24 10:44:54 +01003086 temp_session = nc_connect_ssh_channel(locked_session->session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003087 if (temp_session != NULL) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003088 rpc = nc_rpc_subscribe(NULL, NULL, nc_time2datetime(start, NULL), nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003089 if (rpc == NULL) {
3090 DEBUG("UNLOCK mutex %s", __func__);
3091 pthread_mutex_unlock(&locked_session->lock);
3092 DEBUG("notifications: creating an rpc request failed.");
3093 reply = create_error_reply("notifications: creating an rpc request failed.");
3094 goto finalize;
3095 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003096
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003097 DEBUG("Send NC subscribe.");
3098 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
3099 json_object *res = netconf_unlocked_op(temp_session, rpc);
3100 if (res != NULL) {
3101 DEBUG("UNLOCK mutex %s", __func__);
3102 pthread_mutex_unlock(&locked_session->lock);
3103 DEBUG("Subscription RPC failed.");
3104 reply = res;
3105 goto finalize;
3106 }
3107 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka6b886e02013-07-05 09:53:17 +02003108
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003109 DEBUG("UNLOCK mutex %s", __func__);
3110 pthread_mutex_unlock(&locked_session->lock);
3111 DEBUG("LOCK mutex %s", __func__);
3112 pthread_mutex_lock(&ntf_history_lock);
3113 pthread_mutex_lock(&json_lock);
3114 json_object *notif_history_array = json_object_new_array();
3115 pthread_mutex_unlock(&json_lock);
3116 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
3117 ERROR("notif_history: cannot set thread-specific hash value.");
3118 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003119
Michal Vaskof35ea502016-02-24 10:44:54 +01003120 nc_recv_notif_dispatch(temp_session, notification_history);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003121
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003122 pthread_mutex_lock(&json_lock);
3123 reply = json_object_new_object();
3124 json_object_object_add(reply, "notifications", notif_history_array);
3125 //json_object_put(notif_history_array);
3126 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02003127
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003128 DEBUG("UNLOCK mutex %s", __func__);
3129 pthread_mutex_unlock(&ntf_history_lock);
3130 DEBUG("closing temporal NC session.");
Michal Vaskoa9590052016-03-08 10:12:24 +01003131 nc_session_free(temp_session, NULL);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003132 temp_session = NULL;
3133 } else {
3134 DEBUG("UNLOCK mutex %s", __func__);
3135 pthread_mutex_unlock(&locked_session->lock);
3136 DEBUG("Get history of notification failed due to channel establishment");
3137 reply = create_error_reply("Get history of notification was unsuccessful, connection failed.");
3138 }
3139 } else {
3140 DEBUG("UNLOCK wrlock %s", __func__);
3141 if (pthread_rwlock_unlock(&session_lock) != 0) {
3142 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3143 }
3144 reply = create_error_reply("Invalid session identifier.");
3145 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02003146
Tomas Cejka09629492014-07-10 15:58:06 +02003147finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003148 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02003149}
3150
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003151json_object *
3152handle_op_validate(json_object *request, unsigned int session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02003153{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003154 json_object *reply = NULL;
3155 char *target = NULL;
3156 char *url = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003157 struct nc_rpc *rpc = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003158 NC_DATASTORE target_ds;
Tomas Cejka4003a702013-10-01 00:02:45 +02003159
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003160 DEBUG("Request: validate datastore (session %u)", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02003161
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003162 pthread_mutex_lock(&json_lock);
3163 target = get_param_string(request, "target");
3164 url = get_param_string(request, "url");
3165 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02003166
3167
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003168 if (target == NULL) {
3169 reply = create_error_reply("Missing target parameter.");
3170 goto finalize;
3171 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003172
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003173 /* validation */
3174 target_ds = parse_datastore(target);
Michal Vaskof35ea502016-02-24 10:44:54 +01003175 rpc = nc_rpc_validate(target_ds, url, NC_PARAMTYPE_CONST);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003176 if (rpc == NULL) {
3177 DEBUG("mod_netconf: creating rpc request failed");
3178 reply = create_error_reply("Creation of RPC request failed.");
3179 goto finalize;
3180 }
Tomas Cejka4003a702013-10-01 00:02:45 +02003181
Michal Vaskof35ea502016-02-24 10:44:54 +01003182 if ((reply = netconf_op(session_key, rpc, 0, NULL)) == NULL) {
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003183 CHECK_ERR_SET_REPLY
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003184
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003185 if (reply == NULL) {
3186 DEBUG("Request: validation ok.");
3187 reply = create_ok_reply();
3188 }
3189 }
3190 nc_rpc_free (rpc);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01003191
Tomas Cejka09629492014-07-10 15:58:06 +02003192finalize:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003193 CHECK_AND_FREE(target);
3194 CHECK_AND_FREE(url);
3195 return reply;
Tomas Cejka6b886e02013-07-05 09:53:17 +02003196}
3197
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003198json_object *
3199handle_op_query(json_object *request, unsigned int session_key, int idx)
David Kupka8e60a372012-09-04 09:15:20 +02003200{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003201 json_object *reply = NULL, *filters, *obj;
3202 char *filter = NULL;
3203 int load_children = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003204
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003205 DEBUG("Request: query (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003206
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003207 pthread_mutex_lock(&json_lock);
3208 if (json_object_object_get_ex(request, "filters", &filters) == FALSE) {
3209 pthread_mutex_unlock(&json_lock);
3210 reply = create_error_reply("Missing filters parameter.");
3211 goto finalize;
3212 }
3213 obj = json_object_array_get_idx(filters, idx);
3214 if (!obj) {
3215 pthread_mutex_unlock(&json_lock);
3216 reply = create_error_reply("Filters array parameter shorter than sessions.");
3217 goto finalize;
3218 }
3219 filter = strdup(json_object_get_string(obj));
3220 if (json_object_object_get_ex(request, "load_children", &obj) == TRUE) {
3221 load_children = json_object_get_boolean(obj);
3222 }
3223 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02003224
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003225 reply = libyang_query(session_key, filter, load_children);
David Kupka8e60a372012-09-04 09:15:20 +02003226
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003227 CHECK_ERR_SET_REPLY
3228 if (!reply) {
3229 reply = create_error_reply("Query failed.");
3230 }
David Kupka8e60a372012-09-04 09:15:20 +02003231
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003232finalize:
3233 CHECK_AND_FREE(filter);
3234 return reply;
3235}
David Kupka8e60a372012-09-04 09:15:20 +02003236
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003237json_object *
3238handle_op_merge(json_object *request, unsigned int session_key, int idx)
3239{
3240 json_object *reply = NULL, *configs, *obj;
3241 char *config = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +01003242 struct lyd_node *content;
3243 struct session_with_mutex *locked_session;
David Kupka8e60a372012-09-04 09:15:20 +02003244
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003245 DEBUG("Request: merge (session %u)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02003246
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003247 pthread_mutex_lock(&json_lock);
3248 if (json_object_object_get_ex(request, "configurations", &configs) == FALSE) {
3249 pthread_mutex_unlock(&json_lock);
3250 reply = create_error_reply("Missing configurations parameter.");
3251 goto finalize;
3252 }
3253 obj = json_object_array_get_idx(configs, idx);
3254 if (!obj) {
3255 pthread_mutex_unlock(&json_lock);
3256 reply = create_error_reply("Filters array parameter shorter than sessions.");
3257 goto finalize;
3258 }
3259 config = strdup(json_object_get_string(obj));
3260 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02003261
Michal Vaskof35ea502016-02-24 10:44:54 +01003262 locked_session = session_get_locked(session_key, NULL);
3263 if (!locked_session) {
3264 ERROR("Unknown session or locking failed.");
3265 goto finalize;
3266 }
3267
3268 content = lyd_parse_mem(nc_session_get_ctx(locked_session->session), config, LYD_JSON, LYD_OPT_DATA);
3269 session_unlock(locked_session);
3270
3271 free(config);
3272 lyd_print_mem(&config, content, LYD_XML, LYP_WITHSIBLINGS);
3273 lyd_free_withsiblings(content);
3274
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003275 reply = libyang_merge(session_key, config);
David Kupka8e60a372012-09-04 09:15:20 +02003276
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003277 CHECK_ERR_SET_REPLY
3278 if (!reply) {
3279 reply = create_error_reply("Merge failed.");
3280 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003281
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003282finalize:
3283 CHECK_AND_FREE(config);
3284 return reply;
3285}
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003286
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003287void *
3288thread_routine(void *arg)
3289{
3290 void *retval = NULL;
3291 struct pollfd fds;
3292 json_object *request = NULL, *replies = NULL, *reply, *sessions = NULL;
3293 json_object *js_tmp = NULL;
Michal Vaskodf280a22016-03-17 09:19:53 +01003294 int operation = (-1), count, i, sent;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003295 int status = 0;
3296 const char *msgtext;
3297 unsigned int session_key = 0;
3298 char *chunked_out_msg = NULL;
3299 int client = ((struct pass_to_thread *)arg)->client;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003300
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003301 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02003302
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003303 /* init thread specific err_reply memory */
3304 create_err_reply_p();
David Kupka8e60a372012-09-04 09:15:20 +02003305
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003306 while (!isterminated) {
3307 fds.fd = client;
3308 fds.events = POLLIN;
3309 fds.revents = 0;
David Kupka8e60a372012-09-04 09:15:20 +02003310
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003311 status = poll(&fds, 1, 1000);
Tomas Cejkad5b53772013-06-08 23:01:07 +02003312
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003313 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
3314 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
3315 continue;
3316 } else if (status < 0) {
3317 /* 0: poll time outed
3318 * close socket and ignore this request from the client, it can try it again
3319 * -1: poll failed
3320 * something wrong happend, close this socket and wait for another request
3321 */
3322 close(client);
3323 break;
3324 }
3325 /* status > 0 */
David Kupka8e60a372012-09-04 09:15:20 +02003326
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003327 /* check the status of the socket */
David Kupka8e60a372012-09-04 09:15:20 +02003328
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003329 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
3330 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
3331 /* close client's socket (it's probably already closed by client */
3332 close(client);
3333 break;
3334 }
Tomas Cejka09629492014-07-10 15:58:06 +02003335
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003336 DEBUG("Get framed message...");
3337 buffer = get_framed_message(client);
Tomas Cejka09629492014-07-10 15:58:06 +02003338
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003339 DEBUG("Check read buffer.");
3340 if (buffer != NULL) {
3341 enum json_tokener_error jerr;
3342 pthread_mutex_lock(&json_lock);
3343 request = json_tokener_parse_verbose(buffer, &jerr);
3344 if (jerr != json_tokener_success) {
3345 ERROR("JSON parsing error");
3346 pthread_mutex_unlock(&json_lock);
3347 continue;
3348 }
3349
3350 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
3351 operation = json_object_get_int(js_tmp);
3352 }
3353 pthread_mutex_unlock(&json_lock);
3354 if (operation == -1) {
3355 replies = create_replies();
3356 add_reply(replies, create_error_reply("Missing operation type from frontend."), 0);
3357 goto send_reply;
3358 }
3359
3360 if ((operation < 4) || ((operation > 19) && (operation < 100)) || (operation > 101)) {
3361 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
3362 replies = create_replies();
3363 add_reply(replies, create_error_reply("Operation not supported."), 0);
3364 goto send_reply;
3365 }
3366
3367 DEBUG("operation %d", operation);
3368
3369 /* null global JSON error-reply */
3370 clean_err_reply();
3371
3372 /* clean replies envelope */
3373 if (replies != NULL) {
3374 pthread_mutex_lock(&json_lock);
3375 json_object_put(replies);
3376 pthread_mutex_unlock(&json_lock);
3377 }
3378 replies = create_replies();
3379
3380 if (operation == MSG_CONNECT) {
3381 count = 1;
3382 } else {
3383 pthread_mutex_lock(&json_lock);
3384 if (json_object_object_get_ex(request, "sessions", &sessions) == FALSE) {
Michal Vasko642cad02016-03-17 12:31:18 +01003385 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003386 add_reply(replies, create_error_reply("Operation missing \"sessions\" arg"), 0);
3387 goto send_reply;
3388 }
3389 count = json_object_array_length(sessions);
3390 pthread_mutex_unlock(&json_lock);
3391 }
3392
3393 for (i = 0; i < count; ++i) {
3394 if (operation != MSG_CONNECT) {
3395 js_tmp = json_object_array_get_idx(sessions, i);
3396 session_key = json_object_get_int(js_tmp);
3397 }
3398
3399 /* process required operation */
Michal Vasko977bad92016-03-15 16:20:51 +01003400 reply = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003401 switch (operation) {
3402 case MSG_CONNECT:
3403 reply = handle_op_connect(request);
3404 break;
3405 case MSG_DISCONNECT:
3406 reply = handle_op_disconnect(request, session_key);
3407 break;
3408 case MSG_GET:
3409 reply = handle_op_get(request, session_key);
3410 break;
3411 case MSG_GETCONFIG:
3412 reply = handle_op_getconfig(request, session_key);
3413 break;
3414 case MSG_EDITCONFIG:
3415 reply = handle_op_editconfig(request, session_key, i);
3416 break;
3417 case MSG_COPYCONFIG:
3418 reply = handle_op_copyconfig(request, session_key, i);
3419 break;
3420 case MSG_DELETECONFIG:
3421 reply = handle_op_deleteconfig(request, session_key);
3422 break;
3423 case MSG_LOCK:
3424 reply = handle_op_lock(request, session_key);
3425 break;
3426 case MSG_UNLOCK:
3427 reply = handle_op_unlock(request, session_key);
3428 break;
3429 case MSG_KILL:
3430 reply = handle_op_kill(request, session_key);
3431 break;
3432 case MSG_INFO:
3433 reply = handle_op_info(request, session_key);
3434 break;
3435 case MSG_GENERIC:
3436 reply = handle_op_generic(request, session_key, i);
3437 break;
3438 case MSG_GETSCHEMA:
3439 reply = handle_op_getschema(request, session_key);
3440 break;
3441 case MSG_RELOADHELLO:
3442 reply = handle_op_reloadhello(request, session_key);
3443 break;
3444 case MSG_NTF_GETHISTORY:
3445 reply = handle_op_ntfgethistory(request, session_key);
3446 break;
3447 case MSG_VALIDATE:
3448 reply = handle_op_validate(request, session_key);
3449 break;
3450 case SCH_QUERY:
3451 reply = handle_op_query(request, session_key, i);
3452 break;
3453 case SCH_MERGE:
3454 reply = handle_op_merge(request, session_key, i);
3455 break;
3456 }
3457
3458 add_reply(replies, reply, session_key);
3459 }
3460
3461 /* free parameters */
3462 operation = (-1);
3463
3464 DEBUG("Clean request json object.");
3465 if (request != NULL) {
3466 pthread_mutex_lock(&json_lock);
3467 json_object_put(request);
3468 pthread_mutex_unlock(&json_lock);
Michal Vasko365dc4c2016-03-17 10:03:58 +01003469 request = NULL;
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003470 }
3471 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02003472
3473send_reply:
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003474 /* send reply to caller */
3475 if (replies) {
3476 pthread_mutex_lock(&json_lock);
3477 msgtext = json_object_to_json_string(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003478 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003479 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
3480 if (buffer != NULL) {
3481 free(buffer);
3482 buffer = NULL;
3483 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003484 break;
3485 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01003486
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003487 DEBUG("Send framed reply json object.");
Michal Vaskodf280a22016-03-17 09:19:53 +01003488 i = 0;
3489 sent = 0;
3490 count = strlen(chunked_out_msg) + 1;
3491 while (count && ((i = send(client, chunked_out_msg + sent, count, 0)) != -1)) {
3492 sent += i;
3493 count -= i;
3494 }
3495 if (i == -1) {
3496 ERROR("Sending message failed (%s).", strerror(errno));
3497 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003498 DEBUG("Clean reply json object.");
3499 pthread_mutex_lock(&json_lock);
3500 json_object_put(replies);
Michal Vasko52c91772016-03-17 10:04:12 +01003501 pthread_mutex_unlock(&json_lock);
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003502 replies = NULL;
3503 DEBUG("Clean message buffer.");
3504 CHECK_AND_FREE(chunked_out_msg);
3505 chunked_out_msg = NULL;
3506 if (buffer) {
3507 free(buffer);
3508 buffer = NULL;
3509 }
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003510 clean_err_reply();
3511 } else {
3512 ERROR("Reply is NULL, shouldn't be...");
3513 continue;
3514 }
3515 }
3516 }
3517 free(arg);
3518 free_err_reply();
Michal Vaskod0205992016-03-17 10:04:49 +01003519 nc_thread_destroy();
David Kupka8e60a372012-09-04 09:15:20 +02003520
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003521 return retval;
David Kupka8e60a372012-09-04 09:15:20 +02003522}
3523
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003524/**
3525 * \brief Close all open NETCONF sessions.
3526 *
3527 * During termination of mod_netconf, it is useful to close all remaining
3528 * sessions. This function iterates over the list of sessions and close them
3529 * all.
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003530 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003531static void
3532close_all_nc_sessions(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003533{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003534 struct session_with_mutex *locked_session, *next_session;
3535 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003536
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003537 /* get exclusive access to sessions_list (conns) */
3538 DEBUG("LOCK wrlock %s", __func__);
3539 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
3540 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3541 return;
3542 }
3543 for (next_session = netconf_sessions_list; next_session;) {
3544 locked_session = next_session;
3545 next_session = locked_session->next;
Tomas Cejka47387fd2013-06-10 20:37:46 +02003546
Michal Vaskoc3146782015-11-04 14:46:41 +01003547 /* close_and_free_session handles locking on its own */
Michal Vaskof35ea502016-02-24 10:44:54 +01003548 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 +01003549 close_and_free_session(locked_session);
3550 }
3551 netconf_sessions_list = NULL;
3552
3553 /* get exclusive access to sessions_list (conns) */
3554 DEBUG("UNLOCK wrlock %s", __func__);
3555 if (pthread_rwlock_unlock (&session_lock) != 0) {
3556 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3557 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003558}
3559
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003560static void
3561check_timeout_and_close(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003562{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003563 struct nc_session *ns = NULL;
3564 struct session_with_mutex *locked_session = NULL;
3565 time_t current_time = time(NULL);
3566 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003567
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003568 /* get exclusive access to sessions_list (conns) */
3569 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
3570 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
3571 return;
3572 }
3573 for (locked_session = netconf_sessions_list; locked_session; locked_session = locked_session->next) {
3574 ns = locked_session->session;
3575 if (ns == NULL) {
3576 continue;
3577 }
3578 pthread_mutex_lock(&locked_session->lock);
3579 if ((current_time - locked_session->last_activity) > ACTIVITY_TIMEOUT) {
Michal Vaskof35ea502016-02-24 10:44:54 +01003580 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 +02003581
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003582 /* close_and_free_session handles locking on its own */
3583 close_and_free_session(locked_session);
3584 } else {
3585 pthread_mutex_unlock(&locked_session->lock);
3586 }
3587 }
3588 /* get exclusive access to sessions_list (conns) */
3589 if (pthread_rwlock_unlock(&session_lock) != 0) {
3590 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
3591 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003592}
3593
3594
3595/**
Radek Krejcif23850c2012-07-23 16:14:17 +02003596 * This is actually implementation of NETCONF client
3597 * - requests are received from UNIX socket in the predefined format
3598 * - results are replied through the same way
Michal Vaskoc3146782015-11-04 14:46:41 +01003599 * - the daemon run as a separate process
Radek Krejcif23850c2012-07-23 16:14:17 +02003600 *
3601 */
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003602static void
3603forked_proc(void)
Radek Krejci469aab82012-07-22 18:42:20 +02003604{
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003605 struct timeval tv;
3606 struct sockaddr_un local, remote;
3607 int lsock, client, ret, i, pthread_count = 0;
3608 unsigned int olds = 0, timediff = 0;
3609 socklen_t len;
3610 struct pass_to_thread *arg;
3611 pthread_t *ptids = calloc(1, sizeof(pthread_t));
3612 struct timespec maxtime;
3613 pthread_rwlockattr_t lock_attrs;
3614 #ifdef WITH_NOTIFICATIONS
3615 char use_notifications = 0;
3616 #endif
David Kupka8e60a372012-09-04 09:15:20 +02003617
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003618 /* wait at most 5 seconds for every thread to terminate */
3619 maxtime.tv_sec = 5;
3620 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02003621
Tomas Cejka04e08f42014-03-27 19:52:34 +01003622#ifdef HAVE_UNIXD_SETUP_CHILD
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003623 /* change uid and gid of process for security reasons */
3624 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01003625#else
3626# ifdef SU_GROUP
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003627 if (strlen(SU_GROUP) > 0) {
3628 struct group *g = getgrnam(SU_GROUP);
3629 if (g == NULL) {
3630 ERROR("GID (%s) was not found.", SU_GROUP);
3631 return;
3632 }
3633 if (setgid(g->gr_gid) != 0) {
3634 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
3635 return;
3636 }
3637 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003638# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003639 DEBUG("no SU_GROUP");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003640# endif
3641# ifdef SU_USER
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003642 if (strlen(SU_USER) > 0) {
3643 struct passwd *p = getpwnam(SU_USER);
3644 if (p == NULL) {
3645 ERROR("UID (%s) was not found.", SU_USER);
3646 return;
3647 }
3648 if (setuid(p->pw_uid) != 0) {
3649 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
3650 return;
3651 }
3652 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003653# else
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003654 DEBUG("no SU_USER");
Tomas Cejka04e08f42014-03-27 19:52:34 +01003655# endif
3656#endif
Radek Krejci469aab82012-07-22 18:42:20 +02003657
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003658 /* try to remove if exists */
3659 unlink(sockname);
Tomas Cejka04e08f42014-03-27 19:52:34 +01003660
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003661 /* create listening UNIX socket to accept incoming connections */
3662 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
3663 ERROR("Creating socket failed (%s)", strerror(errno));
3664 goto error_exit;
3665 }
Radek Krejci469aab82012-07-22 18:42:20 +02003666
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003667 local.sun_family = AF_UNIX;
3668 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
3669 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
Radek Krejci469aab82012-07-22 18:42:20 +02003670
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003671 if (bind(lsock, (struct sockaddr *)&local, len) == -1) {
3672 if (errno == EADDRINUSE) {
3673 ERROR("mod_netconf socket address already in use");
3674 goto error_exit;
3675 }
3676 ERROR("Binding socket failed (%s)", strerror(errno));
3677 goto error_exit;
3678 }
Radek Krejci469aab82012-07-22 18:42:20 +02003679
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003680 if (listen(lsock, MAX_SOCKET_CL) == -1) {
3681 ERROR("Setting up listen socket failed (%s)", strerror(errno));
3682 goto error_exit;
3683 }
3684 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02003685
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003686 uid_t user = -1;
3687 if (strlen(CHOWN_USER) > 0) {
3688 struct passwd *p = getpwnam(CHOWN_USER);
3689 if (p != NULL) {
3690 user = p->pw_uid;
3691 }
3692 }
3693 gid_t group = -1;
3694 if (strlen(CHOWN_GROUP) > 0) {
3695 struct group *g = getgrnam(CHOWN_GROUP);
3696 if (g != NULL) {
3697 group = g->gr_gid;
3698 }
3699 }
3700 if (chown(sockname, user, group) == -1) {
3701 ERROR("Chown on socket file failed (%s).", strerror(errno));
3702 }
Tomas Cejka04e08f42014-03-27 19:52:34 +01003703
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003704 /* prepare internal lists */
Tomas Cejkaba21b382013-04-13 02:37:32 +02003705
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003706 #ifdef WITH_NOTIFICATIONS
3707 if (notification_init() == -1) {
3708 ERROR("libwebsockets initialization failed");
3709 use_notifications = 0;
3710 } else {
3711 use_notifications = 1;
3712 }
3713 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003714
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003715 /* setup libnetconf's callbacks */
Michal Vaskod0205992016-03-17 10:04:49 +01003716 nc_client_init();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003717 nc_verbosity(NC_VERB_DEBUG);
Michal Vaskof35ea502016-02-24 10:44:54 +01003718 nc_set_print_clb(clb_print);
3719 nc_client_ssh_set_auth_hostkey_check_clb(netconf_callback_ssh_hostkey_check);
3720 nc_client_ssh_set_auth_interactive_clb(netconf_callback_sshauth_interactive);
3721 nc_client_ssh_set_auth_password_clb(netconf_callback_sshauth_password);
3722 nc_client_ssh_set_auth_privkey_passphrase_clb(netconf_callback_sshauth_passphrase);
Radek Krejci469aab82012-07-22 18:42:20 +02003723
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003724 /* disable publickey authentication */
Michal Vaskof35ea502016-02-24 10:44:54 +01003725 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1);
Radek Krejci469aab82012-07-22 18:42:20 +02003726
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003727 /* create mutex protecting session list */
3728 pthread_rwlockattr_init(&lock_attrs);
3729 /* rwlock is shared only with threads in this process */
3730 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
3731 /* create rw lock */
3732 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
3733 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
3734 goto error_exit;
3735 }
3736 pthread_mutex_init(&ntf_history_lock, NULL);
3737 pthread_mutex_init(&json_lock, NULL);
3738 DEBUG("Initialization of notification history.");
3739 if (pthread_key_create(&notif_history_key, NULL) != 0) {
3740 ERROR("Initialization of notification history failed.");
3741 }
3742 if (pthread_key_create(&err_reply_key, NULL) != 0) {
3743 ERROR("Initialization of reply key failed.");
3744 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02003745
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003746 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
3747 while (isterminated == 0) {
3748 gettimeofday(&tv, NULL);
3749 timediff = (unsigned int)tv.tv_sec - olds;
3750 #ifdef WITH_NOTIFICATIONS
3751 if (use_notifications == 1) {
3752 notification_handle();
3753 }
3754 #endif
3755 if (timediff > ACTIVITY_CHECK_INTERVAL) {
3756 check_timeout_and_close();
3757 }
Radek Krejci469aab82012-07-22 18:42:20 +02003758
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003759 /* open incoming connection if any */
3760 len = sizeof(remote);
3761 client = accept(lsock, (struct sockaddr *) &remote, &len);
3762 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
3763 usleep(SLEEP_TIME * 1000);
3764 continue;
3765 } else if (client == -1 && (errno == EINTR)) {
3766 continue;
3767 } else if (client == -1) {
3768 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
3769 continue;
3770 }
Radek Krejci469aab82012-07-22 18:42:20 +02003771
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003772 /* set client's socket as non-blocking */
3773 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02003774
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003775 arg = malloc(sizeof(struct pass_to_thread));
3776 arg->client = client;
3777 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02003778
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003779 /* start new thread. It will serve this particular request and then terminate */
3780 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void *)arg)) != 0) {
3781 ERROR("Creating POSIX thread failed: %d\n", ret);
3782 } else {
3783 DEBUG("Thread %lu created", ptids[pthread_count]);
3784 pthread_count++;
3785 ptids = realloc (ptids, sizeof(pthread_t) * (pthread_count+1));
3786 ptids[pthread_count] = 0;
3787 }
Radek Krejci469aab82012-07-22 18:42:20 +02003788
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003789 /* check if some thread already terminated, free some resources by joining it */
3790 for (i = 0; i < pthread_count; i++) {
3791 if (pthread_tryjoin_np(ptids[i], (void **)&arg) == 0) {
3792 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
3793 pthread_count--;
3794 if (pthread_count > 0) {
3795 /* place last Thread ID on the place of joined one */
3796 ptids[i] = ptids[pthread_count];
3797 }
3798 }
3799 }
3800 DEBUG("Running %d threads", pthread_count);
3801 }
Radek Krejci469aab82012-07-22 18:42:20 +02003802
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003803 DEBUG("mod_netconf terminating...");
3804 /* join all threads */
3805 for (i = 0; i < pthread_count; i++) {
3806 pthread_timedjoin_np(ptids[i], (void **)&arg, &maxtime);
3807 }
Radek Krejci469aab82012-07-22 18:42:20 +02003808
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003809 #ifdef WITH_NOTIFICATIONS
3810 notification_close();
3811 #endif
Tomas Cejkad340dbf2013-03-24 20:36:57 +01003812
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003813 /* close all NETCONF sessions */
3814 close_all_nc_sessions();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02003815
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003816 /* destroy rwlock */
3817 pthread_rwlock_destroy(&session_lock);
3818 pthread_rwlockattr_destroy(&lock_attrs);
David Kupka8e60a372012-09-04 09:15:20 +02003819
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003820 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02003821
Michal Vaskod0205992016-03-17 10:04:49 +01003822 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003823 free(ptids);
3824 close(lsock);
3825 exit(0);
3826 return;
3827
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003828error_exit:
Michal Vaskod0205992016-03-17 10:04:49 +01003829 nc_client_destroy();
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003830 close(lsock);
3831 free(ptids);
3832 return;
Radek Krejci469aab82012-07-22 18:42:20 +02003833}
3834
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003835int
3836main(int argc, char **argv)
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003837{
Michal Vaskoc3146782015-11-04 14:46:41 +01003838 struct sigaction action;
3839 sigset_t block_mask;
Michal Vaskoa53ef882015-11-24 11:02:01 +01003840 int daemonize = 0, i;
Michal Vaskoc3146782015-11-04 14:46:41 +01003841
Michal Vaskoa53ef882015-11-24 11:02:01 +01003842 if (argc > 3) {
3843 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3844 return 1;
3845 }
3846
3847 sockname = SOCKET_FILENAME;
3848 for (i = 1; i < argc; ++i) {
3849 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
3850 printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
3851 return 0;
3852 } else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon")) {
3853 daemonize = 1;
3854 } else {
3855 sockname = argv[i];
3856 }
3857 }
3858
3859 if (daemonize && (daemon(0, 0) == -1)) {
3860 ERROR("daemon() failed (%s)", strerror(errno));
3861 return 1;
Michal Vaskoc3146782015-11-04 14:46:41 +01003862 }
3863
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003864 sigfillset(&block_mask);
Michal Vaskoc3146782015-11-04 14:46:41 +01003865 action.sa_handler = signal_handler;
3866 action.sa_mask = block_mask;
3867 action.sa_flags = 0;
3868 sigaction(SIGINT, &action, NULL);
3869 sigaction(SIGTERM, &action, NULL);
3870
Michal Vaskoda0ab5c2015-11-13 13:24:51 +01003871 forked_proc();
3872 DEBUG("Terminated");
3873 return 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01003874}