blob: 523dd859019ea683f68fc274ddbc9e1d7edea4e9 [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01008 * \date 2013
Radek Krejci469aab82012-07-22 18:42:20 +02009 */
10/*
Tomas Cejkad340dbf2013-03-24 20:36:57 +010011 * Copyright (C) 2011-2013 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020012 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
Michal Vaskoc3146782015-11-04 14:46:41 +010046#define _GNU_SOURCE
Radek Krejci469aab82012-07-22 18:42:20 +020047
Radek Krejci7b4ddd02012-07-30 08:09:58 +020048#include <unistd.h>
49#include <poll.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010050#include <time.h>
Radek Krejci469aab82012-07-22 18:42:20 +020051#include <sys/types.h>
52#include <sys/socket.h>
53#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010054#include <sys/fcntl.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010055#include <pwd.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010056#include <errno.h>
Tomas Cejka04e08f42014-03-27 19:52:34 +010057#include <grp.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010058#include <signal.h>
David Kupka8e60a372012-09-04 09:15:20 +020059#include <pthread.h>
60#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020061
Michal Vaskoc3146782015-11-04 14:46:41 +010062#include <libyang/libyang.h>
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020063
Radek Krejci469aab82012-07-22 18:42:20 +020064#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020065#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020066
Tomas Cejka04e08f42014-03-27 19:52:34 +010067#include "../config.h"
68
Tomas Cejkad340dbf2013-03-24 20:36:57 +010069#ifdef WITH_NOTIFICATIONS
70#include "notification_module.h"
71#endif
72
Tomas Cejka94da2c52013-01-08 18:20:30 +010073#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020074#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020075
76#define MAX_PROCS 5
Tomas Cejka86f0fc12014-09-17 15:09:38 +020077#define SOCKET_FILENAME "/var/run/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020078#define MAX_SOCKET_CL 10
79#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020080#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020081#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020082
83/* sleep in master process for non-blocking socket reading */
84#define SLEEP_TIME 200
85
86#ifndef offsetof
87#define offsetof(type, member) ((size_t) ((type *) 0)->member)
88#endif
89
Tomas Cejka027f3bc2012-11-10 20:28:36 +010090/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020091struct timeval timeout = { 1, 0 };
92
Tomas Cejka5064c232013-01-17 09:30:58 +010093#define NCWITHDEFAULTS NCWD_MODE_NOTSET
94
95
Radek Krejci469aab82012-07-22 18:42:20 +020096#define MSG_OK 0
97#define MSG_OPEN 1
98#define MSG_DATA 2
99#define MSG_CLOSE 3
100#define MSG_ERROR 4
101#define MSG_UNKNOWN 5
102
Tomas Cejka47387fd2013-06-10 20:37:46 +0200103pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200104pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100105pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100106pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
107
Michal Vaskoc3146782015-11-04 14:46:41 +0100108struct session_with_mutex *netconf_sessions_list = NULL;
109
110static const char *sockname;
David Kupka8e60a372012-09-04 09:15:20 +0200111
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200112static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200113
Tomas Cejka442258e2014-04-01 18:17:18 +0200114pthread_key_t err_reply_key;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100115
Radek Krejci469aab82012-07-22 18:42:20 +0200116volatile int isterminated = 0;
117
118static char* password;
119
Radek Krejci469aab82012-07-22 18:42:20 +0200120static void signal_handler(int sign)
121{
122 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100123 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200124 case SIGTERM:
125 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200126 break;
127 }
128}
129
Michal Vaskoc3146782015-11-04 14:46:41 +0100130int netconf_callback_ssh_hostkey_check(const char* UNUSED(hostname), ssh_session UNUSED(session))
Radek Krejci469aab82012-07-22 18:42:20 +0200131{
132 /* always approve */
133 return (EXIT_SUCCESS);
134}
135
Michal Vaskoc3146782015-11-04 14:46:41 +0100136char *netconf_callback_sshauth_passphrase(const char *UNUSED(username), const char *UNUSED(hostname), const char *UNUSED(priv_key_file))
Radek Krejci469aab82012-07-22 18:42:20 +0200137{
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200138 char *buf;
139 buf = strdup(password);
Radek Krejci469aab82012-07-22 18:42:20 +0200140 return (buf);
141}
142
Michal Vaskoc3146782015-11-04 14:46:41 +0100143char *netconf_callback_sshauth_password(const char* UNUSED(username), const char* UNUSED(hostname))
Radek Krejci469aab82012-07-22 18:42:20 +0200144{
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200145 char *buf;
146 buf = strdup(password);
147 return (buf);
Radek Krejci469aab82012-07-22 18:42:20 +0200148}
149
Michal Vaskoc3146782015-11-04 14:46:41 +0100150char *netconf_callback_sshauth_interactive(const char *UNUSED(name), const char *UNUSED(instruction),
151 const char *UNUSED(prompt), int UNUSED(echo))
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200152{
153 char *buf;
154 buf = strdup(password);
155 return (buf);
156}
157
Michal Vaskoc3146782015-11-04 14:46:41 +0100158void netconf_callback_error_process(const char *UNUSED(tag),
159 const char *UNUSED(type),
160 const char *UNUSED(severity),
161 const char *UNUSED(apptag),
162 const char *UNUSED(path),
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200163 const char *message,
Michal Vaskoc3146782015-11-04 14:46:41 +0100164 const char *UNUSED(attribute),
165 const char *UNUSED(element),
166 const char *UNUSED(ns),
167 const char *UNUSED(sid))
Radek Krejcic11fd862012-07-26 12:41:21 +0200168{
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100169 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
Tomas Cejka442258e2014-04-01 18:17:18 +0200170 if (err_reply_p == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200171 ERROR("Error message was not allocated. %s", __func__);
Tomas Cejka442258e2014-04-01 18:17:18 +0200172 return;
173 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100174 json_object *err_reply = *err_reply_p;
175
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100176 json_object *array = NULL;
177 if (err_reply == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200178 ERROR("error calback: empty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100179 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100180 err_reply = json_object_new_object();
181 array = json_object_new_array();
182 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
183 json_object_object_add(err_reply, "errors", array);
184 if (message != NULL) {
185 json_object_array_add(array, json_object_new_string(message));
186 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100187 pthread_mutex_unlock(&json_lock);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100188 (*err_reply_p) = err_reply;
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100189 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200190 ERROR("error calback: nonempty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100191 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +0200192 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100193 if (message != NULL) {
194 json_object_array_add(array, json_object_new_string(message));
195 }
196 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100197 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100198 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100199 pthread_setspecific(err_reply_key, err_reply_p);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100200 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200201}
202
Tomas Cejka47387fd2013-06-10 20:37:46 +0200203/**
204 * should be used in locked area
205 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100206void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200207{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200208 json_object *json_obj = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +0200209 json_object *js_tmp = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100210 char *old_sid = NULL;
211 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200212 const char *cpbltstr;
213 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200214
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200215 if (s == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200216 ERROR("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200217 return;
218 }
219
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100220 pthread_mutex_lock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200221 if (s->hello_message != NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200222 ERROR("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200223 //json_object_put(s->hello_message);
Tomas Cejka09629492014-07-10 15:58:06 +0200224 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
225 j_old_sid = json_object_get_string(js_tmp);
226 if (j_old_sid != NULL) {
227 old_sid = strdup(j_old_sid);
228 }
229 json_object_put(s->hello_message);
230 json_object_put(js_tmp);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100231 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200232 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200233 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100234 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200235 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100236 if (old_sid != NULL) {
237 /* use previous sid */
238 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
239 free(old_sid);
Tomas Cejkaa3ffdba2014-03-27 15:12:21 +0100240 old_sid = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100241 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200242 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100243 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
244 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200245 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
246 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
247 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
248 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
249 cpblts = nc_session_get_cpblts (session);
250 if (cpblts != NULL) {
251 json_obj = json_object_new_array();
252 nc_cpblts_iter_start (cpblts);
253 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
254 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
255 }
256 json_object_object_add(s->hello_message, "capabilities", json_obj);
257 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100258 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200259 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200260 ERROR("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200261 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
262 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
263 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100264 DEBUG("Status info from hello message prepared");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100265 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200266
267}
268
Tomas Cejka442258e2014-04-01 18:17:18 +0200269void create_err_reply_p()
270{
271 json_object **err_reply = calloc(1, sizeof(json_object **));
272 if (err_reply == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200273 ERROR("Allocation of err_reply storage failed!");
Tomas Cejka442258e2014-04-01 18:17:18 +0200274 return;
275 }
276 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200277 ERROR("cannot set thread-specific value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200278 }
279}
280
281void clean_err_reply()
282{
283 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
284 if (err_reply != NULL) {
285 if (*err_reply != NULL) {
286 pthread_mutex_lock(&json_lock);
287 json_object_put(*err_reply);
288 pthread_mutex_unlock(&json_lock);
289 }
290 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200291 ERROR("Cannot set thread-specific hash value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200292 }
293 }
294}
295
296void free_err_reply()
297{
298 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
299 if (err_reply != NULL) {
300 if (*err_reply != NULL) {
301 pthread_mutex_lock(&json_lock);
302 json_object_put(*err_reply);
303 pthread_mutex_unlock(&json_lock);
304 }
305 free(err_reply);
306 err_reply = NULL;
307 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200308 ERROR("Cannot set thread-specific hash value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200309 }
310 }
311}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200312
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200313/**
314 * \defgroup netconf_operations NETCONF operations
315 * The list of NETCONF operations that mod_netconf supports.
316 * @{
317 */
318
319/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200320 * \brief Send RPC and wait for reply with timeout.
321 *
322 * \param[in] session libnetconf session
323 * \param[in] rpc prepared RPC message
324 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
325 * \param[out] reply reply from the server
326 * \return Value from nc_session_recv_reply() or NC_MSG_UNKNOWN when send_rpc() fails.
327 * On success, it returns NC_MSG_REPLY.
328 */
329NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc,
Tomas Cejkab34b7b12015-06-21 22:54:11 +0200330 int timeout, nc_reply **reply)
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200331{
332 const nc_msgid msgid = NULL;
333 NC_MSG_TYPE ret = NC_MSG_UNKNOWN;
334 msgid = nc_session_send_rpc(session, rpc);
335 if (msgid == NULL) {
336 return ret;
337 }
Tomas Cejka756d1292015-05-06 09:31:46 +0200338 do {
339 ret = nc_session_recv_reply(session, timeout, reply);
340 if (ret == NC_MSG_HELLO) {
341 ERROR("<hello> received instead reply, it will be lost.");
342 nc_reply_free(*reply);
343 }
344 if (ret == NC_MSG_WOULDBLOCK) {
345 ERROR("Timeout for receiving RPC reply expired.");
346 break;
347 }
348 } while (ret == NC_MSG_HELLO || ret == NC_MSG_NOTIFICATION);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200349 return ret;
350}
351
352/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200353 * \brief Connect to NETCONF server
354 *
355 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
356 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100357static const char *netconf_connect(const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200358{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200359 struct nc_session* session = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100360 struct session_with_mutex * locked_session, *last_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200361
362 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200363 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100364 DEBUG("prepare to connect %s@%s:%s", user, host, port);
David Kupka8e60a372012-09-04 09:15:20 +0200365 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100366 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200367
Radek Krejci469aab82012-07-22 18:42:20 +0200368 /* if connected successful, add session to the list */
369 if (session != NULL) {
Tomas Cejka73496bf2014-03-26 15:31:09 +0100370 if ((locked_session = calloc(1, sizeof(struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
David Kupka8e60a372012-09-04 09:15:20 +0200371 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100372 session = NULL;
373 free(locked_session);
374 locked_session = NULL;
Tomas Cejkacf44e522015-04-24 17:29:21 +0200375 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
Michal Vaskoc3146782015-11-04 14:46:41 +0100376 return 0;
David Kupka8e60a372012-09-04 09:15:20 +0200377 }
378 locked_session->session = session;
Michal Vaskoc3146782015-11-04 14:46:41 +0100379 locked_session->last_activity = time(NULL);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200380 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200381 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200382 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100383 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200384 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100385 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200386 if (pthread_rwlock_wrlock(&session_lock) != 0) {
David Kupka8e60a372012-09-04 09:15:20 +0200387 nc_session_free(session);
388 free (locked_session);
Tomas Cejkacf44e522015-04-24 17:29:21 +0200389 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Michal Vaskoc3146782015-11-04 14:46:41 +0100390 return 0;
David Kupka8e60a372012-09-04 09:15:20 +0200391 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200392 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100393 DEBUG("Add connection to the list");
Michal Vaskoc3146782015-11-04 14:46:41 +0100394 if (!netconf_sessions_list) {
395 netconf_sessions_list = locked_session;
396 } else {
397 for (last_session = netconf_sessions_list; last_session->next; last_session = last_session->next);
398 last_session->next = locked_session;
399 locked_session->prev = last_session;
400 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100401 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200402
Tomas Cejka47387fd2013-06-10 20:37:46 +0200403 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100404 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200405 pthread_mutex_lock(&locked_session->lock);
406
407 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100408 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200409 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200410 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200411 }
412
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200413 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100414 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200415
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100416 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200417 pthread_mutex_unlock(&locked_session->lock);
418
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100419 DEBUG("NETCONF session established");
Michal Vaskoc3146782015-11-04 14:46:41 +0100420 return nc_session_get_id(session);
Radek Krejci469aab82012-07-22 18:42:20 +0200421 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200422 ERROR("Connection could not be established");
Michal Vaskoc3146782015-11-04 14:46:41 +0100423 return 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200424 }
425
Radek Krejci469aab82012-07-22 18:42:20 +0200426}
427
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100428static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200429{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100430 DEBUG("lock private lock.");
431 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200432 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200433 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200434 }
435 locked_session->ntfc_subscribed = 0;
436 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100437 if (locked_session->session != NULL) {
438 nc_session_free(locked_session->session);
439 locked_session->session = NULL;
440 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100441 DEBUG("session closed.");
442 DEBUG("unlock private lock.");
443 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200444 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200445 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200446 }
447
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100448 DEBUG("unlock session lock.");
449 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200450 usleep(500000); /* let notification thread stop */
451
452 /* session shouldn't be used by now */
453 /** \todo free all notifications from queue */
Michal Vaskoc3146782015-11-04 14:46:41 +0100454 free(locked_session->notifications);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200455 pthread_mutex_destroy(&locked_session->lock);
456 if (locked_session->hello_message != NULL) {
457 /** \todo free hello_message */
458 //json_object_put(locked_session->hello_message);
459 locked_session->hello_message = NULL;
460 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200461 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200462 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200463 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100464 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200465 return (EXIT_SUCCESS);
466}
467
Michal Vaskoc3146782015-11-04 14:46:41 +0100468static int netconf_close(const char *session_id, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200469{
Michal Vaskoc3146782015-11-04 14:46:41 +0100470 struct session_with_mutex *locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200471
Michal Vaskoc3146782015-11-04 14:46:41 +0100472 DEBUG("Session to close: %s", session_id);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200473
David Kupka8e60a372012-09-04 09:15:20 +0200474 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100475 DEBUG("lock session lock.");
476 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200477 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200478 ERROR("Error while locking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100479 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200480 return EXIT_FAILURE;
481 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200482 /* remove session from the active sessions list -> nobody new can now work with session */
Michal Vaskoc3146782015-11-04 14:46:41 +0100483 for (locked_session = netconf_sessions_list;
484 strcmp(nc_session_get_id(locked_session->session), session_id);
485 locked_session = locked_session->next);
486
487 if (!locked_session) {
488 ERROR("Could not find the session \"%s\" to close.", session_id);
489 (*reply) = create_error("Internal: Error while finding a session.");
490 return EXIT_FAILURE;
491 }
492
493 if (!locked_session->prev) {
494 netconf_sessions_list = netconf_sessions_list->next;
495 netconf_sessions_list->prev = NULL;
496 } else {
497 locked_session->prev->next = locked_session->next;
498 if (locked_session->next) {
499 locked_session->next->prev = locked_session->prev;
500 }
501 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200502
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100503 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200504 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200505 ERROR("Error while unlocking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100506 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200507 }
508
509 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100510 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200511 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200512 ERROR("Unknown session to close");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100513 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200514 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200515 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100516 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200517}
518
Tomas Cejkac7929632013-10-24 19:25:15 +0200519/**
520 * Test reply message type and return error message.
521 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200522 * \param[in] session nc_session internal struct
Michal Vaskoc3146782015-11-04 14:46:41 +0100523 * \param[in] session_key session ID, 0 to disable disconnect on error
Tomas Cejkac7929632013-10-24 19:25:15 +0200524 * \param[in] msgt RPC-REPLY message type
525 * \param[out] data
526 * \return NULL on success
527 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100528json_object *netconf_test_reply(struct nc_session *session, const char *session_id, NC_MSG_TYPE msgt, nc_reply *reply, char **data)
Tomas Cejkac7929632013-10-24 19:25:15 +0200529{
530 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100531 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200532
533 /* process the result of the operation */
534 switch (msgt) {
535 case NC_MSG_UNKNOWN:
536 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200537 ERROR("mod_netconf: receiving rpc-reply failed");
Michal Vaskoc3146782015-11-04 14:46:41 +0100538 if (session_id) {
539 netconf_close(session_id, &err);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100540 }
541 if (err != NULL) {
542 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200543 }
544 return create_error("Internal: Receiving RPC-REPLY failed.");
545 }
546 case NC_MSG_NONE:
547 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100548 if (data != NULL) {
549 free(*data);
Tomas Cejka7b1e3bd2014-04-08 14:34:28 +0200550 (*data) = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100551 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200552 return NULL;
553 case NC_MSG_REPLY:
554 switch (replyt = nc_reply_get_type(reply)) {
555 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100556 if ((data != NULL) && (*data != NULL)) {
557 free(*data);
558 (*data) = NULL;
559 }
560 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200561 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100562 if (((*data) = nc_reply_get_data(reply)) == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200563 ERROR("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200564 return create_error("Internal: No data from reply received.");
565 } else {
566 return NULL;
567 }
568 break;
569 case NC_REPLY_ERROR:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200570 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100571 if (data != NULL) {
572 free(*data);
573 (*data) = NULL;
574 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200575 return create_error(nc_reply_get_errormsg(reply));
576 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200577 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100578 if (data != NULL) {
579 free(*data);
580 (*data) = NULL;
581 }
Tomas Cejka60885252014-03-26 15:45:47 +0100582 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200583 }
584 break;
585 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200586 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100587 if (data != NULL) {
588 free(*data);
589 (*data) = NULL;
590 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200591 return create_error("Internal: Unexpected RPC-REPLY message type.");
592 }
593}
594
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100595json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200596{
597 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200598 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200599
600 /* check requests */
601 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200602 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200603 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200604 }
605
606 if (session != NULL) {
607 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200608 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200609 /* process the result of the operation */
Michal Vaskoc3146782015-11-04 14:46:41 +0100610 return netconf_test_reply(session, 0, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200611 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200612 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200613 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200614 }
615}
616
Tomas Cejkac7929632013-10-24 19:25:15 +0200617/**
618 * Perform RPC method that returns data.
619 *
Michal Vaskoc3146782015-11-04 14:46:41 +0100620 * \param[in] session_id session identifier
Tomas Cejkac7929632013-10-24 19:25:15 +0200621 * \param[in] rpc RPC message to perform
622 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
623 * \return NULL on success, json object with error otherwise
624 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100625static json_object *netconf_op(const char *session_id, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200626{
627 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200628 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200629 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200630 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100631 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100632 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200633
Radek Krejci8e4632a2012-07-26 13:40:34 +0200634 /* check requests */
635 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200636 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200637 res = create_error("Internal: RPC could not be created.");
638 data = NULL;
639 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200640 }
641
David Kupka8e60a372012-09-04 09:15:20 +0200642 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100643 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200644 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200645 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200646 res = create_error("Internal: Lock failed.");
647 data = NULL;
648 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200649 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200650 /* get session where send the RPC */
Michal Vaskoc3146782015-11-04 14:46:41 +0100651 for (locked_session = netconf_sessions_list;
652 strcmp(nc_session_get_id(locked_session->session), session_id);
653 locked_session = locked_session->next);
David Kupka8e60a372012-09-04 09:15:20 +0200654 if (locked_session != NULL) {
655 session = locked_session->session;
656 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200657 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200658 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100659 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200660 if (pthread_mutex_lock(&locked_session->lock) != 0) {
661 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100662 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200663 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200664
Tomas Cejkacf44e522015-04-24 17:29:21 +0200665 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200666 res = create_error("Internal: Could not unlock.");
667 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200668 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200669 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200670 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100671 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200672 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200673
Tomas Cejkacf44e522015-04-24 17:29:21 +0200674 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200675 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200676 }
677
Michal Vaskoc3146782015-11-04 14:46:41 +0100678 locked_session->last_activity = time(NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200679
Radek Krejci035bf4e2012-07-25 10:59:09 +0200680 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200681 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Radek Krejcia332b692012-11-12 16:15:54 +0100682
David Kupka8e60a372012-09-04 09:15:20 +0200683 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100684 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200685 pthread_mutex_unlock(&locked_session->lock);
686 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200687
Michal Vaskoc3146782015-11-04 14:46:41 +0100688 res = netconf_test_reply(session, session_id, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200689 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100690 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100691 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200692 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200693 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100694 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200695 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200696 res = create_error("Unknown session to process.");
697 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200698 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200699finished:
700 nc_reply_free(reply);
701 if (received_data != NULL) {
702 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100703 } else {
704 if (data != NULL) {
705 free(data);
706 data = NULL;
707 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200708 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200709 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200710}
711
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100712static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200713{
714 nc_rpc* rpc;
715 struct nc_filter *f = NULL;
716 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200717 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200718
719 /* create filter if set */
720 if (filter != NULL) {
721 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
722 }
723
724 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100725 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200726 nc_filter_free(f);
727 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200728 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200729 return (NULL);
730 }
731
Tomas Cejka94674662013-09-13 15:55:24 +0200732 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100733#ifdef HAVE_WITHDEFAULTS_TAGGED
Michal Vaskoc3146782015-11-04 14:46:41 +0100734 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED))
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100735#else
Michal Vaskoc3146782015-11-04 14:46:41 +0100736 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET))
737 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL))
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100738#endif
Michal Vaskoc3146782015-11-04 14:46:41 +0100739 {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200740 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200741 }
742
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100743 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200744 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100745 if (res != NULL) {
746 (*err) = res;
747 } else {
748 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200749 }
750
Radek Krejci8e4632a2012-07-26 13:40:34 +0200751 return (data);
752}
753
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100754static char* netconf_getschema(const char* session_key, const char* identifier, const char* version, const char* format, json_object **err)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100755{
756 nc_rpc* rpc;
757 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200758 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100759
760 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100761 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100762 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200763 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100764 return (NULL);
765 }
766
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100767 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100768 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100769 if (res != NULL) {
770 (*err) = res;
771 } else {
772 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200773 }
774
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100775 return (data);
776}
777
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100778static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200779{
780 nc_rpc* rpc;
781 struct nc_filter *f = NULL;
782 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200783 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200784
785 /* create filter if set */
786 if (filter != NULL) {
787 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
788 }
789
790 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100791 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200792 nc_filter_free(f);
793 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200794 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200795 return (NULL);
796 }
797
Tomas Cejka94674662013-09-13 15:55:24 +0200798 /* tell server to show all elements even if they have default values */
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200799 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
800 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
801 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200802 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200803 }
804
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100805 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200806 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200807 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100808 (*err) = res;
809 } else {
810 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200811 }
812
Radek Krejci8e4632a2012-07-26 13:40:34 +0200813 return (data);
814}
815
Tomas Cejkab4d05872014-02-14 22:44:38 +0100816static json_object *netconf_copyconfig(const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *uri_src, const char *uri_trg)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200817{
818 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200819 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200820
821 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100822 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100823 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100824 /* config, url */
825 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100826 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100827 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100828 rpc = nc_rpc_copyconfig(source, target, config);
829 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100830 } else if (source == NC_DATASTORE_URL) {
831 if (target == NC_DATASTORE_URL) {
832 /* url, url */
833 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
834 } else {
835 /* url, datastore */
836 rpc = nc_rpc_copyconfig(source, target, uri_src);
837 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100838 } else {
839 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100840 /* datastore, url */
841 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100842 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100843 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100844 rpc = nc_rpc_copyconfig(source, target);
845 }
846 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200847 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200848 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200849 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200850 }
851
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100852 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200853 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200854
855 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200856}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200857
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100858static json_object *netconf_editconfig(const char* session_key, NC_DATASTORE source, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char* config_or_url)
Radek Krejci62ab34b2012-07-26 13:42:05 +0200859{
860 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200861 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200862
863 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100864 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200865 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200866 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200867 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200868 }
869
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100870 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200871 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200872
873 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200874}
875
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100876static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200877{
878 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200879 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200880
881 /* create requests */
882 rpc = nc_rpc_killsession(sid);
883 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200884 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200885 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200886 }
887
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100888 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200889 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200890 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200891}
892
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100893static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200894{
895 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200896 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200897
898 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200899 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200900 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200901 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200902 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200903 }
904
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100905 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200906 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200907 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200908}
909
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100910static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200911{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200912 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200913 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200914 if (target != NC_DATASTORE_URL) {
915 rpc = nc_rpc_deleteconfig(target);
916 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200917 rpc = nc_rpc_deleteconfig(target, url);
918 }
919 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200920 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200921 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200922 }
923
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100924 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200925 nc_rpc_free (rpc);
926 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200927}
928
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100929static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200930{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100931 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200932}
933
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100934static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200935{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100936 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200937}
938
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100939static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200940{
Tomas Cejka00635972013-06-03 15:10:52 +0200941 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200942 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200943
944 /* create requests */
945 rpc = nc_rpc_generic(content);
946 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200947 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200948 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200949 }
950
Radek Krejcia332b692012-11-12 16:15:54 +0100951 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200952 // TODO ?free(*data);
953 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100954 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200955
956 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100957 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200958 nc_rpc_free (rpc);
959 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200960}
961
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200962/**
963 * @}
964 *//* netconf_operations */
965
Radek Krejci7338bde2012-08-10 12:57:30 +0200966void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200967{
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200968#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +0200969 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
970
971#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200972 break;
973
Radek Krejci7338bde2012-08-10 12:57:30 +0200974 switch (level) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200975 FOREACH(CASE);
976 case NC_VERB_VERBOSE:
977 case NC_VERB_DEBUG:
978 DEBUG("DEBUG: %s", msg);
979 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200980 }
981 if (level == NC_VERB_ERROR) {
982 /* return global error */
983 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
984 NULL /* severity */, NULL /* apptag */,
985 NULL /* path */, msg, NULL /* attribute */,
986 NULL /* element */, NULL /* ns */, NULL /* sid */);
Radek Krejci7338bde2012-08-10 12:57:30 +0200987 }
Radek Krejci469aab82012-07-22 18:42:20 +0200988}
989
Tomas Cejka64b87482013-06-03 16:30:53 +0200990/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200991 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200992 * Caller should free message memory.
993 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200994 * \return pointer to message
995 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100996char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200997{
998 /* read json in chunked framing */
999 unsigned int buffer_size = 0;
1000 ssize_t buffer_len = 0;
1001 char *buffer = NULL;
1002 char c;
1003 ssize_t ret;
1004 int i, chunk_len;
1005 char chunk_len_str[12];
1006
1007 while (1) {
1008 /* read chunk length */
1009 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
1010 if (buffer != NULL) {
1011 free (buffer);
1012 buffer = NULL;
1013 }
1014 break;
1015 }
1016 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
1017 if (buffer != NULL) {
1018 free (buffer);
1019 buffer = NULL;
1020 }
1021 break;
1022 }
1023 i=0;
1024 memset (chunk_len_str, 0, 12);
1025 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1026 if (i==0 && c == '#') {
1027 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1028 /* end but invalid */
1029 if (buffer != NULL) {
1030 free (buffer);
1031 buffer = NULL;
1032 }
1033 }
1034 /* end of message, double-loop break */
1035 goto msg_complete;
1036 }
1037 chunk_len_str[i++] = c;
1038 if (i==11) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001039 ERROR("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001040 break;
1041 }
1042 }
1043 if (c != '\n') {
1044 if (buffer != NULL) {
1045 free (buffer);
1046 buffer = NULL;
1047 }
1048 break;
1049 }
1050 chunk_len_str[i] = 0;
1051 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1052 if (buffer != NULL) {
1053 free (buffer);
1054 buffer = NULL;
1055 }
1056 break;
1057 }
1058 buffer_size += chunk_len+1;
1059 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001060 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001061 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1062 if (buffer != NULL) {
1063 free (buffer);
1064 buffer = NULL;
1065 }
1066 break;
1067 }
1068 buffer_len += ret;
1069 }
1070msg_complete:
1071 return buffer;
1072}
1073
Tomas Cejkad5b53772013-06-08 23:01:07 +02001074NC_DATASTORE parse_datastore(const char *ds)
1075{
1076 if (strcmp(ds, "running") == 0) {
1077 return NC_DATASTORE_RUNNING;
1078 } else if (strcmp(ds, "startup") == 0) {
1079 return NC_DATASTORE_STARTUP;
1080 } else if (strcmp(ds, "candidate") == 0) {
1081 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001082 } else if (strcmp(ds, "url") == 0) {
1083 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001084 } else if (strcmp(ds, "config") == 0) {
1085 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001086 }
1087 return -1;
1088}
1089
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001090NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1091{
1092 if (strcmp(t, "notset") == 0) {
1093 return NC_EDIT_TESTOPT_NOTSET;
1094 } else if (strcmp(t, "testset") == 0) {
1095 return NC_EDIT_TESTOPT_TESTSET;
1096 } else if (strcmp(t, "set") == 0) {
1097 return NC_EDIT_TESTOPT_SET;
1098 } else if (strcmp(t, "test") == 0) {
1099 return NC_EDIT_TESTOPT_TEST;
1100 }
1101 return NC_EDIT_TESTOPT_ERROR;
1102}
1103
Tomas Cejkad5b53772013-06-08 23:01:07 +02001104json_object *create_error(const char *errmess)
1105{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001106 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001107 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001108 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001109 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001110 json_object_array_add(array, json_object_new_string(errmess));
1111 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001112 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001113 return reply;
1114
1115}
1116
1117json_object *create_data(const char *data)
1118{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001119 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001120 json_object *reply = json_object_new_object();
1121 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1122 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001123 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001124 return reply;
1125}
1126
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001127json_object *create_ok()
1128{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001129 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001130 json_object *reply = json_object_new_object();
1131 reply = json_object_new_object();
1132 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001133 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001134 return reply;
1135}
1136
Tomas Cejka09629492014-07-10 15:58:06 +02001137char *get_param_string(json_object *data, const char *name)
1138{
1139 json_object *js_tmp = NULL;
1140 char *res = NULL;
1141 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
1142 res = strdup(json_object_get_string(js_tmp));
1143 json_object_put(js_tmp);
1144 }
1145 return res;
1146}
1147
Michal Vaskoc3146782015-11-04 14:46:41 +01001148json_object *handle_op_connect(json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001149{
Tomas Cejka09629492014-07-10 15:58:06 +02001150 char *host = NULL;
1151 char *port = NULL;
1152 char *user = NULL;
1153 char *pass = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001154 json_object *capabilities = NULL;
1155 json_object *reply = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +01001156 const char *session_id = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001157 struct nc_cpblts* cpblts = NULL;
1158 unsigned int len, i;
1159
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001160 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001161 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001162
Tomas Cejka09629492014-07-10 15:58:06 +02001163 host = get_param_string(request, "host");
1164 port = get_param_string(request, "port");
1165 user = get_param_string(request, "user");
1166 pass = get_param_string(request, "pass");
1167
1168 if (json_object_object_get_ex(request, "capabilities", &capabilities) == TRUE) {
1169 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1170 cpblts = nc_cpblts_new(NULL);
1171 for (i=0; i<len; i++) {
1172 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1173 }
1174 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001175 ERROR("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001176 }
Tomas Cejka09629492014-07-10 15:58:06 +02001177 json_object_put(capabilities);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001178 }
Tomas Cejka09629492014-07-10 15:58:06 +02001179
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001180 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001181
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001182 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001183 if ((host == NULL) || (user == NULL)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001184 ERROR("Cannot connect - insufficient input.");
Michal Vaskoc3146782015-11-04 14:46:41 +01001185 session_id = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001186 } else {
Michal Vaskoc3146782015-11-04 14:46:41 +01001187 session_id = netconf_connect(host, port, user, pass, cpblts);
1188 DEBUG("SID: %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001189 }
1190 if (cpblts != NULL) {
1191 nc_cpblts_free(cpblts);
1192 }
1193
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001194 GETSPEC_ERR_REPLY
1195
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001196 pthread_mutex_lock(&json_lock);
Michal Vaskoc3146782015-11-04 14:46:41 +01001197 if (session_id == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001198 /* negative reply */
1199 if (err_reply == NULL) {
1200 reply = json_object_new_object();
1201 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1202 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkacf44e522015-04-24 17:29:21 +02001203 ERROR("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001204 } else {
1205 /* use filled err_reply from libnetconf's callback */
1206 reply = err_reply;
Tomas Cejkacf44e522015-04-24 17:29:21 +02001207 ERROR("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001208 }
1209 } else {
1210 /* positive reply */
1211 reply = json_object_new_object();
1212 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Michal Vaskoc3146782015-11-04 14:46:41 +01001213 json_object_object_add(reply, "session", json_object_new_string(session_id));
Tomas Cejkad5b53772013-06-08 23:01:07 +02001214 }
Tomas Cejka09629492014-07-10 15:58:06 +02001215 memset(pass, 0, strlen(pass));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001216 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001217 CHECK_AND_FREE(host);
1218 CHECK_AND_FREE(user);
1219 CHECK_AND_FREE(port);
1220 CHECK_AND_FREE(pass);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001221 return reply;
1222}
1223
Michal Vaskoc3146782015-11-04 14:46:41 +01001224json_object *handle_op_get(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001225{
Tomas Cejka09629492014-07-10 15:58:06 +02001226 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001227 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001228 json_object *reply = NULL;
1229
Michal Vaskoc3146782015-11-04 14:46:41 +01001230 DEBUG("Request: get (session %s)", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001231
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001232 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001233 filter = get_param_string(request, "filter");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001234 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001235
Michal Vaskoc3146782015-11-04 14:46:41 +01001236 if ((data = netconf_get(session_id, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001237 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001238 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001239 reply = create_data(data);
1240 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001241 }
1242 return reply;
1243}
1244
Michal Vaskoc3146782015-11-04 14:46:41 +01001245json_object *handle_op_getconfig(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001246{
1247 NC_DATASTORE ds_type_s = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001248 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001249 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001250 char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001251 json_object *reply = NULL;
1252
Michal Vaskoc3146782015-11-04 14:46:41 +01001253 DEBUG("Request: get-config (session %s)", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001255 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001256 filter = get_param_string(request, "filter");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257
Tomas Cejka09629492014-07-10 15:58:06 +02001258 source = get_param_string(request, "source");
1259 if (source != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001260 ds_type_s = parse_datastore(source);
1261 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001262 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001263
Michal Vaskoc3146782015-11-04 14:46:41 +01001264 if ((int)ds_type_s == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001265 reply = create_error("Invalid source repository type requested.");
1266 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001267 }
1268
Michal Vaskoc3146782015-11-04 14:46:41 +01001269 if ((data = netconf_getconfig(session_id, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001270 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001271 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001272 reply = create_data(data);
1273 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001274 }
Tomas Cejka09629492014-07-10 15:58:06 +02001275finalize:
1276 CHECK_AND_FREE(filter);
1277 CHECK_AND_FREE(source);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001278 return reply;
1279}
1280
Michal Vaskoc3146782015-11-04 14:46:41 +01001281json_object *handle_op_getschema(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001282{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001283 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001284 char *identifier = NULL;
1285 char *version = NULL;
1286 char *format = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001287 json_object *reply = NULL;
1288
Michal Vaskoc3146782015-11-04 14:46:41 +01001289 DEBUG("Request: get-schema (session %s)", session_id);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001290 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001291 identifier = get_param_string(request, "identifier");
1292 version = get_param_string(request, "version");
1293 format = get_param_string(request, "format");
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001294 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001295
Tomas Cejkad5b53772013-06-08 23:01:07 +02001296 if (identifier == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001297 reply = create_error("No identifier for get-schema supplied.");
1298 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001299 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001300
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001301 DEBUG("get-schema(version: %s, format: %s)", version, format);
Michal Vaskoc3146782015-11-04 14:46:41 +01001302 if ((data = netconf_getschema(session_id, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001303 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001304 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001305 reply = create_data(data);
1306 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001307 }
Tomas Cejka09629492014-07-10 15:58:06 +02001308finalize:
1309 CHECK_AND_FREE(identifier);
1310 CHECK_AND_FREE(version);
1311 CHECK_AND_FREE(format);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001312 return reply;
1313}
1314
Michal Vaskoc3146782015-11-04 14:46:41 +01001315json_object *handle_op_editconfig(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001316{
1317 NC_DATASTORE ds_type_s = -1;
1318 NC_DATASTORE ds_type_t = -1;
1319 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1320 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001321 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejka09629492014-07-10 15:58:06 +02001322 char *defop = NULL;
1323 char *erropt = NULL;
1324 char *config = NULL;
1325 char *source = NULL;
1326 char *target = NULL;
1327 char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001328 json_object *reply = NULL;
1329
Michal Vaskoc3146782015-11-04 14:46:41 +01001330 DEBUG("Request: edit-config (session %s)", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001331
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001332 pthread_mutex_lock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001333 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001334 defop = get_param_string(request, "default-operation");
1335 erropt = get_param_string(request, "error-option");
1336 target = get_param_string(request, "target");
1337 source = get_param_string(request, "source");
1338 config = get_param_string(request, "config");
1339 testopt = get_param_string(request, "test-option");
1340 pthread_mutex_unlock(&json_lock);
1341
1342 if (target != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001343 ds_type_t = parse_datastore(target);
1344 }
Tomas Cejka09629492014-07-10 15:58:06 +02001345 if (source != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001346 ds_type_s = parse_datastore(source);
1347 } else {
1348 /* source is optional, default value is config */
1349 ds_type_s = NC_DATASTORE_CONFIG;
1350 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001351
Tomas Cejkad5b53772013-06-08 23:01:07 +02001352 if (defop != NULL) {
1353 if (strcmp(defop, "merge") == 0) {
1354 defop_type = NC_EDIT_DEFOP_MERGE;
1355 } else if (strcmp(defop, "replace") == 0) {
1356 defop_type = NC_EDIT_DEFOP_REPLACE;
1357 } else if (strcmp(defop, "none") == 0) {
1358 defop_type = NC_EDIT_DEFOP_NONE;
1359 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001360 reply = create_error("Invalid default-operation parameter.");
1361 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001362 }
1363 } else {
1364 defop_type = NC_EDIT_DEFOP_NOTSET;
1365 }
1366
Tomas Cejkad5b53772013-06-08 23:01:07 +02001367 if (erropt != NULL) {
1368 if (strcmp(erropt, "continue-on-error") == 0) {
1369 erropt_type = NC_EDIT_ERROPT_CONT;
1370 } else if (strcmp(erropt, "stop-on-error") == 0) {
1371 erropt_type = NC_EDIT_ERROPT_STOP;
1372 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1373 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1374 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001375 reply = create_error("Invalid error-option parameter.");
1376 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001377 }
1378 } else {
1379 erropt_type = 0;
1380 }
1381
Michal Vaskoc3146782015-11-04 14:46:41 +01001382 if ((int)ds_type_t == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001383 reply = create_error("Invalid target repository type requested.");
1384 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001385 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001386 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001387 if (config == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001388 reply = create_error("Invalid config data parameter.");
1389 goto finalize;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001390 }
1391 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001392 if (config == NULL) {
1393 config = "";
1394 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001395 }
1396
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001397 if (testopt != NULL) {
1398 testopt_type = parse_testopt(testopt);
1399 } else {
1400 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1401 }
1402
Michal Vaskoc3146782015-11-04 14:46:41 +01001403 reply = netconf_editconfig(session_id, ds_type_s, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001404
1405 CHECK_ERR_SET_REPLY
Tomas Cejka09629492014-07-10 15:58:06 +02001406finalize:
1407 CHECK_AND_FREE(defop);
1408 CHECK_AND_FREE(erropt);
1409 CHECK_AND_FREE(config);
1410 CHECK_AND_FREE(source);
1411 CHECK_AND_FREE(target);
1412 CHECK_AND_FREE(testopt);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001413 return reply;
1414}
1415
Michal Vaskoc3146782015-11-04 14:46:41 +01001416json_object *handle_op_copyconfig(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001417{
1418 NC_DATASTORE ds_type_s = -1;
1419 NC_DATASTORE ds_type_t = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001420 char *config = NULL;
1421 char *target = NULL;
1422 char *source = NULL;
1423 char *uri_src = NULL;
1424 char *uri_trg = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001425
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426 json_object *reply = NULL;
1427
Michal Vaskoc3146782015-11-04 14:46:41 +01001428 DEBUG("Request: copy-config (session %s)", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001429
1430 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001431 pthread_mutex_lock(&json_lock);
1432 target = get_param_string(request, "target");
1433 source = get_param_string(request, "source");
1434 config = get_param_string(request, "config");
1435 uri_src = get_param_string(request, "uri-source");
1436 uri_trg = get_param_string(request, "uri-target");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001437 pthread_mutex_unlock(&json_lock);
1438
Tomas Cejka09629492014-07-10 15:58:06 +02001439 if (target != NULL) {
1440 ds_type_t = parse_datastore(target);
1441 }
1442 if (source != NULL) {
1443 ds_type_s = parse_datastore(source);
1444 } else {
1445 /* source == NULL *//* no explicit source specified -> use config data */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001446 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejka09629492014-07-10 15:58:06 +02001447 }
Michal Vaskoc3146782015-11-04 14:46:41 +01001448 if ((int)ds_type_s == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449 /* source datastore specified, but it is invalid */
Tomas Cejka09629492014-07-10 15:58:06 +02001450 reply = create_error("Invalid source repository type requested.");
1451 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001452 }
1453
Michal Vaskoc3146782015-11-04 14:46:41 +01001454 if ((int)ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001455 /* invalid target datastore specified */
Tomas Cejka09629492014-07-10 15:58:06 +02001456 reply = create_error("Invalid target repository type requested.");
1457 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001458 }
1459
Tomas Cejka55c6df52014-02-20 12:59:33 +01001460 /* source can be missing when config is given */
1461 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001462 reply = create_error("invalid input parameters - source and config is required.");
Tomas Cejka09629492014-07-10 15:58:06 +02001463 goto finalize;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001464 }
1465
1466 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001467 if (uri_src == NULL) {
1468 uri_src = "";
1469 }
1470 }
1471 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001472 if (uri_trg == NULL) {
1473 uri_trg = "";
1474 }
1475 }
Michal Vaskoc3146782015-11-04 14:46:41 +01001476 reply = netconf_copyconfig(session_id, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001477
1478 CHECK_ERR_SET_REPLY
1479
Tomas Cejka09629492014-07-10 15:58:06 +02001480finalize:
1481 CHECK_AND_FREE(config);
1482 CHECK_AND_FREE(target);
1483 CHECK_AND_FREE(source);
1484 CHECK_AND_FREE(uri_src);
1485 CHECK_AND_FREE(uri_trg);
1486
Tomas Cejkad5b53772013-06-08 23:01:07 +02001487 return reply;
1488}
1489
Michal Vaskoc3146782015-11-04 14:46:41 +01001490json_object *handle_op_generic(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001491{
1492 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001493 char *config = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001494 char *data = NULL;
1495
Michal Vaskoc3146782015-11-04 14:46:41 +01001496 DEBUG("Request: generic request for session %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001497
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001498 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001499 config = get_param_string(request, "content");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001500 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001501
Michal Vaskoc3146782015-11-04 14:46:41 +01001502 reply = netconf_generic(session_id, config, &data);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001503 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001504 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001505 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001506 /* use filled err_reply from libnetconf's callback */
1507 reply = err_reply;
1508 }
1509 } else {
1510 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001511 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001512 reply = json_object_new_object();
1513 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001514 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001515 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001516 reply = create_data(data);
1517 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001518 }
1519 }
Tomas Cejka09629492014-07-10 15:58:06 +02001520 CHECK_AND_FREE(config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001521 return reply;
1522}
1523
Michal Vaskoc3146782015-11-04 14:46:41 +01001524json_object *handle_op_disconnect(json_object *UNUSED(request), const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001525{
1526 json_object *reply = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +01001527 DEBUG("Request: Disconnect session %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001528
Michal Vaskoc3146782015-11-04 14:46:41 +01001529 if (netconf_close(session_id, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001530 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001531 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001532 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001533 }
1534 return reply;
1535}
1536
Michal Vaskoc3146782015-11-04 14:46:41 +01001537json_object *handle_op_kill(json_object *request, const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001538{
1539 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001540 char *sid = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001541
Michal Vaskoc3146782015-11-04 14:46:41 +01001542 DEBUG("Request: kill-session, session %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001543
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001544 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001545 sid = get_param_string(request, "session-id");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001546 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001547
1548 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001549 reply = create_error("Missing session-id parameter.");
1550 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001551 }
1552
Michal Vaskoc3146782015-11-04 14:46:41 +01001553 reply = netconf_killsession(session_id, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001554
1555 CHECK_ERR_SET_REPLY
1556
Tomas Cejka09629492014-07-10 15:58:06 +02001557finalize:
1558 CHECK_AND_FREE(sid);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001559 return reply;
1560}
1561
Michal Vaskoc3146782015-11-04 14:46:41 +01001562json_object *handle_op_reloadhello(json_object *UNUSED(request), const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001563{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001564 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001565 struct session_with_mutex * locked_session = NULL;
1566 json_object *reply = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +01001567 DEBUG("Request: get info about session %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001568
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001569 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001570 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001571 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001572 return NULL;
1573 }
1574
Michal Vaskoc3146782015-11-04 14:46:41 +01001575 for (locked_session = netconf_sessions_list;
1576 strcmp(nc_session_get_id(locked_session->session), session_id);
1577 locked_session = locked_session->next);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001578 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001579 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001580 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001581 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001582 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001583 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001584 prepare_status_message(locked_session, temp_session);
1585 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001586 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001587 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001588 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001589 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001590 reply = create_error("Reload was unsuccessful, connection failed.");
1591 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001592 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001593 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001594 DEBUG("UNLOCK wrlock %s", __func__);
1595 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001596 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka93b0e492014-03-26 15:47:45 +01001597 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001598 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001599 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001600 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001601 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001602 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001603 reply = create_error("Invalid session identifier.");
1604 }
1605
1606 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1607 reply = locked_session->hello_message;
1608 }
1609 return reply;
1610}
1611
Michal Vaskoc3146782015-11-04 14:46:41 +01001612json_object *handle_op_info(json_object *UNUSED(request), const char *session_id)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001613{
1614 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001615 struct session_with_mutex * locked_session = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +01001616 DEBUG("Request: get info about session %s", session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001617
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001618 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001619 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001620 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001621 }
1622
Michal Vaskoc3146782015-11-04 14:46:41 +01001623 for (locked_session = netconf_sessions_list;
1624 strcmp(nc_session_get_id(locked_session->session), session_id);
1625 locked_session = locked_session->next);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001626 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001627 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001628 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001629 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001630 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001631 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001632 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001633 if (locked_session->hello_message != NULL) {
1634 reply = locked_session->hello_message;
1635 } else {
1636 reply = create_error("Invalid session identifier.");
1637 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001638 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001639 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001640 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001641 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001642 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001643 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001644 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001645 reply = create_error("Invalid session identifier.");
1646 }
1647
Tomas Cejka47387fd2013-06-10 20:37:46 +02001648
Tomas Cejkad5b53772013-06-08 23:01:07 +02001649 return reply;
1650}
1651
Tomas Cejka6b886e02013-07-05 09:53:17 +02001652void notification_history(time_t eventtime, const char *content)
1653{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001654 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1655 if (notif_history_array == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001656 ERROR("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001657 return;
1658 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001659 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001660 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001661 json_object *notif = json_object_new_object();
1662 if (notif == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001663 ERROR("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001664 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001665 }
1666 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1667 json_object_object_add(notif, "content", json_object_new_string(content));
1668 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001669failed:
1670 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001671}
1672
Michal Vaskoc3146782015-11-04 14:46:41 +01001673json_object *handle_op_ntfgethistory(json_object *request, const char *session_id)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001674{
1675 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001676 json_object *js_tmp = NULL;
1677 char *sid = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001678 struct session_with_mutex *locked_session = NULL;
1679 struct nc_session *temp_session = NULL;
1680 nc_rpc *rpc = NULL;
1681 time_t start = 0;
1682 time_t stop = 0;
Tomas Cejka09629492014-07-10 15:58:06 +02001683 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001684
Michal Vaskoc3146782015-11-04 14:46:41 +01001685 DEBUG("Request: get notification history, session %s", session_id);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001686
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001687 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001688 sid = get_param_string(request, "session");
1689
1690 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
1691 from = json_object_get_int64(js_tmp);
1692 json_object_put(js_tmp);
1693 }
1694 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
1695 to = json_object_get_int64(js_tmp);
1696 json_object_put(js_tmp);
1697 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001698 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001699
1700 start = time(NULL) + from;
1701 stop = time(NULL) + to;
1702
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001703 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001704
1705 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001706 reply = create_error("Missing session parameter.");
1707 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001708 }
1709
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001710 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001711 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001712 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka09629492014-07-10 15:58:06 +02001713 reply = create_error("Internal lock failed.");
1714 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001715 }
1716
Michal Vaskoc3146782015-11-04 14:46:41 +01001717 for (locked_session = netconf_sessions_list;
1718 strcmp(nc_session_get_id(locked_session->session), session_id);
1719 locked_session = locked_session->next);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001720 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001721 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001722 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001723 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001724 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001725 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001726 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001727 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001728 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1729 if (temp_session != NULL) {
1730 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1731 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001732 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001733 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001734 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001735 reply = create_error("notifications: creating an rpc request failed.");
1736 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001737 }
1738
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001739 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001740 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001741 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001742 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001743 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001744 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001745 DEBUG("Subscription RPC failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001746 reply = res;
1747 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001748 }
1749 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1750
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001751 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001752 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001753 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001754 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001755 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001756 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001757 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001758 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001759 ERROR("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001760 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001761
1762 ncntf_dispatch_receive(temp_session, notification_history);
1763
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001764 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001765 reply = json_object_new_object();
1766 json_object_object_add(reply, "notifications", notif_history_array);
1767 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001768 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001769
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001770 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001771 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001772 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001773 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001774 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001775 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001776 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001777 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001778 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001779 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1780 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001781 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001782 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001783 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001784 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001785 }
1786 reply = create_error("Invalid session identifier.");
1787 }
1788
Tomas Cejka09629492014-07-10 15:58:06 +02001789finalize:
1790 CHECK_AND_FREE(sid);
Tomas Cejka4003a702013-10-01 00:02:45 +02001791 return reply;
1792}
1793
Michal Vaskoc3146782015-11-04 14:46:41 +01001794json_object *handle_op_validate(json_object *request, const char *session_id)
Tomas Cejka4003a702013-10-01 00:02:45 +02001795{
1796 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001797 char *sid = NULL;
1798 char *target = NULL;
1799 char *url = NULL;
Tomas Cejka4003a702013-10-01 00:02:45 +02001800 nc_rpc *rpc = NULL;
1801 NC_DATASTORE target_ds;
1802
Michal Vaskoc3146782015-11-04 14:46:41 +01001803 DEBUG("Request: validate datastore, session %s", session_id);
Tomas Cejka4003a702013-10-01 00:02:45 +02001804
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001805 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001806 sid = get_param_string(request, "session");
1807 target = get_param_string(request, "target");
1808 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001809 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001810
1811
1812 if ((sid == NULL) || (target == NULL)) {
Tomas Cejka09629492014-07-10 15:58:06 +02001813 reply = create_error("Missing session parameter.");
1814 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001815 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001816
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001817 /* validation */
1818 target_ds = parse_datastore(target);
1819 if (target_ds == NC_DATASTORE_URL) {
1820 if (url != NULL) {
1821 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001822 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001823 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001824 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001825 rpc = nc_rpc_validate(target_ds);
1826 }
1827 if (rpc == NULL) {
1828 DEBUG("mod_netconf: creating rpc request failed");
1829 reply = create_error("Creation of RPC request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001830 goto finalize;
Tomas Cejka4003a702013-10-01 00:02:45 +02001831 }
1832
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001833 DEBUG("Request: validate datastore");
Michal Vaskoc3146782015-11-04 14:46:41 +01001834 if ((reply = netconf_op(session_id, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001835
1836 CHECK_ERR_SET_REPLY
1837
1838 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001839 DEBUG("Request: validation ok.");
1840 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001841 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001842 }
1843 nc_rpc_free (rpc);
Tomas Cejka09629492014-07-10 15:58:06 +02001844finalize:
1845 CHECK_AND_FREE(sid);
1846 CHECK_AND_FREE(target);
1847 CHECK_AND_FREE(url);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001848 return reply;
1849}
1850
David Kupka8e60a372012-09-04 09:15:20 +02001851void * thread_routine (void * arg)
1852{
1853 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001854 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001855 json_object *request = NULL;
1856 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001857 json_object *js_tmp = NULL;
1858 int operation = (-1);
1859 NC_DATASTORE ds_type_t = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001860 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001861 const char *msgtext;
Michal Vaskoc3146782015-11-04 14:46:41 +01001862 char *session_id = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001863 char *target = NULL;
1864 char *url = NULL;
Tomas Cejka64b87482013-06-03 16:30:53 +02001865 char *chunked_out_msg = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001866 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001867 int client = ((struct pass_to_thread*)arg)->client;
1868
Tomas Cejka00635972013-06-03 15:10:52 +02001869 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001870
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001871 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001872 create_err_reply_p();
1873
David Kupka8e60a372012-09-04 09:15:20 +02001874 while (!isterminated) {
1875 fds.fd = client;
1876 fds.events = POLLIN;
1877 fds.revents = 0;
1878
1879 status = poll(&fds, 1, 1000);
1880
1881 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1882 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
David Kupka8e60a372012-09-04 09:15:20 +02001883 continue;
1884 } else if (status < 0) {
1885 /* 0: poll time outed
1886 * close socket and ignore this request from the client, it can try it again
1887 * -1: poll failed
1888 * something wrong happend, close this socket and wait for another request
1889 */
David Kupka8e60a372012-09-04 09:15:20 +02001890 close(client);
1891 break;
1892 }
1893 /* status > 0 */
1894
1895 /* check the status of the socket */
1896
1897 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1898 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1899 /* close client's socket (it's probably already closed by client */
David Kupka8e60a372012-09-04 09:15:20 +02001900 close(client);
1901 break;
1902 }
1903
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001904 DEBUG("Get framed message...");
1905 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001906
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001907 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001908 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001909 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001910 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001911 request = json_tokener_parse_verbose(buffer, &jerr);
1912 if (jerr != json_tokener_success) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001913 ERROR("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001914 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001915 continue;
1916 }
David Kupka8e60a372012-09-04 09:15:20 +02001917
Michal Vaskoc3146782015-11-04 14:46:41 +01001918 session_id = get_param_string(request, "session");
Tomas Cejka09629492014-07-10 15:58:06 +02001919 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
1920 operation = json_object_get_int(js_tmp);
1921 json_object_put(js_tmp);
1922 js_tmp = NULL;
1923 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001924 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001925 if (operation == -1) {
1926 reply = create_error("Missing operation type form frontend.");
1927 goto send_reply;
1928 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001929
Michal Vaskoc3146782015-11-04 14:46:41 +01001930 DEBUG("operation %d session_id %s.", operation, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02001931 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
Michal Vaskoc3146782015-11-04 14:46:41 +01001932 if (operation != MSG_CONNECT && session_id == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001933 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001934 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001935 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001936 pthread_mutex_unlock(&json_lock);
1937
David Kupka8e60a372012-09-04 09:15:20 +02001938 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001939
1940 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001941 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001942 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001943 /* there is some stupid client, so close the connection to give a chance to some other client */
1944 close(client);
1945 break;
1946 }
1947
David Kupka8e60a372012-09-04 09:15:20 +02001948 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001949 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001950
1951 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001952 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001953 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001954 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001955 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001956 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001957 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001958
1959 /* process required operation */
1960 switch (operation) {
1961 case MSG_CONNECT:
Michal Vaskoc3146782015-11-04 14:46:41 +01001962 reply = handle_op_connect(request);
David Kupka8e60a372012-09-04 09:15:20 +02001963 break;
1964 case MSG_GET:
Michal Vaskoc3146782015-11-04 14:46:41 +01001965 reply = handle_op_get(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02001966 break;
1967 case MSG_GETCONFIG:
Michal Vaskoc3146782015-11-04 14:46:41 +01001968 reply = handle_op_getconfig(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02001969 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001970 case MSG_GETSCHEMA:
Michal Vaskoc3146782015-11-04 14:46:41 +01001971 reply = handle_op_getschema(request, session_id);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001972 break;
David Kupka8e60a372012-09-04 09:15:20 +02001973 case MSG_EDITCONFIG:
Michal Vaskoc3146782015-11-04 14:46:41 +01001974 reply = handle_op_editconfig(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02001975 break;
1976 case MSG_COPYCONFIG:
Michal Vaskoc3146782015-11-04 14:46:41 +01001977 reply = handle_op_copyconfig(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02001978 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001979
David Kupka8e60a372012-09-04 09:15:20 +02001980 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001981 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001982 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001983 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001984 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001985 target = get_param_string(request, "target");
1986 pthread_mutex_unlock(&json_lock);
1987 if (target != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001988 ds_type_t = parse_datastore(target);
1989 }
David Kupka8e60a372012-09-04 09:15:20 +02001990
Michal Vaskoc3146782015-11-04 14:46:41 +01001991 if ((int)ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001992 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001993 break;
1994 }
David Kupka8e60a372012-09-04 09:15:20 +02001995 switch(operation) {
1996 case MSG_DELETECONFIG:
Michal Vaskoc3146782015-11-04 14:46:41 +01001997 DEBUG("Request: delete-config (session %s)", session_id);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001998 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001999 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002000 pthread_mutex_unlock(&json_lock);
Michal Vaskoc3146782015-11-04 14:46:41 +01002001 reply = netconf_deleteconfig(session_id, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02002002 break;
2003 case MSG_LOCK:
Michal Vaskoc3146782015-11-04 14:46:41 +01002004 DEBUG("Request: lock (session %s)", session_id);
2005 reply = netconf_lock(session_id, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002006 break;
2007 case MSG_UNLOCK:
Michal Vaskoc3146782015-11-04 14:46:41 +01002008 DEBUG("Request: unlock (session %s)", session_id);
2009 reply = netconf_unlock(session_id, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002010 break;
2011 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02002012 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02002013 break;
2014 }
2015
Tomas Cejka442258e2014-04-01 18:17:18 +02002016 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02002017 if (reply == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02002018 reply = create_ok();
David Kupka8e60a372012-09-04 09:15:20 +02002019 }
2020 break;
2021 case MSG_KILL:
Michal Vaskoc3146782015-11-04 14:46:41 +01002022 reply = handle_op_kill(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02002023 break;
2024 case MSG_DISCONNECT:
Michal Vaskoc3146782015-11-04 14:46:41 +01002025 reply = handle_op_disconnect(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02002026 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002027 case MSG_RELOADHELLO:
Michal Vaskoc3146782015-11-04 14:46:41 +01002028 reply = handle_op_reloadhello(request, session_id);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002029 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002030 case MSG_INFO:
Michal Vaskoc3146782015-11-04 14:46:41 +01002031 reply = handle_op_info(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02002032 break;
2033 case MSG_GENERIC:
Michal Vaskoc3146782015-11-04 14:46:41 +01002034 reply = handle_op_generic(request, session_id);
David Kupka8e60a372012-09-04 09:15:20 +02002035 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02002036 case MSG_NTF_GETHISTORY:
Michal Vaskoc3146782015-11-04 14:46:41 +01002037 reply = handle_op_ntfgethistory(request, session_id);
Tomas Cejka6b886e02013-07-05 09:53:17 +02002038 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02002039 case MSG_VALIDATE:
Michal Vaskoc3146782015-11-04 14:46:41 +01002040 reply = handle_op_validate(request, session_id);
Tomas Cejka4003a702013-10-01 00:02:45 +02002041 break;
David Kupka8e60a372012-09-04 09:15:20 +02002042 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002043 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002044 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02002045 break;
2046 }
Tomas Cejka09629492014-07-10 15:58:06 +02002047 /* free parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02002048 CHECK_AND_FREE(url);
2049 CHECK_AND_FREE(target);
2050 request = NULL;
2051 operation = (-1);
2052 ds_type_t = (-1);
2053
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002054 DEBUG("Clean request json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002055 if (request != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002056 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002057 json_object_put(request);
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002058 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002059 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002060 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002061
2062
2063send_reply:
David Kupka1e3e4c82012-09-04 09:32:15 +02002064 /* send reply to caller */
2065 if (reply != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002066 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002067 msgtext = json_object_to_json_string(reply);
Tomas Cejka866f2282014-09-18 15:20:26 +02002068 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int) strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02002069 if (buffer != NULL) {
2070 free(buffer);
2071 buffer = NULL;
2072 }
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002073 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02002074 break;
2075 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002076 pthread_mutex_unlock(&json_lock);
2077
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002078 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02002079 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002080 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002081 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002082 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002083 reply = NULL;
2084 DEBUG("Clean message buffer.");
Tomas Cejka09629492014-07-10 15:58:06 +02002085 CHECK_AND_FREE(chunked_out_msg);
Tomas Cejka64b87482013-06-03 16:30:53 +02002086 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02002087 if (buffer != NULL) {
2088 free(buffer);
2089 buffer = NULL;
2090 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002091 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02002092 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02002093 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002094 ERROR("Reply is NULL, shouldn't be...");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002095 continue;
David Kupka8e60a372012-09-04 09:15:20 +02002096 }
2097 }
2098 }
David Kupka8e60a372012-09-04 09:15:20 +02002099 free (arg);
2100
Tomas Cejka442258e2014-04-01 18:17:18 +02002101 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002102
David Kupka8e60a372012-09-04 09:15:20 +02002103 return retval;
2104}
2105
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002106/**
2107 * \brief Close all open NETCONF sessions.
2108 *
2109 * During termination of mod_netconf, it is useful to close all remaining
2110 * sessions. This function iterates over the list of sessions and close them
2111 * all.
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002112 */
Michal Vaskoc3146782015-11-04 14:46:41 +01002113static void close_all_nc_sessions(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002114{
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002115 struct session_with_mutex *swm = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002116 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002117
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002118 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002119 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002120 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002121 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002122 return;
2123 }
Michal Vaskoc3146782015-11-04 14:46:41 +01002124 for (swm = netconf_sessions_list; swm; swm = swm->next) {
2125 DEBUG("Closing NETCONF session (%s).", nc_session_get_id(swm->session));
Tomas Cejka47387fd2013-06-10 20:37:46 +02002126
Michal Vaskoc3146782015-11-04 14:46:41 +01002127 /* close_and_free_session handles locking on its own */
2128 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002129 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002130 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002131 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002132 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002133 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002134 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002135}
2136
Michal Vaskoc3146782015-11-04 14:46:41 +01002137static void check_timeout_and_close(void)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002138{
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002139 struct nc_session *ns = NULL;
2140 struct session_with_mutex *swm = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +01002141 time_t current_time = time(NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002142 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002143
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002144 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002145 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002146 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002147 return;
2148 }
Michal Vaskoc3146782015-11-04 14:46:41 +01002149 for (swm = netconf_sessions_list; swm; swm = swm->next) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002150 ns = swm->session;
2151 if (ns == NULL) {
2152 continue;
2153 }
2154 pthread_mutex_lock(&swm->lock);
Michal Vaskoc3146782015-11-04 14:46:41 +01002155 if ((current_time - swm->last_activity) > ACTIVITY_TIMEOUT) {
2156 DEBUG("Closing NETCONF session (%s).", nc_session_get_id(swm->session));
Tomas Cejka47387fd2013-06-10 20:37:46 +02002157
2158 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002159 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002160 } else {
2161 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002162 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002163 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002164 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002165 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002166 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002167 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002168}
2169
2170
2171/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002172 * This is actually implementation of NETCONF client
2173 * - requests are received from UNIX socket in the predefined format
2174 * - results are replied through the same way
Michal Vaskoc3146782015-11-04 14:46:41 +01002175 * - the daemon run as a separate process
Radek Krejcif23850c2012-07-23 16:14:17 +02002176 *
2177 */
Michal Vaskoc3146782015-11-04 14:46:41 +01002178static void forked_proc(void)
Radek Krejci469aab82012-07-22 18:42:20 +02002179{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002180 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002181 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002182 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002183 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002184 socklen_t len;
David Kupka8e60a372012-09-04 09:15:20 +02002185 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002186 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002187 struct timespec maxtime;
2188 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002189 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002190 #ifdef WITH_NOTIFICATIONS
2191 char use_notifications = 0;
2192 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002193
Tomas Cejka404d37e2013-04-13 02:31:35 +02002194 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002195 maxtime.tv_sec = 5;
2196 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002197
Tomas Cejka04e08f42014-03-27 19:52:34 +01002198#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002199 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002200 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002201#else
2202# ifdef SU_GROUP
2203 if (strlen(SU_GROUP) > 0) {
2204 struct group *g = getgrnam(SU_GROUP);
2205 if (g == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002206 ERROR("GID (%s) was not found.", SU_GROUP);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002207 return;
2208 }
2209 if (setgid(g->gr_gid) != 0) {
2210
Tomas Cejkacf44e522015-04-24 17:29:21 +02002211 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002212 return;
2213 }
2214 }
2215# else
2216 DEBUG("no SU_GROUP");
2217# endif
2218# ifdef SU_USER
2219 if (strlen(SU_USER) > 0) {
2220 struct passwd *p = getpwnam(SU_USER);
2221 if (p == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002222 ERROR("UID (%s) was not found.", SU_USER);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002223 return;
2224 }
2225 if (setuid(p->pw_uid) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002226 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002227 return;
2228 }
2229 }
2230# else
2231 DEBUG("no SU_USER");
2232# endif
2233#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002234
Tomas Cejka04e08f42014-03-27 19:52:34 +01002235 /* try to remove if exists */
2236 unlink(sockname);
2237
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002238 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002239 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002240 ERROR("Creating socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002241 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002242 }
2243
2244 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002245 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002246 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2247
2248 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2249 if (errno == EADDRINUSE) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002250 ERROR("mod_netconf socket address already in use");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002251 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002252 }
Tomas Cejkacf44e522015-04-24 17:29:21 +02002253 ERROR("Binding socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002254 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002255 }
2256
2257 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002258 ERROR("Setting up listen socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002259 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002260 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002261 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002262
Tomas Cejka04e08f42014-03-27 19:52:34 +01002263 uid_t user = -1;
2264 if (strlen(CHOWN_USER) > 0) {
2265 struct passwd *p = getpwnam(CHOWN_USER);
2266 if (p != NULL) {
2267 user = p->pw_uid;
2268 }
2269 }
2270 gid_t group = -1;
2271 if (strlen(CHOWN_GROUP) > 0) {
2272 struct group *g = getgrnam(CHOWN_GROUP);
2273 if (g != NULL) {
2274 group = g->gr_gid;
2275 }
2276 }
2277 if (chown(sockname, user, group) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002278 ERROR("Chown on socket file failed (%s).", strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002279 }
2280
Tomas Cejkaba21b382013-04-13 02:37:32 +02002281 /* prepare internal lists */
Tomas Cejkaba21b382013-04-13 02:37:32 +02002282
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002283 #ifdef WITH_NOTIFICATIONS
Michal Vaskoc3146782015-11-04 14:46:41 +01002284 if (notification_init() == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002285 ERROR("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002286 use_notifications = 0;
2287 } else {
2288 use_notifications = 1;
2289 }
2290 #endif
2291
Radek Krejci469aab82012-07-22 18:42:20 +02002292 /* setup libnetconf's callbacks */
2293 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002294 nc_callback_print(clb_print);
2295 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2296 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2297 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Tomas Cejkab34b7b12015-06-21 22:54:11 +02002298 nc_callback_sshauth_passphrase(netconf_callback_sshauth_passphrase);
Radek Krejcic11fd862012-07-26 12:41:21 +02002299 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002300
2301 /* disable publickey authentication */
2302 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2303
David Kupka8e60a372012-09-04 09:15:20 +02002304 /* create mutex protecting session list */
2305 pthread_rwlockattr_init(&lock_attrs);
2306 /* rwlock is shared only with threads in this process */
2307 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2308 /* create rw lock */
2309 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002310 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002311 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002312 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002313 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002314 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkacf44e522015-04-24 17:29:21 +02002315 DEBUG("Initialization of notification history.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002316 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002317 ERROR("Initialization of notification history failed.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002318 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002319 if (pthread_key_create(&err_reply_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002320 ERROR("Initialization of reply key failed.");
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002321 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002322
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002323 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002324 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002325 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002326 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002327 #ifdef WITH_NOTIFICATIONS
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002328 if (use_notifications == 1) {
2329 notification_handle();
2330 }
2331 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002332 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Michal Vaskoc3146782015-11-04 14:46:41 +01002333 check_timeout_and_close();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002334 }
Radek Krejci469aab82012-07-22 18:42:20 +02002335
2336 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002337 len = sizeof(remote);
2338 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002339 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
Michal Vaskoc3146782015-11-04 14:46:41 +01002340 sleep(SLEEP_TIME);
Radek Krejci469aab82012-07-22 18:42:20 +02002341 continue;
2342 } else if (client == -1 && (errno == EINTR)) {
2343 continue;
2344 } else if (client == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002345 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002346 continue;
2347 }
Radek Krejci469aab82012-07-22 18:42:20 +02002348
2349 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002350 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002351
Michal Vaskoc3146782015-11-04 14:46:41 +01002352 arg = malloc(sizeof(struct pass_to_thread));
David Kupka8e60a372012-09-04 09:15:20 +02002353 arg->client = client;
David Kupka8e60a372012-09-04 09:15:20 +02002354 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002355
David Kupka8e60a372012-09-04 09:15:20 +02002356 /* start new thread. It will serve this particular request and then terminate */
2357 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002358 ERROR("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002359 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002360 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002361 pthread_count++;
2362 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2363 ptids[pthread_count] = 0;
2364 }
Radek Krejci469aab82012-07-22 18:42:20 +02002365
David Kupka8e60a372012-09-04 09:15:20 +02002366 /* check if some thread already terminated, free some resources by joining it */
2367 for (i=0; i<pthread_count; i++) {
2368 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002369 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002370 pthread_count--;
2371 if (pthread_count > 0) {
2372 /* place last Thread ID on the place of joined one */
2373 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002374 }
Radek Krejci469aab82012-07-22 18:42:20 +02002375 }
2376 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002377 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002378 }
2379
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002380 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002381 /* join all threads */
2382 for (i=0; i<pthread_count; i++) {
2383 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2384 }
Radek Krejci469aab82012-07-22 18:42:20 +02002385
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002386 #ifdef WITH_NOTIFICATIONS
2387 notification_close();
2388 #endif
2389
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002390 /* close all NETCONF sessions */
Michal Vaskoc3146782015-11-04 14:46:41 +01002391 close_all_nc_sessions();
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002392
David Kupka8e60a372012-09-04 09:15:20 +02002393 /* destroy rwlock */
2394 pthread_rwlock_destroy(&session_lock);
2395 pthread_rwlockattr_destroy(&lock_attrs);
2396
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002397 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002398
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002399 free(ptids);
2400 close(lsock);
Michal Vaskoc3146782015-11-04 14:46:41 +01002401 exit(0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002402 return;
2403error_exit:
2404 close(lsock);
2405 free(ptids);
2406 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002407}
2408
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002409int main(int argc, char **argv)
2410{
Michal Vaskoc3146782015-11-04 14:46:41 +01002411 struct sigaction action;
2412 sigset_t block_mask;
2413
2414 if (argc > 1) {
2415 sockname = argv[1];
2416 } else {
2417 sockname = SOCKET_FILENAME;
2418 }
2419
2420 sigfillset (&block_mask);
2421 action.sa_handler = signal_handler;
2422 action.sa_mask = block_mask;
2423 action.sa_flags = 0;
2424 sigaction(SIGINT, &action, NULL);
2425 sigaction(SIGTERM, &action, NULL);
2426
2427 forked_proc();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002428 DEBUG("Terminated");
2429 return 0;
2430}