blob: 8bc2435f99d2f9b27a5c512c14b9c5a4acf3b506 [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>
Tomas Cejka04e08f42014-03-27 19:52:34 +010053#include <pwd.h>
54#include <grp.h>
David Kupka8e60a372012-09-04 09:15:20 +020055#include <pthread.h>
56#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020057
58#include <unixd.h>
59#include <httpd.h>
60#include <http_log.h>
61#include <http_config.h>
62
63#include <apr_sha1.h>
64#include <apr_hash.h>
65#include <apr_signal.h>
66#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020067
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020068#include <json/json.h>
69
Radek Krejci469aab82012-07-22 18:42:20 +020070#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020071#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020072
Tomas Cejka04e08f42014-03-27 19:52:34 +010073#include "../config.h"
74
Tomas Cejkad340dbf2013-03-24 20:36:57 +010075#ifdef WITH_NOTIFICATIONS
76#include "notification_module.h"
77#endif
78
Tomas Cejka94da2c52013-01-08 18:20:30 +010079#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020080#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020081
82#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020083#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020084#define MAX_SOCKET_CL 10
85#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020086#define NOTIFICATION_QUEUE_SIZE 10
87#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020088#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020089
90/* sleep in master process for non-blocking socket reading */
91#define SLEEP_TIME 200
92
93#ifndef offsetof
94#define offsetof(type, member) ((size_t) ((type *) 0)->member)
95#endif
96
Tomas Cejkaef531ee2013-11-12 16:07:00 +010097server_rec *http_server = NULL;
98
Tomas Cejka027f3bc2012-11-10 20:28:36 +010099/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +0200100struct timeval timeout = { 1, 0 };
101
Tomas Cejka5064c232013-01-17 09:30:58 +0100102#define NCWITHDEFAULTS NCWD_MODE_NOTSET
103
104
Radek Krejci469aab82012-07-22 18:42:20 +0200105#define MSG_OK 0
106#define MSG_OPEN 1
107#define MSG_DATA 2
108#define MSG_CLOSE 3
109#define MSG_ERROR 4
110#define MSG_UNKNOWN 5
111
Radek Krejci469aab82012-07-22 18:42:20 +0200112module AP_MODULE_DECLARE_DATA netconf_module;
113
Tomas Cejka47387fd2013-06-10 20:37:46 +0200114pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200115pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100116pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100117pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
118
Tomas Cejka47387fd2013-06-10 20:37:46 +0200119apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200120
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200121static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200122
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100123static pthread_key_t err_reply_key;
124
125#define GETSPEC_ERR_REPLY json_object *err_reply = *((json_object **) pthread_getspecific(err_reply_key));
126
127#define CHECK_ERR_SET_REPLY \
128if (reply == NULL) { \
129 GETSPEC_ERR_REPLY \
130 if (err_reply != NULL) { \
131 /* use filled err_reply from libnetconf's callback */ \
132 reply = err_reply; \
133 } \
134}
135
136#define CHECK_ERR_SET_REPLY_ERR(errmsg) \
137if (reply == NULL) { \
138 GETSPEC_ERR_REPLY \
139 if (err_reply == NULL) { \
140 reply = create_error(errmsg); \
141 } else { \
142 /* use filled err_reply from libnetconf's callback */ \
143 reply = err_reply; \
144 } \
145}
146
Radek Krejci469aab82012-07-22 18:42:20 +0200147volatile int isterminated = 0;
148
149static char* password;
150
Radek Krejci469aab82012-07-22 18:42:20 +0200151static void signal_handler(int sign)
152{
153 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100154 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200155 case SIGTERM:
156 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200157 break;
158 }
159}
160
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200161static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200162{
Radek Krejcif23850c2012-07-23 16:14:17 +0200163 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
164 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200165 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200166
Radek Krejci469aab82012-07-22 18:42:20 +0200167 apr_sha1_ctx_t sha1_ctx;
168 apr_sha1_init(&sha1_ctx);
169 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
170 apr_sha1_update(&sha1_ctx, port, strlen(port));
171 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200172 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200173
Radek Krejcif23850c2012-07-23 16:14:17 +0200174 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200175 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200176 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200177 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200178 }
179 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200180
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200181 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200182}
183
184int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
185{
186 /* always approve */
187 return (EXIT_SUCCESS);
188}
189
190char* netconf_callback_sshauth_password (const char* username, const char* hostname)
191{
192 char* buf;
193
194 buf = malloc ((strlen(password) + 1) * sizeof(char));
195 apr_cpystrn(buf, password, strlen(password) + 1);
196
197 return (buf);
198}
199
200void netconf_callback_sshauth_interactive (const char* name,
201 int name_len,
202 const char* instruction,
203 int instruction_len,
204 int num_prompts,
205 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
206 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
207 void** abstract)
208{
209 int i;
210
211 for (i=0; i<num_prompts; i++) {
212 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
213 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
214 responses[i].length = strlen(responses[i].text) + 1;
215 }
216
217 return;
218}
219
Radek Krejcic11fd862012-07-26 12:41:21 +0200220void netconf_callback_error_process(const char* tag,
221 const char* type,
222 const char* severity,
223 const char* apptag,
224 const char* path,
225 const char* message,
226 const char* attribute,
227 const char* element,
228 const char* ns,
229 const char* sid)
230{
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100231 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
232 json_object *err_reply = *err_reply_p;
233
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100234 json_object *array = NULL;
235 if (err_reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100236 DEBUG("error calback: empty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100237 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100238 err_reply = json_object_new_object();
239 array = json_object_new_array();
240 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
241 json_object_object_add(err_reply, "errors", array);
242 if (message != NULL) {
243 json_object_array_add(array, json_object_new_string(message));
244 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100245 pthread_mutex_unlock(&json_lock);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100246 (*err_reply_p) = err_reply;
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100247 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100248 DEBUG("error calback: nonempty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100249 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100250 array = json_object_object_get(err_reply, "errors");
251 if (array != NULL) {
252 if (message != NULL) {
253 json_object_array_add(array, json_object_new_string(message));
254 }
255 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100256 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100257 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100258 pthread_setspecific(err_reply_key, err_reply_p);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100259 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200260}
261
Tomas Cejka47387fd2013-06-10 20:37:46 +0200262/**
263 * should be used in locked area
264 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100265void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200266{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200267 json_object *json_obj = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100268 char *old_sid = NULL;
269 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200270 const char *cpbltstr;
271 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200272
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200273 if (s == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100274 DEBUG("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200275 return;
276 }
277
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100278 pthread_mutex_lock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200279 if (s->hello_message != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100280 DEBUG("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200281 //json_object_put(s->hello_message);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100282 j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid"));
283 if (j_old_sid != NULL) {
284 old_sid = strdup(j_old_sid);
285 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100286 json_object_put(s->hello_message);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200287 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200288 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100289 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200290 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100291 if (old_sid != NULL) {
292 /* use previous sid */
293 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
294 free(old_sid);
Tomas Cejkaa3ffdba2014-03-27 15:12:21 +0100295 old_sid = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100296 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200297 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100298 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
299 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200300 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
301 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
302 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
303 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
304 cpblts = nc_session_get_cpblts (session);
305 if (cpblts != NULL) {
306 json_obj = json_object_new_array();
307 nc_cpblts_iter_start (cpblts);
308 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
309 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
310 }
311 json_object_object_add(s->hello_message, "capabilities", json_obj);
312 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100313 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200314 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100315 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200316 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
317 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
318 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100319 DEBUG("Status info from hello message prepared");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100320 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200321
322}
323
324
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200325/**
326 * \defgroup netconf_operations NETCONF operations
327 * The list of NETCONF operations that mod_netconf supports.
328 * @{
329 */
330
331/**
332 * \brief Connect to NETCONF server
333 *
334 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
335 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100336static 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 +0200337{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200338 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200339 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200340 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200341
342 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200343 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100344 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200345 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200346 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100347 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200348
Radek Krejci469aab82012-07-22 18:42:20 +0200349 /* if connected successful, add session to the list */
350 if (session != NULL) {
351 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200352 session_key = gen_ncsession_hash(
353 (host==NULL) ? "localhost" : host,
354 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200355 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200356
Tomas Cejkaba21b382013-04-13 02:37:32 +0200357 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100358 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 +0200359 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100360 session = NULL;
361 free(locked_session);
362 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100363 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200364 return NULL;
365 }
366 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200367 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200368 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200369 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200370 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100371 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200372 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100373 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200374 if (pthread_rwlock_wrlock (&session_lock) != 0) {
375 nc_session_free(session);
376 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100377 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200378 return NULL;
379 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200380 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200381 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100382 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200383 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 +0100384 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200385
Tomas Cejka47387fd2013-06-10 20:37:46 +0200386 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100387 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200388 pthread_mutex_lock(&locked_session->lock);
389
390 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100391 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200392 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100393 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200394 }
395
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200396 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100397 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200398
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100399 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200400 pthread_mutex_unlock(&locked_session->lock);
401
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100402 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200403 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200404 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100405 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200406 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200407 }
408
Radek Krejci469aab82012-07-22 18:42:20 +0200409}
410
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100411static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200412{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100413 DEBUG("lock private lock.");
414 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200415 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100416 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200417 }
418 locked_session->ntfc_subscribed = 0;
419 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100420 if (locked_session->session != NULL) {
421 nc_session_free(locked_session->session);
422 locked_session->session = NULL;
423 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100424 DEBUG("session closed.");
425 DEBUG("unlock private lock.");
426 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200427 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100428 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200429 }
430
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100431 DEBUG("unlock session lock.");
432 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200433 usleep(500000); /* let notification thread stop */
434
435 /* session shouldn't be used by now */
436 /** \todo free all notifications from queue */
437 apr_array_clear(locked_session->notifications);
438 pthread_mutex_destroy(&locked_session->lock);
439 if (locked_session->hello_message != NULL) {
440 /** \todo free hello_message */
441 //json_object_put(locked_session->hello_message);
442 locked_session->hello_message = NULL;
443 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200444 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200445 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200446 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200448 return (EXIT_SUCCESS);
449}
450
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100451static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200452{
David Kupka8e60a372012-09-04 09:15:20 +0200453 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200454
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100455 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200456
David Kupka8e60a372012-09-04 09:15:20 +0200457 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100458 DEBUG("lock session lock.");
459 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200460 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100461 DEBUG("Error while locking rwlock");
462 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200463 return EXIT_FAILURE;
464 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200465 /* get session to close */
466 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
467 /* remove session from the active sessions list -> nobody new can now work with session */
468 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200469
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100470 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200471 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100472 DEBUG("Error while unlocking rwlock");
473 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200474 }
475
476 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100477 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200478 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100479 DEBUG("Unknown session to close");
480 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200481 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200482 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100483 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200484}
485
Tomas Cejkac7929632013-10-24 19:25:15 +0200486/**
487 * Test reply message type and return error message.
488 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200489 * \param[in] session nc_session internal struct
490 * \param[in] session_key session key, NULL to disable disconnect on error
491 * \param[in] msgt RPC-REPLY message type
492 * \param[out] data
493 * \return NULL on success
494 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100495json_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 +0200496{
497 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100498 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200499
500 /* process the result of the operation */
501 switch (msgt) {
502 case NC_MSG_UNKNOWN:
503 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100504 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200505 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100506 netconf_close(session_key, &err);
507 }
508 if (err != NULL) {
509 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200510 }
511 return create_error("Internal: Receiving RPC-REPLY failed.");
512 }
513 case NC_MSG_NONE:
514 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100515 if (data != NULL) {
516 free(*data);
517 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200518 (*data) = NULL;
519 return NULL;
520 case NC_MSG_REPLY:
521 switch (replyt = nc_reply_get_type(reply)) {
522 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100523 if ((data != NULL) && (*data != NULL)) {
524 free(*data);
525 (*data) = NULL;
526 }
527 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200528 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100529 if (((*data) = nc_reply_get_data(reply)) == NULL) {
530 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200531 return create_error("Internal: No data from reply received.");
532 } else {
533 return NULL;
534 }
535 break;
536 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100537 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
538 if (data != NULL) {
539 free(*data);
540 (*data) = NULL;
541 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200542 return create_error(nc_reply_get_errormsg(reply));
543 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100544 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
545 if (data != NULL) {
546 free(*data);
547 (*data) = NULL;
548 }
Tomas Cejka60885252014-03-26 15:45:47 +0100549 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200550 }
551 break;
552 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100553 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
554 if (data != NULL) {
555 free(*data);
556 (*data) = NULL;
557 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200558 return create_error("Internal: Unexpected RPC-REPLY message type.");
559 }
560}
561
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100562json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200563{
564 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200565 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200566
567 /* check requests */
568 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100569 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200570 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200571 }
572
573 if (session != NULL) {
574 /* send the request and get the reply */
575 msgt = nc_session_send_recv(session, rpc, &reply);
576 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100577 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200578 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100579 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200580 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200581 }
582}
583
Tomas Cejkac7929632013-10-24 19:25:15 +0200584/**
585 * Perform RPC method that returns data.
586 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200587 * \param[in] session_key session identifier
588 * \param[in] rpc RPC message to perform
589 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
590 * \return NULL on success, json object with error otherwise
591 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100592static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200593{
594 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200595 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200596 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200597 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100598 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100599 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200600
Radek Krejci8e4632a2012-07-26 13:40:34 +0200601 /* check requests */
602 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100603 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200604 res = create_error("Internal: RPC could not be created.");
605 data = NULL;
606 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200607 }
608
David Kupka8e60a372012-09-04 09:15:20 +0200609 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100610 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200611 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100612 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200613 res = create_error("Internal: Lock failed.");
614 data = NULL;
615 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200616 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200617 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200618 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 +0200619 if (locked_session != NULL) {
620 session = locked_session->session;
621 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200622 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200623 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100624 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200625 if (pthread_mutex_lock(&locked_session->lock) != 0) {
626 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200628 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200629
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100630 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200631 res = create_error("Internal: Could not unlock.");
632 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200633 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200634 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200635 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100636 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200637 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200638
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100639 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200640 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200641 }
642
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200643 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200644
Radek Krejci035bf4e2012-07-25 10:59:09 +0200645 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100646 msgt = nc_session_send_recv(session, rpc, &reply);
647
David Kupka8e60a372012-09-04 09:15:20 +0200648 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100649 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200650 pthread_mutex_unlock(&locked_session->lock);
651 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200652
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100653 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200654 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100655 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100657 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100658 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100659 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100660 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200661 res = create_error("Unknown session to process.");
662 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200663 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200664finished:
665 nc_reply_free(reply);
666 if (received_data != NULL) {
667 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100668 } else {
669 if (data != NULL) {
670 free(data);
671 data = NULL;
672 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200673 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200674 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200675}
676
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100677static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200678{
679 nc_rpc* rpc;
680 struct nc_filter *f = NULL;
681 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200682 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200683
684 /* create filter if set */
685 if (filter != NULL) {
686 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
687 }
688
689 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100690 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200691 nc_filter_free(f);
692 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100693 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200694 return (NULL);
695 }
696
Tomas Cejka94674662013-09-13 15:55:24 +0200697 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100698#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100699 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100700#else
701 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
702#endif
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100703 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200704 }
705
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100706 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200707 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100708 if (res != NULL) {
709 (*err) = res;
710 } else {
711 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200712 }
713
Radek Krejci8e4632a2012-07-26 13:40:34 +0200714 return (data);
715}
716
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100717static 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 +0100718{
719 nc_rpc* rpc;
720 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200721 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100722
723 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100724 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100725 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100726 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100727 return (NULL);
728 }
729
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100730 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100731 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100732 if (res != NULL) {
733 (*err) = res;
734 } else {
735 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200736 }
737
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100738 return (data);
739}
740
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100741static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200742{
743 nc_rpc* rpc;
744 struct nc_filter *f = NULL;
745 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200746 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200747
748 /* create filter if set */
749 if (filter != NULL) {
750 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
751 }
752
753 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100754 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200755 nc_filter_free(f);
756 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100757 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200758 return (NULL);
759 }
760
Tomas Cejka94674662013-09-13 15:55:24 +0200761 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100762 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100763 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200764 }
765
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100766 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200767 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200768 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100769 (*err) = res;
770 } else {
771 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200772 }
773
Radek Krejci8e4632a2012-07-26 13:40:34 +0200774 return (data);
775}
776
Tomas Cejkab4d05872014-02-14 22:44:38 +0100777static 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 +0200778{
779 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200780 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200781
782 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100783 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100784 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100785 /* config, url */
786 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100787 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100788 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100789 rpc = nc_rpc_copyconfig(source, target, config);
790 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100791 } else if (source == NC_DATASTORE_URL) {
792 if (target == NC_DATASTORE_URL) {
793 /* url, url */
794 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
795 } else {
796 /* url, datastore */
797 rpc = nc_rpc_copyconfig(source, target, uri_src);
798 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100799 } else {
800 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100801 /* datastore, url */
802 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100803 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100804 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100805 rpc = nc_rpc_copyconfig(source, target);
806 }
807 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200808 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100809 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200810 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200811 }
812
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100813 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200814 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200815
816 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200817}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200818
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100819static 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 +0200820{
821 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200822 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200823
824 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100825 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200826 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100827 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200828 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200829 }
830
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100831 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200832 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200833
834 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200835}
836
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100837static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200838{
839 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200840 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200841
842 /* create requests */
843 rpc = nc_rpc_killsession(sid);
844 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100845 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200846 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200847 }
848
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100849 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200850 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200851 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200852}
853
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100854static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200855{
856 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200857 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200858
859 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200860 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200861 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100862 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200863 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200864 }
865
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100866 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200867 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200868 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200869}
870
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100871static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200872{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200873 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200874 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200875 if (target != NC_DATASTORE_URL) {
876 rpc = nc_rpc_deleteconfig(target);
877 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200878 rpc = nc_rpc_deleteconfig(target, url);
879 }
880 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100881 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200882 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200883 }
884
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100885 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200886 nc_rpc_free (rpc);
887 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200888}
889
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100890static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200891{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100892 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200893}
894
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100895static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200896{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100897 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200898}
899
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100900static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200901{
Tomas Cejka00635972013-06-03 15:10:52 +0200902 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200903 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200904
905 /* create requests */
906 rpc = nc_rpc_generic(content);
907 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100908 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200909 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200910 }
911
Radek Krejcia332b692012-11-12 16:15:54 +0100912 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200913 // TODO ?free(*data);
914 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100915 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200916
917 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100918 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200919 nc_rpc_free (rpc);
920 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200921}
922
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200923/**
924 * @}
925 *//* netconf_operations */
926
Radek Krejci7338bde2012-08-10 12:57:30 +0200927void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200928{
Radek Krejci7338bde2012-08-10 12:57:30 +0200929 switch (level) {
930 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100931 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200932 break;
933 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100934 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200935 break;
936 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100937 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200938 break;
939 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100940 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200941 break;
942 }
Radek Krejci469aab82012-07-22 18:42:20 +0200943}
944
Tomas Cejka64b87482013-06-03 16:30:53 +0200945/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200946 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200947 * Caller should free message memory.
948 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200949 * \return pointer to message
950 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100951char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200952{
953 /* read json in chunked framing */
954 unsigned int buffer_size = 0;
955 ssize_t buffer_len = 0;
956 char *buffer = NULL;
957 char c;
958 ssize_t ret;
959 int i, chunk_len;
960 char chunk_len_str[12];
961
962 while (1) {
963 /* read chunk length */
964 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
965 if (buffer != NULL) {
966 free (buffer);
967 buffer = NULL;
968 }
969 break;
970 }
971 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
972 if (buffer != NULL) {
973 free (buffer);
974 buffer = NULL;
975 }
976 break;
977 }
978 i=0;
979 memset (chunk_len_str, 0, 12);
980 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
981 if (i==0 && c == '#') {
982 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
983 /* end but invalid */
984 if (buffer != NULL) {
985 free (buffer);
986 buffer = NULL;
987 }
988 }
989 /* end of message, double-loop break */
990 goto msg_complete;
991 }
992 chunk_len_str[i++] = c;
993 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100994 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +0200995 break;
996 }
997 }
998 if (c != '\n') {
999 if (buffer != NULL) {
1000 free (buffer);
1001 buffer = NULL;
1002 }
1003 break;
1004 }
1005 chunk_len_str[i] = 0;
1006 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1007 if (buffer != NULL) {
1008 free (buffer);
1009 buffer = NULL;
1010 }
1011 break;
1012 }
1013 buffer_size += chunk_len+1;
1014 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001015 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001016 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1017 if (buffer != NULL) {
1018 free (buffer);
1019 buffer = NULL;
1020 }
1021 break;
1022 }
1023 buffer_len += ret;
1024 }
1025msg_complete:
1026 return buffer;
1027}
1028
Tomas Cejkad5b53772013-06-08 23:01:07 +02001029NC_DATASTORE parse_datastore(const char *ds)
1030{
1031 if (strcmp(ds, "running") == 0) {
1032 return NC_DATASTORE_RUNNING;
1033 } else if (strcmp(ds, "startup") == 0) {
1034 return NC_DATASTORE_STARTUP;
1035 } else if (strcmp(ds, "candidate") == 0) {
1036 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001037 } else if (strcmp(ds, "url") == 0) {
1038 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001039 } else if (strcmp(ds, "config") == 0) {
1040 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001041 }
1042 return -1;
1043}
1044
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001045NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1046{
1047 if (strcmp(t, "notset") == 0) {
1048 return NC_EDIT_TESTOPT_NOTSET;
1049 } else if (strcmp(t, "testset") == 0) {
1050 return NC_EDIT_TESTOPT_TESTSET;
1051 } else if (strcmp(t, "set") == 0) {
1052 return NC_EDIT_TESTOPT_SET;
1053 } else if (strcmp(t, "test") == 0) {
1054 return NC_EDIT_TESTOPT_TEST;
1055 }
1056 return NC_EDIT_TESTOPT_ERROR;
1057}
1058
Tomas Cejkad5b53772013-06-08 23:01:07 +02001059json_object *create_error(const char *errmess)
1060{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001061 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001062 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001063 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001064 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001065 json_object_array_add(array, json_object_new_string(errmess));
1066 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001067 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001068 return reply;
1069
1070}
1071
1072json_object *create_data(const char *data)
1073{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001074 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001075 json_object *reply = json_object_new_object();
1076 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1077 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001078 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001079 return reply;
1080}
1081
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001082json_object *create_ok()
1083{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001084 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001085 json_object *reply = json_object_new_object();
1086 reply = json_object_new_object();
1087 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001088 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001089 return reply;
1090}
1091
1092json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001093{
1094 const char *host = NULL;
1095 const char *port = NULL;
1096 const char *user = NULL;
1097 const char *pass = NULL;
1098 json_object *capabilities = NULL;
1099 json_object *reply = NULL;
1100 char *session_key_hash = NULL;
1101 struct nc_cpblts* cpblts = NULL;
1102 unsigned int len, i;
1103
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001104 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001105 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001106 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1107 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1108 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1109 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1110
1111 capabilities = json_object_object_get((json_object *) request, "capabilities");
1112 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1113 cpblts = nc_cpblts_new(NULL);
1114 for (i=0; i<len; i++) {
1115 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1116 }
1117 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001118 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001119 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001120 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001121
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001122 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001123 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001124 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001125 session_key_hash = NULL;
1126 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001127 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1128 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001129 }
1130 if (cpblts != NULL) {
1131 nc_cpblts_free(cpblts);
1132 }
1133
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001134 GETSPEC_ERR_REPLY
1135
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001136 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001137 if (session_key_hash == NULL) {
1138 /* negative reply */
1139 if (err_reply == NULL) {
1140 reply = json_object_new_object();
1141 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1142 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001143 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001144 } else {
1145 /* use filled err_reply from libnetconf's callback */
1146 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001147 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001148 }
1149 } else {
1150 /* positive reply */
1151 reply = json_object_new_object();
1152 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1153 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1154
1155 free(session_key_hash);
1156 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001157 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001158 return reply;
1159}
1160
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001161json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001162{
1163 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001164 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001165 json_object *reply = NULL;
1166
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001167 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001168
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001169 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001170 filter = json_object_get_string(json_object_object_get(request, "filter"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001171 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001172
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001173 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001174 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001175 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001176 reply = create_data(data);
1177 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001178 }
1179 return reply;
1180}
1181
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001182json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001183{
1184 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001185 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001186 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001187 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001188 json_object *reply = NULL;
1189
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001190 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001191
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001192 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001193 filter = json_object_get_string(json_object_object_get(request, "filter"));
1194
Tomas Cejkad5b53772013-06-08 23:01:07 +02001195 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1196 ds_type_s = parse_datastore(source);
1197 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001198 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001199 if (ds_type_s == -1) {
1200 return create_error("Invalid source repository type requested.");
1201 }
1202
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001203 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001204 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001205 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001206 reply = create_data(data);
1207 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001208 }
1209 return reply;
1210}
1211
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001212json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001213{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001214 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001215 const char *identifier = NULL;
1216 const char *version = NULL;
1217 const char *format = NULL;
1218 json_object *reply = NULL;
1219
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001220 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001221 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001222 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001223 version = json_object_get_string(json_object_object_get(request, "version"));
1224 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001225 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001226
Tomas Cejkad5b53772013-06-08 23:01:07 +02001227 if (identifier == NULL) {
1228 return create_error("No identifier for get-schema supplied.");
1229 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001230
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001231 DEBUG("get-schema(version: %s, format: %s)", version, format);
1232 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001233 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001234 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001235 reply = create_data(data);
1236 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001237 }
1238 return reply;
1239}
1240
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001241json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001242{
1243 NC_DATASTORE ds_type_s = -1;
1244 NC_DATASTORE ds_type_t = -1;
1245 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1246 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001247 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001248 const char *defop = NULL;
1249 const char *erropt = NULL;
1250 const char *config = NULL;
1251 const char *source = NULL;
1252 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001253 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254 json_object *reply = NULL;
1255
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001256 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001258 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001259 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001260 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1261 /* get parameters */
1262 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1263 ds_type_t = parse_datastore(target);
1264 }
1265 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1266 ds_type_s = parse_datastore(source);
1267 } else {
1268 /* source is optional, default value is config */
1269 ds_type_s = NC_DATASTORE_CONFIG;
1270 }
1271 config = json_object_get_string(json_object_object_get(request, "config"));
1272 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1273 pthread_mutex_unlock(&json_lock);
1274
Tomas Cejkad5b53772013-06-08 23:01:07 +02001275 if (defop != NULL) {
1276 if (strcmp(defop, "merge") == 0) {
1277 defop_type = NC_EDIT_DEFOP_MERGE;
1278 } else if (strcmp(defop, "replace") == 0) {
1279 defop_type = NC_EDIT_DEFOP_REPLACE;
1280 } else if (strcmp(defop, "none") == 0) {
1281 defop_type = NC_EDIT_DEFOP_NONE;
1282 } else {
1283 return create_error("Invalid default-operation parameter.");
1284 }
1285 } else {
1286 defop_type = NC_EDIT_DEFOP_NOTSET;
1287 }
1288
Tomas Cejkad5b53772013-06-08 23:01:07 +02001289 if (erropt != NULL) {
1290 if (strcmp(erropt, "continue-on-error") == 0) {
1291 erropt_type = NC_EDIT_ERROPT_CONT;
1292 } else if (strcmp(erropt, "stop-on-error") == 0) {
1293 erropt_type = NC_EDIT_ERROPT_STOP;
1294 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1295 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1296 } else {
1297 return create_error("Invalid error-option parameter.");
1298 }
1299 } else {
1300 erropt_type = 0;
1301 }
1302
Tomas Cejkad5b53772013-06-08 23:01:07 +02001303 if (ds_type_t == -1) {
1304 return create_error("Invalid target repository type requested.");
1305 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001306 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001307 if (config == NULL) {
1308 return create_error("Invalid config data parameter.");
1309 }
1310 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001311 if (config == NULL) {
1312 config = "";
1313 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001314 }
1315
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001316 if (testopt != NULL) {
1317 testopt_type = parse_testopt(testopt);
1318 } else {
1319 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1320 }
1321
1322 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 +01001323
1324 CHECK_ERR_SET_REPLY
1325
Tomas Cejkad5b53772013-06-08 23:01:07 +02001326 return reply;
1327}
1328
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001329json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001330{
1331 NC_DATASTORE ds_type_s = -1;
1332 NC_DATASTORE ds_type_t = -1;
1333 const char *config = NULL;
1334 const char *target = NULL;
1335 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001336 const char *uri_src = NULL;
1337 const char *uri_trg = NULL;
1338
Tomas Cejkad5b53772013-06-08 23:01:07 +02001339 json_object *reply = NULL;
1340
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001341 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001342
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001343 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001344 /* get parameters */
1345 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1346 ds_type_t = parse_datastore(target);
1347 }
1348 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1349 ds_type_s = parse_datastore(source);
1350 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001351 config = json_object_get_string(json_object_object_get(request, "config"));
1352 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
1353 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
1354 pthread_mutex_unlock(&json_lock);
1355
Tomas Cejkad5b53772013-06-08 23:01:07 +02001356 if (source == NULL) {
1357 /* no explicit source specified -> use config data */
1358 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001359 } else if (ds_type_s == -1) {
1360 /* source datastore specified, but it is invalid */
1361 return create_error("Invalid source repository type requested.");
1362 }
1363
1364 if (ds_type_t == -1) {
1365 /* invalid target datastore specified */
1366 return create_error("Invalid target repository type requested.");
1367 }
1368
Tomas Cejka55c6df52014-02-20 12:59:33 +01001369 /* source can be missing when config is given */
1370 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001371 reply = create_error("invalid input parameters - source and config is required.");
1372 return reply;
1373 }
1374
1375 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001376 if (uri_src == NULL) {
1377 uri_src = "";
1378 }
1379 }
1380 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001381 if (uri_trg == NULL) {
1382 uri_trg = "";
1383 }
1384 }
1385 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001386
1387 CHECK_ERR_SET_REPLY
1388
Tomas Cejkad5b53772013-06-08 23:01:07 +02001389 return reply;
1390}
1391
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001392json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001393{
1394 json_object *reply = NULL;
1395 const char *config = NULL;
1396 char *data = NULL;
1397
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001398 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001399
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001400 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001401 config = json_object_get_string(json_object_object_get(request, "content"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001402 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001403
1404 if (config == NULL) {
1405 return create_error("Missing content parameter.");
1406 }
1407
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001408 /* TODO */
1409 reply = netconf_generic(session_key, config, &data);
1410 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001411 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001412 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001413 /* use filled err_reply from libnetconf's callback */
1414 reply = err_reply;
1415 }
1416 } else {
1417 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001418 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001419 reply = json_object_new_object();
1420 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001421 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001422 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001423 reply = create_data(data);
1424 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001425 }
1426 }
1427 return reply;
1428}
1429
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001430json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001431{
1432 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001433 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001434
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001435 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001436 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001437 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001438 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001439 }
1440 return reply;
1441}
1442
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001443json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001444{
1445 json_object *reply = NULL;
1446 const char *sid = NULL;
1447
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001448 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001450 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001451 sid = json_object_get_string(json_object_object_get(request, "session-id"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001452 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001453
1454 if (sid == NULL) {
1455 return create_error("Missing session-id parameter.");
1456 }
1457
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001458 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001459
1460 CHECK_ERR_SET_REPLY
1461
Tomas Cejkad5b53772013-06-08 23:01:07 +02001462 return reply;
1463}
1464
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001465json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001466{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001467 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001468 struct session_with_mutex * locked_session = NULL;
1469 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001470 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001471
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001472 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001473 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001474 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001475 return NULL;
1476 }
1477
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1479 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001480 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001481 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001482 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001483 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001484 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001485 prepare_status_message(locked_session, temp_session);
1486 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001487 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001488 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001489 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001490 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001491 reply = create_error("Reload was unsuccessful, connection failed.");
1492 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001493 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001494 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001495 DEBUG("UNLOCK wrlock %s", __func__);
1496 if (pthread_rwlock_unlock(&session_lock) != 0) {
1497 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1498 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001499 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001500 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001501 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001502 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001503 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001504 reply = create_error("Invalid session identifier.");
1505 }
1506
1507 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1508 reply = locked_session->hello_message;
1509 }
1510 return reply;
1511}
1512
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001513json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001514{
1515 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001516 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001517 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001518
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001519 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001520 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001521 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001522 }
1523
Tomas Cejkad5b53772013-06-08 23:01:07 +02001524 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1525 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001526 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001527 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001528 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001529 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001530 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001531 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001532 if (locked_session->hello_message != NULL) {
1533 reply = locked_session->hello_message;
1534 } else {
1535 reply = create_error("Invalid session identifier.");
1536 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001537 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001538 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001539 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001540 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001541 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001542 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001543 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001544 reply = create_error("Invalid session identifier.");
1545 }
1546
Tomas Cejka47387fd2013-06-10 20:37:46 +02001547
Tomas Cejkad5b53772013-06-08 23:01:07 +02001548 return reply;
1549}
1550
Tomas Cejka6b886e02013-07-05 09:53:17 +02001551void notification_history(time_t eventtime, const char *content)
1552{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001553 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1554 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001555 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001556 return;
1557 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001558 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001559 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001560 json_object *notif = json_object_new_object();
1561 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001562 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001563 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001564 }
1565 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1566 json_object_object_add(notif, "content", json_object_new_string(content));
1567 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001568failed:
1569 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001570}
1571
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001572json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001573{
1574 json_object *reply = NULL;
1575 const char *sid = NULL;
1576 struct session_with_mutex *locked_session = NULL;
1577 struct nc_session *temp_session = NULL;
1578 nc_rpc *rpc = NULL;
1579 time_t start = 0;
1580 time_t stop = 0;
1581 int64_t from, to;
1582
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001583 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001584
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001585 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001586 sid = json_object_get_string(json_object_object_get(request, "session"));
1587 from = json_object_get_int64(json_object_object_get(request, "from"));
1588 to = json_object_get_int64(json_object_object_get(request, "to"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001589 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001590
1591 start = time(NULL) + from;
1592 stop = time(NULL) + to;
1593
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001594 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001595
1596 if (sid == NULL) {
1597 return create_error("Missing session parameter.");
1598 }
1599
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001600 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001601 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001602 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001603 return NULL;
1604 }
1605
1606 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1607 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001608 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001609 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001610 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001611 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001612 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001613 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001614 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001615 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1616 if (temp_session != NULL) {
1617 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1618 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001619 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001620 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001621 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001622 return create_error("notifications: creating an rpc request failed.");
1623 }
1624
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001625 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001626 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001627 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001628 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001629 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001630 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001631 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001632 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001633 }
1634 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1635
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001636 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001637 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001638 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001639 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001640 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001641 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001642 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001643 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001644 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001645 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001646
1647 ncntf_dispatch_receive(temp_session, notification_history);
1648
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001649 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001650 reply = json_object_new_object();
1651 json_object_object_add(reply, "notifications", notif_history_array);
1652 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001653 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001654
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001655 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001656 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001657 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001658 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001659 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001660 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001661 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001662 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001663 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001664 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1665 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001666 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001667 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001668 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001669 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001670 }
1671 reply = create_error("Invalid session identifier.");
1672 }
1673
Tomas Cejka4003a702013-10-01 00:02:45 +02001674 return reply;
1675}
1676
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001677json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001678{
1679 json_object *reply = NULL;
1680 const char *sid = NULL;
1681 const char *target = NULL;
1682 const char *url = NULL;
1683 struct session_with_mutex *locked_session = NULL;
1684 nc_rpc *rpc = NULL;
1685 NC_DATASTORE target_ds;
1686
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001687 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001688
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001689 pthread_mutex_lock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001690 sid = json_object_get_string(json_object_object_get(request, "session"));
1691 target = json_object_get_string(json_object_object_get(request, "target"));
1692 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001693 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001694
1695
1696 if ((sid == NULL) || (target == NULL)) {
1697 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001698 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001699
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001700 /* validation */
1701 target_ds = parse_datastore(target);
1702 if (target_ds == NC_DATASTORE_URL) {
1703 if (url != NULL) {
1704 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001705 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001706 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001707 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001708 rpc = nc_rpc_validate(target_ds);
1709 }
1710 if (rpc == NULL) {
1711 DEBUG("mod_netconf: creating rpc request failed");
1712 reply = create_error("Creation of RPC request failed.");
1713 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001714 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001715 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001716 }
1717
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001718 DEBUG("Request: validate datastore");
1719 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001720
1721 CHECK_ERR_SET_REPLY
1722
1723 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001724 DEBUG("Request: validation ok.");
1725 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001726 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001727 }
1728 nc_rpc_free (rpc);
1729
Tomas Cejka6b886e02013-07-05 09:53:17 +02001730 return reply;
1731}
1732
David Kupka8e60a372012-09-04 09:15:20 +02001733void * thread_routine (void * arg)
1734{
1735 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001736 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001737 json_object *request = NULL;
1738 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001739 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001740 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001741 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001742 const char *session_key;
1743 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001744 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001745 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001746 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001747 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001748 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001749 int client = ((struct pass_to_thread*)arg)->client;
1750
Tomas Cejka00635972013-06-03 15:10:52 +02001751 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001752
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001753 /* init thread specific err_reply memory */
1754 json_object **err_reply = calloc(1, sizeof(json_object **));
1755 if (err_reply == NULL) {
1756 DEBUG("Allocation of err_reply storage failed!");
1757 isterminated = 1;
1758 }
1759 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
1760 DEBUG("notif_history: cannot set thread-specific hash value.");
1761 }
David Kupka8e60a372012-09-04 09:15:20 +02001762 while (!isterminated) {
1763 fds.fd = client;
1764 fds.events = POLLIN;
1765 fds.revents = 0;
1766
1767 status = poll(&fds, 1, 1000);
1768
1769 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1770 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001771 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001772 continue;
1773 } else if (status < 0) {
1774 /* 0: poll time outed
1775 * close socket and ignore this request from the client, it can try it again
1776 * -1: poll failed
1777 * something wrong happend, close this socket and wait for another request
1778 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001779 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001780 close(client);
1781 break;
1782 }
1783 /* status > 0 */
1784
1785 /* check the status of the socket */
1786
1787 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1788 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1789 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001790 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001791 close(client);
1792 break;
1793 }
1794
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001795 DEBUG("Get framed message...");
1796 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001797
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001798 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001799 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001800 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001801 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001802 request = json_tokener_parse_verbose(buffer, &jerr);
1803 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001804 DEBUG("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001805 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001806 continue;
1807 }
David Kupka8e60a372012-09-04 09:15:20 +02001808
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001809 operation = json_object_get_int(json_object_object_get(request, "type"));
Tomas Cejka64b87482013-06-03 16:30:53 +02001810 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001811 pthread_mutex_unlock(&json_lock);
1812
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001813 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001814 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1815 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001816 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001817 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001818 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001819 pthread_mutex_unlock(&json_lock);
1820
David Kupka8e60a372012-09-04 09:15:20 +02001821 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001822
1823 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001824 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001825 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001826 /* there is some stupid client, so close the connection to give a chance to some other client */
1827 close(client);
1828 break;
1829 }
1830
David Kupka8e60a372012-09-04 09:15:20 +02001831 /* null global JSON error-reply */
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001832 (*err_reply) = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001833
1834 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001835 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001836 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001837 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001838 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001839 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001840 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001841
1842 /* process required operation */
1843 switch (operation) {
1844 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001845 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001846 break;
1847 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001848 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001849 break;
1850 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001851 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001852 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001853 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001854 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001855 break;
David Kupka8e60a372012-09-04 09:15:20 +02001856 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001857 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001858 break;
1859 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001860 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001861 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001862
David Kupka8e60a372012-09-04 09:15:20 +02001863 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001864 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001865 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001866 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001867 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001868 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1869 ds_type_t = parse_datastore(target);
1870 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001871 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001872
1873 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001874 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001875 break;
1876 }
David Kupka8e60a372012-09-04 09:15:20 +02001877 switch(operation) {
1878 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001879 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001880 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001881 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001882 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001883 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001884 break;
1885 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001886 DEBUG("Request: lock (session %s)", session_key);
1887 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001888 break;
1889 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001890 DEBUG("Request: unlock (session %s)", session_key);
1891 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001892 break;
1893 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001894 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001895 break;
1896 }
1897
Tomas Cejkac7929632013-10-24 19:25:15 +02001898 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001899 if (*err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001900 /** \todo more clever error message wanted */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001901 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001902 reply = json_object_new_object();
1903 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001904 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001905 } else {
1906 /* use filled err_reply from libnetconf's callback */
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001907 reply = *err_reply;
David Kupka8e60a372012-09-04 09:15:20 +02001908 }
David Kupka8e60a372012-09-04 09:15:20 +02001909 }
1910 break;
1911 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001912 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001913 break;
1914 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001915 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001916 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001917 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001918 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001919 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001920 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001921 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001922 break;
1923 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001924 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001925 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001926 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001927 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001928 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001929 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001930 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001931 break;
David Kupka8e60a372012-09-04 09:15:20 +02001932 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001933 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001934 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001935 break;
1936 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001937 DEBUG("Clean request json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001938 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001939 json_object_put(request);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001940 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001941 /* send reply to caller */
1942 if (reply != NULL) {
1943 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001944 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001945 if (buffer != NULL) {
1946 free(buffer);
1947 buffer = NULL;
1948 }
David Kupka8e60a372012-09-04 09:15:20 +02001949 break;
1950 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001951 pthread_mutex_unlock(&json_lock);
1952
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001953 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001954 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001955 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001956 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001957 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001958 reply = NULL;
1959 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001960 free(chunked_out_msg);
1961 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001962 if (buffer != NULL) {
1963 free(buffer);
1964 buffer = NULL;
1965 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001966 if ((err_reply != NULL) && (*err_reply != NULL)) {
1967 json_object_put(*err_reply);
1968 *err_reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001969 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001970 pthread_mutex_unlock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001971 } else {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001972 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001973 DEBUG("Reply is NULL, shouldn't be...");
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001974 if (*err_reply == NULL) {
1975 DEBUG("No error was set - really strange situation");
1976 } else {
1977 DEBUG("Error was set but it was not sent.");
1978 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001979 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001980 }
1981 }
1982 }
David Kupka8e60a372012-09-04 09:15:20 +02001983 free (arg);
1984
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001985 err_reply = (json_object **) pthread_getspecific(err_reply_key);
1986 if (err_reply != NULL) {
1987 if (*err_reply != NULL) {
1988 pthread_mutex_lock(&json_lock);
1989 json_object_put(*err_reply);
1990 pthread_mutex_unlock(&json_lock);
1991 }
1992 free(err_reply);
1993 err_reply = NULL;
1994 }
1995
David Kupka8e60a372012-09-04 09:15:20 +02001996 return retval;
1997}
1998
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001999/**
2000 * \brief Close all open NETCONF sessions.
2001 *
2002 * During termination of mod_netconf, it is useful to close all remaining
2003 * sessions. This function iterates over the list of sessions and close them
2004 * all.
2005 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002006 * \param[in] p apr pool needed for hash table iterating
2007 * \param[in] ht hash table of session_with_mutex structs
2008 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002009static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002010{
2011 apr_hash_index_t *hi;
2012 void *val = NULL;
2013 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002014 const char *hashed_key = NULL;
2015 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002016 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002017
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002018 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002019 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002020 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002021 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002022 return;
2023 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002024 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002025 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002026 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002027 swm = (struct session_with_mutex *) val;
2028 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002029 DEBUG("LOCK mutex %s", __func__);
2030 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002031 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002032 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002033 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002034
2035 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002036 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002037 }
2038 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002039 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002040 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002041 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002042 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002043 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002044}
2045
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002046static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002047{
2048 apr_hash_index_t *hi;
2049 void *val = NULL;
2050 struct nc_session *ns = NULL;
2051 struct session_with_mutex *swm = NULL;
2052 const char *hashed_key = NULL;
2053 apr_ssize_t hashed_key_length;
2054 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002055 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002056
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002057 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002058//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002059 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002060 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002061 return;
2062 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002063 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002064 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2065 swm = (struct session_with_mutex *) val;
2066 if (swm == NULL) {
2067 continue;
2068 }
2069 ns = swm->session;
2070 if (ns == NULL) {
2071 continue;
2072 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002073//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002074 pthread_mutex_lock(&swm->lock);
2075 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002076 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002077 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002078 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002079//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002080 pthread_mutex_unlock(&swm->lock);
2081
2082 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002083 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002084 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002085//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002086 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002087 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002088 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002089 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002090//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002091 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002092 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002093 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002094}
2095
2096
2097/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002098 * This is actually implementation of NETCONF client
2099 * - requests are received from UNIX socket in the predefined format
2100 * - results are replied through the same way
2101 * - the daemon run as a separate process, but it is started and stopped
2102 * automatically by Apache.
2103 *
2104 */
Radek Krejci469aab82012-07-22 18:42:20 +02002105static void forked_proc(apr_pool_t * pool, server_rec * server)
2106{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002107 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002108 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002109 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002110 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002111 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002112 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002113 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002114 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002115 struct timespec maxtime;
2116 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002117 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002118 #ifdef WITH_NOTIFICATIONS
2119 char use_notifications = 0;
2120 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002121
Tomas Cejka6b886e02013-07-05 09:53:17 +02002122 http_server = server;
2123
Tomas Cejka404d37e2013-04-13 02:31:35 +02002124 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002125 maxtime.tv_sec = 5;
2126 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002127
Tomas Cejka04e08f42014-03-27 19:52:34 +01002128#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002129 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002130 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002131#else
2132# ifdef SU_GROUP
2133 if (strlen(SU_GROUP) > 0) {
2134 struct group *g = getgrnam(SU_GROUP);
2135 if (g == NULL) {
2136 DEBUG("GID (%s) was not found.", SU_GROUP);
2137 return;
2138 }
2139 if (setgid(g->gr_gid) != 0) {
2140
2141 DEBUG("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
2142 return;
2143 }
2144 }
2145# else
2146 DEBUG("no SU_GROUP");
2147# endif
2148# ifdef SU_USER
2149 if (strlen(SU_USER) > 0) {
2150 struct passwd *p = getpwnam(SU_USER);
2151 if (p == NULL) {
2152 DEBUG("UID (%s) was not found.", SU_USER);
2153 return;
2154 }
2155 if (setuid(p->pw_uid) != 0) {
2156 DEBUG("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
2157 return;
2158 }
2159 }
2160# else
2161 DEBUG("no SU_USER");
2162# endif
2163#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002164
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002165 if (server != NULL) {
2166 cfg = ap_get_module_config(server->module_config, &netconf_module);
2167 if (cfg == NULL) {
2168 DEBUG("Getting mod_netconf configuration failed");
2169 return;
2170 }
2171 sockname = cfg->sockname;
2172 } else {
2173 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002174 }
Radek Krejci469aab82012-07-22 18:42:20 +02002175
Tomas Cejka04e08f42014-03-27 19:52:34 +01002176 /* try to remove if exists */
2177 unlink(sockname);
2178
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002179 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002180 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002181 DEBUG("Creating socket failed (%s)", strerror(errno));
2182 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002183 }
2184
2185 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002186 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002187 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2188
2189 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2190 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002191 DEBUG("mod_netconf socket address already in use");
2192 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002193 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002194 DEBUG("Binding socket failed (%s)", strerror(errno));
2195 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002196 }
2197
2198 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002199 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2200 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002201 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002202 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002203
Tomas Cejka04e08f42014-03-27 19:52:34 +01002204 uid_t user = -1;
2205 if (strlen(CHOWN_USER) > 0) {
2206 struct passwd *p = getpwnam(CHOWN_USER);
2207 if (p != NULL) {
2208 user = p->pw_uid;
2209 }
2210 }
2211 gid_t group = -1;
2212 if (strlen(CHOWN_GROUP) > 0) {
2213 struct group *g = getgrnam(CHOWN_GROUP);
2214 if (g != NULL) {
2215 group = g->gr_gid;
2216 }
2217 }
2218 if (chown(sockname, user, group) == -1) {
2219 DEBUG("Chown on socket file failed (%s).", strerror(errno));
2220 }
2221
Tomas Cejkaba21b382013-04-13 02:37:32 +02002222 /* prepare internal lists */
2223 netconf_sessions_list = apr_hash_make(pool);
2224
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002225 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002226 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002227 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002228 use_notifications = 0;
2229 } else {
2230 use_notifications = 1;
2231 }
2232 #endif
2233
Radek Krejci469aab82012-07-22 18:42:20 +02002234 /* setup libnetconf's callbacks */
2235 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002236 nc_callback_print(clb_print);
2237 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2238 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2239 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002240 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002241
2242 /* disable publickey authentication */
2243 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2244
David Kupka8e60a372012-09-04 09:15:20 +02002245 /* create mutex protecting session list */
2246 pthread_rwlockattr_init(&lock_attrs);
2247 /* rwlock is shared only with threads in this process */
2248 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2249 /* create rw lock */
2250 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002251 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2252 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002253 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002254 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002255 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002256 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002257 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002258 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002259 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002260 DEBUG("init of err_reply_key.");
2261 if (pthread_key_create(&err_reply_key, NULL) != 0) {
2262 DEBUG("init of err_reply_key failed");
2263 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002264
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002265 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002266 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002267 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002268 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002269 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002270 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002271 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002272 }
2273 if (use_notifications == 1) {
2274 notification_handle();
2275 }
2276 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002277 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002278 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002279 }
Radek Krejci469aab82012-07-22 18:42:20 +02002280
2281 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002282 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002283 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002284 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002285 olds = tv.tv_sec;
2286 }
David Kupka8e60a372012-09-04 09:15:20 +02002287 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002288 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2289 apr_sleep(SLEEP_TIME);
2290 continue;
2291 } else if (client == -1 && (errno == EINTR)) {
2292 continue;
2293 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002294 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002295 continue;
2296 }
Radek Krejci469aab82012-07-22 18:42:20 +02002297
2298 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002299 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002300
David Kupka8e60a372012-09-04 09:15:20 +02002301 arg = malloc (sizeof(struct pass_to_thread));
2302 arg->client = client;
2303 arg->pool = pool;
2304 arg->server = server;
2305 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002306
David Kupka8e60a372012-09-04 09:15:20 +02002307 /* start new thread. It will serve this particular request and then terminate */
2308 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002309 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002310 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002311 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002312 pthread_count++;
2313 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2314 ptids[pthread_count] = 0;
2315 }
Radek Krejci469aab82012-07-22 18:42:20 +02002316
David Kupka8e60a372012-09-04 09:15:20 +02002317 /* check if some thread already terminated, free some resources by joining it */
2318 for (i=0; i<pthread_count; i++) {
2319 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002320 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002321 pthread_count--;
2322 if (pthread_count > 0) {
2323 /* place last Thread ID on the place of joined one */
2324 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002325 }
Radek Krejci469aab82012-07-22 18:42:20 +02002326 }
2327 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002328 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002329 }
2330
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002331 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002332 /* join all threads */
2333 for (i=0; i<pthread_count; i++) {
2334 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2335 }
Radek Krejci469aab82012-07-22 18:42:20 +02002336
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002337 #ifdef WITH_NOTIFICATIONS
2338 notification_close();
2339 #endif
2340
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002341 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002342 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002343
David Kupka8e60a372012-09-04 09:15:20 +02002344 /* destroy rwlock */
2345 pthread_rwlock_destroy(&session_lock);
2346 pthread_rwlockattr_destroy(&lock_attrs);
2347
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002348 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002349
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002350 free(ptids);
2351 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002352 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002353 return;
2354error_exit:
2355 close(lsock);
2356 free(ptids);
2357 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002358}
2359
Radek Krejcif23850c2012-07-23 16:14:17 +02002360static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002361{
Radek Krejcif23850c2012-07-23 16:14:17 +02002362 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2363 apr_pool_create(&config->pool, pool);
2364 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002365 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002366
2367 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002368}
2369
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002370#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002371static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2372 apr_pool_t * plog, server_rec * s)
2373{
Radek Krejcif23850c2012-07-23 16:14:17 +02002374 mod_netconf_cfg *config;
2375 apr_status_t res;
2376
Radek Krejci469aab82012-07-22 18:42:20 +02002377 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002378 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002379 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002380
2381 /*
2382 * The following checks if this routine has been called before.
2383 * This is necessary because the parent process gets initialized
2384 * a couple of times as the server starts up.
2385 */
2386 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2387 if (!data) {
2388 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2389 return (OK);
2390 }
2391
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002392 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002393 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002394
Radek Krejcif23850c2012-07-23 16:14:17 +02002395 if (config && config->forkproc == NULL) {
2396 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2397 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002398 switch (res) {
2399 case APR_INCHILD:
2400 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002401 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002402 apr_signal(SIGTERM, signal_handler);
2403
2404 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002405 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002406
2407 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002408 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002409
Radek Krejcif23850c2012-07-23 16:14:17 +02002410 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002411 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002412 exit(APR_EGENERAL);
2413 break;
2414 case APR_INPARENT:
2415 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002416 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002417 break;
2418 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002419 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002420 break;
2421 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002422 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002423 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002424 }
2425
2426 return OK;
2427}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002428#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002429
Radek Krejci469aab82012-07-22 18:42:20 +02002430/**
2431 * Register module hooks
2432 */
2433static void mod_netconf_register_hooks(apr_pool_t * p)
2434{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002435#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002436 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002437#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002438}
2439
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002440static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2441{
2442 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2443 return NULL;
2444}
2445
2446static const command_rec netconf_cmds[] = {
2447 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2448 {NULL}
2449};
2450
Radek Krejci469aab82012-07-22 18:42:20 +02002451/* Dispatch list for API hooks */
2452module AP_MODULE_DECLARE_DATA netconf_module = {
2453 STANDARD20_MODULE_STUFF,
2454 NULL, /* create per-dir config structures */
2455 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002456 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002457 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002458 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002459 mod_netconf_register_hooks /* register hooks */
2460};
Radek Krejcia332b692012-11-12 16:15:54 +01002461
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002462int main(int argc, char **argv)
2463{
2464 apr_pool_t *pool;
2465 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2466 apr_signal(SIGTERM, signal_handler);
2467 apr_signal(SIGINT, signal_handler);
2468 apr_pool_create(&pool, NULL);
2469 forked_proc(pool, NULL);
2470 apr_pool_destroy(pool);
2471 apr_terminate();
2472 DEBUG("Terminated");
2473 return 0;
2474}