blob: 9148fae737aaaa52ad6dd97a889417beefd159da [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 */
46
Radek Krejci7b4ddd02012-07-30 08:09:58 +020047#include <unistd.h>
48#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020049#include <sys/types.h>
50#include <sys/socket.h>
51#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010052#include <sys/fcntl.h>
David Kupka8e60a372012-09-04 09:15:20 +020053#include <pthread.h>
54#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020055
56#include <unixd.h>
57#include <httpd.h>
58#include <http_log.h>
59#include <http_config.h>
60
61#include <apr_sha1.h>
62#include <apr_hash.h>
63#include <apr_signal.h>
64#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020065
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020066#include <json/json.h>
67
Radek Krejci469aab82012-07-22 18:42:20 +020068#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020069#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020070
Tomas Cejkad340dbf2013-03-24 20:36:57 +010071#ifdef WITH_NOTIFICATIONS
72#include "notification_module.h"
73#endif
74
Tomas Cejka94da2c52013-01-08 18:20:30 +010075#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020076#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020077
78#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020079#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020080#define MAX_SOCKET_CL 10
81#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020082#define NOTIFICATION_QUEUE_SIZE 10
83#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020084#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020085
86/* sleep in master process for non-blocking socket reading */
87#define SLEEP_TIME 200
88
89#ifndef offsetof
90#define offsetof(type, member) ((size_t) ((type *) 0)->member)
91#endif
92
Tomas Cejkaef531ee2013-11-12 16:07:00 +010093server_rec *http_server = NULL;
94
Tomas Cejka027f3bc2012-11-10 20:28:36 +010095/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020096struct timeval timeout = { 1, 0 };
97
Tomas Cejka5064c232013-01-17 09:30:58 +010098#define NCWITHDEFAULTS NCWD_MODE_NOTSET
99
100
Radek Krejci469aab82012-07-22 18:42:20 +0200101#define MSG_OK 0
102#define MSG_OPEN 1
103#define MSG_DATA 2
104#define MSG_CLOSE 3
105#define MSG_ERROR 4
106#define MSG_UNKNOWN 5
107
Radek Krejci469aab82012-07-22 18:42:20 +0200108module AP_MODULE_DECLARE_DATA netconf_module;
109
Tomas Cejka47387fd2013-06-10 20:37:46 +0200110pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200111pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100112pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100113pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
114
Tomas Cejka47387fd2013-06-10 20:37:46 +0200115apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200116
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200117static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200118
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100119static pthread_key_t err_reply_key;
120
121#define GETSPEC_ERR_REPLY json_object *err_reply = *((json_object **) pthread_getspecific(err_reply_key));
122
123#define CHECK_ERR_SET_REPLY \
124if (reply == NULL) { \
125 GETSPEC_ERR_REPLY \
126 if (err_reply != NULL) { \
127 /* use filled err_reply from libnetconf's callback */ \
128 reply = err_reply; \
129 } \
130}
131
132#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
133if (reply == NULL) { \
134 GETSPEC_ERR_REPLY \
135 if (err_reply == NULL) { \
136 reply = create_error(errmsg); \
137 } else { \
138 /* use filled err_reply from libnetconf's callback */ \
139 reply = err_reply; \
140 } \
141}
142
Radek Krejci469aab82012-07-22 18:42:20 +0200143volatile int isterminated = 0;
144
145static char* password;
146
Radek Krejci469aab82012-07-22 18:42:20 +0200147static void signal_handler(int sign)
148{
149 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100150 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200151 case SIGTERM:
152 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200153 break;
154 }
155}
156
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200157static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200158{
Radek Krejcif23850c2012-07-23 16:14:17 +0200159 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
160 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200161 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200162
Radek Krejci469aab82012-07-22 18:42:20 +0200163 apr_sha1_ctx_t sha1_ctx;
164 apr_sha1_init(&sha1_ctx);
165 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
166 apr_sha1_update(&sha1_ctx, port, strlen(port));
167 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200168 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200169
Radek Krejcif23850c2012-07-23 16:14:17 +0200170 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200171 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200172 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200173 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200174 }
175 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200176
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200177 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200178}
179
180int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
181{
182 /* always approve */
183 return (EXIT_SUCCESS);
184}
185
186char* netconf_callback_sshauth_password (const char* username, const char* hostname)
187{
188 char* buf;
189
190 buf = malloc ((strlen(password) + 1) * sizeof(char));
191 apr_cpystrn(buf, password, strlen(password) + 1);
192
193 return (buf);
194}
195
196void netconf_callback_sshauth_interactive (const char* name,
197 int name_len,
198 const char* instruction,
199 int instruction_len,
200 int num_prompts,
201 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
202 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
203 void** abstract)
204{
205 int i;
206
207 for (i=0; i<num_prompts; i++) {
208 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
209 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
210 responses[i].length = strlen(responses[i].text) + 1;
211 }
212
213 return;
214}
215
Radek Krejcic11fd862012-07-26 12:41:21 +0200216void netconf_callback_error_process(const char* tag,
217 const char* type,
218 const char* severity,
219 const char* apptag,
220 const char* path,
221 const char* message,
222 const char* attribute,
223 const char* element,
224 const char* ns,
225 const char* sid)
226{
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100227 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
228 json_object *err_reply = *err_reply_p;
229
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100230 json_object *array = NULL;
231 if (err_reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100232 DEBUG("error calback: empty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100233 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100234 err_reply = json_object_new_object();
235 array = json_object_new_array();
236 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
237 json_object_object_add(err_reply, "errors", array);
238 if (message != NULL) {
239 json_object_array_add(array, json_object_new_string(message));
240 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100241 pthread_mutex_unlock(&json_lock);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100242 (*err_reply_p) = err_reply;
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100243 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100244 DEBUG("error calback: nonempty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100245 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100246 array = json_object_object_get(err_reply, "errors");
247 if (array != NULL) {
248 if (message != NULL) {
249 json_object_array_add(array, json_object_new_string(message));
250 }
251 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100252 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100253 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100254 pthread_setspecific(err_reply_key, err_reply_p);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100255 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200256}
257
Tomas Cejka47387fd2013-06-10 20:37:46 +0200258/**
259 * should be used in locked area
260 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100261void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200262{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200263 json_object *json_obj = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100264 char *old_sid = NULL;
265 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200266 const char *cpbltstr;
267 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200268
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200269 if (s == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100270 DEBUG("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200271 return;
272 }
273
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100274 pthread_mutex_lock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200275 if (s->hello_message != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100276 DEBUG("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200277 //json_object_put(s->hello_message);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100278 j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid"));
279 if (j_old_sid != NULL) {
280 old_sid = strdup(j_old_sid);
281 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100282 json_object_put(s->hello_message);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200283 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200284 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100285 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200286 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100287 if (old_sid != NULL) {
288 /* use previous sid */
289 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
290 free(old_sid);
Tomas Cejkaa3ffdba2014-03-27 15:12:21 +0100291 old_sid = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100292 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200293 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100294 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
295 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200296 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
297 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
298 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
299 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
300 cpblts = nc_session_get_cpblts (session);
301 if (cpblts != NULL) {
302 json_obj = json_object_new_array();
303 nc_cpblts_iter_start (cpblts);
304 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
305 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
306 }
307 json_object_object_add(s->hello_message, "capabilities", json_obj);
308 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100309 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200310 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100311 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200312 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
313 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
314 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100315 DEBUG("Status info from hello message prepared");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100316 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200317
318}
319
320
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200321/**
322 * \defgroup netconf_operations NETCONF operations
323 * The list of NETCONF operations that mod_netconf supports.
324 * @{
325 */
326
327/**
328 * \brief Connect to NETCONF server
329 *
330 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
331 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100332static char* netconf_connect(apr_pool_t* pool, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200333{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200334 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200335 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200336 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200337
338 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200339 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100340 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200341 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200342 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100343 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200344
Radek Krejci469aab82012-07-22 18:42:20 +0200345 /* if connected successful, add session to the list */
346 if (session != NULL) {
347 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200348 session_key = gen_ncsession_hash(
349 (host==NULL) ? "localhost" : host,
350 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200351 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200352
Tomas Cejkaba21b382013-04-13 02:37:32 +0200353 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100354 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 +0200355 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100356 session = NULL;
357 free(locked_session);
358 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100359 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200360 return NULL;
361 }
362 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200363 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200364 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200365 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200366 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100367 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200368 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100369 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200370 if (pthread_rwlock_wrlock (&session_lock) != 0) {
371 nc_session_free(session);
372 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100373 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200374 return NULL;
375 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200376 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200377 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100378 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200379 apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100380 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200381
Tomas Cejka47387fd2013-06-10 20:37:46 +0200382 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100383 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200384 pthread_mutex_lock(&locked_session->lock);
385
386 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100387 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200388 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100389 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200390 }
391
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200392 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100393 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200394
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100395 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200396 pthread_mutex_unlock(&locked_session->lock);
397
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100398 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200399 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200400 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100401 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200402 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200403 }
404
Radek Krejci469aab82012-07-22 18:42:20 +0200405}
406
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100407static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200408{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100409 DEBUG("lock private lock.");
410 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200411 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100412 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200413 }
414 locked_session->ntfc_subscribed = 0;
415 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100416 if (locked_session->session != NULL) {
417 nc_session_free(locked_session->session);
418 locked_session->session = NULL;
419 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100420 DEBUG("session closed.");
421 DEBUG("unlock private lock.");
422 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200423 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100424 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200425 }
426
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100427 DEBUG("unlock session lock.");
428 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200429 usleep(500000); /* let notification thread stop */
430
431 /* session shouldn't be used by now */
432 /** \todo free all notifications from queue */
433 apr_array_clear(locked_session->notifications);
434 pthread_mutex_destroy(&locked_session->lock);
435 if (locked_session->hello_message != NULL) {
436 /** \todo free hello_message */
437 //json_object_put(locked_session->hello_message);
438 locked_session->hello_message = NULL;
439 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200440 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200441 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200442 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100443 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200444 return (EXIT_SUCCESS);
445}
446
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200448{
David Kupka8e60a372012-09-04 09:15:20 +0200449 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200450
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100451 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200452
David Kupka8e60a372012-09-04 09:15:20 +0200453 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100454 DEBUG("lock session lock.");
455 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200456 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100457 DEBUG("Error while locking rwlock");
458 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200459 return EXIT_FAILURE;
460 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200461 /* get session to close */
462 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
463 /* remove session from the active sessions list -> nobody new can now work with session */
464 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200465
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100466 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200467 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100468 DEBUG("Error while unlocking rwlock");
469 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200470 }
471
472 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100473 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200474 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100475 DEBUG("Unknown session to close");
476 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200477 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200478 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100479 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200480}
481
Tomas Cejkac7929632013-10-24 19:25:15 +0200482/**
483 * Test reply message type and return error message.
484 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200485 * \param[in] session nc_session internal struct
486 * \param[in] session_key session key, NULL to disable disconnect on error
487 * \param[in] msgt RPC-REPLY message type
488 * \param[out] data
489 * \return NULL on success
490 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100491json_object *netconf_test_reply(struct nc_session *session, const char *session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data)
Tomas Cejkac7929632013-10-24 19:25:15 +0200492{
493 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100494 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200495
496 /* process the result of the operation */
497 switch (msgt) {
498 case NC_MSG_UNKNOWN:
499 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100500 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200501 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100502 netconf_close(session_key, &err);
503 }
504 if (err != NULL) {
505 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200506 }
507 return create_error("Internal: Receiving RPC-REPLY failed.");
508 }
509 case NC_MSG_NONE:
510 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100511 if (data != NULL) {
512 free(*data);
513 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200514 (*data) = NULL;
515 return NULL;
516 case NC_MSG_REPLY:
517 switch (replyt = nc_reply_get_type(reply)) {
518 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100519 if ((data != NULL) && (*data != NULL)) {
520 free(*data);
521 (*data) = NULL;
522 }
523 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200524 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100525 if (((*data) = nc_reply_get_data(reply)) == NULL) {
526 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200527 return create_error("Internal: No data from reply received.");
528 } else {
529 return NULL;
530 }
531 break;
532 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100533 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
534 if (data != NULL) {
535 free(*data);
536 (*data) = NULL;
537 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200538 return create_error(nc_reply_get_errormsg(reply));
539 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100540 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
541 if (data != NULL) {
542 free(*data);
543 (*data) = NULL;
544 }
Tomas Cejka60885252014-03-26 15:45:47 +0100545 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200546 }
547 break;
548 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100549 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
550 if (data != NULL) {
551 free(*data);
552 (*data) = NULL;
553 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200554 return create_error("Internal: Unexpected RPC-REPLY message type.");
555 }
556}
557
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100558json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200559{
560 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200561 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200562
563 /* check requests */
564 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100565 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200566 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200567 }
568
569 if (session != NULL) {
570 /* send the request and get the reply */
571 msgt = nc_session_send_recv(session, rpc, &reply);
572 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100573 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200574 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100575 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200576 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200577 }
578}
579
Tomas Cejkac7929632013-10-24 19:25:15 +0200580/**
581 * Perform RPC method that returns data.
582 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200583 * \param[in] session_key session identifier
584 * \param[in] rpc RPC message to perform
585 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
586 * \return NULL on success, json object with error otherwise
587 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100588static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200589{
590 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200591 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200592 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200593 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100594 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100595 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200596
Radek Krejci8e4632a2012-07-26 13:40:34 +0200597 /* check requests */
598 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100599 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200600 res = create_error("Internal: RPC could not be created.");
601 data = NULL;
602 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200603 }
604
David Kupka8e60a372012-09-04 09:15:20 +0200605 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100606 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200607 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100608 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200609 res = create_error("Internal: Lock failed.");
610 data = NULL;
611 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200612 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200613 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200614 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
David Kupka8e60a372012-09-04 09:15:20 +0200615 if (locked_session != NULL) {
616 session = locked_session->session;
617 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200618 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200619 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100620 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200621 if (pthread_mutex_lock(&locked_session->lock) != 0) {
622 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100623 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200624 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200625
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100626 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200627 res = create_error("Internal: Could not unlock.");
628 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200629 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200630 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200631 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100632 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200633 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200634
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100635 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200636 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200637 }
638
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200639 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200640
Radek Krejci035bf4e2012-07-25 10:59:09 +0200641 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100642 msgt = nc_session_send_recv(session, rpc, &reply);
643
David Kupka8e60a372012-09-04 09:15:20 +0200644 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100645 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200646 pthread_mutex_unlock(&locked_session->lock);
647 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200648
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100649 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200650 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100651 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100652 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100653 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100654 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100655 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200657 res = create_error("Unknown session to process.");
658 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200659 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200660finished:
661 nc_reply_free(reply);
662 if (received_data != NULL) {
663 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100664 } else {
665 if (data != NULL) {
666 free(data);
667 data = NULL;
668 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200669 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200670 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200671}
672
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100673static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200674{
675 nc_rpc* rpc;
676 struct nc_filter *f = NULL;
677 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200678 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200679
680 /* create filter if set */
681 if (filter != NULL) {
682 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
683 }
684
685 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100686 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200687 nc_filter_free(f);
688 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100689 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200690 return (NULL);
691 }
692
Tomas Cejka94674662013-09-13 15:55:24 +0200693 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100694#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100695 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100696#else
697 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
698#endif
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100699 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200700 }
701
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100702 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200703 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100704 if (res != NULL) {
705 (*err) = res;
706 } else {
707 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200708 }
709
Radek Krejci8e4632a2012-07-26 13:40:34 +0200710 return (data);
711}
712
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100713static 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 +0100714{
715 nc_rpc* rpc;
716 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200717 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100718
719 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100720 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100721 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100722 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100723 return (NULL);
724 }
725
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100726 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100727 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100728 if (res != NULL) {
729 (*err) = res;
730 } else {
731 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200732 }
733
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100734 return (data);
735}
736
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100737static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200738{
739 nc_rpc* rpc;
740 struct nc_filter *f = NULL;
741 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200742 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200743
744 /* create filter if set */
745 if (filter != NULL) {
746 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
747 }
748
749 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100750 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200751 nc_filter_free(f);
752 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100753 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200754 return (NULL);
755 }
756
Tomas Cejka94674662013-09-13 15:55:24 +0200757 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100758 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100759 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200760 }
761
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100762 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200763 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200764 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100765 (*err) = res;
766 } else {
767 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200768 }
769
Radek Krejci8e4632a2012-07-26 13:40:34 +0200770 return (data);
771}
772
Tomas Cejkab4d05872014-02-14 22:44:38 +0100773static 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 +0200774{
775 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200776 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200777
778 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100779 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100780 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100781 /* config, url */
782 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100783 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100784 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100785 rpc = nc_rpc_copyconfig(source, target, config);
786 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100787 } else if (source == NC_DATASTORE_URL) {
788 if (target == NC_DATASTORE_URL) {
789 /* url, url */
790 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
791 } else {
792 /* url, datastore */
793 rpc = nc_rpc_copyconfig(source, target, uri_src);
794 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100795 } else {
796 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100797 /* datastore, url */
798 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100799 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100800 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100801 rpc = nc_rpc_copyconfig(source, target);
802 }
803 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200804 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100805 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200806 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200807 }
808
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100809 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200810 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200811
812 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200813}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200814
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100815static 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 +0200816{
817 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200818 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200819
820 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100821 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200822 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100823 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200824 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200825 }
826
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100827 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200828 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200829
830 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200831}
832
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100833static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200834{
835 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200836 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200837
838 /* create requests */
839 rpc = nc_rpc_killsession(sid);
840 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100841 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200842 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200843 }
844
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100845 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200846 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200847 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200848}
849
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100850static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200851{
852 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200853 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200854
855 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200856 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200857 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100858 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200859 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200860 }
861
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100862 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200863 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200864 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200865}
866
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100867static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200868{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200869 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200870 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200871 if (target != NC_DATASTORE_URL) {
872 rpc = nc_rpc_deleteconfig(target);
873 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200874 rpc = nc_rpc_deleteconfig(target, url);
875 }
876 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100877 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200878 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200879 }
880
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100881 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200882 nc_rpc_free (rpc);
883 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200884}
885
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100886static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200887{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100888 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200889}
890
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100891static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200892{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100893 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200894}
895
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100896static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200897{
Tomas Cejka00635972013-06-03 15:10:52 +0200898 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200899 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200900
901 /* create requests */
902 rpc = nc_rpc_generic(content);
903 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100904 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200905 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200906 }
907
Radek Krejcia332b692012-11-12 16:15:54 +0100908 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200909 // TODO ?free(*data);
910 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100911 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200912
913 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100914 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200915 nc_rpc_free (rpc);
916 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200917}
918
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200919/**
920 * @}
921 *//* netconf_operations */
922
Radek Krejci7338bde2012-08-10 12:57:30 +0200923void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200924{
Radek Krejci7338bde2012-08-10 12:57:30 +0200925 switch (level) {
926 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100927 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200928 break;
929 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100930 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200931 break;
932 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100933 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200934 break;
935 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100936 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200937 break;
938 }
Radek Krejci469aab82012-07-22 18:42:20 +0200939}
940
Tomas Cejka64b87482013-06-03 16:30:53 +0200941/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200942 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200943 * Caller should free message memory.
944 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200945 * \return pointer to message
946 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100947char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200948{
949 /* read json in chunked framing */
950 unsigned int buffer_size = 0;
951 ssize_t buffer_len = 0;
952 char *buffer = NULL;
953 char c;
954 ssize_t ret;
955 int i, chunk_len;
956 char chunk_len_str[12];
957
958 while (1) {
959 /* read chunk length */
960 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
961 if (buffer != NULL) {
962 free (buffer);
963 buffer = NULL;
964 }
965 break;
966 }
967 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
968 if (buffer != NULL) {
969 free (buffer);
970 buffer = NULL;
971 }
972 break;
973 }
974 i=0;
975 memset (chunk_len_str, 0, 12);
976 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
977 if (i==0 && c == '#') {
978 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
979 /* end but invalid */
980 if (buffer != NULL) {
981 free (buffer);
982 buffer = NULL;
983 }
984 }
985 /* end of message, double-loop break */
986 goto msg_complete;
987 }
988 chunk_len_str[i++] = c;
989 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100990 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +0200991 break;
992 }
993 }
994 if (c != '\n') {
995 if (buffer != NULL) {
996 free (buffer);
997 buffer = NULL;
998 }
999 break;
1000 }
1001 chunk_len_str[i] = 0;
1002 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1003 if (buffer != NULL) {
1004 free (buffer);
1005 buffer = NULL;
1006 }
1007 break;
1008 }
1009 buffer_size += chunk_len+1;
1010 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001011 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001012 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1013 if (buffer != NULL) {
1014 free (buffer);
1015 buffer = NULL;
1016 }
1017 break;
1018 }
1019 buffer_len += ret;
1020 }
1021msg_complete:
1022 return buffer;
1023}
1024
Tomas Cejkad5b53772013-06-08 23:01:07 +02001025NC_DATASTORE parse_datastore(const char *ds)
1026{
1027 if (strcmp(ds, "running") == 0) {
1028 return NC_DATASTORE_RUNNING;
1029 } else if (strcmp(ds, "startup") == 0) {
1030 return NC_DATASTORE_STARTUP;
1031 } else if (strcmp(ds, "candidate") == 0) {
1032 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001033 } else if (strcmp(ds, "url") == 0) {
1034 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001035 } else if (strcmp(ds, "config") == 0) {
1036 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001037 }
1038 return -1;
1039}
1040
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001041NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1042{
1043 if (strcmp(t, "notset") == 0) {
1044 return NC_EDIT_TESTOPT_NOTSET;
1045 } else if (strcmp(t, "testset") == 0) {
1046 return NC_EDIT_TESTOPT_TESTSET;
1047 } else if (strcmp(t, "set") == 0) {
1048 return NC_EDIT_TESTOPT_SET;
1049 } else if (strcmp(t, "test") == 0) {
1050 return NC_EDIT_TESTOPT_TEST;
1051 }
1052 return NC_EDIT_TESTOPT_ERROR;
1053}
1054
Tomas Cejkad5b53772013-06-08 23:01:07 +02001055json_object *create_error(const char *errmess)
1056{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001057 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001058 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001059 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001060 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001061 json_object_array_add(array, json_object_new_string(errmess));
1062 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001063 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001064 return reply;
1065
1066}
1067
1068json_object *create_data(const char *data)
1069{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001070 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001071 json_object *reply = json_object_new_object();
1072 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1073 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001074 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001075 return reply;
1076}
1077
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001078json_object *create_ok()
1079{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001080 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001081 json_object *reply = json_object_new_object();
1082 reply = json_object_new_object();
1083 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001084 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001085 return reply;
1086}
1087
1088json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001089{
1090 const char *host = NULL;
1091 const char *port = NULL;
1092 const char *user = NULL;
1093 const char *pass = NULL;
1094 json_object *capabilities = NULL;
1095 json_object *reply = NULL;
1096 char *session_key_hash = NULL;
1097 struct nc_cpblts* cpblts = NULL;
1098 unsigned int len, i;
1099
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001100 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001101 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001102 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1103 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1104 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1105 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1106
1107 capabilities = json_object_object_get((json_object *) request, "capabilities");
1108 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1109 cpblts = nc_cpblts_new(NULL);
1110 for (i=0; i<len; i++) {
1111 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1112 }
1113 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001114 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001115 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001116 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001117
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001118 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001119 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001120 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001121 session_key_hash = NULL;
1122 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001123 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1124 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001125 }
1126 if (cpblts != NULL) {
1127 nc_cpblts_free(cpblts);
1128 }
1129
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001130 GETSPEC_ERR_REPLY
1131
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001132 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001133 if (session_key_hash == NULL) {
1134 /* negative reply */
1135 if (err_reply == NULL) {
1136 reply = json_object_new_object();
1137 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1138 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001139 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001140 } else {
1141 /* use filled err_reply from libnetconf's callback */
1142 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001143 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001144 }
1145 } else {
1146 /* positive reply */
1147 reply = json_object_new_object();
1148 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1149 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1150
1151 free(session_key_hash);
1152 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001153 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001154 return reply;
1155}
1156
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001157json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001158{
1159 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001160 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001161 json_object *reply = NULL;
1162
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001163 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001164
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001165 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001166 filter = json_object_get_string(json_object_object_get(request, "filter"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001167 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001168
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001169 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001170 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001171 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001172 reply = create_data(data);
1173 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001174 }
1175 return reply;
1176}
1177
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001178json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001179{
1180 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001181 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001182 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001183 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001184 json_object *reply = NULL;
1185
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001186 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001187
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001188 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001189 filter = json_object_get_string(json_object_object_get(request, "filter"));
1190
Tomas Cejkad5b53772013-06-08 23:01:07 +02001191 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1192 ds_type_s = parse_datastore(source);
1193 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001194 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001195 if (ds_type_s == -1) {
1196 return create_error("Invalid source repository type requested.");
1197 }
1198
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001199 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001200 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001201 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001202 reply = create_data(data);
1203 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001204 }
1205 return reply;
1206}
1207
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001208json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001209{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001210 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001211 const char *identifier = NULL;
1212 const char *version = NULL;
1213 const char *format = NULL;
1214 json_object *reply = NULL;
1215
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001216 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001217 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001218 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001219 version = json_object_get_string(json_object_object_get(request, "version"));
1220 format = json_object_get_string(json_object_object_get(request, "format"));
1221 pthread_mutex_lock(&json_lock);
1222
Tomas Cejkad5b53772013-06-08 23:01:07 +02001223 if (identifier == NULL) {
1224 return create_error("No identifier for get-schema supplied.");
1225 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001226
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001227 DEBUG("get-schema(version: %s, format: %s)", version, format);
1228 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001229 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001230 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001231 reply = create_data(data);
1232 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001233 }
1234 return reply;
1235}
1236
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001237json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001238{
1239 NC_DATASTORE ds_type_s = -1;
1240 NC_DATASTORE ds_type_t = -1;
1241 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1242 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001243 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001244 const char *defop = NULL;
1245 const char *erropt = NULL;
1246 const char *config = NULL;
1247 const char *source = NULL;
1248 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001249 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001250 json_object *reply = NULL;
1251
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001252 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001253
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001254 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001255 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001256 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1257 /* get parameters */
1258 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1259 ds_type_t = parse_datastore(target);
1260 }
1261 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1262 ds_type_s = parse_datastore(source);
1263 } else {
1264 /* source is optional, default value is config */
1265 ds_type_s = NC_DATASTORE_CONFIG;
1266 }
1267 config = json_object_get_string(json_object_object_get(request, "config"));
1268 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1269 pthread_mutex_unlock(&json_lock);
1270
Tomas Cejkad5b53772013-06-08 23:01:07 +02001271 if (defop != NULL) {
1272 if (strcmp(defop, "merge") == 0) {
1273 defop_type = NC_EDIT_DEFOP_MERGE;
1274 } else if (strcmp(defop, "replace") == 0) {
1275 defop_type = NC_EDIT_DEFOP_REPLACE;
1276 } else if (strcmp(defop, "none") == 0) {
1277 defop_type = NC_EDIT_DEFOP_NONE;
1278 } else {
1279 return create_error("Invalid default-operation parameter.");
1280 }
1281 } else {
1282 defop_type = NC_EDIT_DEFOP_NOTSET;
1283 }
1284
Tomas Cejkad5b53772013-06-08 23:01:07 +02001285 if (erropt != NULL) {
1286 if (strcmp(erropt, "continue-on-error") == 0) {
1287 erropt_type = NC_EDIT_ERROPT_CONT;
1288 } else if (strcmp(erropt, "stop-on-error") == 0) {
1289 erropt_type = NC_EDIT_ERROPT_STOP;
1290 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1291 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1292 } else {
1293 return create_error("Invalid error-option parameter.");
1294 }
1295 } else {
1296 erropt_type = 0;
1297 }
1298
Tomas Cejkad5b53772013-06-08 23:01:07 +02001299 if (ds_type_t == -1) {
1300 return create_error("Invalid target repository type requested.");
1301 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001302 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001303 if (config == NULL) {
1304 return create_error("Invalid config data parameter.");
1305 }
1306 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001307 if (config == NULL) {
1308 config = "";
1309 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001310 }
1311
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001312 if (testopt != NULL) {
1313 testopt_type = parse_testopt(testopt);
1314 } else {
1315 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1316 }
1317
1318 reply = netconf_editconfig(session_key, ds_type_s, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001319
1320 CHECK_ERR_SET_REPLY
1321
Tomas Cejkad5b53772013-06-08 23:01:07 +02001322 return reply;
1323}
1324
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001325json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001326{
1327 NC_DATASTORE ds_type_s = -1;
1328 NC_DATASTORE ds_type_t = -1;
1329 const char *config = NULL;
1330 const char *target = NULL;
1331 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001332 const char *uri_src = NULL;
1333 const char *uri_trg = NULL;
1334
Tomas Cejkad5b53772013-06-08 23:01:07 +02001335 json_object *reply = NULL;
1336
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001337 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001338
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001339 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001340 /* get parameters */
1341 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1342 ds_type_t = parse_datastore(target);
1343 }
1344 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1345 ds_type_s = parse_datastore(source);
1346 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001347 config = json_object_get_string(json_object_object_get(request, "config"));
1348 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
1349 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
1350 pthread_mutex_unlock(&json_lock);
1351
Tomas Cejkad5b53772013-06-08 23:01:07 +02001352 if (source == NULL) {
1353 /* no explicit source specified -> use config data */
1354 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001355 } else if (ds_type_s == -1) {
1356 /* source datastore specified, but it is invalid */
1357 return create_error("Invalid source repository type requested.");
1358 }
1359
1360 if (ds_type_t == -1) {
1361 /* invalid target datastore specified */
1362 return create_error("Invalid target repository type requested.");
1363 }
1364
Tomas Cejka55c6df52014-02-20 12:59:33 +01001365 /* source can be missing when config is given */
1366 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001367 reply = create_error("invalid input parameters - source and config is required.");
1368 return reply;
1369 }
1370
1371 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001372 if (uri_src == NULL) {
1373 uri_src = "";
1374 }
1375 }
1376 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001377 if (uri_trg == NULL) {
1378 uri_trg = "";
1379 }
1380 }
1381 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001382
1383 CHECK_ERR_SET_REPLY
1384
Tomas Cejkad5b53772013-06-08 23:01:07 +02001385 return reply;
1386}
1387
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001388json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001389{
1390 json_object *reply = NULL;
1391 const char *config = NULL;
1392 char *data = NULL;
1393
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001394 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001395
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001396 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001397 config = json_object_get_string(json_object_object_get(request, "content"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001398 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001399
1400 if (config == NULL) {
1401 return create_error("Missing content parameter.");
1402 }
1403
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001404 /* TODO */
1405 reply = netconf_generic(session_key, config, &data);
1406 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001407 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001408 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001409 /* use filled err_reply from libnetconf's callback */
1410 reply = err_reply;
1411 }
1412 } else {
1413 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001414 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001415 reply = json_object_new_object();
1416 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001417 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001418 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001419 reply = create_data(data);
1420 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001421 }
1422 }
1423 return reply;
1424}
1425
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001426json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001427{
1428 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001429 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001430
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001431 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001432 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001433 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001434 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001435 }
1436 return reply;
1437}
1438
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001439json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001440{
1441 json_object *reply = NULL;
1442 const char *sid = NULL;
1443
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001444 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001445
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001446 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001447 sid = json_object_get_string(json_object_object_get(request, "session-id"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001448 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449
1450 if (sid == NULL) {
1451 return create_error("Missing session-id parameter.");
1452 }
1453
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001454 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001455
1456 CHECK_ERR_SET_REPLY
1457
Tomas Cejkad5b53772013-06-08 23:01:07 +02001458 return reply;
1459}
1460
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001461json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001462{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001463 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001464 struct session_with_mutex * locked_session = NULL;
1465 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001466 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001467
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001468 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001469 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001470 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001471 return NULL;
1472 }
1473
Tomas Cejkad5b53772013-06-08 23:01:07 +02001474 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1475 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001476 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001477 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001478 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001479 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001480 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001481 prepare_status_message(locked_session, temp_session);
1482 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001483 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001484 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001485 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001486 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001487 reply = create_error("Reload was unsuccessful, connection failed.");
1488 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001489 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001490 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001491 DEBUG("UNLOCK wrlock %s", __func__);
1492 if (pthread_rwlock_unlock(&session_lock) != 0) {
1493 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1494 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001495 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001496 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001497 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001498 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001499 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001500 reply = create_error("Invalid session identifier.");
1501 }
1502
1503 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1504 reply = locked_session->hello_message;
1505 }
1506 return reply;
1507}
1508
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001509json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001510{
1511 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001512 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001513 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001514
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001516 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001517 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001518 }
1519
Tomas Cejkad5b53772013-06-08 23:01:07 +02001520 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1521 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001522 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001523 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001524 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001525 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001526 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001527 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001528 if (locked_session->hello_message != NULL) {
1529 reply = locked_session->hello_message;
1530 } else {
1531 reply = create_error("Invalid session identifier.");
1532 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001533 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001534 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001535 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001536 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001537 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001538 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001539 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001540 reply = create_error("Invalid session identifier.");
1541 }
1542
Tomas Cejka47387fd2013-06-10 20:37:46 +02001543
Tomas Cejkad5b53772013-06-08 23:01:07 +02001544 return reply;
1545}
1546
Tomas Cejka6b886e02013-07-05 09:53:17 +02001547void notification_history(time_t eventtime, const char *content)
1548{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001549 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1550 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001551 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001552 return;
1553 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001554 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001555 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001556 json_object *notif = json_object_new_object();
1557 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001558 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001559 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001560 }
1561 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1562 json_object_object_add(notif, "content", json_object_new_string(content));
1563 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001564failed:
1565 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001566}
1567
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001568json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001569{
1570 json_object *reply = NULL;
1571 const char *sid = NULL;
1572 struct session_with_mutex *locked_session = NULL;
1573 struct nc_session *temp_session = NULL;
1574 nc_rpc *rpc = NULL;
1575 time_t start = 0;
1576 time_t stop = 0;
1577 int64_t from, to;
1578
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001579 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001580
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001581 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001582 sid = json_object_get_string(json_object_object_get(request, "session"));
1583 from = json_object_get_int64(json_object_object_get(request, "from"));
1584 to = json_object_get_int64(json_object_object_get(request, "to"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001585 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001586
1587 start = time(NULL) + from;
1588 stop = time(NULL) + to;
1589
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001590 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001591
1592 if (sid == NULL) {
1593 return create_error("Missing session parameter.");
1594 }
1595
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001596 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001597 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001598 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001599 return NULL;
1600 }
1601
1602 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1603 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001604 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001605 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001606 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001607 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001608 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001609 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001610 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001611 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1612 if (temp_session != NULL) {
1613 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1614 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001615 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001616 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001617 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001618 return create_error("notifications: creating an rpc request failed.");
1619 }
1620
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001621 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001622 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001623 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001624 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001625 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001626 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001627 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001628 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001629 }
1630 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1631
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001632 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001633 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001634 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001635 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001636 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001637 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001638 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001639 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001640 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001641 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001642
1643 ncntf_dispatch_receive(temp_session, notification_history);
1644
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001645 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001646 reply = json_object_new_object();
1647 json_object_object_add(reply, "notifications", notif_history_array);
1648 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001649 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001650
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001651 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001652 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001653 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001654 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001655 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001656 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001657 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001658 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001659 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001660 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1661 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001662 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001663 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001664 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001665 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001666 }
1667 reply = create_error("Invalid session identifier.");
1668 }
1669
Tomas Cejka4003a702013-10-01 00:02:45 +02001670 return reply;
1671}
1672
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001673json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001674{
1675 json_object *reply = NULL;
1676 const char *sid = NULL;
1677 const char *target = NULL;
1678 const char *url = NULL;
1679 struct session_with_mutex *locked_session = NULL;
1680 nc_rpc *rpc = NULL;
1681 NC_DATASTORE target_ds;
1682
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001683 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001684
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001685 pthread_mutex_lock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001686 sid = json_object_get_string(json_object_object_get(request, "session"));
1687 target = json_object_get_string(json_object_object_get(request, "target"));
1688 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001689 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001690
1691
1692 if ((sid == NULL) || (target == NULL)) {
1693 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001694 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001695
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001696 /* validation */
1697 target_ds = parse_datastore(target);
1698 if (target_ds == NC_DATASTORE_URL) {
1699 if (url != NULL) {
1700 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001701 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001702 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001703 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001704 rpc = nc_rpc_validate(target_ds);
1705 }
1706 if (rpc == NULL) {
1707 DEBUG("mod_netconf: creating rpc request failed");
1708 reply = create_error("Creation of RPC request failed.");
1709 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001710 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001711 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001712 }
1713
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001714 DEBUG("Request: validate datastore");
1715 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001716
1717 CHECK_ERR_SET_REPLY
1718
1719 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001720 DEBUG("Request: validation ok.");
1721 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001722 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001723 }
1724 nc_rpc_free (rpc);
1725
Tomas Cejka6b886e02013-07-05 09:53:17 +02001726 return reply;
1727}
1728
David Kupka8e60a372012-09-04 09:15:20 +02001729void * thread_routine (void * arg)
1730{
1731 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001732 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001733 json_object *request = NULL;
1734 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001735 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001736 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001737 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001738 const char *session_key;
1739 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001740 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001741 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001742 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001743 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001744 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001745 int client = ((struct pass_to_thread*)arg)->client;
1746
Tomas Cejka00635972013-06-03 15:10:52 +02001747 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001748
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001749 /* init thread specific err_reply memory */
1750 json_object **err_reply = calloc(1, sizeof(json_object **));
1751 if (err_reply == NULL) {
1752 DEBUG("Allocation of err_reply storage failed!");
1753 isterminated = 1;
1754 }
1755 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
1756 DEBUG("notif_history: cannot set thread-specific hash value.");
1757 }
David Kupka8e60a372012-09-04 09:15:20 +02001758 while (!isterminated) {
1759 fds.fd = client;
1760 fds.events = POLLIN;
1761 fds.revents = 0;
1762
1763 status = poll(&fds, 1, 1000);
1764
1765 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1766 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001767 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001768 continue;
1769 } else if (status < 0) {
1770 /* 0: poll time outed
1771 * close socket and ignore this request from the client, it can try it again
1772 * -1: poll failed
1773 * something wrong happend, close this socket and wait for another request
1774 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001775 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001776 close(client);
1777 break;
1778 }
1779 /* status > 0 */
1780
1781 /* check the status of the socket */
1782
1783 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1784 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1785 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001786 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001787 close(client);
1788 break;
1789 }
1790
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001791 DEBUG("Get framed message...");
1792 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001793
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001794 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001795 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001796 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001797 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001798 request = json_tokener_parse_verbose(buffer, &jerr);
1799 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001800 DEBUG("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001801 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001802 continue;
1803 }
David Kupka8e60a372012-09-04 09:15:20 +02001804
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001805 operation = json_object_get_int(json_object_object_get(request, "type"));
Tomas Cejka64b87482013-06-03 16:30:53 +02001806 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001807 pthread_mutex_unlock(&json_lock);
1808
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001809 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001810 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1811 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001812 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001813 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001814 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001815 pthread_mutex_unlock(&json_lock);
1816
David Kupka8e60a372012-09-04 09:15:20 +02001817 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001818
1819 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001820 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001821 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001822 /* there is some stupid client, so close the connection to give a chance to some other client */
1823 close(client);
1824 break;
1825 }
1826
David Kupka8e60a372012-09-04 09:15:20 +02001827 /* null global JSON error-reply */
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001828 (*err_reply) = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001829
1830 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001831 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001832 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001833 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001834 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001835 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001836 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001837
1838 /* process required operation */
1839 switch (operation) {
1840 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001841 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001842 break;
1843 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001844 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001845 break;
1846 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001847 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001848 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001849 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001850 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001851 break;
David Kupka8e60a372012-09-04 09:15:20 +02001852 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001853 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001854 break;
1855 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001856 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001857 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001858
David Kupka8e60a372012-09-04 09:15:20 +02001859 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001860 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001861 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001862 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001863 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001864 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1865 ds_type_t = parse_datastore(target);
1866 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001867 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001868
1869 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001870 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001871 break;
1872 }
David Kupka8e60a372012-09-04 09:15:20 +02001873 switch(operation) {
1874 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001875 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001876 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001877 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001878 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001879 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001880 break;
1881 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001882 DEBUG("Request: lock (session %s)", session_key);
1883 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001884 break;
1885 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001886 DEBUG("Request: unlock (session %s)", session_key);
1887 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001888 break;
1889 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001890 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001891 break;
1892 }
1893
Tomas Cejkac7929632013-10-24 19:25:15 +02001894 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001895 if (*err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001896 /** \todo more clever error message wanted */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001897 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001898 reply = json_object_new_object();
1899 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001900 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001901 } else {
1902 /* use filled err_reply from libnetconf's callback */
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001903 reply = *err_reply;
David Kupka8e60a372012-09-04 09:15:20 +02001904 }
David Kupka8e60a372012-09-04 09:15:20 +02001905 }
1906 break;
1907 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001908 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001909 break;
1910 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001911 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001912 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001913 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001914 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001915 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001916 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001917 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001918 break;
1919 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001920 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001921 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001922 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001923 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001924 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001925 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001926 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001927 break;
David Kupka8e60a372012-09-04 09:15:20 +02001928 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001929 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001930 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001931 break;
1932 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001933 DEBUG("Clean request json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001934 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001935 json_object_put(request);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001936 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001937 /* send reply to caller */
1938 if (reply != NULL) {
1939 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001940 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001941 if (buffer != NULL) {
1942 free(buffer);
1943 buffer = NULL;
1944 }
David Kupka8e60a372012-09-04 09:15:20 +02001945 break;
1946 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001947 pthread_mutex_unlock(&json_lock);
1948
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001949 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001950 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001951 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001952 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001953 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001954 reply = NULL;
1955 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001956 free(chunked_out_msg);
1957 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001958 if (buffer != NULL) {
1959 free(buffer);
1960 buffer = NULL;
1961 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001962 if ((err_reply != NULL) && (*err_reply != NULL)) {
1963 json_object_put(*err_reply);
1964 *err_reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001965 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001966 pthread_mutex_unlock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001967 } else {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001968 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001969 DEBUG("Reply is NULL, shouldn't be...");
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001970 if (*err_reply == NULL) {
1971 DEBUG("No error was set - really strange situation");
1972 } else {
1973 DEBUG("Error was set but it was not sent.");
1974 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001975 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001976 }
1977 }
1978 }
David Kupka8e60a372012-09-04 09:15:20 +02001979 free (arg);
1980
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001981 err_reply = (json_object **) pthread_getspecific(err_reply_key);
1982 if (err_reply != NULL) {
1983 if (*err_reply != NULL) {
1984 pthread_mutex_lock(&json_lock);
1985 json_object_put(*err_reply);
1986 pthread_mutex_unlock(&json_lock);
1987 }
1988 free(err_reply);
1989 err_reply = NULL;
1990 }
1991
David Kupka8e60a372012-09-04 09:15:20 +02001992 return retval;
1993}
1994
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001995/**
1996 * \brief Close all open NETCONF sessions.
1997 *
1998 * During termination of mod_netconf, it is useful to close all remaining
1999 * sessions. This function iterates over the list of sessions and close them
2000 * all.
2001 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002002 * \param[in] p apr pool needed for hash table iterating
2003 * \param[in] ht hash table of session_with_mutex structs
2004 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002005static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002006{
2007 apr_hash_index_t *hi;
2008 void *val = NULL;
2009 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002010 const char *hashed_key = NULL;
2011 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002012 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002013
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002014 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002015 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002016 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002017 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002018 return;
2019 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002020 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002021 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002022 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002023 swm = (struct session_with_mutex *) val;
2024 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002025 DEBUG("LOCK mutex %s", __func__);
2026 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002027 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002028 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002029 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002030
2031 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002032 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002033 }
2034 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002035 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002036 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002037 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002038 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002039 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002040}
2041
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002042static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002043{
2044 apr_hash_index_t *hi;
2045 void *val = NULL;
2046 struct nc_session *ns = NULL;
2047 struct session_with_mutex *swm = NULL;
2048 const char *hashed_key = NULL;
2049 apr_ssize_t hashed_key_length;
2050 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002051 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002052
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002053 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002054//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002055 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002056 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002057 return;
2058 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002059 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002060 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2061 swm = (struct session_with_mutex *) val;
2062 if (swm == NULL) {
2063 continue;
2064 }
2065 ns = swm->session;
2066 if (ns == NULL) {
2067 continue;
2068 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002069//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002070 pthread_mutex_lock(&swm->lock);
2071 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002072 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002073 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002074 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002075//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002076 pthread_mutex_unlock(&swm->lock);
2077
2078 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002079 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002080 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002081//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002082 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002083 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002084 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002085 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002086//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002087 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002088 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002089 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002090}
2091
2092
2093/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002094 * This is actually implementation of NETCONF client
2095 * - requests are received from UNIX socket in the predefined format
2096 * - results are replied through the same way
2097 * - the daemon run as a separate process, but it is started and stopped
2098 * automatically by Apache.
2099 *
2100 */
Radek Krejci469aab82012-07-22 18:42:20 +02002101static void forked_proc(apr_pool_t * pool, server_rec * server)
2102{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002103 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002104 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002105 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002106 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002107 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002108 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002109 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002110 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002111 struct timespec maxtime;
2112 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002113 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002114 #ifdef WITH_NOTIFICATIONS
2115 char use_notifications = 0;
2116 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002117
Tomas Cejka6b886e02013-07-05 09:53:17 +02002118 http_server = server;
2119
Tomas Cejka404d37e2013-04-13 02:31:35 +02002120 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002121 maxtime.tv_sec = 5;
2122 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002123
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002124 #ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002125 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002126 unixd_setup_child();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002127 #endif
Radek Krejci469aab82012-07-22 18:42:20 +02002128
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002129 if (server != NULL) {
2130 cfg = ap_get_module_config(server->module_config, &netconf_module);
2131 if (cfg == NULL) {
2132 DEBUG("Getting mod_netconf configuration failed");
2133 return;
2134 }
2135 sockname = cfg->sockname;
2136 } else {
2137 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002138 }
Radek Krejci469aab82012-07-22 18:42:20 +02002139
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002140 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002141 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002142 DEBUG("Creating socket failed (%s)", strerror(errno));
2143 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002144 }
2145
2146 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002147 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002148 unlink(local.sun_path);
2149 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2150
2151 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2152 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002153 DEBUG("mod_netconf socket address already in use");
2154 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002155 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002156 DEBUG("Binding socket failed (%s)", strerror(errno));
2157 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002158 }
2159
2160 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002161 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2162 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002163 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002164 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002165
Tomas Cejkaba21b382013-04-13 02:37:32 +02002166 /* prepare internal lists */
2167 netconf_sessions_list = apr_hash_make(pool);
2168
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002169 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002170 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002171 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002172 use_notifications = 0;
2173 } else {
2174 use_notifications = 1;
2175 }
2176 #endif
2177
Radek Krejci469aab82012-07-22 18:42:20 +02002178 /* setup libnetconf's callbacks */
2179 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002180 nc_callback_print(clb_print);
2181 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2182 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2183 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002184 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002185
2186 /* disable publickey authentication */
2187 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2188
David Kupka8e60a372012-09-04 09:15:20 +02002189 /* create mutex protecting session list */
2190 pthread_rwlockattr_init(&lock_attrs);
2191 /* rwlock is shared only with threads in this process */
2192 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2193 /* create rw lock */
2194 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002195 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2196 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002197 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002198 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002199 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002200 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002201 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002202 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002203 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002204 DEBUG("init of err_reply_key.");
2205 if (pthread_key_create(&err_reply_key, NULL) != 0) {
2206 DEBUG("init of err_reply_key failed");
2207 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002208
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002209 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002210 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002211 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002212 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002213 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002214 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002215 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002216 }
2217 if (use_notifications == 1) {
2218 notification_handle();
2219 }
2220 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002221 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002222 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002223 }
Radek Krejci469aab82012-07-22 18:42:20 +02002224
2225 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002226 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002227 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002228 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002229 olds = tv.tv_sec;
2230 }
David Kupka8e60a372012-09-04 09:15:20 +02002231 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002232 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2233 apr_sleep(SLEEP_TIME);
2234 continue;
2235 } else if (client == -1 && (errno == EINTR)) {
2236 continue;
2237 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002238 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002239 continue;
2240 }
Radek Krejci469aab82012-07-22 18:42:20 +02002241
2242 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002243 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002244
David Kupka8e60a372012-09-04 09:15:20 +02002245 arg = malloc (sizeof(struct pass_to_thread));
2246 arg->client = client;
2247 arg->pool = pool;
2248 arg->server = server;
2249 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002250
David Kupka8e60a372012-09-04 09:15:20 +02002251 /* start new thread. It will serve this particular request and then terminate */
2252 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002253 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002254 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002255 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002256 pthread_count++;
2257 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2258 ptids[pthread_count] = 0;
2259 }
Radek Krejci469aab82012-07-22 18:42:20 +02002260
David Kupka8e60a372012-09-04 09:15:20 +02002261 /* check if some thread already terminated, free some resources by joining it */
2262 for (i=0; i<pthread_count; i++) {
2263 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002264 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002265 pthread_count--;
2266 if (pthread_count > 0) {
2267 /* place last Thread ID on the place of joined one */
2268 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002269 }
Radek Krejci469aab82012-07-22 18:42:20 +02002270 }
2271 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002272 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002273 }
2274
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002275 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002276 /* join all threads */
2277 for (i=0; i<pthread_count; i++) {
2278 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2279 }
Radek Krejci469aab82012-07-22 18:42:20 +02002280
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002281 #ifdef WITH_NOTIFICATIONS
2282 notification_close();
2283 #endif
2284
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002285 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002286 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002287
David Kupka8e60a372012-09-04 09:15:20 +02002288 /* destroy rwlock */
2289 pthread_rwlock_destroy(&session_lock);
2290 pthread_rwlockattr_destroy(&lock_attrs);
2291
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002292 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002293
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002294 free(ptids);
2295 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002296 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002297 return;
2298error_exit:
2299 close(lsock);
2300 free(ptids);
2301 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002302}
2303
Radek Krejcif23850c2012-07-23 16:14:17 +02002304static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002305{
Radek Krejcif23850c2012-07-23 16:14:17 +02002306 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2307 apr_pool_create(&config->pool, pool);
2308 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002309 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002310
2311 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002312}
2313
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002314#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002315static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2316 apr_pool_t * plog, server_rec * s)
2317{
Radek Krejcif23850c2012-07-23 16:14:17 +02002318 mod_netconf_cfg *config;
2319 apr_status_t res;
2320
Radek Krejci469aab82012-07-22 18:42:20 +02002321 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002322 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002323 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002324
2325 /*
2326 * The following checks if this routine has been called before.
2327 * This is necessary because the parent process gets initialized
2328 * a couple of times as the server starts up.
2329 */
2330 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2331 if (!data) {
2332 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2333 return (OK);
2334 }
2335
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002336 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002337 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002338
Radek Krejcif23850c2012-07-23 16:14:17 +02002339 if (config && config->forkproc == NULL) {
2340 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2341 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002342 switch (res) {
2343 case APR_INCHILD:
2344 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002345 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002346 apr_signal(SIGTERM, signal_handler);
2347
2348 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002349 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002350
2351 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002352 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002353
Radek Krejcif23850c2012-07-23 16:14:17 +02002354 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002355 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002356 exit(APR_EGENERAL);
2357 break;
2358 case APR_INPARENT:
2359 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002360 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002361 break;
2362 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002363 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002364 break;
2365 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002366 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002367 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002368 }
2369
2370 return OK;
2371}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002372#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002373
Radek Krejci469aab82012-07-22 18:42:20 +02002374/**
2375 * Register module hooks
2376 */
2377static void mod_netconf_register_hooks(apr_pool_t * p)
2378{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002379#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002380 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002381#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002382}
2383
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002384static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2385{
2386 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2387 return NULL;
2388}
2389
2390static const command_rec netconf_cmds[] = {
2391 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2392 {NULL}
2393};
2394
Radek Krejci469aab82012-07-22 18:42:20 +02002395/* Dispatch list for API hooks */
2396module AP_MODULE_DECLARE_DATA netconf_module = {
2397 STANDARD20_MODULE_STUFF,
2398 NULL, /* create per-dir config structures */
2399 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002400 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002401 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002402 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002403 mod_netconf_register_hooks /* register hooks */
2404};
Radek Krejcia332b692012-11-12 16:15:54 +01002405
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002406int main(int argc, char **argv)
2407{
2408 apr_pool_t *pool;
2409 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2410 apr_signal(SIGTERM, signal_handler);
2411 apr_signal(SIGINT, signal_handler);
2412 apr_pool_create(&pool, NULL);
2413 forked_proc(pool, NULL);
2414 apr_pool_destroy(pool);
2415 apr_terminate();
2416 DEBUG("Terminated");
2417 return 0;
2418}