blob: e38ebea589bae1d8e6f9a7882768e916f2efdcac [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
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200725 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
726 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100727#endif
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100728 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200729 }
730
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100731 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200732 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100733 if (res != NULL) {
734 (*err) = res;
735 } else {
736 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200737 }
738
Radek Krejci8e4632a2012-07-26 13:40:34 +0200739 return (data);
740}
741
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100742static 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 +0100743{
744 nc_rpc* rpc;
745 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200746 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100747
748 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100749 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100750 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100751 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100752 return (NULL);
753 }
754
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100755 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100756 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100757 if (res != NULL) {
758 (*err) = res;
759 } else {
760 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200761 }
762
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100763 return (data);
764}
765
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100766static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200767{
768 nc_rpc* rpc;
769 struct nc_filter *f = NULL;
770 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200771 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200772
773 /* create filter if set */
774 if (filter != NULL) {
775 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
776 }
777
778 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100779 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200780 nc_filter_free(f);
781 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100782 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200783 return (NULL);
784 }
785
Tomas Cejka94674662013-09-13 15:55:24 +0200786 /* tell server to show all elements even if they have default values */
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200787 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
788 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
789 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100790 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200791 }
792
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100793 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200794 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200795 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100796 (*err) = res;
797 } else {
798 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200799 }
800
Radek Krejci8e4632a2012-07-26 13:40:34 +0200801 return (data);
802}
803
Tomas Cejkab4d05872014-02-14 22:44:38 +0100804static 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 +0200805{
806 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200807 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200808
809 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100810 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100811 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100812 /* config, url */
813 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100814 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100815 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100816 rpc = nc_rpc_copyconfig(source, target, config);
817 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100818 } else if (source == NC_DATASTORE_URL) {
819 if (target == NC_DATASTORE_URL) {
820 /* url, url */
821 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
822 } else {
823 /* url, datastore */
824 rpc = nc_rpc_copyconfig(source, target, uri_src);
825 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100826 } else {
827 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100828 /* datastore, url */
829 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100830 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100831 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100832 rpc = nc_rpc_copyconfig(source, target);
833 }
834 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200835 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100836 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200837 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200838 }
839
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100840 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200841 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200842
843 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200844}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200845
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100846static 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 +0200847{
848 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200849 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200850
851 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100852 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200853 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100854 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200855 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200856 }
857
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100858 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200859 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200860
861 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200862}
863
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100864static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200865{
866 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200867 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200868
869 /* create requests */
870 rpc = nc_rpc_killsession(sid);
871 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100872 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200873 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200874 }
875
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100876 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200877 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200878 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200879}
880
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100881static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200882{
883 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200884 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200885
886 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200887 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200888 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100889 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200890 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200891 }
892
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100893 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200894 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200895 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200896}
897
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100898static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200899{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200900 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200901 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200902 if (target != NC_DATASTORE_URL) {
903 rpc = nc_rpc_deleteconfig(target);
904 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200905 rpc = nc_rpc_deleteconfig(target, url);
906 }
907 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100908 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200909 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200910 }
911
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100912 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200913 nc_rpc_free (rpc);
914 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200915}
916
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100917static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200918{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100919 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200920}
921
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100922static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200923{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100924 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200925}
926
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100927static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200928{
Tomas Cejka00635972013-06-03 15:10:52 +0200929 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200930 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200931
932 /* create requests */
933 rpc = nc_rpc_generic(content);
934 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100935 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200936 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200937 }
938
Radek Krejcia332b692012-11-12 16:15:54 +0100939 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200940 // TODO ?free(*data);
941 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100942 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200943
944 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100945 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200946 nc_rpc_free (rpc);
947 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200948}
949
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200950/**
951 * @}
952 *//* netconf_operations */
953
Radek Krejci7338bde2012-08-10 12:57:30 +0200954void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200955{
Radek Krejci7338bde2012-08-10 12:57:30 +0200956 switch (level) {
957 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100958 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200959 break;
960 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100961 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200962 break;
963 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100964 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200965 break;
966 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100967 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200968 break;
969 }
Radek Krejci469aab82012-07-22 18:42:20 +0200970}
971
Tomas Cejka64b87482013-06-03 16:30:53 +0200972/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200973 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200974 * Caller should free message memory.
975 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200976 * \return pointer to message
977 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100978char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200979{
980 /* read json in chunked framing */
981 unsigned int buffer_size = 0;
982 ssize_t buffer_len = 0;
983 char *buffer = NULL;
984 char c;
985 ssize_t ret;
986 int i, chunk_len;
987 char chunk_len_str[12];
988
989 while (1) {
990 /* read chunk length */
991 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
992 if (buffer != NULL) {
993 free (buffer);
994 buffer = NULL;
995 }
996 break;
997 }
998 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
999 if (buffer != NULL) {
1000 free (buffer);
1001 buffer = NULL;
1002 }
1003 break;
1004 }
1005 i=0;
1006 memset (chunk_len_str, 0, 12);
1007 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1008 if (i==0 && c == '#') {
1009 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1010 /* end but invalid */
1011 if (buffer != NULL) {
1012 free (buffer);
1013 buffer = NULL;
1014 }
1015 }
1016 /* end of message, double-loop break */
1017 goto msg_complete;
1018 }
1019 chunk_len_str[i++] = c;
1020 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001021 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001022 break;
1023 }
1024 }
1025 if (c != '\n') {
1026 if (buffer != NULL) {
1027 free (buffer);
1028 buffer = NULL;
1029 }
1030 break;
1031 }
1032 chunk_len_str[i] = 0;
1033 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1034 if (buffer != NULL) {
1035 free (buffer);
1036 buffer = NULL;
1037 }
1038 break;
1039 }
1040 buffer_size += chunk_len+1;
1041 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001042 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001043 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1044 if (buffer != NULL) {
1045 free (buffer);
1046 buffer = NULL;
1047 }
1048 break;
1049 }
1050 buffer_len += ret;
1051 }
1052msg_complete:
1053 return buffer;
1054}
1055
Tomas Cejkad5b53772013-06-08 23:01:07 +02001056NC_DATASTORE parse_datastore(const char *ds)
1057{
1058 if (strcmp(ds, "running") == 0) {
1059 return NC_DATASTORE_RUNNING;
1060 } else if (strcmp(ds, "startup") == 0) {
1061 return NC_DATASTORE_STARTUP;
1062 } else if (strcmp(ds, "candidate") == 0) {
1063 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001064 } else if (strcmp(ds, "url") == 0) {
1065 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001066 } else if (strcmp(ds, "config") == 0) {
1067 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001068 }
1069 return -1;
1070}
1071
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001072NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1073{
1074 if (strcmp(t, "notset") == 0) {
1075 return NC_EDIT_TESTOPT_NOTSET;
1076 } else if (strcmp(t, "testset") == 0) {
1077 return NC_EDIT_TESTOPT_TESTSET;
1078 } else if (strcmp(t, "set") == 0) {
1079 return NC_EDIT_TESTOPT_SET;
1080 } else if (strcmp(t, "test") == 0) {
1081 return NC_EDIT_TESTOPT_TEST;
1082 }
1083 return NC_EDIT_TESTOPT_ERROR;
1084}
1085
Tomas Cejkad5b53772013-06-08 23:01:07 +02001086json_object *create_error(const char *errmess)
1087{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001088 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001089 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001090 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001091 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001092 json_object_array_add(array, json_object_new_string(errmess));
1093 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001094 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001095 return reply;
1096
1097}
1098
1099json_object *create_data(const char *data)
1100{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001101 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001102 json_object *reply = json_object_new_object();
1103 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1104 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001105 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001106 return reply;
1107}
1108
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001109json_object *create_ok()
1110{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001111 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001112 json_object *reply = json_object_new_object();
1113 reply = json_object_new_object();
1114 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001115 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001116 return reply;
1117}
1118
1119json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001120{
1121 const char *host = NULL;
1122 const char *port = NULL;
1123 const char *user = NULL;
1124 const char *pass = NULL;
1125 json_object *capabilities = NULL;
1126 json_object *reply = NULL;
1127 char *session_key_hash = NULL;
1128 struct nc_cpblts* cpblts = NULL;
1129 unsigned int len, i;
1130
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001131 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001132 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001133 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1134 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1135 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1136 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1137
1138 capabilities = json_object_object_get((json_object *) request, "capabilities");
1139 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1140 cpblts = nc_cpblts_new(NULL);
1141 for (i=0; i<len; i++) {
1142 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1143 }
1144 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001145 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001146 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001147 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001148
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001149 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001150 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001151 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001152 session_key_hash = NULL;
1153 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001154 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1155 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001156 }
1157 if (cpblts != NULL) {
1158 nc_cpblts_free(cpblts);
1159 }
1160
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001161 GETSPEC_ERR_REPLY
1162
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001163 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001164 if (session_key_hash == NULL) {
1165 /* negative reply */
1166 if (err_reply == NULL) {
1167 reply = json_object_new_object();
1168 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1169 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001170 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001171 } else {
1172 /* use filled err_reply from libnetconf's callback */
1173 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001174 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001175 }
1176 } else {
1177 /* positive reply */
1178 reply = json_object_new_object();
1179 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1180 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1181
1182 free(session_key_hash);
1183 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001184 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001185 return reply;
1186}
1187
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001188json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001189{
1190 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001191 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001192 json_object *reply = NULL;
1193
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001194 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001195
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001196 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001197 filter = json_object_get_string(json_object_object_get(request, "filter"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001198 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001199
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001200 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001201 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001202 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001203 reply = create_data(data);
1204 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001205 }
1206 return reply;
1207}
1208
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001209json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001210{
1211 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001212 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001213 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001214 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001215 json_object *reply = NULL;
1216
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001217 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001218
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001219 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001220 filter = json_object_get_string(json_object_object_get(request, "filter"));
1221
Tomas Cejkad5b53772013-06-08 23:01:07 +02001222 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1223 ds_type_s = parse_datastore(source);
1224 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001225 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001226 if (ds_type_s == -1) {
1227 return create_error("Invalid source repository type requested.");
1228 }
1229
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001230 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001231 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001232 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001233 reply = create_data(data);
1234 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001235 }
1236 return reply;
1237}
1238
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001239json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001240{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001241 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001242 const char *identifier = NULL;
1243 const char *version = NULL;
1244 const char *format = NULL;
1245 json_object *reply = NULL;
1246
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001247 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001248 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001249 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001250 version = json_object_get_string(json_object_object_get(request, "version"));
1251 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001252 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001253
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254 if (identifier == NULL) {
1255 return create_error("No identifier for get-schema supplied.");
1256 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001258 DEBUG("get-schema(version: %s, format: %s)", version, format);
1259 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001260 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001261 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001262 reply = create_data(data);
1263 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001264 }
1265 return reply;
1266}
1267
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001268json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001269{
1270 NC_DATASTORE ds_type_s = -1;
1271 NC_DATASTORE ds_type_t = -1;
1272 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1273 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001274 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001275 const char *defop = NULL;
1276 const char *erropt = NULL;
1277 const char *config = NULL;
1278 const char *source = NULL;
1279 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001280 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001281 json_object *reply = NULL;
1282
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001283 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001284
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001285 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001286 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001287 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1288 /* get parameters */
1289 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1290 ds_type_t = parse_datastore(target);
1291 }
1292 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1293 ds_type_s = parse_datastore(source);
1294 } else {
1295 /* source is optional, default value is config */
1296 ds_type_s = NC_DATASTORE_CONFIG;
1297 }
1298 config = json_object_get_string(json_object_object_get(request, "config"));
1299 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1300 pthread_mutex_unlock(&json_lock);
1301
Tomas Cejkad5b53772013-06-08 23:01:07 +02001302 if (defop != NULL) {
1303 if (strcmp(defop, "merge") == 0) {
1304 defop_type = NC_EDIT_DEFOP_MERGE;
1305 } else if (strcmp(defop, "replace") == 0) {
1306 defop_type = NC_EDIT_DEFOP_REPLACE;
1307 } else if (strcmp(defop, "none") == 0) {
1308 defop_type = NC_EDIT_DEFOP_NONE;
1309 } else {
1310 return create_error("Invalid default-operation parameter.");
1311 }
1312 } else {
1313 defop_type = NC_EDIT_DEFOP_NOTSET;
1314 }
1315
Tomas Cejkad5b53772013-06-08 23:01:07 +02001316 if (erropt != NULL) {
1317 if (strcmp(erropt, "continue-on-error") == 0) {
1318 erropt_type = NC_EDIT_ERROPT_CONT;
1319 } else if (strcmp(erropt, "stop-on-error") == 0) {
1320 erropt_type = NC_EDIT_ERROPT_STOP;
1321 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1322 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1323 } else {
1324 return create_error("Invalid error-option parameter.");
1325 }
1326 } else {
1327 erropt_type = 0;
1328 }
1329
Tomas Cejkad5b53772013-06-08 23:01:07 +02001330 if (ds_type_t == -1) {
1331 return create_error("Invalid target repository type requested.");
1332 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001333 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001334 if (config == NULL) {
1335 return create_error("Invalid config data parameter.");
1336 }
1337 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001338 if (config == NULL) {
1339 config = "";
1340 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001341 }
1342
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001343 if (testopt != NULL) {
1344 testopt_type = parse_testopt(testopt);
1345 } else {
1346 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1347 }
1348
1349 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 +01001350
1351 CHECK_ERR_SET_REPLY
1352
Tomas Cejkad5b53772013-06-08 23:01:07 +02001353 return reply;
1354}
1355
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001356json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001357{
1358 NC_DATASTORE ds_type_s = -1;
1359 NC_DATASTORE ds_type_t = -1;
1360 const char *config = NULL;
1361 const char *target = NULL;
1362 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001363 const char *uri_src = NULL;
1364 const char *uri_trg = NULL;
1365
Tomas Cejkad5b53772013-06-08 23:01:07 +02001366 json_object *reply = NULL;
1367
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001368 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001369
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001370 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001371 /* get parameters */
1372 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1373 ds_type_t = parse_datastore(target);
1374 }
1375 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1376 ds_type_s = parse_datastore(source);
1377 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001378 config = json_object_get_string(json_object_object_get(request, "config"));
1379 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
1380 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
1381 pthread_mutex_unlock(&json_lock);
1382
Tomas Cejkad5b53772013-06-08 23:01:07 +02001383 if (source == NULL) {
1384 /* no explicit source specified -> use config data */
1385 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001386 } else if (ds_type_s == -1) {
1387 /* source datastore specified, but it is invalid */
1388 return create_error("Invalid source repository type requested.");
1389 }
1390
1391 if (ds_type_t == -1) {
1392 /* invalid target datastore specified */
1393 return create_error("Invalid target repository type requested.");
1394 }
1395
Tomas Cejka55c6df52014-02-20 12:59:33 +01001396 /* source can be missing when config is given */
1397 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001398 reply = create_error("invalid input parameters - source and config is required.");
1399 return reply;
1400 }
1401
1402 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001403 if (uri_src == NULL) {
1404 uri_src = "";
1405 }
1406 }
1407 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001408 if (uri_trg == NULL) {
1409 uri_trg = "";
1410 }
1411 }
1412 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001413
1414 CHECK_ERR_SET_REPLY
1415
Tomas Cejkad5b53772013-06-08 23:01:07 +02001416 return reply;
1417}
1418
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001419json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001420{
1421 json_object *reply = NULL;
1422 const char *config = NULL;
1423 char *data = NULL;
1424
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001425 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001427 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001428 config = json_object_get_string(json_object_object_get(request, "content"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001429 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001430
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001431 reply = netconf_generic(session_key, config, &data);
1432 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001433 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001434 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001435 /* use filled err_reply from libnetconf's callback */
1436 reply = err_reply;
1437 }
1438 } else {
1439 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001440 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001441 reply = json_object_new_object();
1442 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001443 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001444 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001445 reply = create_data(data);
1446 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001447 }
1448 }
1449 return reply;
1450}
1451
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001452json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001453{
1454 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001455 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001456
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001457 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001458 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001459 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001460 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001461 }
1462 return reply;
1463}
1464
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001465json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001466{
1467 json_object *reply = NULL;
1468 const char *sid = NULL;
1469
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001470 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001471
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001472 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001473 sid = json_object_get_string(json_object_object_get(request, "session-id"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001474 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001475
1476 if (sid == NULL) {
1477 return create_error("Missing session-id parameter.");
1478 }
1479
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001480 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001481
1482 CHECK_ERR_SET_REPLY
1483
Tomas Cejkad5b53772013-06-08 23:01:07 +02001484 return reply;
1485}
1486
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001487json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001488{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001489 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001490 struct session_with_mutex * locked_session = NULL;
1491 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001492 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001493
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001494 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001495 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001496 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001497 return NULL;
1498 }
1499
Tomas Cejkad5b53772013-06-08 23:01:07 +02001500 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1501 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001502 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001503 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001504 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001505 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001506 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001507 prepare_status_message(locked_session, temp_session);
1508 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001509 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001510 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001511 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001512 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001513 reply = create_error("Reload was unsuccessful, connection failed.");
1514 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001516 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001517 DEBUG("UNLOCK wrlock %s", __func__);
1518 if (pthread_rwlock_unlock(&session_lock) != 0) {
1519 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1520 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001521 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001522 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001523 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001524 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001525 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001526 reply = create_error("Invalid session identifier.");
1527 }
1528
1529 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1530 reply = locked_session->hello_message;
1531 }
1532 return reply;
1533}
1534
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001535json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001536{
1537 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001538 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001539 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001540
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001541 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001542 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001543 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001544 }
1545
Tomas Cejkad5b53772013-06-08 23:01:07 +02001546 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1547 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001548 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001549 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001550 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001551 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001552 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001553 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001554 if (locked_session->hello_message != NULL) {
1555 reply = locked_session->hello_message;
1556 } else {
1557 reply = create_error("Invalid session identifier.");
1558 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001559 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001560 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001561 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001562 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001563 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001564 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001565 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001566 reply = create_error("Invalid session identifier.");
1567 }
1568
Tomas Cejka47387fd2013-06-10 20:37:46 +02001569
Tomas Cejkad5b53772013-06-08 23:01:07 +02001570 return reply;
1571}
1572
Tomas Cejka6b886e02013-07-05 09:53:17 +02001573void notification_history(time_t eventtime, const char *content)
1574{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001575 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1576 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001577 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001578 return;
1579 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001580 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001581 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001582 json_object *notif = json_object_new_object();
1583 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001584 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001585 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001586 }
1587 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1588 json_object_object_add(notif, "content", json_object_new_string(content));
1589 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001590failed:
1591 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001592}
1593
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001594json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001595{
1596 json_object *reply = NULL;
1597 const char *sid = NULL;
1598 struct session_with_mutex *locked_session = NULL;
1599 struct nc_session *temp_session = NULL;
1600 nc_rpc *rpc = NULL;
1601 time_t start = 0;
1602 time_t stop = 0;
1603 int64_t from, to;
1604
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001605 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001606
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001607 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001608 sid = json_object_get_string(json_object_object_get(request, "session"));
1609 from = json_object_get_int64(json_object_object_get(request, "from"));
1610 to = json_object_get_int64(json_object_object_get(request, "to"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001611 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001612
1613 start = time(NULL) + from;
1614 stop = time(NULL) + to;
1615
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001616 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001617
1618 if (sid == NULL) {
1619 return create_error("Missing session parameter.");
1620 }
1621
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001622 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001623 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001624 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001625 return NULL;
1626 }
1627
1628 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1629 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001630 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001631 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001632 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001633 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001634 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001635 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001636 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001637 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1638 if (temp_session != NULL) {
1639 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1640 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001641 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001642 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001643 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001644 return create_error("notifications: creating an rpc request failed.");
1645 }
1646
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001647 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001648 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001649 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001650 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001651 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001652 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001653 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001654 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001655 }
1656 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1657
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001658 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001659 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001660 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001661 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001662 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001663 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001664 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001665 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001666 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001667 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001668
1669 ncntf_dispatch_receive(temp_session, notification_history);
1670
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001671 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001672 reply = json_object_new_object();
1673 json_object_object_add(reply, "notifications", notif_history_array);
1674 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001675 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001676
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001677 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001678 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001679 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001680 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001681 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001682 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001683 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001684 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001685 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001686 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1687 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001688 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001689 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001690 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001691 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001692 }
1693 reply = create_error("Invalid session identifier.");
1694 }
1695
Tomas Cejka4003a702013-10-01 00:02:45 +02001696 return reply;
1697}
1698
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001699json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001700{
1701 json_object *reply = NULL;
1702 const char *sid = NULL;
1703 const char *target = NULL;
1704 const char *url = NULL;
Tomas Cejka4003a702013-10-01 00:02:45 +02001705 nc_rpc *rpc = NULL;
1706 NC_DATASTORE target_ds;
1707
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001708 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001709
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001710 pthread_mutex_lock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001711 sid = json_object_get_string(json_object_object_get(request, "session"));
1712 target = json_object_get_string(json_object_object_get(request, "target"));
1713 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001714 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001715
1716
1717 if ((sid == NULL) || (target == NULL)) {
1718 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001719 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001720
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001721 /* validation */
1722 target_ds = parse_datastore(target);
1723 if (target_ds == NC_DATASTORE_URL) {
1724 if (url != NULL) {
1725 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001726 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001727 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001728 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001729 rpc = nc_rpc_validate(target_ds);
1730 }
1731 if (rpc == NULL) {
1732 DEBUG("mod_netconf: creating rpc request failed");
1733 reply = create_error("Creation of RPC request failed.");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001734 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001735 }
1736
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001737 DEBUG("Request: validate datastore");
1738 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001739
1740 CHECK_ERR_SET_REPLY
1741
1742 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001743 DEBUG("Request: validation ok.");
1744 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001745 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001746 }
1747 nc_rpc_free (rpc);
1748
Tomas Cejka6b886e02013-07-05 09:53:17 +02001749 return reply;
1750}
1751
David Kupka8e60a372012-09-04 09:15:20 +02001752void * thread_routine (void * arg)
1753{
1754 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001755 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001756 json_object *request = NULL;
1757 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001758 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001759 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001760 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001761 const char *session_key;
1762 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001763 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001764 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001765 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001766 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001767 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001768 int client = ((struct pass_to_thread*)arg)->client;
1769
Tomas Cejka00635972013-06-03 15:10:52 +02001770 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001771
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001772 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001773 create_err_reply_p();
1774
David Kupka8e60a372012-09-04 09:15:20 +02001775 while (!isterminated) {
1776 fds.fd = client;
1777 fds.events = POLLIN;
1778 fds.revents = 0;
1779
1780 status = poll(&fds, 1, 1000);
1781
1782 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1783 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001784 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001785 continue;
1786 } else if (status < 0) {
1787 /* 0: poll time outed
1788 * close socket and ignore this request from the client, it can try it again
1789 * -1: poll failed
1790 * something wrong happend, close this socket and wait for another request
1791 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001792 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001793 close(client);
1794 break;
1795 }
1796 /* status > 0 */
1797
1798 /* check the status of the socket */
1799
1800 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1801 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1802 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001803 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001804 close(client);
1805 break;
1806 }
1807
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001808 DEBUG("Get framed message...");
1809 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001810
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001811 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001812 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001813 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001814 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001815 request = json_tokener_parse_verbose(buffer, &jerr);
1816 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001817 DEBUG("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001818 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001819 continue;
1820 }
David Kupka8e60a372012-09-04 09:15:20 +02001821
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001822 operation = json_object_get_int(json_object_object_get(request, "type"));
Tomas Cejka64b87482013-06-03 16:30:53 +02001823 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001824 pthread_mutex_unlock(&json_lock);
1825
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001826 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001827 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1828 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001829 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001830 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001831 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001832 pthread_mutex_unlock(&json_lock);
1833
David Kupka8e60a372012-09-04 09:15:20 +02001834 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001835
1836 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001837 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001838 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001839 /* there is some stupid client, so close the connection to give a chance to some other client */
1840 close(client);
1841 break;
1842 }
1843
David Kupka8e60a372012-09-04 09:15:20 +02001844 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001845 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001846
1847 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001848 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001849 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001850 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001851 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001852 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001853 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001854
1855 /* process required operation */
1856 switch (operation) {
1857 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001858 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001859 break;
1860 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001861 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001862 break;
1863 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001864 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001865 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001866 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001867 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001868 break;
David Kupka8e60a372012-09-04 09:15:20 +02001869 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001870 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001871 break;
1872 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001873 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001874 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001875
David Kupka8e60a372012-09-04 09:15:20 +02001876 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001877 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001878 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001879 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001880 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001881 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1882 ds_type_t = parse_datastore(target);
1883 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001884 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001885
1886 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001887 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001888 break;
1889 }
David Kupka8e60a372012-09-04 09:15:20 +02001890 switch(operation) {
1891 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001892 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001893 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001894 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001895 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001896 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001897 break;
1898 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001899 DEBUG("Request: lock (session %s)", session_key);
1900 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001901 break;
1902 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001903 DEBUG("Request: unlock (session %s)", session_key);
1904 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001905 break;
1906 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001907 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001908 break;
1909 }
1910
Tomas Cejka442258e2014-04-01 18:17:18 +02001911 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02001912 if (reply == NULL) {
Tomas Cejka442258e2014-04-01 18:17:18 +02001913 pthread_mutex_lock(&json_lock);
1914 reply = json_object_new_object();
1915 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1916 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001917 }
1918 break;
1919 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001920 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001921 break;
1922 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001923 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001924 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001925 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001926 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001927 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001928 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001929 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001930 break;
1931 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001932 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001933 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001934 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001935 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001936 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001937 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001938 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001939 break;
David Kupka8e60a372012-09-04 09:15:20 +02001940 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001941 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001942 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001943 break;
1944 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001945 DEBUG("Clean request json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001946 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001947 json_object_put(request);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001948 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001949 /* send reply to caller */
1950 if (reply != NULL) {
1951 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001952 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001953 if (buffer != NULL) {
1954 free(buffer);
1955 buffer = NULL;
1956 }
David Kupka8e60a372012-09-04 09:15:20 +02001957 break;
1958 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001959 pthread_mutex_unlock(&json_lock);
1960
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001961 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001962 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001963 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001964 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001965 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001966 reply = NULL;
1967 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001968 free(chunked_out_msg);
1969 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001970 if (buffer != NULL) {
1971 free(buffer);
1972 buffer = NULL;
1973 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001974 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02001975 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02001976 } else {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001977 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001978 DEBUG("Reply is NULL, shouldn't be...");
1979 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001980 }
1981 }
1982 }
David Kupka8e60a372012-09-04 09:15:20 +02001983 free (arg);
1984
Tomas Cejka442258e2014-04-01 18:17:18 +02001985 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001986
David Kupka8e60a372012-09-04 09:15:20 +02001987 return retval;
1988}
1989
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001990/**
1991 * \brief Close all open NETCONF sessions.
1992 *
1993 * During termination of mod_netconf, it is useful to close all remaining
1994 * sessions. This function iterates over the list of sessions and close them
1995 * all.
1996 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001997 * \param[in] p apr pool needed for hash table iterating
1998 * \param[in] ht hash table of session_with_mutex structs
1999 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002000static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002001{
2002 apr_hash_index_t *hi;
2003 void *val = NULL;
2004 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002005 const char *hashed_key = NULL;
2006 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002007 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002008
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002009 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002010 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002011 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002012 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002013 return;
2014 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002015 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002016 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002017 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002018 swm = (struct session_with_mutex *) val;
2019 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002020 DEBUG("LOCK mutex %s", __func__);
2021 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002022 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002023 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002024 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002025
2026 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002027 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002028 }
2029 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002030 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002031 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002032 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002033 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002034 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002035}
2036
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002037static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002038{
2039 apr_hash_index_t *hi;
2040 void *val = NULL;
2041 struct nc_session *ns = NULL;
2042 struct session_with_mutex *swm = NULL;
2043 const char *hashed_key = NULL;
2044 apr_ssize_t hashed_key_length;
2045 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002046 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002047
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002048 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002049//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002050 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002051 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002052 return;
2053 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002054 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002055 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2056 swm = (struct session_with_mutex *) val;
2057 if (swm == NULL) {
2058 continue;
2059 }
2060 ns = swm->session;
2061 if (ns == NULL) {
2062 continue;
2063 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002064//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002065 pthread_mutex_lock(&swm->lock);
2066 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002067 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002068 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002069 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002070//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002071 pthread_mutex_unlock(&swm->lock);
2072
2073 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002074 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002075 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002076//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002077 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002078 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002079 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002080 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002081//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002082 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002083 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002084 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002085}
2086
2087
2088/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002089 * This is actually implementation of NETCONF client
2090 * - requests are received from UNIX socket in the predefined format
2091 * - results are replied through the same way
2092 * - the daemon run as a separate process, but it is started and stopped
2093 * automatically by Apache.
2094 *
2095 */
Radek Krejci469aab82012-07-22 18:42:20 +02002096static void forked_proc(apr_pool_t * pool, server_rec * server)
2097{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002098 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002099 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002100 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002101 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002102 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002103 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002104 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002105 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002106 struct timespec maxtime;
2107 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002108 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002109 #ifdef WITH_NOTIFICATIONS
2110 char use_notifications = 0;
2111 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002112
Tomas Cejka6b886e02013-07-05 09:53:17 +02002113 http_server = server;
2114
Tomas Cejka404d37e2013-04-13 02:31:35 +02002115 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002116 maxtime.tv_sec = 5;
2117 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002118
Tomas Cejka04e08f42014-03-27 19:52:34 +01002119#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002120 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002121 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002122#else
2123# ifdef SU_GROUP
2124 if (strlen(SU_GROUP) > 0) {
2125 struct group *g = getgrnam(SU_GROUP);
2126 if (g == NULL) {
2127 DEBUG("GID (%s) was not found.", SU_GROUP);
2128 return;
2129 }
2130 if (setgid(g->gr_gid) != 0) {
2131
2132 DEBUG("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
2133 return;
2134 }
2135 }
2136# else
2137 DEBUG("no SU_GROUP");
2138# endif
2139# ifdef SU_USER
2140 if (strlen(SU_USER) > 0) {
2141 struct passwd *p = getpwnam(SU_USER);
2142 if (p == NULL) {
2143 DEBUG("UID (%s) was not found.", SU_USER);
2144 return;
2145 }
2146 if (setuid(p->pw_uid) != 0) {
2147 DEBUG("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
2148 return;
2149 }
2150 }
2151# else
2152 DEBUG("no SU_USER");
2153# endif
2154#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002155
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002156 if (server != NULL) {
2157 cfg = ap_get_module_config(server->module_config, &netconf_module);
2158 if (cfg == NULL) {
2159 DEBUG("Getting mod_netconf configuration failed");
2160 return;
2161 }
2162 sockname = cfg->sockname;
2163 } else {
2164 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002165 }
Radek Krejci469aab82012-07-22 18:42:20 +02002166
Tomas Cejka04e08f42014-03-27 19:52:34 +01002167 /* try to remove if exists */
2168 unlink(sockname);
2169
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002170 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002171 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002172 DEBUG("Creating socket failed (%s)", strerror(errno));
2173 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002174 }
2175
2176 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002177 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002178 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2179
2180 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2181 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002182 DEBUG("mod_netconf socket address already in use");
2183 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002184 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002185 DEBUG("Binding socket failed (%s)", strerror(errno));
2186 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002187 }
2188
2189 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002190 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2191 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002192 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002193 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002194
Tomas Cejka04e08f42014-03-27 19:52:34 +01002195 uid_t user = -1;
2196 if (strlen(CHOWN_USER) > 0) {
2197 struct passwd *p = getpwnam(CHOWN_USER);
2198 if (p != NULL) {
2199 user = p->pw_uid;
2200 }
2201 }
2202 gid_t group = -1;
2203 if (strlen(CHOWN_GROUP) > 0) {
2204 struct group *g = getgrnam(CHOWN_GROUP);
2205 if (g != NULL) {
2206 group = g->gr_gid;
2207 }
2208 }
2209 if (chown(sockname, user, group) == -1) {
2210 DEBUG("Chown on socket file failed (%s).", strerror(errno));
2211 }
2212
Tomas Cejkaba21b382013-04-13 02:37:32 +02002213 /* prepare internal lists */
2214 netconf_sessions_list = apr_hash_make(pool);
2215
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002216 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002217 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002218 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002219 use_notifications = 0;
2220 } else {
2221 use_notifications = 1;
2222 }
2223 #endif
2224
Radek Krejci469aab82012-07-22 18:42:20 +02002225 /* setup libnetconf's callbacks */
2226 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002227 nc_callback_print(clb_print);
2228 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2229 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2230 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002231 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002232
2233 /* disable publickey authentication */
2234 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2235
David Kupka8e60a372012-09-04 09:15:20 +02002236 /* create mutex protecting session list */
2237 pthread_rwlockattr_init(&lock_attrs);
2238 /* rwlock is shared only with threads in this process */
2239 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2240 /* create rw lock */
2241 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002242 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2243 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002244 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002245 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002246 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002247 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002248 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002249 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002250 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002251 DEBUG("init of err_reply_key.");
2252 if (pthread_key_create(&err_reply_key, NULL) != 0) {
2253 DEBUG("init of err_reply_key failed");
2254 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002255
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002256 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002257 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002258 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002259 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002260 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002261 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002262 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002263 }
2264 if (use_notifications == 1) {
2265 notification_handle();
2266 }
2267 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002268 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002269 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002270 }
Radek Krejci469aab82012-07-22 18:42:20 +02002271
2272 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002273 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002274 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002275 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002276 olds = tv.tv_sec;
2277 }
David Kupka8e60a372012-09-04 09:15:20 +02002278 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002279 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2280 apr_sleep(SLEEP_TIME);
2281 continue;
2282 } else if (client == -1 && (errno == EINTR)) {
2283 continue;
2284 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002285 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002286 continue;
2287 }
Radek Krejci469aab82012-07-22 18:42:20 +02002288
2289 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002290 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002291
David Kupka8e60a372012-09-04 09:15:20 +02002292 arg = malloc (sizeof(struct pass_to_thread));
2293 arg->client = client;
2294 arg->pool = pool;
2295 arg->server = server;
2296 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002297
David Kupka8e60a372012-09-04 09:15:20 +02002298 /* start new thread. It will serve this particular request and then terminate */
2299 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002300 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002301 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002302 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002303 pthread_count++;
2304 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2305 ptids[pthread_count] = 0;
2306 }
Radek Krejci469aab82012-07-22 18:42:20 +02002307
David Kupka8e60a372012-09-04 09:15:20 +02002308 /* check if some thread already terminated, free some resources by joining it */
2309 for (i=0; i<pthread_count; i++) {
2310 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002311 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002312 pthread_count--;
2313 if (pthread_count > 0) {
2314 /* place last Thread ID on the place of joined one */
2315 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002316 }
Radek Krejci469aab82012-07-22 18:42:20 +02002317 }
2318 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002319 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002320 }
2321
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002322 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002323 /* join all threads */
2324 for (i=0; i<pthread_count; i++) {
2325 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2326 }
Radek Krejci469aab82012-07-22 18:42:20 +02002327
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002328 #ifdef WITH_NOTIFICATIONS
2329 notification_close();
2330 #endif
2331
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002332 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002333 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002334
David Kupka8e60a372012-09-04 09:15:20 +02002335 /* destroy rwlock */
2336 pthread_rwlock_destroy(&session_lock);
2337 pthread_rwlockattr_destroy(&lock_attrs);
2338
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002339 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002340
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002341 free(ptids);
2342 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002343 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002344 return;
2345error_exit:
2346 close(lsock);
2347 free(ptids);
2348 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002349}
2350
Radek Krejcif23850c2012-07-23 16:14:17 +02002351static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002352{
Radek Krejcif23850c2012-07-23 16:14:17 +02002353 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2354 apr_pool_create(&config->pool, pool);
2355 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002356 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002357
2358 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002359}
2360
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002361#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002362static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2363 apr_pool_t * plog, server_rec * s)
2364{
Radek Krejcif23850c2012-07-23 16:14:17 +02002365 mod_netconf_cfg *config;
2366 apr_status_t res;
2367
Radek Krejci469aab82012-07-22 18:42:20 +02002368 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002369 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002370 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002371
2372 /*
2373 * The following checks if this routine has been called before.
2374 * This is necessary because the parent process gets initialized
2375 * a couple of times as the server starts up.
2376 */
2377 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2378 if (!data) {
2379 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2380 return (OK);
2381 }
2382
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002383 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002384 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002385
Radek Krejcif23850c2012-07-23 16:14:17 +02002386 if (config && config->forkproc == NULL) {
2387 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2388 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002389 switch (res) {
2390 case APR_INCHILD:
2391 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002392 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002393 apr_signal(SIGTERM, signal_handler);
2394
2395 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002396 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002397
2398 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002399 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002400
Radek Krejcif23850c2012-07-23 16:14:17 +02002401 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002402 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002403 exit(APR_EGENERAL);
2404 break;
2405 case APR_INPARENT:
2406 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002407 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002408 break;
2409 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002410 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002411 break;
2412 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002413 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002414 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002415 }
2416
2417 return OK;
2418}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002419#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002420
Radek Krejci469aab82012-07-22 18:42:20 +02002421/**
2422 * Register module hooks
2423 */
2424static void mod_netconf_register_hooks(apr_pool_t * p)
2425{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002426#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002427 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002428#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002429}
2430
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002431static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2432{
2433 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2434 return NULL;
2435}
2436
2437static const command_rec netconf_cmds[] = {
2438 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2439 {NULL}
2440};
2441
Radek Krejci469aab82012-07-22 18:42:20 +02002442/* Dispatch list for API hooks */
2443module AP_MODULE_DECLARE_DATA netconf_module = {
2444 STANDARD20_MODULE_STUFF,
2445 NULL, /* create per-dir config structures */
2446 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002447 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002448 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002449 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002450 mod_netconf_register_hooks /* register hooks */
2451};
Radek Krejcia332b692012-11-12 16:15:54 +01002452
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002453int main(int argc, char **argv)
2454{
2455 apr_pool_t *pool;
2456 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2457 apr_signal(SIGTERM, signal_handler);
2458 apr_signal(SIGINT, signal_handler);
2459 apr_pool_create(&pool, NULL);
2460 forked_proc(pool, NULL);
2461 apr_pool_destroy(pool);
2462 apr_terminate();
2463 DEBUG("Terminated");
2464 return 0;
2465}