blob: 2abc8b02a4618f8f804b914f730d609573c06187 [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
162int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
163{
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);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200370 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200371 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100372 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200373
Radek Krejci469aab82012-07-22 18:42:20 +0200374 /* if connected successful, add session to the list */
375 if (session != NULL) {
376 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200377 session_key = gen_ncsession_hash(
378 (host==NULL) ? "localhost" : host,
379 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200380 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200381
Tomas Cejkaba21b382013-04-13 02:37:32 +0200382 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100383 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 +0200384 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100385 session = NULL;
386 free(locked_session);
387 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100388 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200389 return NULL;
390 }
391 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200392 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200393 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200394 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200395 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100396 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200397 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100398 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200399 if (pthread_rwlock_wrlock (&session_lock) != 0) {
400 nc_session_free(session);
401 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100402 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200403 return NULL;
404 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200405 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200406 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100407 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200408 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 +0100409 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200410
Tomas Cejka47387fd2013-06-10 20:37:46 +0200411 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100412 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200413 pthread_mutex_lock(&locked_session->lock);
414
415 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100416 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200417 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100418 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200419 }
420
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200421 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100422 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200423
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100424 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200425 pthread_mutex_unlock(&locked_session->lock);
426
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100427 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200428 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200429 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100430 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200431 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200432 }
433
Radek Krejci469aab82012-07-22 18:42:20 +0200434}
435
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100436static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200437{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100438 DEBUG("lock private lock.");
439 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200440 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100441 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200442 }
443 locked_session->ntfc_subscribed = 0;
444 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100445 if (locked_session->session != NULL) {
446 nc_session_free(locked_session->session);
447 locked_session->session = NULL;
448 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100449 DEBUG("session closed.");
450 DEBUG("unlock private lock.");
451 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200452 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100453 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200454 }
455
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100456 DEBUG("unlock session lock.");
457 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200458 usleep(500000); /* let notification thread stop */
459
460 /* session shouldn't be used by now */
461 /** \todo free all notifications from queue */
462 apr_array_clear(locked_session->notifications);
463 pthread_mutex_destroy(&locked_session->lock);
464 if (locked_session->hello_message != NULL) {
465 /** \todo free hello_message */
466 //json_object_put(locked_session->hello_message);
467 locked_session->hello_message = NULL;
468 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200469 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200470 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200471 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100472 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200473 return (EXIT_SUCCESS);
474}
475
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100476static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200477{
David Kupka8e60a372012-09-04 09:15:20 +0200478 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200479
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100480 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200481
David Kupka8e60a372012-09-04 09:15:20 +0200482 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100483 DEBUG("lock session lock.");
484 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200485 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100486 DEBUG("Error while locking rwlock");
487 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200488 return EXIT_FAILURE;
489 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200490 /* get session to close */
491 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
492 /* remove session from the active sessions list -> nobody new can now work with session */
493 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200494
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100495 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200496 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100497 DEBUG("Error while unlocking rwlock");
498 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200499 }
500
501 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100502 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200503 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100504 DEBUG("Unknown session to close");
505 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200506 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200507 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100508 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200509}
510
Tomas Cejkac7929632013-10-24 19:25:15 +0200511/**
512 * Test reply message type and return error message.
513 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200514 * \param[in] session nc_session internal struct
515 * \param[in] session_key session key, NULL to disable disconnect on error
516 * \param[in] msgt RPC-REPLY message type
517 * \param[out] data
518 * \return NULL on success
519 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100520json_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 +0200521{
522 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100523 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200524
525 /* process the result of the operation */
526 switch (msgt) {
527 case NC_MSG_UNKNOWN:
528 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100529 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200530 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100531 netconf_close(session_key, &err);
532 }
533 if (err != NULL) {
534 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200535 }
536 return create_error("Internal: Receiving RPC-REPLY failed.");
537 }
538 case NC_MSG_NONE:
539 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100540 if (data != NULL) {
541 free(*data);
542 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200543 (*data) = NULL;
544 return NULL;
545 case NC_MSG_REPLY:
546 switch (replyt = nc_reply_get_type(reply)) {
547 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100548 if ((data != NULL) && (*data != NULL)) {
549 free(*data);
550 (*data) = NULL;
551 }
552 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200553 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100554 if (((*data) = nc_reply_get_data(reply)) == NULL) {
555 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200556 return create_error("Internal: No data from reply received.");
557 } else {
558 return NULL;
559 }
560 break;
561 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100562 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
563 if (data != NULL) {
564 free(*data);
565 (*data) = NULL;
566 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200567 return create_error(nc_reply_get_errormsg(reply));
568 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100569 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
570 if (data != NULL) {
571 free(*data);
572 (*data) = NULL;
573 }
Tomas Cejka60885252014-03-26 15:45:47 +0100574 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200575 }
576 break;
577 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100578 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
579 if (data != NULL) {
580 free(*data);
581 (*data) = NULL;
582 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200583 return create_error("Internal: Unexpected RPC-REPLY message type.");
584 }
585}
586
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100587json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200588{
589 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200590 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200591
592 /* check requests */
593 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100594 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200595 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200596 }
597
598 if (session != NULL) {
599 /* send the request and get the reply */
600 msgt = nc_session_send_recv(session, rpc, &reply);
601 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100602 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200603 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100604 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200605 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200606 }
607}
608
Tomas Cejkac7929632013-10-24 19:25:15 +0200609/**
610 * Perform RPC method that returns data.
611 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200612 * \param[in] session_key session identifier
613 * \param[in] rpc RPC message to perform
614 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
615 * \return NULL on success, json object with error otherwise
616 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100617static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200618{
619 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200620 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200621 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200622 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100623 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100624 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200625
Radek Krejci8e4632a2012-07-26 13:40:34 +0200626 /* check requests */
627 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100628 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200629 res = create_error("Internal: RPC could not be created.");
630 data = NULL;
631 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200632 }
633
David Kupka8e60a372012-09-04 09:15:20 +0200634 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100635 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200636 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100637 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200638 res = create_error("Internal: Lock failed.");
639 data = NULL;
640 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200641 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200642 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200643 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 +0200644 if (locked_session != NULL) {
645 session = locked_session->session;
646 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200647 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200648 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100649 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200650 if (pthread_mutex_lock(&locked_session->lock) != 0) {
651 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100652 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200653 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200654
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100655 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200656 res = create_error("Internal: Could not unlock.");
657 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200658 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200659 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200660 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100661 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200662 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200663
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100664 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200665 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200666 }
667
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200668 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200669
Radek Krejci035bf4e2012-07-25 10:59:09 +0200670 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100671 msgt = nc_session_send_recv(session, rpc, &reply);
672
David Kupka8e60a372012-09-04 09:15:20 +0200673 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100674 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200675 pthread_mutex_unlock(&locked_session->lock);
676 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200677
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100678 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200679 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100680 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100681 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100682 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100683 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100684 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100685 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200686 res = create_error("Unknown session to process.");
687 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200688 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200689finished:
690 nc_reply_free(reply);
691 if (received_data != NULL) {
692 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100693 } else {
694 if (data != NULL) {
695 free(data);
696 data = NULL;
697 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200698 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200699 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200700}
701
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100702static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200703{
704 nc_rpc* rpc;
705 struct nc_filter *f = NULL;
706 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200707 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200708
709 /* create filter if set */
710 if (filter != NULL) {
711 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
712 }
713
714 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100715 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200716 nc_filter_free(f);
717 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100718 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200719 return (NULL);
720 }
721
Tomas Cejka94674662013-09-13 15:55:24 +0200722 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100723#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100724 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100725#else
726 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
727#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 Cejka1c3840d2014-01-29 21:08:23 +0100787 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100788 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200789 }
790
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100791 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200792 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200793 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100794 (*err) = res;
795 } else {
796 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200797 }
798
Radek Krejci8e4632a2012-07-26 13:40:34 +0200799 return (data);
800}
801
Tomas Cejkab4d05872014-02-14 22:44:38 +0100802static 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 +0200803{
804 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200805 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200806
807 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100808 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100809 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100810 /* config, url */
811 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100812 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100813 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100814 rpc = nc_rpc_copyconfig(source, target, config);
815 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100816 } else if (source == NC_DATASTORE_URL) {
817 if (target == NC_DATASTORE_URL) {
818 /* url, url */
819 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
820 } else {
821 /* url, datastore */
822 rpc = nc_rpc_copyconfig(source, target, uri_src);
823 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100824 } else {
825 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100826 /* datastore, url */
827 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100828 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100829 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100830 rpc = nc_rpc_copyconfig(source, target);
831 }
832 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200833 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100834 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200835 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200836 }
837
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100838 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200839 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200840
841 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200842}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200843
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100844static 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 +0200845{
846 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200847 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200848
849 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100850 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200851 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100852 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200853 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200854 }
855
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100856 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200857 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200858
859 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200860}
861
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100862static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200863{
864 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200865 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200866
867 /* create requests */
868 rpc = nc_rpc_killsession(sid);
869 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100870 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200871 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200872 }
873
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100874 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200875 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200876 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200877}
878
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100879static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200880{
881 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200882 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200883
884 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200885 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200886 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100887 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200888 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200889 }
890
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100891 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200892 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200893 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200894}
895
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100896static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200897{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200898 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200899 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200900 if (target != NC_DATASTORE_URL) {
901 rpc = nc_rpc_deleteconfig(target);
902 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200903 rpc = nc_rpc_deleteconfig(target, url);
904 }
905 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100906 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200907 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200908 }
909
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100910 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200911 nc_rpc_free (rpc);
912 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200913}
914
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100915static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200916{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100917 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200918}
919
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100920static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200921{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100922 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200923}
924
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100925static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200926{
Tomas Cejka00635972013-06-03 15:10:52 +0200927 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200928 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200929
930 /* create requests */
931 rpc = nc_rpc_generic(content);
932 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100933 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200934 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200935 }
936
Radek Krejcia332b692012-11-12 16:15:54 +0100937 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200938 // TODO ?free(*data);
939 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100940 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200941
942 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100943 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200944 nc_rpc_free (rpc);
945 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200946}
947
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200948/**
949 * @}
950 *//* netconf_operations */
951
Radek Krejci7338bde2012-08-10 12:57:30 +0200952void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200953{
Radek Krejci7338bde2012-08-10 12:57:30 +0200954 switch (level) {
955 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100956 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200957 break;
958 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100959 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200960 break;
961 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100962 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200963 break;
964 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100965 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200966 break;
967 }
Radek Krejci469aab82012-07-22 18:42:20 +0200968}
969
Tomas Cejka64b87482013-06-03 16:30:53 +0200970/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200971 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200972 * Caller should free message memory.
973 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200974 * \return pointer to message
975 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100976char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200977{
978 /* read json in chunked framing */
979 unsigned int buffer_size = 0;
980 ssize_t buffer_len = 0;
981 char *buffer = NULL;
982 char c;
983 ssize_t ret;
984 int i, chunk_len;
985 char chunk_len_str[12];
986
987 while (1) {
988 /* read chunk length */
989 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
990 if (buffer != NULL) {
991 free (buffer);
992 buffer = NULL;
993 }
994 break;
995 }
996 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
997 if (buffer != NULL) {
998 free (buffer);
999 buffer = NULL;
1000 }
1001 break;
1002 }
1003 i=0;
1004 memset (chunk_len_str, 0, 12);
1005 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1006 if (i==0 && c == '#') {
1007 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1008 /* end but invalid */
1009 if (buffer != NULL) {
1010 free (buffer);
1011 buffer = NULL;
1012 }
1013 }
1014 /* end of message, double-loop break */
1015 goto msg_complete;
1016 }
1017 chunk_len_str[i++] = c;
1018 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001019 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001020 break;
1021 }
1022 }
1023 if (c != '\n') {
1024 if (buffer != NULL) {
1025 free (buffer);
1026 buffer = NULL;
1027 }
1028 break;
1029 }
1030 chunk_len_str[i] = 0;
1031 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1032 if (buffer != NULL) {
1033 free (buffer);
1034 buffer = NULL;
1035 }
1036 break;
1037 }
1038 buffer_size += chunk_len+1;
1039 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001040 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001041 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1042 if (buffer != NULL) {
1043 free (buffer);
1044 buffer = NULL;
1045 }
1046 break;
1047 }
1048 buffer_len += ret;
1049 }
1050msg_complete:
1051 return buffer;
1052}
1053
Tomas Cejkad5b53772013-06-08 23:01:07 +02001054NC_DATASTORE parse_datastore(const char *ds)
1055{
1056 if (strcmp(ds, "running") == 0) {
1057 return NC_DATASTORE_RUNNING;
1058 } else if (strcmp(ds, "startup") == 0) {
1059 return NC_DATASTORE_STARTUP;
1060 } else if (strcmp(ds, "candidate") == 0) {
1061 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001062 } else if (strcmp(ds, "url") == 0) {
1063 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001064 } else if (strcmp(ds, "config") == 0) {
1065 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001066 }
1067 return -1;
1068}
1069
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001070NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1071{
1072 if (strcmp(t, "notset") == 0) {
1073 return NC_EDIT_TESTOPT_NOTSET;
1074 } else if (strcmp(t, "testset") == 0) {
1075 return NC_EDIT_TESTOPT_TESTSET;
1076 } else if (strcmp(t, "set") == 0) {
1077 return NC_EDIT_TESTOPT_SET;
1078 } else if (strcmp(t, "test") == 0) {
1079 return NC_EDIT_TESTOPT_TEST;
1080 }
1081 return NC_EDIT_TESTOPT_ERROR;
1082}
1083
Tomas Cejkad5b53772013-06-08 23:01:07 +02001084json_object *create_error(const char *errmess)
1085{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001086 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001087 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001088 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001089 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001090 json_object_array_add(array, json_object_new_string(errmess));
1091 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001092 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001093 return reply;
1094
1095}
1096
1097json_object *create_data(const char *data)
1098{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001099 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001100 json_object *reply = json_object_new_object();
1101 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1102 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001103 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001104 return reply;
1105}
1106
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001107json_object *create_ok()
1108{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001109 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001110 json_object *reply = json_object_new_object();
1111 reply = json_object_new_object();
1112 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001113 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001114 return reply;
1115}
1116
1117json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001118{
1119 const char *host = NULL;
1120 const char *port = NULL;
1121 const char *user = NULL;
1122 const char *pass = NULL;
1123 json_object *capabilities = NULL;
1124 json_object *reply = NULL;
1125 char *session_key_hash = NULL;
1126 struct nc_cpblts* cpblts = NULL;
1127 unsigned int len, i;
1128
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001129 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001130 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001131 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1132 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1133 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1134 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1135
1136 capabilities = json_object_object_get((json_object *) request, "capabilities");
1137 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1138 cpblts = nc_cpblts_new(NULL);
1139 for (i=0; i<len; i++) {
1140 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1141 }
1142 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001143 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001144 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001145 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001146
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001147 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001148 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001149 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001150 session_key_hash = NULL;
1151 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001152 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1153 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001154 }
1155 if (cpblts != NULL) {
1156 nc_cpblts_free(cpblts);
1157 }
1158
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001159 GETSPEC_ERR_REPLY
1160
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001161 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001162 if (session_key_hash == NULL) {
1163 /* negative reply */
1164 if (err_reply == NULL) {
1165 reply = json_object_new_object();
1166 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1167 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001168 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001169 } else {
1170 /* use filled err_reply from libnetconf's callback */
1171 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001172 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001173 }
1174 } else {
1175 /* positive reply */
1176 reply = json_object_new_object();
1177 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1178 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1179
1180 free(session_key_hash);
1181 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001182 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001183 return reply;
1184}
1185
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001186json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001187{
1188 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001189 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001190 json_object *reply = NULL;
1191
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001192 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001193
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001194 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001195 filter = json_object_get_string(json_object_object_get(request, "filter"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001196 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001197
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001198 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001199 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001200 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001201 reply = create_data(data);
1202 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001203 }
1204 return reply;
1205}
1206
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001207json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001208{
1209 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001210 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001211 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001212 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001213 json_object *reply = NULL;
1214
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001215 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001216
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001217 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001218 filter = json_object_get_string(json_object_object_get(request, "filter"));
1219
Tomas Cejkad5b53772013-06-08 23:01:07 +02001220 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1221 ds_type_s = parse_datastore(source);
1222 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001223 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001224 if (ds_type_s == -1) {
1225 return create_error("Invalid source repository type requested.");
1226 }
1227
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001228 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001229 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001230 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001231 reply = create_data(data);
1232 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001233 }
1234 return reply;
1235}
1236
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001237json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001238{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001239 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001240 const char *identifier = NULL;
1241 const char *version = NULL;
1242 const char *format = NULL;
1243 json_object *reply = NULL;
1244
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001245 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001246 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001247 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001248 version = json_object_get_string(json_object_object_get(request, "version"));
1249 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001250 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001251
Tomas Cejkad5b53772013-06-08 23:01:07 +02001252 if (identifier == NULL) {
1253 return create_error("No identifier for get-schema supplied.");
1254 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001255
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001256 DEBUG("get-schema(version: %s, format: %s)", version, format);
1257 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001258 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001259 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001260 reply = create_data(data);
1261 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001262 }
1263 return reply;
1264}
1265
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001266json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001267{
1268 NC_DATASTORE ds_type_s = -1;
1269 NC_DATASTORE ds_type_t = -1;
1270 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1271 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001272 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001273 const char *defop = NULL;
1274 const char *erropt = NULL;
1275 const char *config = NULL;
1276 const char *source = NULL;
1277 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001278 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001279 json_object *reply = NULL;
1280
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001281 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001282
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001283 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001284 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001285 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1286 /* get parameters */
1287 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1288 ds_type_t = parse_datastore(target);
1289 }
1290 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1291 ds_type_s = parse_datastore(source);
1292 } else {
1293 /* source is optional, default value is config */
1294 ds_type_s = NC_DATASTORE_CONFIG;
1295 }
1296 config = json_object_get_string(json_object_object_get(request, "config"));
1297 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1298 pthread_mutex_unlock(&json_lock);
1299
Tomas Cejkad5b53772013-06-08 23:01:07 +02001300 if (defop != NULL) {
1301 if (strcmp(defop, "merge") == 0) {
1302 defop_type = NC_EDIT_DEFOP_MERGE;
1303 } else if (strcmp(defop, "replace") == 0) {
1304 defop_type = NC_EDIT_DEFOP_REPLACE;
1305 } else if (strcmp(defop, "none") == 0) {
1306 defop_type = NC_EDIT_DEFOP_NONE;
1307 } else {
1308 return create_error("Invalid default-operation parameter.");
1309 }
1310 } else {
1311 defop_type = NC_EDIT_DEFOP_NOTSET;
1312 }
1313
Tomas Cejkad5b53772013-06-08 23:01:07 +02001314 if (erropt != NULL) {
1315 if (strcmp(erropt, "continue-on-error") == 0) {
1316 erropt_type = NC_EDIT_ERROPT_CONT;
1317 } else if (strcmp(erropt, "stop-on-error") == 0) {
1318 erropt_type = NC_EDIT_ERROPT_STOP;
1319 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1320 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1321 } else {
1322 return create_error("Invalid error-option parameter.");
1323 }
1324 } else {
1325 erropt_type = 0;
1326 }
1327
Tomas Cejkad5b53772013-06-08 23:01:07 +02001328 if (ds_type_t == -1) {
1329 return create_error("Invalid target repository type requested.");
1330 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001331 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001332 if (config == NULL) {
1333 return create_error("Invalid config data parameter.");
1334 }
1335 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001336 if (config == NULL) {
1337 config = "";
1338 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001339 }
1340
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001341 if (testopt != NULL) {
1342 testopt_type = parse_testopt(testopt);
1343 } else {
1344 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1345 }
1346
1347 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 +01001348
1349 CHECK_ERR_SET_REPLY
1350
Tomas Cejkad5b53772013-06-08 23:01:07 +02001351 return reply;
1352}
1353
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001354json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001355{
1356 NC_DATASTORE ds_type_s = -1;
1357 NC_DATASTORE ds_type_t = -1;
1358 const char *config = NULL;
1359 const char *target = NULL;
1360 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001361 const char *uri_src = NULL;
1362 const char *uri_trg = NULL;
1363
Tomas Cejkad5b53772013-06-08 23:01:07 +02001364 json_object *reply = NULL;
1365
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001366 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001367
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001368 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001369 /* get parameters */
1370 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1371 ds_type_t = parse_datastore(target);
1372 }
1373 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1374 ds_type_s = parse_datastore(source);
1375 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001376 config = json_object_get_string(json_object_object_get(request, "config"));
1377 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
1378 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
1379 pthread_mutex_unlock(&json_lock);
1380
Tomas Cejkad5b53772013-06-08 23:01:07 +02001381 if (source == NULL) {
1382 /* no explicit source specified -> use config data */
1383 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001384 } else if (ds_type_s == -1) {
1385 /* source datastore specified, but it is invalid */
1386 return create_error("Invalid source repository type requested.");
1387 }
1388
1389 if (ds_type_t == -1) {
1390 /* invalid target datastore specified */
1391 return create_error("Invalid target repository type requested.");
1392 }
1393
Tomas Cejka55c6df52014-02-20 12:59:33 +01001394 /* source can be missing when config is given */
1395 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001396 reply = create_error("invalid input parameters - source and config is required.");
1397 return reply;
1398 }
1399
1400 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001401 if (uri_src == NULL) {
1402 uri_src = "";
1403 }
1404 }
1405 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001406 if (uri_trg == NULL) {
1407 uri_trg = "";
1408 }
1409 }
1410 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001411
1412 CHECK_ERR_SET_REPLY
1413
Tomas Cejkad5b53772013-06-08 23:01:07 +02001414 return reply;
1415}
1416
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001417json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001418{
1419 json_object *reply = NULL;
1420 const char *config = NULL;
1421 char *data = NULL;
1422
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001423 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001424
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001425 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426 config = json_object_get_string(json_object_object_get(request, "content"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001427 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001428
1429 if (config == NULL) {
1430 return create_error("Missing content parameter.");
1431 }
1432
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001433 /* TODO */
1434 reply = netconf_generic(session_key, config, &data);
1435 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001436 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001437 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001438 /* use filled err_reply from libnetconf's callback */
1439 reply = err_reply;
1440 }
1441 } else {
1442 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001443 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001444 reply = json_object_new_object();
1445 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001446 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001447 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001448 reply = create_data(data);
1449 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001450 }
1451 }
1452 return reply;
1453}
1454
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001455json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001456{
1457 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001458 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001459
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001460 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001461 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001462 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001463 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001464 }
1465 return reply;
1466}
1467
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001468json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001469{
1470 json_object *reply = NULL;
1471 const char *sid = NULL;
1472
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001473 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001474
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001475 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001476 sid = json_object_get_string(json_object_object_get(request, "session-id"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001477 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478
1479 if (sid == NULL) {
1480 return create_error("Missing session-id parameter.");
1481 }
1482
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001483 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001484
1485 CHECK_ERR_SET_REPLY
1486
Tomas Cejkad5b53772013-06-08 23:01:07 +02001487 return reply;
1488}
1489
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001490json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001491{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001492 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001493 struct session_with_mutex * locked_session = NULL;
1494 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001495 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001496
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001497 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001498 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001499 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001500 return NULL;
1501 }
1502
Tomas Cejkad5b53772013-06-08 23:01:07 +02001503 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1504 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001505 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001506 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001507 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001508 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001509 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001510 prepare_status_message(locked_session, temp_session);
1511 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001512 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001513 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001514 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001516 reply = create_error("Reload was unsuccessful, connection failed.");
1517 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001518 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001519 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001520 DEBUG("UNLOCK wrlock %s", __func__);
1521 if (pthread_rwlock_unlock(&session_lock) != 0) {
1522 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1523 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001524 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001525 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001526 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001527 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001528 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001529 reply = create_error("Invalid session identifier.");
1530 }
1531
1532 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1533 reply = locked_session->hello_message;
1534 }
1535 return reply;
1536}
1537
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001538json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001539{
1540 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001541 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001542 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001543
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001544 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001545 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001546 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001547 }
1548
Tomas Cejkad5b53772013-06-08 23:01:07 +02001549 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1550 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001551 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001552 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001553 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001554 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001555 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001556 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001557 if (locked_session->hello_message != NULL) {
1558 reply = locked_session->hello_message;
1559 } else {
1560 reply = create_error("Invalid session identifier.");
1561 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001562 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001563 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001564 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001565 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001566 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001567 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001568 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001569 reply = create_error("Invalid session identifier.");
1570 }
1571
Tomas Cejka47387fd2013-06-10 20:37:46 +02001572
Tomas Cejkad5b53772013-06-08 23:01:07 +02001573 return reply;
1574}
1575
Tomas Cejka6b886e02013-07-05 09:53:17 +02001576void notification_history(time_t eventtime, const char *content)
1577{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001578 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1579 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001580 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001581 return;
1582 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001583 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001584 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001585 json_object *notif = json_object_new_object();
1586 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001587 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001588 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001589 }
1590 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1591 json_object_object_add(notif, "content", json_object_new_string(content));
1592 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001593failed:
1594 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001595}
1596
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001597json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001598{
1599 json_object *reply = NULL;
1600 const char *sid = NULL;
1601 struct session_with_mutex *locked_session = NULL;
1602 struct nc_session *temp_session = NULL;
1603 nc_rpc *rpc = NULL;
1604 time_t start = 0;
1605 time_t stop = 0;
1606 int64_t from, to;
1607
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001608 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001609
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001610 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001611 sid = json_object_get_string(json_object_object_get(request, "session"));
1612 from = json_object_get_int64(json_object_object_get(request, "from"));
1613 to = json_object_get_int64(json_object_object_get(request, "to"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001614 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001615
1616 start = time(NULL) + from;
1617 stop = time(NULL) + to;
1618
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001619 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001620
1621 if (sid == NULL) {
1622 return create_error("Missing session parameter.");
1623 }
1624
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001625 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001626 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001627 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001628 return NULL;
1629 }
1630
1631 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1632 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001633 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001634 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001635 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001636 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001637 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001638 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001639 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001640 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1641 if (temp_session != NULL) {
1642 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1643 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001644 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001645 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001646 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001647 return create_error("notifications: creating an rpc request failed.");
1648 }
1649
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001650 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001651 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001652 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001653 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001654 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001655 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001656 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001657 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001658 }
1659 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1660
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001661 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001662 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001663 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001664 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001665 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001666 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001667 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001668 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001669 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001670 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001671
1672 ncntf_dispatch_receive(temp_session, notification_history);
1673
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001674 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001675 reply = json_object_new_object();
1676 json_object_object_add(reply, "notifications", notif_history_array);
1677 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001678 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001679
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001680 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001681 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001682 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001683 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001684 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001685 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001686 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001687 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001688 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001689 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1690 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001691 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001692 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001693 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001694 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001695 }
1696 reply = create_error("Invalid session identifier.");
1697 }
1698
Tomas Cejka4003a702013-10-01 00:02:45 +02001699 return reply;
1700}
1701
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001702json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001703{
1704 json_object *reply = NULL;
1705 const char *sid = NULL;
1706 const char *target = NULL;
1707 const char *url = NULL;
1708 struct session_with_mutex *locked_session = NULL;
1709 nc_rpc *rpc = NULL;
1710 NC_DATASTORE target_ds;
1711
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001712 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001713
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001714 pthread_mutex_lock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001715 sid = json_object_get_string(json_object_object_get(request, "session"));
1716 target = json_object_get_string(json_object_object_get(request, "target"));
1717 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001718 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001719
1720
1721 if ((sid == NULL) || (target == NULL)) {
1722 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001723 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001724
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001725 /* validation */
1726 target_ds = parse_datastore(target);
1727 if (target_ds == NC_DATASTORE_URL) {
1728 if (url != NULL) {
1729 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001730 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001731 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001732 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001733 rpc = nc_rpc_validate(target_ds);
1734 }
1735 if (rpc == NULL) {
1736 DEBUG("mod_netconf: creating rpc request failed");
1737 reply = create_error("Creation of RPC request failed.");
1738 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001739 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001740 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001741 }
1742
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001743 DEBUG("Request: validate datastore");
1744 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001745
1746 CHECK_ERR_SET_REPLY
1747
1748 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001749 DEBUG("Request: validation ok.");
1750 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001751 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001752 }
1753 nc_rpc_free (rpc);
1754
Tomas Cejka6b886e02013-07-05 09:53:17 +02001755 return reply;
1756}
1757
David Kupka8e60a372012-09-04 09:15:20 +02001758void * thread_routine (void * arg)
1759{
1760 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001761 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001762 json_object *request = NULL;
1763 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001764 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001765 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001766 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001767 const char *session_key;
1768 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001769 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001770 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001771 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001772 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001773 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001774 int client = ((struct pass_to_thread*)arg)->client;
1775
Tomas Cejka00635972013-06-03 15:10:52 +02001776 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001777
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001778 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001779 create_err_reply_p();
1780
David Kupka8e60a372012-09-04 09:15:20 +02001781 while (!isterminated) {
1782 fds.fd = client;
1783 fds.events = POLLIN;
1784 fds.revents = 0;
1785
1786 status = poll(&fds, 1, 1000);
1787
1788 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1789 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001790 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001791 continue;
1792 } else if (status < 0) {
1793 /* 0: poll time outed
1794 * close socket and ignore this request from the client, it can try it again
1795 * -1: poll failed
1796 * something wrong happend, close this socket and wait for another request
1797 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001798 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001799 close(client);
1800 break;
1801 }
1802 /* status > 0 */
1803
1804 /* check the status of the socket */
1805
1806 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1807 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1808 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001809 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001810 close(client);
1811 break;
1812 }
1813
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001814 DEBUG("Get framed message...");
1815 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001816
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001817 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001818 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001819 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001820 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001821 request = json_tokener_parse_verbose(buffer, &jerr);
1822 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001823 DEBUG("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001824 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001825 continue;
1826 }
David Kupka8e60a372012-09-04 09:15:20 +02001827
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001828 operation = json_object_get_int(json_object_object_get(request, "type"));
Tomas Cejka64b87482013-06-03 16:30:53 +02001829 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001830 pthread_mutex_unlock(&json_lock);
1831
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001832 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001833 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1834 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001835 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001836 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001837 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001838 pthread_mutex_unlock(&json_lock);
1839
David Kupka8e60a372012-09-04 09:15:20 +02001840 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001841
1842 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001843 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001844 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001845 /* there is some stupid client, so close the connection to give a chance to some other client */
1846 close(client);
1847 break;
1848 }
1849
David Kupka8e60a372012-09-04 09:15:20 +02001850 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001851 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001852
1853 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001854 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001855 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001856 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001857 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001858 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001859 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001860
1861 /* process required operation */
1862 switch (operation) {
1863 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001864 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001865 break;
1866 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001867 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001868 break;
1869 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001870 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001871 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001872 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001873 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001874 break;
David Kupka8e60a372012-09-04 09:15:20 +02001875 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001876 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001877 break;
1878 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001879 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001880 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001881
David Kupka8e60a372012-09-04 09:15:20 +02001882 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001883 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001884 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001885 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001886 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001887 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1888 ds_type_t = parse_datastore(target);
1889 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001890 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001891
1892 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001893 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001894 break;
1895 }
David Kupka8e60a372012-09-04 09:15:20 +02001896 switch(operation) {
1897 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001898 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001899 pthread_mutex_lock(&json_lock);
Tomas Cejkac7929632013-10-24 19:25:15 +02001900 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001901 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001902 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001903 break;
1904 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001905 DEBUG("Request: lock (session %s)", session_key);
1906 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001907 break;
1908 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001909 DEBUG("Request: unlock (session %s)", session_key);
1910 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001911 break;
1912 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001913 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001914 break;
1915 }
1916
Tomas Cejka442258e2014-04-01 18:17:18 +02001917 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02001918 if (reply == NULL) {
Tomas Cejka442258e2014-04-01 18:17:18 +02001919 pthread_mutex_lock(&json_lock);
1920 reply = json_object_new_object();
1921 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1922 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001923 }
1924 break;
1925 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001926 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001927 break;
1928 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001929 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001930 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001931 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001932 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001933 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001934 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001935 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001936 break;
1937 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001938 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001939 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001940 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001941 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001942 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001943 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001944 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001945 break;
David Kupka8e60a372012-09-04 09:15:20 +02001946 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001947 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001948 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001949 break;
1950 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001951 DEBUG("Clean request json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001952 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001953 json_object_put(request);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001954 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001955 /* send reply to caller */
1956 if (reply != NULL) {
1957 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001958 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001959 if (buffer != NULL) {
1960 free(buffer);
1961 buffer = NULL;
1962 }
David Kupka8e60a372012-09-04 09:15:20 +02001963 break;
1964 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001965 pthread_mutex_unlock(&json_lock);
1966
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001967 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001968 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001969 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001970 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02001971 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001972 reply = NULL;
1973 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001974 free(chunked_out_msg);
1975 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001976 if (buffer != NULL) {
1977 free(buffer);
1978 buffer = NULL;
1979 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001980 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02001981 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02001982 } else {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001983 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001984 DEBUG("Reply is NULL, shouldn't be...");
1985 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001986 }
1987 }
1988 }
David Kupka8e60a372012-09-04 09:15:20 +02001989 free (arg);
1990
Tomas Cejka442258e2014-04-01 18:17:18 +02001991 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001992
David Kupka8e60a372012-09-04 09:15:20 +02001993 return retval;
1994}
1995
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001996/**
1997 * \brief Close all open NETCONF sessions.
1998 *
1999 * During termination of mod_netconf, it is useful to close all remaining
2000 * sessions. This function iterates over the list of sessions and close them
2001 * all.
2002 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002003 * \param[in] p apr pool needed for hash table iterating
2004 * \param[in] ht hash table of session_with_mutex structs
2005 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002006static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002007{
2008 apr_hash_index_t *hi;
2009 void *val = NULL;
2010 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002011 const char *hashed_key = NULL;
2012 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002013 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002014
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002015 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002016 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002017 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002018 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002019 return;
2020 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002021 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002022 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002023 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002024 swm = (struct session_with_mutex *) val;
2025 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002026 DEBUG("LOCK mutex %s", __func__);
2027 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002028 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002029 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002030 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002031
2032 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002033 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002034 }
2035 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002036 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002037 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002038 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002039 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002040 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002041}
2042
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002043static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002044{
2045 apr_hash_index_t *hi;
2046 void *val = NULL;
2047 struct nc_session *ns = NULL;
2048 struct session_with_mutex *swm = NULL;
2049 const char *hashed_key = NULL;
2050 apr_ssize_t hashed_key_length;
2051 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002052 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002053
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002054 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002055//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002056 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002057 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002058 return;
2059 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002060 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002061 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2062 swm = (struct session_with_mutex *) val;
2063 if (swm == NULL) {
2064 continue;
2065 }
2066 ns = swm->session;
2067 if (ns == NULL) {
2068 continue;
2069 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002070//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002071 pthread_mutex_lock(&swm->lock);
2072 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002073 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002074 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002075 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
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);
2078
2079 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002080 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002081 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002082//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002083 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002084 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002085 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002086 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002087//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002088 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002089 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002090 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002091}
2092
2093
2094/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002095 * This is actually implementation of NETCONF client
2096 * - requests are received from UNIX socket in the predefined format
2097 * - results are replied through the same way
2098 * - the daemon run as a separate process, but it is started and stopped
2099 * automatically by Apache.
2100 *
2101 */
Radek Krejci469aab82012-07-22 18:42:20 +02002102static void forked_proc(apr_pool_t * pool, server_rec * server)
2103{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002104 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002105 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002106 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002107 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002108 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002109 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002110 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002111 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002112 struct timespec maxtime;
2113 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002114 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002115 #ifdef WITH_NOTIFICATIONS
2116 char use_notifications = 0;
2117 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002118
Tomas Cejka6b886e02013-07-05 09:53:17 +02002119 http_server = server;
2120
Tomas Cejka404d37e2013-04-13 02:31:35 +02002121 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002122 maxtime.tv_sec = 5;
2123 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002124
Tomas Cejka04e08f42014-03-27 19:52:34 +01002125#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002126 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002127 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002128#else
2129# ifdef SU_GROUP
2130 if (strlen(SU_GROUP) > 0) {
2131 struct group *g = getgrnam(SU_GROUP);
2132 if (g == NULL) {
2133 DEBUG("GID (%s) was not found.", SU_GROUP);
2134 return;
2135 }
2136 if (setgid(g->gr_gid) != 0) {
2137
2138 DEBUG("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
2139 return;
2140 }
2141 }
2142# else
2143 DEBUG("no SU_GROUP");
2144# endif
2145# ifdef SU_USER
2146 if (strlen(SU_USER) > 0) {
2147 struct passwd *p = getpwnam(SU_USER);
2148 if (p == NULL) {
2149 DEBUG("UID (%s) was not found.", SU_USER);
2150 return;
2151 }
2152 if (setuid(p->pw_uid) != 0) {
2153 DEBUG("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
2154 return;
2155 }
2156 }
2157# else
2158 DEBUG("no SU_USER");
2159# endif
2160#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002161
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002162 if (server != NULL) {
2163 cfg = ap_get_module_config(server->module_config, &netconf_module);
2164 if (cfg == NULL) {
2165 DEBUG("Getting mod_netconf configuration failed");
2166 return;
2167 }
2168 sockname = cfg->sockname;
2169 } else {
2170 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002171 }
Radek Krejci469aab82012-07-22 18:42:20 +02002172
Tomas Cejka04e08f42014-03-27 19:52:34 +01002173 /* try to remove if exists */
2174 unlink(sockname);
2175
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002176 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002177 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002178 DEBUG("Creating socket failed (%s)", strerror(errno));
2179 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002180 }
2181
2182 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002183 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002184 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2185
2186 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2187 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002188 DEBUG("mod_netconf socket address already in use");
2189 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002190 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002191 DEBUG("Binding socket failed (%s)", strerror(errno));
2192 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002193 }
2194
2195 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002196 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2197 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002198 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002199 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002200
Tomas Cejka04e08f42014-03-27 19:52:34 +01002201 uid_t user = -1;
2202 if (strlen(CHOWN_USER) > 0) {
2203 struct passwd *p = getpwnam(CHOWN_USER);
2204 if (p != NULL) {
2205 user = p->pw_uid;
2206 }
2207 }
2208 gid_t group = -1;
2209 if (strlen(CHOWN_GROUP) > 0) {
2210 struct group *g = getgrnam(CHOWN_GROUP);
2211 if (g != NULL) {
2212 group = g->gr_gid;
2213 }
2214 }
2215 if (chown(sockname, user, group) == -1) {
2216 DEBUG("Chown on socket file failed (%s).", strerror(errno));
2217 }
2218
Tomas Cejkaba21b382013-04-13 02:37:32 +02002219 /* prepare internal lists */
2220 netconf_sessions_list = apr_hash_make(pool);
2221
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002222 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002223 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002224 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002225 use_notifications = 0;
2226 } else {
2227 use_notifications = 1;
2228 }
2229 #endif
2230
Radek Krejci469aab82012-07-22 18:42:20 +02002231 /* setup libnetconf's callbacks */
2232 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002233 nc_callback_print(clb_print);
2234 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2235 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2236 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002237 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002238
2239 /* disable publickey authentication */
2240 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2241
David Kupka8e60a372012-09-04 09:15:20 +02002242 /* create mutex protecting session list */
2243 pthread_rwlockattr_init(&lock_attrs);
2244 /* rwlock is shared only with threads in this process */
2245 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2246 /* create rw lock */
2247 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002248 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2249 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002250 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002251 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002252 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002253 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002254 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002255 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002256 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002257 DEBUG("init of err_reply_key.");
2258 if (pthread_key_create(&err_reply_key, NULL) != 0) {
2259 DEBUG("init of err_reply_key failed");
2260 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002261
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002262 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002263 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002264 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002265 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002266 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002267 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002268 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002269 }
2270 if (use_notifications == 1) {
2271 notification_handle();
2272 }
2273 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002274 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002275 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002276 }
Radek Krejci469aab82012-07-22 18:42:20 +02002277
2278 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002279 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002280 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002281 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002282 olds = tv.tv_sec;
2283 }
David Kupka8e60a372012-09-04 09:15:20 +02002284 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002285 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2286 apr_sleep(SLEEP_TIME);
2287 continue;
2288 } else if (client == -1 && (errno == EINTR)) {
2289 continue;
2290 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002291 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002292 continue;
2293 }
Radek Krejci469aab82012-07-22 18:42:20 +02002294
2295 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002296 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002297
David Kupka8e60a372012-09-04 09:15:20 +02002298 arg = malloc (sizeof(struct pass_to_thread));
2299 arg->client = client;
2300 arg->pool = pool;
2301 arg->server = server;
2302 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002303
David Kupka8e60a372012-09-04 09:15:20 +02002304 /* start new thread. It will serve this particular request and then terminate */
2305 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002306 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002307 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002308 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002309 pthread_count++;
2310 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2311 ptids[pthread_count] = 0;
2312 }
Radek Krejci469aab82012-07-22 18:42:20 +02002313
David Kupka8e60a372012-09-04 09:15:20 +02002314 /* check if some thread already terminated, free some resources by joining it */
2315 for (i=0; i<pthread_count; i++) {
2316 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002317 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002318 pthread_count--;
2319 if (pthread_count > 0) {
2320 /* place last Thread ID on the place of joined one */
2321 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002322 }
Radek Krejci469aab82012-07-22 18:42:20 +02002323 }
2324 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002325 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002326 }
2327
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002328 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002329 /* join all threads */
2330 for (i=0; i<pthread_count; i++) {
2331 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2332 }
Radek Krejci469aab82012-07-22 18:42:20 +02002333
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002334 #ifdef WITH_NOTIFICATIONS
2335 notification_close();
2336 #endif
2337
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002338 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002339 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002340
David Kupka8e60a372012-09-04 09:15:20 +02002341 /* destroy rwlock */
2342 pthread_rwlock_destroy(&session_lock);
2343 pthread_rwlockattr_destroy(&lock_attrs);
2344
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002345 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002346
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002347 free(ptids);
2348 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002349 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002350 return;
2351error_exit:
2352 close(lsock);
2353 free(ptids);
2354 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002355}
2356
Radek Krejcif23850c2012-07-23 16:14:17 +02002357static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002358{
Radek Krejcif23850c2012-07-23 16:14:17 +02002359 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2360 apr_pool_create(&config->pool, pool);
2361 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002362 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002363
2364 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002365}
2366
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002367#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002368static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2369 apr_pool_t * plog, server_rec * s)
2370{
Radek Krejcif23850c2012-07-23 16:14:17 +02002371 mod_netconf_cfg *config;
2372 apr_status_t res;
2373
Radek Krejci469aab82012-07-22 18:42:20 +02002374 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002375 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002376 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002377
2378 /*
2379 * The following checks if this routine has been called before.
2380 * This is necessary because the parent process gets initialized
2381 * a couple of times as the server starts up.
2382 */
2383 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2384 if (!data) {
2385 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2386 return (OK);
2387 }
2388
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002389 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002390 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002391
Radek Krejcif23850c2012-07-23 16:14:17 +02002392 if (config && config->forkproc == NULL) {
2393 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2394 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002395 switch (res) {
2396 case APR_INCHILD:
2397 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002398 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002399 apr_signal(SIGTERM, signal_handler);
2400
2401 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002402 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002403
2404 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002405 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002406
Radek Krejcif23850c2012-07-23 16:14:17 +02002407 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002408 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002409 exit(APR_EGENERAL);
2410 break;
2411 case APR_INPARENT:
2412 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002413 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002414 break;
2415 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002416 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002417 break;
2418 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002419 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002420 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002421 }
2422
2423 return OK;
2424}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002425#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002426
Radek Krejci469aab82012-07-22 18:42:20 +02002427/**
2428 * Register module hooks
2429 */
2430static void mod_netconf_register_hooks(apr_pool_t * p)
2431{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002432#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002433 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002434#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002435}
2436
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002437static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2438{
2439 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2440 return NULL;
2441}
2442
2443static const command_rec netconf_cmds[] = {
2444 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2445 {NULL}
2446};
2447
Radek Krejci469aab82012-07-22 18:42:20 +02002448/* Dispatch list for API hooks */
2449module AP_MODULE_DECLARE_DATA netconf_module = {
2450 STANDARD20_MODULE_STUFF,
2451 NULL, /* create per-dir config structures */
2452 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002453 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002454 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002455 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002456 mod_netconf_register_hooks /* register hooks */
2457};
Radek Krejcia332b692012-11-12 16:15:54 +01002458
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002459int main(int argc, char **argv)
2460{
2461 apr_pool_t *pool;
2462 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2463 apr_signal(SIGTERM, signal_handler);
2464 apr_signal(SIGINT, signal_handler);
2465 apr_pool_create(&pool, NULL);
2466 forked_proc(pool, NULL);
2467 apr_pool_destroy(pool);
2468 apr_terminate();
2469 DEBUG("Terminated");
2470 return 0;
2471}