blob: 9ae6f616c5165e3ce0cba0fc7bb02758a50ea182 [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 Cejka442258e2014-04-01 18:17:18 +0200123pthread_key_t err_reply_key;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100124
Radek Krejci469aab82012-07-22 18:42:20 +0200125volatile int isterminated = 0;
126
127static char* password;
128
Radek Krejci469aab82012-07-22 18:42:20 +0200129static void signal_handler(int sign)
130{
131 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100132 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200133 case SIGTERM:
134 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200135 break;
136 }
137}
138
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200139static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200140{
Radek Krejcif23850c2012-07-23 16:14:17 +0200141 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
142 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200144
Radek Krejci469aab82012-07-22 18:42:20 +0200145 apr_sha1_ctx_t sha1_ctx;
146 apr_sha1_init(&sha1_ctx);
147 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
148 apr_sha1_update(&sha1_ctx, port, strlen(port));
149 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200150 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200151
Radek Krejcif23850c2012-07-23 16:14:17 +0200152 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200153 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200154 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200155 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200156 }
157 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200158
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200159 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200160}
161
Tomas Cejka8254dba2014-06-22 17:58:59 +0200162int netconf_callback_ssh_hostkey_check (const char* hostname, LIBSSH2_SESSION *session)
Radek Krejci469aab82012-07-22 18:42:20 +0200163{
164 /* always approve */
165 return (EXIT_SUCCESS);
166}
167
168char* netconf_callback_sshauth_password (const char* username, const char* hostname)
169{
170 char* buf;
171
172 buf = malloc ((strlen(password) + 1) * sizeof(char));
173 apr_cpystrn(buf, password, strlen(password) + 1);
174
175 return (buf);
176}
177
178void netconf_callback_sshauth_interactive (const char* name,
179 int name_len,
180 const char* instruction,
181 int instruction_len,
182 int num_prompts,
183 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
184 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
185 void** abstract)
186{
187 int i;
188
189 for (i=0; i<num_prompts; i++) {
190 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
191 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
192 responses[i].length = strlen(responses[i].text) + 1;
193 }
194
195 return;
196}
197
Radek Krejcic11fd862012-07-26 12:41:21 +0200198void netconf_callback_error_process(const char* tag,
199 const char* type,
200 const char* severity,
201 const char* apptag,
202 const char* path,
203 const char* message,
204 const char* attribute,
205 const char* element,
206 const char* ns,
207 const char* sid)
208{
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100209 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
Tomas Cejka442258e2014-04-01 18:17:18 +0200210 if (err_reply_p == NULL) {
211 DEBUG("Error message was not allocated. %s", __func__);
212 return;
213 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100214 json_object *err_reply = *err_reply_p;
215
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100216 json_object *array = NULL;
217 if (err_reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100218 DEBUG("error calback: empty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100219 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100220 err_reply = json_object_new_object();
221 array = json_object_new_array();
222 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
223 json_object_object_add(err_reply, "errors", array);
224 if (message != NULL) {
225 json_object_array_add(array, json_object_new_string(message));
226 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100227 pthread_mutex_unlock(&json_lock);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100228 (*err_reply_p) = err_reply;
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100229 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100230 DEBUG("error calback: nonempty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100231 pthread_mutex_lock(&json_lock);
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100232 array = json_object_object_get(err_reply, "errors");
233 if (array != NULL) {
234 if (message != NULL) {
235 json_object_array_add(array, json_object_new_string(message));
236 }
237 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100238 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100239 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100240 pthread_setspecific(err_reply_key, err_reply_p);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100241 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200242}
243
Tomas Cejka47387fd2013-06-10 20:37:46 +0200244/**
245 * should be used in locked area
246 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100247void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200248{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200249 json_object *json_obj = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100250 char *old_sid = NULL;
251 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200252 const char *cpbltstr;
253 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200254
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200255 if (s == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100256 DEBUG("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200257 return;
258 }
259
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100260 pthread_mutex_lock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200261 if (s->hello_message != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100262 DEBUG("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200263 //json_object_put(s->hello_message);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100264 j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid"));
265 if (j_old_sid != NULL) {
266 old_sid = strdup(j_old_sid);
267 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100268 json_object_put(s->hello_message);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200269 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200270 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100271 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200272 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100273 if (old_sid != NULL) {
274 /* use previous sid */
275 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
276 free(old_sid);
Tomas Cejkaa3ffdba2014-03-27 15:12:21 +0100277 old_sid = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100278 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200279 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100280 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
281 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200282 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
283 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
284 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
285 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
286 cpblts = nc_session_get_cpblts (session);
287 if (cpblts != NULL) {
288 json_obj = json_object_new_array();
289 nc_cpblts_iter_start (cpblts);
290 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
291 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
292 }
293 json_object_object_add(s->hello_message, "capabilities", json_obj);
294 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100295 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200296 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100297 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200298 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
299 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
300 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100301 DEBUG("Status info from hello message prepared");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100302 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200303
304}
305
Tomas Cejka442258e2014-04-01 18:17:18 +0200306void create_err_reply_p()
307{
308 json_object **err_reply = calloc(1, sizeof(json_object **));
309 if (err_reply == NULL) {
310 DEBUG("Allocation of err_reply storage failed!");
311 return;
312 }
313 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
314 DEBUG("cannot set thread-specific value.");
315 }
316}
317
318void clean_err_reply()
319{
320 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
321 if (err_reply != NULL) {
322 if (*err_reply != NULL) {
323 pthread_mutex_lock(&json_lock);
324 json_object_put(*err_reply);
325 pthread_mutex_unlock(&json_lock);
326 }
327 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
328 DEBUG("Cannot set thread-specific hash value.");
329 }
330 }
331}
332
333void free_err_reply()
334{
335 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
336 if (err_reply != NULL) {
337 if (*err_reply != NULL) {
338 pthread_mutex_lock(&json_lock);
339 json_object_put(*err_reply);
340 pthread_mutex_unlock(&json_lock);
341 }
342 free(err_reply);
343 err_reply = NULL;
344 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
345 DEBUG("Cannot set thread-specific hash value.");
346 }
347 }
348}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200349
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200350/**
351 * \defgroup netconf_operations NETCONF operations
352 * The list of NETCONF operations that mod_netconf supports.
353 * @{
354 */
355
356/**
357 * \brief Connect to NETCONF server
358 *
359 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
360 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100361static 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 +0200362{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200363 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200364 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200365 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200366
367 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200368 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100369 DEBUG("prepare to connect %s@%s:%s", user, host, port);
David Kupka8e60a372012-09-04 09:15:20 +0200370 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100371 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200372
Radek Krejci469aab82012-07-22 18:42:20 +0200373 /* if connected successful, add session to the list */
374 if (session != NULL) {
375 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200376 session_key = gen_ncsession_hash(
377 (host==NULL) ? "localhost" : host,
378 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200379 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200380
Tomas Cejkaba21b382013-04-13 02:37:32 +0200381 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100382 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 +0200383 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100384 session = NULL;
385 free(locked_session);
386 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100387 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200388 return NULL;
389 }
390 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200391 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200392 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200393 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200394 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100395 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200396 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100397 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200398 if (pthread_rwlock_wrlock (&session_lock) != 0) {
399 nc_session_free(session);
400 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100401 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200402 return NULL;
403 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200404 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200405 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100406 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200407 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 +0100408 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200409
Tomas Cejka47387fd2013-06-10 20:37:46 +0200410 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100411 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200412 pthread_mutex_lock(&locked_session->lock);
413
414 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100415 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200416 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100417 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200418 }
419
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200420 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100421 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200422
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100423 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200424 pthread_mutex_unlock(&locked_session->lock);
425
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100426 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200427 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200428 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100429 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200430 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200431 }
432
Radek Krejci469aab82012-07-22 18:42:20 +0200433}
434
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100435static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200436{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100437 DEBUG("lock private lock.");
438 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200439 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100440 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200441 }
442 locked_session->ntfc_subscribed = 0;
443 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100444 if (locked_session->session != NULL) {
445 nc_session_free(locked_session->session);
446 locked_session->session = NULL;
447 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100448 DEBUG("session closed.");
449 DEBUG("unlock private lock.");
450 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200451 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100452 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200453 }
454
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100455 DEBUG("unlock session lock.");
456 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200457 usleep(500000); /* let notification thread stop */
458
459 /* session shouldn't be used by now */
460 /** \todo free all notifications from queue */
461 apr_array_clear(locked_session->notifications);
462 pthread_mutex_destroy(&locked_session->lock);
463 if (locked_session->hello_message != NULL) {
464 /** \todo free hello_message */
465 //json_object_put(locked_session->hello_message);
466 locked_session->hello_message = NULL;
467 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200468 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200469 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200470 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100471 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200472 return (EXIT_SUCCESS);
473}
474
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100475static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200476{
David Kupka8e60a372012-09-04 09:15:20 +0200477 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200478
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100479 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200480
David Kupka8e60a372012-09-04 09:15:20 +0200481 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100482 DEBUG("lock session lock.");
483 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200484 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100485 DEBUG("Error while locking rwlock");
486 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200487 return EXIT_FAILURE;
488 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200489 /* get session to close */
490 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
491 /* remove session from the active sessions list -> nobody new can now work with session */
492 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200493
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100494 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200495 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100496 DEBUG("Error while unlocking rwlock");
497 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200498 }
499
500 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100501 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200502 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100503 DEBUG("Unknown session to close");
504 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200505 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200506 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100507 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200508}
509
Tomas Cejkac7929632013-10-24 19:25:15 +0200510/**
511 * Test reply message type and return error message.
512 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200513 * \param[in] session nc_session internal struct
514 * \param[in] session_key session key, NULL to disable disconnect on error
515 * \param[in] msgt RPC-REPLY message type
516 * \param[out] data
517 * \return NULL on success
518 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100519json_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 +0200520{
521 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100522 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200523
524 /* process the result of the operation */
525 switch (msgt) {
526 case NC_MSG_UNKNOWN:
527 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100528 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200529 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100530 netconf_close(session_key, &err);
531 }
532 if (err != NULL) {
533 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200534 }
535 return create_error("Internal: Receiving RPC-REPLY failed.");
536 }
537 case NC_MSG_NONE:
538 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100539 if (data != NULL) {
540 free(*data);
Tomas Cejka7b1e3bd2014-04-08 14:34:28 +0200541 (*data) = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100542 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200543 return NULL;
544 case NC_MSG_REPLY:
545 switch (replyt = nc_reply_get_type(reply)) {
546 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100547 if ((data != NULL) && (*data != NULL)) {
548 free(*data);
549 (*data) = NULL;
550 }
551 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200552 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100553 if (((*data) = nc_reply_get_data(reply)) == NULL) {
554 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200555 return create_error("Internal: No data from reply received.");
556 } else {
557 return NULL;
558 }
559 break;
560 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100561 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
562 if (data != NULL) {
563 free(*data);
564 (*data) = NULL;
565 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200566 return create_error(nc_reply_get_errormsg(reply));
567 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100568 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
569 if (data != NULL) {
570 free(*data);
571 (*data) = NULL;
572 }
Tomas Cejka60885252014-03-26 15:45:47 +0100573 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200574 }
575 break;
576 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100577 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
578 if (data != NULL) {
579 free(*data);
580 (*data) = NULL;
581 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200582 return create_error("Internal: Unexpected RPC-REPLY message type.");
583 }
584}
585
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100586json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200587{
588 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200589 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200590
591 /* check requests */
592 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100593 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200594 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200595 }
596
597 if (session != NULL) {
598 /* send the request and get the reply */
599 msgt = nc_session_send_recv(session, rpc, &reply);
600 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100601 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200602 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100603 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200604 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200605 }
606}
607
Tomas Cejkac7929632013-10-24 19:25:15 +0200608/**
609 * Perform RPC method that returns data.
610 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200611 * \param[in] session_key session identifier
612 * \param[in] rpc RPC message to perform
613 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
614 * \return NULL on success, json object with error otherwise
615 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100616static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200617{
618 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200619 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200620 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200621 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100622 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100623 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200624
Radek Krejci8e4632a2012-07-26 13:40:34 +0200625 /* check requests */
626 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200628 res = create_error("Internal: RPC could not be created.");
629 data = NULL;
630 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200631 }
632
David Kupka8e60a372012-09-04 09:15:20 +0200633 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100634 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200635 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100636 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200637 res = create_error("Internal: Lock failed.");
638 data = NULL;
639 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200640 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200641 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200642 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 +0200643 if (locked_session != NULL) {
644 session = locked_session->session;
645 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200646 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200647 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100648 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200649 if (pthread_mutex_lock(&locked_session->lock) != 0) {
650 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100651 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200652 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200653
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100654 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200655 res = create_error("Internal: Could not unlock.");
656 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200657 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200658 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200659 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100660 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200661 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200662
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100663 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200664 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200665 }
666
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200667 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200668
Radek Krejci035bf4e2012-07-25 10:59:09 +0200669 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100670 msgt = nc_session_send_recv(session, rpc, &reply);
671
David Kupka8e60a372012-09-04 09:15:20 +0200672 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100673 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200674 pthread_mutex_unlock(&locked_session->lock);
675 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200676
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100677 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200678 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100679 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100680 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100681 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100682 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100683 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100684 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200685 res = create_error("Unknown session to process.");
686 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200687 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200688finished:
689 nc_reply_free(reply);
690 if (received_data != NULL) {
691 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100692 } else {
693 if (data != NULL) {
694 free(data);
695 data = NULL;
696 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200697 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200698 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200699}
700
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100701static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200702{
703 nc_rpc* rpc;
704 struct nc_filter *f = NULL;
705 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200706 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200707
708 /* create filter if set */
709 if (filter != NULL) {
710 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
711 }
712
713 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100714 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200715 nc_filter_free(f);
716 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100717 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200718 return (NULL);
719 }
720
Tomas Cejka94674662013-09-13 15:55:24 +0200721 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100722#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100723 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100724#else
725 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
726#endif
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100727 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200728 }
729
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100730 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200731 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
Radek Krejci8e4632a2012-07-26 13:40:34 +0200738 return (data);
739}
740
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100741static 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 +0100742{
743 nc_rpc* rpc;
744 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200745 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100746
747 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100748 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100749 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100750 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100751 return (NULL);
752 }
753
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100754 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100755 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100756 if (res != NULL) {
757 (*err) = res;
758 } else {
759 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200760 }
761
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100762 return (data);
763}
764
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100765static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200766{
767 nc_rpc* rpc;
768 struct nc_filter *f = NULL;
769 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200770 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200771
772 /* create filter if set */
773 if (filter != NULL) {
774 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
775 }
776
777 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100778 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200779 nc_filter_free(f);
780 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100781 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200782 return (NULL);
783 }
784
Tomas Cejka94674662013-09-13 15:55:24 +0200785 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100786 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100787 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200788 }
789
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100790 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200791 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200792 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100793 (*err) = res;
794 } else {
795 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200796 }
797
Radek Krejci8e4632a2012-07-26 13:40:34 +0200798 return (data);
799}
800
Tomas Cejkab4d05872014-02-14 22:44:38 +0100801static 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 +0200802{
803 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200804 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200805
806 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100807 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100808 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100809 /* config, url */
810 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100811 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100812 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100813 rpc = nc_rpc_copyconfig(source, target, config);
814 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100815 } else if (source == NC_DATASTORE_URL) {
816 if (target == NC_DATASTORE_URL) {
817 /* url, url */
818 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
819 } else {
820 /* url, datastore */
821 rpc = nc_rpc_copyconfig(source, target, uri_src);
822 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100823 } else {
824 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100825 /* datastore, url */
826 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100827 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100828 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100829 rpc = nc_rpc_copyconfig(source, target);
830 }
831 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200832 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100833 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200834 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200835 }
836
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100837 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200838 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200839
840 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200841}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200842
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100843static 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 +0200844{
845 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200846 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200847
848 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100849 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200850 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100851 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200852 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200853 }
854
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100855 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200856 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200857
858 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200859}
860
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100861static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200862{
863 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200864 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200865
866 /* create requests */
867 rpc = nc_rpc_killsession(sid);
868 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100869 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200870 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200871 }
872
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100873 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200874 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200875 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200876}
877
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100878static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200879{
880 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200881 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200882
883 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200884 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200885 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100886 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200887 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200888 }
889
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100890 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200891 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200892 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200893}
894
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100895static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200896{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200897 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200898 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200899 if (target != NC_DATASTORE_URL) {
900 rpc = nc_rpc_deleteconfig(target);
901 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200902 rpc = nc_rpc_deleteconfig(target, url);
903 }
904 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100905 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200906 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200907 }
908
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100909 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200910 nc_rpc_free (rpc);
911 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200912}
913
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100914static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200915{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100916 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200917}
918
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100919static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200920{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100921 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200922}
923
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100924static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200925{
Tomas Cejka00635972013-06-03 15:10:52 +0200926 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200927 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200928
929 /* create requests */
930 rpc = nc_rpc_generic(content);
931 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100932 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200933 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200934 }
935
Radek Krejcia332b692012-11-12 16:15:54 +0100936 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200937 // TODO ?free(*data);
938 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100939 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200940
941 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100942 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200943 nc_rpc_free (rpc);
944 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200945}
946
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200947/**
948 * @}
949 *//* netconf_operations */
950
Radek Krejci7338bde2012-08-10 12:57:30 +0200951void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200952{
Radek Krejci7338bde2012-08-10 12:57:30 +0200953 switch (level) {
954 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100955 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200956 break;
957 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100958 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200959 break;
960 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100961 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200962 break;
963 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100964 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200965 break;
966 }
Radek Krejci469aab82012-07-22 18:42:20 +0200967}
968
Tomas Cejka64b87482013-06-03 16:30:53 +0200969/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200970 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200971 * Caller should free message memory.
972 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200973 * \return pointer to message
974 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100975char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200976{
977 /* read json in chunked framing */
978 unsigned int buffer_size = 0;
979 ssize_t buffer_len = 0;
980 char *buffer = NULL;
981 char c;
982 ssize_t ret;
983 int i, chunk_len;
984 char chunk_len_str[12];
985
986 while (1) {
987 /* read chunk length */
988 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
989 if (buffer != NULL) {
990 free (buffer);
991 buffer = NULL;
992 }
993 break;
994 }
995 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
996 if (buffer != NULL) {
997 free (buffer);
998 buffer = NULL;
999 }
1000 break;
1001 }
1002 i=0;
1003 memset (chunk_len_str, 0, 12);
1004 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1005 if (i==0 && c == '#') {
1006 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1007 /* end but invalid */
1008 if (buffer != NULL) {
1009 free (buffer);
1010 buffer = NULL;
1011 }
1012 }
1013 /* end of message, double-loop break */
1014 goto msg_complete;
1015 }
1016 chunk_len_str[i++] = c;
1017 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001018 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001019 break;
1020 }
1021 }
1022 if (c != '\n') {
1023 if (buffer != NULL) {
1024 free (buffer);
1025 buffer = NULL;
1026 }
1027 break;
1028 }
1029 chunk_len_str[i] = 0;
1030 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1031 if (buffer != NULL) {
1032 free (buffer);
1033 buffer = NULL;
1034 }
1035 break;
1036 }
1037 buffer_size += chunk_len+1;
1038 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001039 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001040 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1041 if (buffer != NULL) {
1042 free (buffer);
1043 buffer = NULL;
1044 }
1045 break;
1046 }
1047 buffer_len += ret;
1048 }
1049msg_complete:
1050 return buffer;
1051}
1052
Tomas Cejkad5b53772013-06-08 23:01:07 +02001053NC_DATASTORE parse_datastore(const char *ds)
1054{
1055 if (strcmp(ds, "running") == 0) {
1056 return NC_DATASTORE_RUNNING;
1057 } else if (strcmp(ds, "startup") == 0) {
1058 return NC_DATASTORE_STARTUP;
1059 } else if (strcmp(ds, "candidate") == 0) {
1060 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001061 } else if (strcmp(ds, "url") == 0) {
1062 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001063 } else if (strcmp(ds, "config") == 0) {
1064 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001065 }
1066 return -1;
1067}
1068
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001069NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1070{
1071 if (strcmp(t, "notset") == 0) {
1072 return NC_EDIT_TESTOPT_NOTSET;
1073 } else if (strcmp(t, "testset") == 0) {
1074 return NC_EDIT_TESTOPT_TESTSET;
1075 } else if (strcmp(t, "set") == 0) {
1076 return NC_EDIT_TESTOPT_SET;
1077 } else if (strcmp(t, "test") == 0) {
1078 return NC_EDIT_TESTOPT_TEST;
1079 }
1080 return NC_EDIT_TESTOPT_ERROR;
1081}
1082
Tomas Cejkad5b53772013-06-08 23:01:07 +02001083json_object *create_error(const char *errmess)
1084{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001085 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001086 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001087 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001088 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001089 json_object_array_add(array, json_object_new_string(errmess));
1090 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001091 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001092 return reply;
1093
1094}
1095
1096json_object *create_data(const char *data)
1097{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001098 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001099 json_object *reply = json_object_new_object();
1100 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1101 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001102 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001103 return reply;
1104}
1105
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001106json_object *create_ok()
1107{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001108 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001109 json_object *reply = json_object_new_object();
1110 reply = json_object_new_object();
1111 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001112 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001113 return reply;
1114}
1115
1116json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001117{
1118 const char *host = NULL;
1119 const char *port = NULL;
1120 const char *user = NULL;
1121 const char *pass = NULL;
1122 json_object *capabilities = NULL;
1123 json_object *reply = NULL;
1124 char *session_key_hash = NULL;
1125 struct nc_cpblts* cpblts = NULL;
1126 unsigned int len, i;
1127
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001128 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001129 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001130 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1131 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1132 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1133 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1134
1135 capabilities = json_object_object_get((json_object *) request, "capabilities");
1136 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1137 cpblts = nc_cpblts_new(NULL);
1138 for (i=0; i<len; i++) {
1139 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1140 }
1141 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001142 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001143 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001144 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001145
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001146 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001147 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001148 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001149 session_key_hash = NULL;
1150 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001151 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1152 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001153 }
1154 if (cpblts != NULL) {
1155 nc_cpblts_free(cpblts);
1156 }
1157
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001158 GETSPEC_ERR_REPLY
1159
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001160 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001161 if (session_key_hash == NULL) {
1162 /* negative reply */
1163 if (err_reply == NULL) {
1164 reply = json_object_new_object();
1165 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1166 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001167 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001168 } else {
1169 /* use filled err_reply from libnetconf's callback */
1170 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001171 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001172 }
1173 } else {
1174 /* positive reply */
1175 reply = json_object_new_object();
1176 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1177 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1178
1179 free(session_key_hash);
1180 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001181 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001182 return reply;
1183}
1184
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001185json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001186{
1187 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001188 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001189 json_object *reply = NULL;
1190
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001191 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001192
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001193 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001194 filter = json_object_get_string(json_object_object_get(request, "filter"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001195 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001196
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001197 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001198 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001199 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001200 reply = create_data(data);
1201 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001202 }
1203 return reply;
1204}
1205
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001206json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001207{
1208 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001209 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001210 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001211 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001212 json_object *reply = NULL;
1213
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001214 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001215
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001216 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001217 filter = json_object_get_string(json_object_object_get(request, "filter"));
1218
Tomas Cejkad5b53772013-06-08 23:01:07 +02001219 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1220 ds_type_s = parse_datastore(source);
1221 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001222 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001223 if (ds_type_s == -1) {
1224 return create_error("Invalid source repository type requested.");
1225 }
1226
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001227 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001228 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001229 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001230 reply = create_data(data);
1231 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001232 }
1233 return reply;
1234}
1235
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001236json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001237{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001238 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001239 const char *identifier = NULL;
1240 const char *version = NULL;
1241 const char *format = NULL;
1242 json_object *reply = NULL;
1243
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001244 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001245 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001246 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001247 version = json_object_get_string(json_object_object_get(request, "version"));
1248 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001249 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001250
Tomas Cejkad5b53772013-06-08 23:01:07 +02001251 if (identifier == NULL) {
1252 return create_error("No identifier for get-schema supplied.");
1253 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001255 DEBUG("get-schema(version: %s, format: %s)", version, format);
1256 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001257 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001258 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001259 reply = create_data(data);
1260 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001261 }
1262 return reply;
1263}
1264
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001265json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001266{
1267 NC_DATASTORE ds_type_s = -1;
1268 NC_DATASTORE ds_type_t = -1;
1269 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1270 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001271 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001272 const char *defop = NULL;
1273 const char *erropt = NULL;
1274 const char *config = NULL;
1275 const char *source = NULL;
1276 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001277 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001278 json_object *reply = NULL;
1279
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001280 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001281
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001282 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001283 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001284 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1285 /* get parameters */
1286 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1287 ds_type_t = parse_datastore(target);
1288 }
1289 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1290 ds_type_s = parse_datastore(source);
1291 } else {
1292 /* source is optional, default value is config */
1293 ds_type_s = NC_DATASTORE_CONFIG;
1294 }
1295 config = json_object_get_string(json_object_object_get(request, "config"));
1296 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1297 pthread_mutex_unlock(&json_lock);
1298
Tomas Cejkad5b53772013-06-08 23:01:07 +02001299 if (defop != NULL) {
1300 if (strcmp(defop, "merge") == 0) {
1301 defop_type = NC_EDIT_DEFOP_MERGE;
1302 } else if (strcmp(defop, "replace") == 0) {
1303 defop_type = NC_EDIT_DEFOP_REPLACE;
1304 } else if (strcmp(defop, "none") == 0) {
1305 defop_type = NC_EDIT_DEFOP_NONE;
1306 } else {
1307 return create_error("Invalid default-operation parameter.");
1308 }
1309 } else {
1310 defop_type = NC_EDIT_DEFOP_NOTSET;
1311 }
1312
Tomas Cejkad5b53772013-06-08 23:01:07 +02001313 if (erropt != NULL) {
1314 if (strcmp(erropt, "continue-on-error") == 0) {
1315 erropt_type = NC_EDIT_ERROPT_CONT;
1316 } else if (strcmp(erropt, "stop-on-error") == 0) {
1317 erropt_type = NC_EDIT_ERROPT_STOP;
1318 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1319 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1320 } else {
1321 return create_error("Invalid error-option parameter.");
1322 }
1323 } else {
1324 erropt_type = 0;
1325 }
1326
Tomas Cejkad5b53772013-06-08 23:01:07 +02001327 if (ds_type_t == -1) {
1328 return create_error("Invalid target repository type requested.");
1329 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001330 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001331 if (config == NULL) {
1332 return create_error("Invalid config data parameter.");
1333 }
1334 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001335 if (config == NULL) {
1336 config = "";
1337 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001338 }
1339
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001340 if (testopt != NULL) {
1341 testopt_type = parse_testopt(testopt);
1342 } else {
1343 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1344 }
1345
1346 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 +01001347
1348 CHECK_ERR_SET_REPLY
1349
Tomas Cejkad5b53772013-06-08 23:01:07 +02001350 return reply;
1351}
1352
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001353json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001354{
1355 NC_DATASTORE ds_type_s = -1;
1356 NC_DATASTORE ds_type_t = -1;
1357 const char *config = NULL;
1358 const char *target = NULL;
1359 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001360 const char *uri_src = NULL;
1361 const char *uri_trg = NULL;
1362
Tomas Cejkad5b53772013-06-08 23:01:07 +02001363 json_object *reply = NULL;
1364
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001365 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001366
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001367 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001368 /* get parameters */
1369 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1370 ds_type_t = parse_datastore(target);
1371 }
1372 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1373 ds_type_s = parse_datastore(source);
1374 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001375 config = json_object_get_string(json_object_object_get(request, "config"));
1376 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
1377 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
1378 pthread_mutex_unlock(&json_lock);
1379
Tomas Cejkad5b53772013-06-08 23:01:07 +02001380 if (source == NULL) {
1381 /* no explicit source specified -> use config data */
1382 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001383 } else if (ds_type_s == -1) {
1384 /* source datastore specified, but it is invalid */
1385 return create_error("Invalid source repository type requested.");
1386 }
1387
1388 if (ds_type_t == -1) {
1389 /* invalid target datastore specified */
1390 return create_error("Invalid target repository type requested.");
1391 }
1392
Tomas Cejka55c6df52014-02-20 12:59:33 +01001393 /* source can be missing when config is given */
1394 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001395 reply = create_error("invalid input parameters - source and config is required.");
1396 return reply;
1397 }
1398
1399 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001400 if (uri_src == NULL) {
1401 uri_src = "";
1402 }
1403 }
1404 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001405 if (uri_trg == NULL) {
1406 uri_trg = "";
1407 }
1408 }
1409 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001410
1411 CHECK_ERR_SET_REPLY
1412
Tomas Cejkad5b53772013-06-08 23:01:07 +02001413 return reply;
1414}
1415
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001416json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001417{
1418 json_object *reply = NULL;
1419 const char *config = NULL;
1420 char *data = NULL;
1421
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001422 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001423
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001424 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001425 config = json_object_get_string(json_object_object_get(request, "content"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001426 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001427
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001428 reply = netconf_generic(session_key, config, &data);
1429 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001430 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001431 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001432 /* use filled err_reply from libnetconf's callback */
1433 reply = err_reply;
1434 }
1435 } else {
1436 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001437 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001438 reply = json_object_new_object();
1439 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001440 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001441 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001442 reply = create_data(data);
1443 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001444 }
1445 }
1446 return reply;
1447}
1448
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001449json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001450{
1451 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001452 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001453
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001454 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001455 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001456 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001457 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001458 }
1459 return reply;
1460}
1461
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001462json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001463{
1464 json_object *reply = NULL;
1465 const char *sid = NULL;
1466
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001467 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001468
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001469 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001470 sid = json_object_get_string(json_object_object_get(request, "session-id"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001471 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472
1473 if (sid == NULL) {
1474 return create_error("Missing session-id parameter.");
1475 }
1476
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001477 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001478
1479 CHECK_ERR_SET_REPLY
1480
Tomas Cejkad5b53772013-06-08 23:01:07 +02001481 return reply;
1482}
1483
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001484json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001485{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001486 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001487 struct session_with_mutex * locked_session = NULL;
1488 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001489 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001490
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001491 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001492 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001493 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001494 return NULL;
1495 }
1496
Tomas Cejkad5b53772013-06-08 23:01:07 +02001497 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1498 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001499 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001500 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001501 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001502 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001503 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001504 prepare_status_message(locked_session, temp_session);
1505 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001506 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001507 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001508 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001509 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001510 reply = create_error("Reload was unsuccessful, connection failed.");
1511 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001512 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001513 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001514 DEBUG("UNLOCK wrlock %s", __func__);
1515 if (pthread_rwlock_unlock(&session_lock) != 0) {
1516 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1517 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001518 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001519 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001520 if (pthread_rwlock_unlock(&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 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001523 reply = create_error("Invalid session identifier.");
1524 }
1525
1526 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1527 reply = locked_session->hello_message;
1528 }
1529 return reply;
1530}
1531
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001532json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001533{
1534 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001535 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001536 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001537
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001538 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001539 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001540 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001541 }
1542
Tomas Cejkad5b53772013-06-08 23:01:07 +02001543 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1544 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001545 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001546 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001547 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001548 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001549 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001550 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001551 if (locked_session->hello_message != NULL) {
1552 reply = locked_session->hello_message;
1553 } else {
1554 reply = create_error("Invalid session identifier.");
1555 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001556 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001557 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001558 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001559 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001560 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001561 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001562 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001563 reply = create_error("Invalid session identifier.");
1564 }
1565
Tomas Cejka47387fd2013-06-10 20:37:46 +02001566
Tomas Cejkad5b53772013-06-08 23:01:07 +02001567 return reply;
1568}
1569
Tomas Cejka6b886e02013-07-05 09:53:17 +02001570void notification_history(time_t eventtime, const char *content)
1571{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001572 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1573 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001574 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001575 return;
1576 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001577 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001578 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001579 json_object *notif = json_object_new_object();
1580 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001581 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001582 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001583 }
1584 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1585 json_object_object_add(notif, "content", json_object_new_string(content));
1586 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001587failed:
1588 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001589}
1590
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001591json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001592{
1593 json_object *reply = NULL;
1594 const char *sid = NULL;
1595 struct session_with_mutex *locked_session = NULL;
1596 struct nc_session *temp_session = NULL;
1597 nc_rpc *rpc = NULL;
1598 time_t start = 0;
1599 time_t stop = 0;
1600 int64_t from, to;
1601
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001602 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001603
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001604 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001605 sid = json_object_get_string(json_object_object_get(request, "session"));
1606 from = json_object_get_int64(json_object_object_get(request, "from"));
1607 to = json_object_get_int64(json_object_object_get(request, "to"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001608 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001609
1610 start = time(NULL) + from;
1611 stop = time(NULL) + to;
1612
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001613 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001614
1615 if (sid == NULL) {
1616 return create_error("Missing session parameter.");
1617 }
1618
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001619 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001620 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001621 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001622 return NULL;
1623 }
1624
1625 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1626 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001627 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001628 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001629 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001630 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001631 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001632 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001633 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001634 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1635 if (temp_session != NULL) {
1636 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1637 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001638 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001639 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001640 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001641 return create_error("notifications: creating an rpc request failed.");
1642 }
1643
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001644 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001645 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001646 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001647 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001648 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001649 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001650 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001651 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001652 }
1653 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1654
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001655 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001656 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001657 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001658 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001659 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001660 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001661 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001662 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001663 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001664 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001665
1666 ncntf_dispatch_receive(temp_session, notification_history);
1667
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001668 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001669 reply = json_object_new_object();
1670 json_object_object_add(reply, "notifications", notif_history_array);
1671 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001672 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001673
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001674 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001675 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001676 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001677 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001678 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001679 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001680 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001681 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001682 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001683 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1684 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001685 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001686 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001687 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001688 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001689 }
1690 reply = create_error("Invalid session identifier.");
1691 }
1692
Tomas Cejka4003a702013-10-01 00:02:45 +02001693 return reply;
1694}
1695
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001696json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001697{
1698 json_object *reply = NULL;
1699 const char *sid = NULL;
1700 const char *target = NULL;
1701 const char *url = NULL;
Tomas Cejka4003a702013-10-01 00:02:45 +02001702 nc_rpc *rpc = NULL;
1703 NC_DATASTORE target_ds;
1704
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001705 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001706
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001707 pthread_mutex_lock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001708 sid = json_object_get_string(json_object_object_get(request, "session"));
1709 target = json_object_get_string(json_object_object_get(request, "target"));
1710 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001711 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001712
1713
1714 if ((sid == NULL) || (target == NULL)) {
1715 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001716 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001717
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001718 /* validation */
1719 target_ds = parse_datastore(target);
1720 if (target_ds == NC_DATASTORE_URL) {
1721 if (url != NULL) {
1722 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001723 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001724 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001725 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001726 rpc = nc_rpc_validate(target_ds);
1727 }
1728 if (rpc == NULL) {
1729 DEBUG("mod_netconf: creating rpc request failed");
1730 reply = create_error("Creation of RPC request failed.");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001731 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001732 }
1733
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001734 DEBUG("Request: validate datastore");
1735 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001736
1737 CHECK_ERR_SET_REPLY
1738
1739 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001740 DEBUG("Request: validation ok.");
1741 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001742 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001743 }
1744 nc_rpc_free (rpc);
1745
Tomas Cejka6b886e02013-07-05 09:53:17 +02001746 return reply;
1747}
1748
David Kupka8e60a372012-09-04 09:15:20 +02001749void * thread_routine (void * arg)
1750{
1751 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001752 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001753 json_object *request = NULL;
1754 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001755 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001756 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001757 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001758 const char *session_key;
1759 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001760 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001761 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001762 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001763 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001764 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001765 int client = ((struct pass_to_thread*)arg)->client;
1766
Tomas Cejka00635972013-06-03 15:10:52 +02001767 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001768
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001769 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001770 create_err_reply_p();
1771
David Kupka8e60a372012-09-04 09:15:20 +02001772 while (!isterminated) {
1773 fds.fd = client;
1774 fds.events = POLLIN;
1775 fds.revents = 0;
1776
1777 status = poll(&fds, 1, 1000);
1778
1779 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1780 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001781 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001782 continue;
1783 } else if (status < 0) {
1784 /* 0: poll time outed
1785 * close socket and ignore this request from the client, it can try it again
1786 * -1: poll failed
1787 * something wrong happend, close this socket and wait for another request
1788 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001789 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001790 close(client);
1791 break;
1792 }
1793 /* status > 0 */
1794
1795 /* check the status of the socket */
1796
1797 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1798 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1799 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001800 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001801 close(client);
1802 break;
1803 }
1804
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001805 DEBUG("Get framed message...");
1806 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001807
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001808 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001809 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001810 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001811 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001812 request = json_tokener_parse_verbose(buffer, &jerr);
1813 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001814 DEBUG("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001815 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001816 continue;
1817 }
David Kupka8e60a372012-09-04 09:15:20 +02001818
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001819 operation = json_object_get_int(json_object_object_get(request, "type"));
Tomas Cejka64b87482013-06-03 16:30:53 +02001820 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001821 pthread_mutex_unlock(&json_lock);
1822
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001823 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001824 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1825 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001826 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001827 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001828 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001829 pthread_mutex_unlock(&json_lock);
1830
David Kupka8e60a372012-09-04 09:15:20 +02001831 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001832
1833 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001834 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001835 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001836 /* there is some stupid client, so close the connection to give a chance to some other client */
1837 close(client);
1838 break;
1839 }
1840
David Kupka8e60a372012-09-04 09:15:20 +02001841 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001842 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001843
1844 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001845 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001846 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001847 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001848 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001849 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001850 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001851
1852 /* process required operation */
1853 switch (operation) {
1854 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001855 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001856 break;
1857 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001858 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001859 break;
1860 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001861 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001862 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001863 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001864 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001865 break;
David Kupka8e60a372012-09-04 09:15:20 +02001866 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001867 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001868 break;
1869 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001870 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001871 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001872
David Kupka8e60a372012-09-04 09:15:20 +02001873 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001874 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001875 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001876 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001877 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001878 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1879 ds_type_t = parse_datastore(target);
1880 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001881 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001882
1883 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001884 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001885 break;
1886 }
David Kupka8e60a372012-09-04 09:15:20 +02001887 switch(operation) {
1888 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001889 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001890 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001891 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001892 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001893 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001894 break;
1895 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001896 DEBUG("Request: lock (session %s)", session_key);
1897 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001898 break;
1899 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001900 DEBUG("Request: unlock (session %s)", session_key);
1901 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001902 break;
1903 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001904 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001905 break;
1906 }
1907
Tomas Cejka442258e2014-04-01 18:17:18 +02001908 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02001909 if (reply == NULL) {
Tomas Cejka442258e2014-04-01 18:17:18 +02001910 pthread_mutex_lock(&json_lock);
1911 reply = json_object_new_object();
1912 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1913 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001914 }
1915 break;
1916 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001917 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001918 break;
1919 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001920 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001921 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001922 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001923 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001924 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001925 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001926 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001927 break;
1928 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001929 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001930 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001931 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001932 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001933 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001934 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001935 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001936 break;
David Kupka8e60a372012-09-04 09:15:20 +02001937 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001938 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001939 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001940 break;
1941 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001942 DEBUG("Clean request json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001943 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001944 json_object_put(request);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001945 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001946 /* send reply to caller */
1947 if (reply != NULL) {
1948 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001949 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001950 if (buffer != NULL) {
1951 free(buffer);
1952 buffer = NULL;
1953 }
David Kupka8e60a372012-09-04 09:15:20 +02001954 break;
1955 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001956 pthread_mutex_unlock(&json_lock);
1957
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001958 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001959 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001960 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001961 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001962 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001963 reply = NULL;
1964 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001965 free(chunked_out_msg);
1966 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001967 if (buffer != NULL) {
1968 free(buffer);
1969 buffer = NULL;
1970 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001971 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02001972 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02001973 } else {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001974 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001975 DEBUG("Reply is NULL, shouldn't be...");
1976 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001977 }
1978 }
1979 }
David Kupka8e60a372012-09-04 09:15:20 +02001980 free (arg);
1981
Tomas Cejka442258e2014-04-01 18:17:18 +02001982 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001983
David Kupka8e60a372012-09-04 09:15:20 +02001984 return retval;
1985}
1986
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001987/**
1988 * \brief Close all open NETCONF sessions.
1989 *
1990 * During termination of mod_netconf, it is useful to close all remaining
1991 * sessions. This function iterates over the list of sessions and close them
1992 * all.
1993 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001994 * \param[in] p apr pool needed for hash table iterating
1995 * \param[in] ht hash table of session_with_mutex structs
1996 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001997static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001998{
1999 apr_hash_index_t *hi;
2000 void *val = NULL;
2001 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002002 const char *hashed_key = NULL;
2003 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002004 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002005
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002006 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002007 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002008 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002009 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002010 return;
2011 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002012 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002013 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002014 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002015 swm = (struct session_with_mutex *) val;
2016 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002017 DEBUG("LOCK mutex %s", __func__);
2018 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002019 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002020 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002021 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002022
2023 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002024 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002025 }
2026 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002027 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002028 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002029 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002030 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002031 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002032}
2033
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002034static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002035{
2036 apr_hash_index_t *hi;
2037 void *val = NULL;
2038 struct nc_session *ns = NULL;
2039 struct session_with_mutex *swm = NULL;
2040 const char *hashed_key = NULL;
2041 apr_ssize_t hashed_key_length;
2042 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002043 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002044
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002045 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002046//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002047 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002048 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002049 return;
2050 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002051 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002052 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2053 swm = (struct session_with_mutex *) val;
2054 if (swm == NULL) {
2055 continue;
2056 }
2057 ns = swm->session;
2058 if (ns == NULL) {
2059 continue;
2060 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002061//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002062 pthread_mutex_lock(&swm->lock);
2063 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002064 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002065 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002066 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002067//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002068 pthread_mutex_unlock(&swm->lock);
2069
2070 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002071 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002072 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002073//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002074 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002075 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002076 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002077 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002078//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002079 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002080 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002081 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002082}
2083
2084
2085/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002086 * This is actually implementation of NETCONF client
2087 * - requests are received from UNIX socket in the predefined format
2088 * - results are replied through the same way
2089 * - the daemon run as a separate process, but it is started and stopped
2090 * automatically by Apache.
2091 *
2092 */
Radek Krejci469aab82012-07-22 18:42:20 +02002093static void forked_proc(apr_pool_t * pool, server_rec * server)
2094{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002095 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002096 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002097 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002098 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002099 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002100 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002101 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002102 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002103 struct timespec maxtime;
2104 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002105 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002106 #ifdef WITH_NOTIFICATIONS
2107 char use_notifications = 0;
2108 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002109
Tomas Cejka6b886e02013-07-05 09:53:17 +02002110 http_server = server;
2111
Tomas Cejka404d37e2013-04-13 02:31:35 +02002112 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002113 maxtime.tv_sec = 5;
2114 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002115
Tomas Cejka04e08f42014-03-27 19:52:34 +01002116#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002117 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002118 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002119#else
2120# ifdef SU_GROUP
2121 if (strlen(SU_GROUP) > 0) {
2122 struct group *g = getgrnam(SU_GROUP);
2123 if (g == NULL) {
2124 DEBUG("GID (%s) was not found.", SU_GROUP);
2125 return;
2126 }
2127 if (setgid(g->gr_gid) != 0) {
2128
2129 DEBUG("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
2130 return;
2131 }
2132 }
2133# else
2134 DEBUG("no SU_GROUP");
2135# endif
2136# ifdef SU_USER
2137 if (strlen(SU_USER) > 0) {
2138 struct passwd *p = getpwnam(SU_USER);
2139 if (p == NULL) {
2140 DEBUG("UID (%s) was not found.", SU_USER);
2141 return;
2142 }
2143 if (setuid(p->pw_uid) != 0) {
2144 DEBUG("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
2145 return;
2146 }
2147 }
2148# else
2149 DEBUG("no SU_USER");
2150# endif
2151#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002152
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002153 if (server != NULL) {
2154 cfg = ap_get_module_config(server->module_config, &netconf_module);
2155 if (cfg == NULL) {
2156 DEBUG("Getting mod_netconf configuration failed");
2157 return;
2158 }
2159 sockname = cfg->sockname;
2160 } else {
2161 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002162 }
Radek Krejci469aab82012-07-22 18:42:20 +02002163
Tomas Cejka04e08f42014-03-27 19:52:34 +01002164 /* try to remove if exists */
2165 unlink(sockname);
2166
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002167 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002168 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002169 DEBUG("Creating socket failed (%s)", strerror(errno));
2170 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002171 }
2172
2173 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002174 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002175 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2176
2177 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2178 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002179 DEBUG("mod_netconf socket address already in use");
2180 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002181 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002182 DEBUG("Binding socket failed (%s)", strerror(errno));
2183 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002184 }
2185
2186 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002187 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2188 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002189 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002190 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002191
Tomas Cejka04e08f42014-03-27 19:52:34 +01002192 uid_t user = -1;
2193 if (strlen(CHOWN_USER) > 0) {
2194 struct passwd *p = getpwnam(CHOWN_USER);
2195 if (p != NULL) {
2196 user = p->pw_uid;
2197 }
2198 }
2199 gid_t group = -1;
2200 if (strlen(CHOWN_GROUP) > 0) {
2201 struct group *g = getgrnam(CHOWN_GROUP);
2202 if (g != NULL) {
2203 group = g->gr_gid;
2204 }
2205 }
2206 if (chown(sockname, user, group) == -1) {
2207 DEBUG("Chown on socket file failed (%s).", strerror(errno));
2208 }
2209
Tomas Cejkaba21b382013-04-13 02:37:32 +02002210 /* prepare internal lists */
2211 netconf_sessions_list = apr_hash_make(pool);
2212
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002213 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002214 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002215 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002216 use_notifications = 0;
2217 } else {
2218 use_notifications = 1;
2219 }
2220 #endif
2221
Radek Krejci469aab82012-07-22 18:42:20 +02002222 /* setup libnetconf's callbacks */
2223 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002224 nc_callback_print(clb_print);
2225 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2226 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2227 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002228 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002229
2230 /* disable publickey authentication */
2231 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2232
David Kupka8e60a372012-09-04 09:15:20 +02002233 /* create mutex protecting session list */
2234 pthread_rwlockattr_init(&lock_attrs);
2235 /* rwlock is shared only with threads in this process */
2236 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2237 /* create rw lock */
2238 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002239 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2240 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002241 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002242 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002243 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002244 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002245 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002246 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002247 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002248 DEBUG("init of err_reply_key.");
2249 if (pthread_key_create(&err_reply_key, NULL) != 0) {
2250 DEBUG("init of err_reply_key failed");
2251 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002252
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002253 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002254 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002255 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002256 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002257 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002258 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002259 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002260 }
2261 if (use_notifications == 1) {
2262 notification_handle();
2263 }
2264 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002265 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002266 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002267 }
Radek Krejci469aab82012-07-22 18:42:20 +02002268
2269 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002270 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002271 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002272 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002273 olds = tv.tv_sec;
2274 }
David Kupka8e60a372012-09-04 09:15:20 +02002275 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002276 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2277 apr_sleep(SLEEP_TIME);
2278 continue;
2279 } else if (client == -1 && (errno == EINTR)) {
2280 continue;
2281 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002282 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002283 continue;
2284 }
Radek Krejci469aab82012-07-22 18:42:20 +02002285
2286 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002287 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002288
David Kupka8e60a372012-09-04 09:15:20 +02002289 arg = malloc (sizeof(struct pass_to_thread));
2290 arg->client = client;
2291 arg->pool = pool;
2292 arg->server = server;
2293 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002294
David Kupka8e60a372012-09-04 09:15:20 +02002295 /* start new thread. It will serve this particular request and then terminate */
2296 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002297 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002298 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002299 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002300 pthread_count++;
2301 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2302 ptids[pthread_count] = 0;
2303 }
Radek Krejci469aab82012-07-22 18:42:20 +02002304
David Kupka8e60a372012-09-04 09:15:20 +02002305 /* check if some thread already terminated, free some resources by joining it */
2306 for (i=0; i<pthread_count; i++) {
2307 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002308 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002309 pthread_count--;
2310 if (pthread_count > 0) {
2311 /* place last Thread ID on the place of joined one */
2312 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002313 }
Radek Krejci469aab82012-07-22 18:42:20 +02002314 }
2315 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002316 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002317 }
2318
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002319 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002320 /* join all threads */
2321 for (i=0; i<pthread_count; i++) {
2322 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2323 }
Radek Krejci469aab82012-07-22 18:42:20 +02002324
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002325 #ifdef WITH_NOTIFICATIONS
2326 notification_close();
2327 #endif
2328
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002329 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002330 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002331
David Kupka8e60a372012-09-04 09:15:20 +02002332 /* destroy rwlock */
2333 pthread_rwlock_destroy(&session_lock);
2334 pthread_rwlockattr_destroy(&lock_attrs);
2335
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002336 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002337
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002338 free(ptids);
2339 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002340 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002341 return;
2342error_exit:
2343 close(lsock);
2344 free(ptids);
2345 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002346}
2347
Radek Krejcif23850c2012-07-23 16:14:17 +02002348static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002349{
Radek Krejcif23850c2012-07-23 16:14:17 +02002350 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2351 apr_pool_create(&config->pool, pool);
2352 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002353 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002354
2355 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002356}
2357
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002358#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002359static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2360 apr_pool_t * plog, server_rec * s)
2361{
Radek Krejcif23850c2012-07-23 16:14:17 +02002362 mod_netconf_cfg *config;
2363 apr_status_t res;
2364
Radek Krejci469aab82012-07-22 18:42:20 +02002365 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002366 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002367 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002368
2369 /*
2370 * The following checks if this routine has been called before.
2371 * This is necessary because the parent process gets initialized
2372 * a couple of times as the server starts up.
2373 */
2374 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2375 if (!data) {
2376 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2377 return (OK);
2378 }
2379
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002380 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002381 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002382
Radek Krejcif23850c2012-07-23 16:14:17 +02002383 if (config && config->forkproc == NULL) {
2384 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2385 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002386 switch (res) {
2387 case APR_INCHILD:
2388 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002389 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002390 apr_signal(SIGTERM, signal_handler);
2391
2392 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002393 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002394
2395 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002396 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002397
Radek Krejcif23850c2012-07-23 16:14:17 +02002398 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002399 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002400 exit(APR_EGENERAL);
2401 break;
2402 case APR_INPARENT:
2403 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002404 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002405 break;
2406 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002407 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002408 break;
2409 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002410 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002411 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002412 }
2413
2414 return OK;
2415}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002416#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002417
Radek Krejci469aab82012-07-22 18:42:20 +02002418/**
2419 * Register module hooks
2420 */
2421static void mod_netconf_register_hooks(apr_pool_t * p)
2422{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002423#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002424 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002425#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002426}
2427
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002428static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2429{
2430 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2431 return NULL;
2432}
2433
2434static const command_rec netconf_cmds[] = {
2435 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2436 {NULL}
2437};
2438
Radek Krejci469aab82012-07-22 18:42:20 +02002439/* Dispatch list for API hooks */
2440module AP_MODULE_DECLARE_DATA netconf_module = {
2441 STANDARD20_MODULE_STUFF,
2442 NULL, /* create per-dir config structures */
2443 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002444 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002445 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002446 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002447 mod_netconf_register_hooks /* register hooks */
2448};
Radek Krejcia332b692012-11-12 16:15:54 +01002449
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002450int main(int argc, char **argv)
2451{
2452 apr_pool_t *pool;
2453 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2454 apr_signal(SIGTERM, signal_handler);
2455 apr_signal(SIGINT, signal_handler);
2456 apr_pool_create(&pool, NULL);
2457 forked_proc(pool, NULL);
2458 apr_pool_destroy(pool);
2459 apr_terminate();
2460 DEBUG("Terminated");
2461 return 0;
2462}