blob: fa8fbe16e99557cf3e16e9a876510403e80a3809 [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
Tomas Cejka86f0fc12014-09-17 15:09:38 +020083#define SOCKET_FILENAME "/var/run/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020084#define MAX_SOCKET_CL 10
85#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020086#define NOTIFICATION_QUEUE_SIZE 10
87#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020088#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020089
90/* sleep in master process for non-blocking socket reading */
91#define SLEEP_TIME 200
92
93#ifndef offsetof
94#define offsetof(type, member) ((size_t) ((type *) 0)->member)
95#endif
96
Tomas Cejkaef531ee2013-11-12 16:07:00 +010097server_rec *http_server = NULL;
98
Tomas Cejka027f3bc2012-11-10 20:28:36 +010099/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +0200100struct timeval timeout = { 1, 0 };
101
Tomas Cejka5064c232013-01-17 09:30:58 +0100102#define NCWITHDEFAULTS NCWD_MODE_NOTSET
103
104
Radek Krejci469aab82012-07-22 18:42:20 +0200105#define MSG_OK 0
106#define MSG_OPEN 1
107#define MSG_DATA 2
108#define MSG_CLOSE 3
109#define MSG_ERROR 4
110#define MSG_UNKNOWN 5
111
Radek Krejci469aab82012-07-22 18:42:20 +0200112module AP_MODULE_DECLARE_DATA netconf_module;
113
Tomas Cejka47387fd2013-06-10 20:37:46 +0200114pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200115pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100116pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100117pthread_mutex_t json_lock; /**< mutex for protecting json-c calls */
118
Tomas Cejka47387fd2013-06-10 20:37:46 +0200119apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200120
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200121static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200122
Tomas Cejka442258e2014-04-01 18:17:18 +0200123pthread_key_t err_reply_key;
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100124
Radek Krejci469aab82012-07-22 18:42:20 +0200125volatile int isterminated = 0;
126
127static char* password;
128
Radek Krejci469aab82012-07-22 18:42:20 +0200129static void signal_handler(int sign)
130{
131 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100132 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200133 case SIGTERM:
134 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200135 break;
136 }
137}
138
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200139static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200140{
Radek Krejcif23850c2012-07-23 16:14:17 +0200141 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
142 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200144
Radek Krejci469aab82012-07-22 18:42:20 +0200145 apr_sha1_ctx_t sha1_ctx;
146 apr_sha1_init(&sha1_ctx);
147 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
148 apr_sha1_update(&sha1_ctx, port, strlen(port));
149 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200150 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200151
Radek Krejcif23850c2012-07-23 16:14:17 +0200152 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200153 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200154 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200155 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200156 }
157 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200158
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200159 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200160}
161
Tomas Cejka8254dba2014-06-22 17:58:59 +0200162int netconf_callback_ssh_hostkey_check (const char* hostname, LIBSSH2_SESSION *session)
Radek Krejci469aab82012-07-22 18:42:20 +0200163{
164 /* always approve */
165 return (EXIT_SUCCESS);
166}
167
168char* netconf_callback_sshauth_password (const char* username, const char* hostname)
169{
170 char* buf;
171
172 buf = malloc ((strlen(password) + 1) * sizeof(char));
173 apr_cpystrn(buf, password, strlen(password) + 1);
174
175 return (buf);
176}
177
178void netconf_callback_sshauth_interactive (const char* name,
179 int name_len,
180 const char* instruction,
181 int instruction_len,
182 int num_prompts,
183 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
184 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
185 void** abstract)
186{
187 int i;
188
189 for (i=0; i<num_prompts; i++) {
190 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
191 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
192 responses[i].length = strlen(responses[i].text) + 1;
193 }
194
195 return;
196}
197
Radek Krejcic11fd862012-07-26 12:41:21 +0200198void netconf_callback_error_process(const char* tag,
199 const char* type,
200 const char* severity,
201 const char* apptag,
202 const char* path,
203 const char* message,
204 const char* attribute,
205 const char* element,
206 const char* ns,
207 const char* sid)
208{
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100209 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
Tomas Cejka442258e2014-04-01 18:17:18 +0200210 if (err_reply_p == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200211 ERROR("Error message was not allocated. %s", __func__);
Tomas Cejka442258e2014-04-01 18:17:18 +0200212 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 Cejkacf44e522015-04-24 17:29:21 +0200218 ERROR("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 Cejkacf44e522015-04-24 17:29:21 +0200230 ERROR("error calback: nonempty error list");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100231 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +0200232 if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100233 if (message != NULL) {
234 json_object_array_add(array, json_object_new_string(message));
235 }
236 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100237 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100238 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +0100239 pthread_setspecific(err_reply_key, err_reply_p);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100240 return;
Radek Krejcic11fd862012-07-26 12:41:21 +0200241}
242
Tomas Cejka47387fd2013-06-10 20:37:46 +0200243/**
244 * should be used in locked area
245 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100246void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200247{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200248 json_object *json_obj = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +0200249 json_object *js_tmp = 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 Cejkacf44e522015-04-24 17:29:21 +0200256 ERROR("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 Cejkacf44e522015-04-24 17:29:21 +0200262 ERROR("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200263 //json_object_put(s->hello_message);
Tomas Cejka09629492014-07-10 15:58:06 +0200264 if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
265 j_old_sid = json_object_get_string(js_tmp);
266 if (j_old_sid != NULL) {
267 old_sid = strdup(j_old_sid);
268 }
269 json_object_put(s->hello_message);
270 json_object_put(js_tmp);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100271 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200272 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200273 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100274 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200275 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100276 if (old_sid != NULL) {
277 /* use previous sid */
278 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
279 free(old_sid);
Tomas Cejkaa3ffdba2014-03-27 15:12:21 +0100280 old_sid = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100281 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200282 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100283 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
284 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200285 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
286 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
287 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
288 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
289 cpblts = nc_session_get_cpblts (session);
290 if (cpblts != NULL) {
291 json_obj = json_object_new_array();
292 nc_cpblts_iter_start (cpblts);
293 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
294 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
295 }
296 json_object_object_add(s->hello_message, "capabilities", json_obj);
297 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100298 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200299 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200300 ERROR("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200301 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
302 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
303 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100304 DEBUG("Status info from hello message prepared");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +0100305 pthread_mutex_unlock(&json_lock);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200306
307}
308
Tomas Cejka442258e2014-04-01 18:17:18 +0200309void create_err_reply_p()
310{
311 json_object **err_reply = calloc(1, sizeof(json_object **));
312 if (err_reply == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200313 ERROR("Allocation of err_reply storage failed!");
Tomas Cejka442258e2014-04-01 18:17:18 +0200314 return;
315 }
316 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200317 ERROR("cannot set thread-specific value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200318 }
319}
320
321void clean_err_reply()
322{
323 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
324 if (err_reply != NULL) {
325 if (*err_reply != NULL) {
326 pthread_mutex_lock(&json_lock);
327 json_object_put(*err_reply);
328 pthread_mutex_unlock(&json_lock);
329 }
330 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200331 ERROR("Cannot set thread-specific hash value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200332 }
333 }
334}
335
336void free_err_reply()
337{
338 json_object **err_reply = (json_object **) pthread_getspecific(err_reply_key);
339 if (err_reply != NULL) {
340 if (*err_reply != NULL) {
341 pthread_mutex_lock(&json_lock);
342 json_object_put(*err_reply);
343 pthread_mutex_unlock(&json_lock);
344 }
345 free(err_reply);
346 err_reply = NULL;
347 if (pthread_setspecific(err_reply_key, err_reply) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200348 ERROR("Cannot set thread-specific hash value.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200349 }
350 }
351}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200352
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200353/**
354 * \defgroup netconf_operations NETCONF operations
355 * The list of NETCONF operations that mod_netconf supports.
356 * @{
357 */
358
359/**
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200360 * \brief Send RPC and wait for reply with timeout.
361 *
362 * \param[in] session libnetconf session
363 * \param[in] rpc prepared RPC message
364 * \param[in] timeout timeout in miliseconds, -1 for blocking, 0 for non-blocking
365 * \param[out] reply reply from the server
366 * \return Value from nc_session_recv_reply() or NC_MSG_UNKNOWN when send_rpc() fails.
367 * On success, it returns NC_MSG_REPLY.
368 */
369NC_MSG_TYPE netconf_send_recv_timed(struct nc_session *session, nc_rpc *rpc,
370 int timeout, nc_reply **reply)
371{
372 const nc_msgid msgid = NULL;
373 NC_MSG_TYPE ret = NC_MSG_UNKNOWN;
374 msgid = nc_session_send_rpc(session, rpc);
375 if (msgid == NULL) {
376 return ret;
377 }
Tomas Cejka756d1292015-05-06 09:31:46 +0200378 do {
379 ret = nc_session_recv_reply(session, timeout, reply);
380 if (ret == NC_MSG_HELLO) {
381 ERROR("<hello> received instead reply, it will be lost.");
382 nc_reply_free(*reply);
383 }
384 if (ret == NC_MSG_WOULDBLOCK) {
385 ERROR("Timeout for receiving RPC reply expired.");
386 break;
387 }
388 } while (ret == NC_MSG_HELLO || ret == NC_MSG_NOTIFICATION);
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200389 return ret;
390}
391
392/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200393 * \brief Connect to NETCONF server
394 *
395 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
396 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100397static 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 +0200398{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200399 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200400 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200401 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200402
403 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200404 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100405 DEBUG("prepare to connect %s@%s:%s", user, host, port);
David Kupka8e60a372012-09-04 09:15:20 +0200406 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100407 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200408
Radek Krejci469aab82012-07-22 18:42:20 +0200409 /* if connected successful, add session to the list */
410 if (session != NULL) {
411 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200412 session_key = gen_ncsession_hash(
413 (host==NULL) ? "localhost" : host,
414 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200415 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200416
Tomas Cejkaba21b382013-04-13 02:37:32 +0200417 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100418 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 +0200419 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100420 session = NULL;
421 free(locked_session);
422 locked_session = NULL;
Tomas Cejkacf44e522015-04-24 17:29:21 +0200423 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200424 return NULL;
425 }
426 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200427 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200428 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200429 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200430 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100431 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200432 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100433 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200434 if (pthread_rwlock_wrlock(&session_lock) != 0) {
David Kupka8e60a372012-09-04 09:15:20 +0200435 nc_session_free(session);
436 free (locked_session);
Tomas Cejkacf44e522015-04-24 17:29:21 +0200437 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200438 return NULL;
439 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200440 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200441 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100442 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200443 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 +0100444 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200445
Tomas Cejka47387fd2013-06-10 20:37:46 +0200446 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200448 pthread_mutex_lock(&locked_session->lock);
449
450 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100451 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200452 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200453 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200454 }
455
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200456 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100457 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200458
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100459 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200460 pthread_mutex_unlock(&locked_session->lock);
461
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100462 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200463 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200464 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200465 ERROR("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200466 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200467 }
468
Radek Krejci469aab82012-07-22 18:42:20 +0200469}
470
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100471static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200472{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100473 DEBUG("lock private lock.");
474 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200475 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200476 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200477 }
478 locked_session->ntfc_subscribed = 0;
479 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100480 if (locked_session->session != NULL) {
481 nc_session_free(locked_session->session);
482 locked_session->session = NULL;
483 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100484 DEBUG("session closed.");
485 DEBUG("unlock private lock.");
486 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200487 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200488 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200489 }
490
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100491 DEBUG("unlock session lock.");
492 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200493 usleep(500000); /* let notification thread stop */
494
495 /* session shouldn't be used by now */
496 /** \todo free all notifications from queue */
497 apr_array_clear(locked_session->notifications);
498 pthread_mutex_destroy(&locked_session->lock);
499 if (locked_session->hello_message != NULL) {
500 /** \todo free hello_message */
501 //json_object_put(locked_session->hello_message);
502 locked_session->hello_message = NULL;
503 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200504 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200505 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200506 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100507 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200508 return (EXIT_SUCCESS);
509}
510
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100511static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200512{
David Kupka8e60a372012-09-04 09:15:20 +0200513 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200514
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100515 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200516
David Kupka8e60a372012-09-04 09:15:20 +0200517 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100518 DEBUG("lock session lock.");
519 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200520 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200521 ERROR("Error while locking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100522 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200523 return EXIT_FAILURE;
524 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200525 /* get session to close */
526 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
527 /* remove session from the active sessions list -> nobody new can now work with session */
528 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200529
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100530 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200531 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200532 ERROR("Error while unlocking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100533 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200534 }
535
536 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100537 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200538 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200539 ERROR("Unknown session to close");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100540 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200541 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200542 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100543 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200544}
545
Tomas Cejkac7929632013-10-24 19:25:15 +0200546/**
547 * Test reply message type and return error message.
548 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200549 * \param[in] session nc_session internal struct
550 * \param[in] session_key session key, NULL to disable disconnect on error
551 * \param[in] msgt RPC-REPLY message type
552 * \param[out] data
553 * \return NULL on success
554 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100555json_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 +0200556{
557 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100558 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200559
560 /* process the result of the operation */
561 switch (msgt) {
562 case NC_MSG_UNKNOWN:
563 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200564 ERROR("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200565 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100566 netconf_close(session_key, &err);
567 }
568 if (err != NULL) {
569 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200570 }
571 return create_error("Internal: Receiving RPC-REPLY failed.");
572 }
573 case NC_MSG_NONE:
574 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100575 if (data != NULL) {
576 free(*data);
Tomas Cejka7b1e3bd2014-04-08 14:34:28 +0200577 (*data) = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100578 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200579 return NULL;
580 case NC_MSG_REPLY:
581 switch (replyt = nc_reply_get_type(reply)) {
582 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100583 if ((data != NULL) && (*data != NULL)) {
584 free(*data);
585 (*data) = NULL;
586 }
587 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200588 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100589 if (((*data) = nc_reply_get_data(reply)) == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200590 ERROR("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200591 return create_error("Internal: No data from reply received.");
592 } else {
593 return NULL;
594 }
595 break;
596 case NC_REPLY_ERROR:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200597 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100598 if (data != NULL) {
599 free(*data);
600 (*data) = NULL;
601 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200602 return create_error(nc_reply_get_errormsg(reply));
603 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200604 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100605 if (data != NULL) {
606 free(*data);
607 (*data) = NULL;
608 }
Tomas Cejka60885252014-03-26 15:45:47 +0100609 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200610 }
611 break;
612 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200613 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100614 if (data != NULL) {
615 free(*data);
616 (*data) = NULL;
617 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200618 return create_error("Internal: Unexpected RPC-REPLY message type.");
619 }
620}
621
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100622json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200623{
624 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200625 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200626
627 /* check requests */
628 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200629 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200630 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200631 }
632
633 if (session != NULL) {
634 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200635 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200636 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100637 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200638 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200639 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200640 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200641 }
642}
643
Tomas Cejkac7929632013-10-24 19:25:15 +0200644/**
645 * Perform RPC method that returns data.
646 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200647 * \param[in] session_key session identifier
648 * \param[in] rpc RPC message to perform
649 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
650 * \return NULL on success, json object with error otherwise
651 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100652static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200653{
654 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200655 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200656 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200657 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100658 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100659 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200660
Radek Krejci8e4632a2012-07-26 13:40:34 +0200661 /* check requests */
662 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200663 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200664 res = create_error("Internal: RPC could not be created.");
665 data = NULL;
666 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200667 }
668
David Kupka8e60a372012-09-04 09:15:20 +0200669 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100670 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200671 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200672 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200673 res = create_error("Internal: Lock failed.");
674 data = NULL;
675 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200676 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200677 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200678 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 +0200679 if (locked_session != NULL) {
680 session = locked_session->session;
681 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200682 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200683 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100684 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200685 if (pthread_mutex_lock(&locked_session->lock) != 0) {
686 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100687 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200688 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200689
Tomas Cejkacf44e522015-04-24 17:29:21 +0200690 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200691 res = create_error("Internal: Could not unlock.");
692 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200693 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200694 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200695 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100696 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200697 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200698
Tomas Cejkacf44e522015-04-24 17:29:21 +0200699 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200700 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200701 }
702
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200703 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200704
Radek Krejci035bf4e2012-07-25 10:59:09 +0200705 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200706 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Radek Krejcia332b692012-11-12 16:15:54 +0100707
David Kupka8e60a372012-09-04 09:15:20 +0200708 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100709 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200710 pthread_mutex_unlock(&locked_session->lock);
711 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200712
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100713 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200714 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100715 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100716 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200717 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200718 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100719 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200720 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200721 res = create_error("Unknown session to process.");
722 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200723 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200724finished:
725 nc_reply_free(reply);
726 if (received_data != NULL) {
727 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100728 } else {
729 if (data != NULL) {
730 free(data);
731 data = NULL;
732 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200733 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200734 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200735}
736
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100737static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200738{
739 nc_rpc* rpc;
740 struct nc_filter *f = NULL;
741 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200742 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200743
744 /* create filter if set */
745 if (filter != NULL) {
746 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
747 }
748
749 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100750 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200751 nc_filter_free(f);
752 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200753 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200754 return (NULL);
755 }
756
Tomas Cejka94674662013-09-13 15:55:24 +0200757 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100758#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100759 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100760#else
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200761 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
762 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100763#endif
Tomas Cejkacf44e522015-04-24 17:29:21 +0200764 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200765 }
766
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100767 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200768 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100769 if (res != NULL) {
770 (*err) = res;
771 } else {
772 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200773 }
774
Radek Krejci8e4632a2012-07-26 13:40:34 +0200775 return (data);
776}
777
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100778static 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 +0100779{
780 nc_rpc* rpc;
781 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200782 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100783
784 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100785 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100786 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200787 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100788 return (NULL);
789 }
790
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100791 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100792 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100793 if (res != NULL) {
794 (*err) = res;
795 } else {
796 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200797 }
798
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100799 return (data);
800}
801
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100802static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200803{
804 nc_rpc* rpc;
805 struct nc_filter *f = NULL;
806 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200807 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200808
809 /* create filter if set */
810 if (filter != NULL) {
811 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
812 }
813
814 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100815 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200816 nc_filter_free(f);
817 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200818 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200819 return (NULL);
820 }
821
Tomas Cejka94674662013-09-13 15:55:24 +0200822 /* tell server to show all elements even if they have default values */
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200823 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
824 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
825 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200826 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200827 }
828
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100829 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200830 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200831 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100832 (*err) = res;
833 } else {
834 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200835 }
836
Radek Krejci8e4632a2012-07-26 13:40:34 +0200837 return (data);
838}
839
Tomas Cejkab4d05872014-02-14 22:44:38 +0100840static 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 +0200841{
842 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200843 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200844
845 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100846 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100847 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100848 /* config, url */
849 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100850 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100851 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100852 rpc = nc_rpc_copyconfig(source, target, config);
853 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100854 } else if (source == NC_DATASTORE_URL) {
855 if (target == NC_DATASTORE_URL) {
856 /* url, url */
857 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
858 } else {
859 /* url, datastore */
860 rpc = nc_rpc_copyconfig(source, target, uri_src);
861 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100862 } else {
863 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100864 /* datastore, url */
865 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100866 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100867 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100868 rpc = nc_rpc_copyconfig(source, target);
869 }
870 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200871 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200872 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200873 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200874 }
875
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100876 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200877 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200878
879 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200880}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200881
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100882static 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 +0200883{
884 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200885 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200886
887 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100888 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200889 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200890 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200891 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200892 }
893
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100894 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200895 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200896
897 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200898}
899
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100900static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200901{
902 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200903 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200904
905 /* create requests */
906 rpc = nc_rpc_killsession(sid);
907 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200908 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200909 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200910 }
911
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100912 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200913 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200914 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200915}
916
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100917static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200918{
919 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200920 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200921
922 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200923 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200924 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200925 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200926 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200927 }
928
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100929 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200930 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200931 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200932}
933
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100934static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200935{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200936 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200937 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200938 if (target != NC_DATASTORE_URL) {
939 rpc = nc_rpc_deleteconfig(target);
940 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200941 rpc = nc_rpc_deleteconfig(target, url);
942 }
943 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200944 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200945 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200946 }
947
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100948 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200949 nc_rpc_free (rpc);
950 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200951}
952
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100953static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200954{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100955 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200956}
957
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100958static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200959{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100960 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200961}
962
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100963static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200964{
Tomas Cejka00635972013-06-03 15:10:52 +0200965 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200966 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200967
968 /* create requests */
969 rpc = nc_rpc_generic(content);
970 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200971 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200972 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200973 }
974
Radek Krejcia332b692012-11-12 16:15:54 +0100975 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200976 // TODO ?free(*data);
977 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100978 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200979
980 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100981 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200982 nc_rpc_free (rpc);
983 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200984}
985
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200986/**
987 * @}
988 *//* netconf_operations */
989
Radek Krejci7338bde2012-08-10 12:57:30 +0200990void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200991{
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200992#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +0200993 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
994
995#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200996 break;
997
Radek Krejci7338bde2012-08-10 12:57:30 +0200998 switch (level) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200999 FOREACH(CASE);
1000 case NC_VERB_VERBOSE:
1001 case NC_VERB_DEBUG:
1002 DEBUG("DEBUG: %s", msg);
1003 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +02001004 }
1005 if (level == NC_VERB_ERROR) {
1006 /* return global error */
1007 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
1008 NULL /* severity */, NULL /* apptag */,
1009 NULL /* path */, msg, NULL /* attribute */,
1010 NULL /* element */, NULL /* ns */, NULL /* sid */);
Radek Krejci7338bde2012-08-10 12:57:30 +02001011 }
Radek Krejci469aab82012-07-22 18:42:20 +02001012}
1013
Tomas Cejka64b87482013-06-03 16:30:53 +02001014/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02001015 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02001016 * Caller should free message memory.
1017 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02001018 * \return pointer to message
1019 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001020char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02001021{
1022 /* read json in chunked framing */
1023 unsigned int buffer_size = 0;
1024 ssize_t buffer_len = 0;
1025 char *buffer = NULL;
1026 char c;
1027 ssize_t ret;
1028 int i, chunk_len;
1029 char chunk_len_str[12];
1030
1031 while (1) {
1032 /* read chunk length */
1033 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
1034 if (buffer != NULL) {
1035 free (buffer);
1036 buffer = NULL;
1037 }
1038 break;
1039 }
1040 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
1041 if (buffer != NULL) {
1042 free (buffer);
1043 buffer = NULL;
1044 }
1045 break;
1046 }
1047 i=0;
1048 memset (chunk_len_str, 0, 12);
1049 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1050 if (i==0 && c == '#') {
1051 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1052 /* end but invalid */
1053 if (buffer != NULL) {
1054 free (buffer);
1055 buffer = NULL;
1056 }
1057 }
1058 /* end of message, double-loop break */
1059 goto msg_complete;
1060 }
1061 chunk_len_str[i++] = c;
1062 if (i==11) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001063 ERROR("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001064 break;
1065 }
1066 }
1067 if (c != '\n') {
1068 if (buffer != NULL) {
1069 free (buffer);
1070 buffer = NULL;
1071 }
1072 break;
1073 }
1074 chunk_len_str[i] = 0;
1075 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1076 if (buffer != NULL) {
1077 free (buffer);
1078 buffer = NULL;
1079 }
1080 break;
1081 }
1082 buffer_size += chunk_len+1;
1083 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001084 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001085 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1086 if (buffer != NULL) {
1087 free (buffer);
1088 buffer = NULL;
1089 }
1090 break;
1091 }
1092 buffer_len += ret;
1093 }
1094msg_complete:
1095 return buffer;
1096}
1097
Tomas Cejkad5b53772013-06-08 23:01:07 +02001098NC_DATASTORE parse_datastore(const char *ds)
1099{
1100 if (strcmp(ds, "running") == 0) {
1101 return NC_DATASTORE_RUNNING;
1102 } else if (strcmp(ds, "startup") == 0) {
1103 return NC_DATASTORE_STARTUP;
1104 } else if (strcmp(ds, "candidate") == 0) {
1105 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001106 } else if (strcmp(ds, "url") == 0) {
1107 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001108 } else if (strcmp(ds, "config") == 0) {
1109 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001110 }
1111 return -1;
1112}
1113
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001114NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1115{
1116 if (strcmp(t, "notset") == 0) {
1117 return NC_EDIT_TESTOPT_NOTSET;
1118 } else if (strcmp(t, "testset") == 0) {
1119 return NC_EDIT_TESTOPT_TESTSET;
1120 } else if (strcmp(t, "set") == 0) {
1121 return NC_EDIT_TESTOPT_SET;
1122 } else if (strcmp(t, "test") == 0) {
1123 return NC_EDIT_TESTOPT_TEST;
1124 }
1125 return NC_EDIT_TESTOPT_ERROR;
1126}
1127
Tomas Cejkad5b53772013-06-08 23:01:07 +02001128json_object *create_error(const char *errmess)
1129{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001130 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001131 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001132 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001133 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001134 json_object_array_add(array, json_object_new_string(errmess));
1135 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001136 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001137 return reply;
1138
1139}
1140
1141json_object *create_data(const char *data)
1142{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001143 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001144 json_object *reply = json_object_new_object();
1145 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1146 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001147 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001148 return reply;
1149}
1150
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001151json_object *create_ok()
1152{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001153 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001154 json_object *reply = json_object_new_object();
1155 reply = json_object_new_object();
1156 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001157 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001158 return reply;
1159}
1160
Tomas Cejka09629492014-07-10 15:58:06 +02001161char *get_param_string(json_object *data, const char *name)
1162{
1163 json_object *js_tmp = NULL;
1164 char *res = NULL;
1165 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
1166 res = strdup(json_object_get_string(js_tmp));
1167 json_object_put(js_tmp);
1168 }
1169 return res;
1170}
1171
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001172json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001173{
Tomas Cejka09629492014-07-10 15:58:06 +02001174 char *host = NULL;
1175 char *port = NULL;
1176 char *user = NULL;
1177 char *pass = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001178 json_object *capabilities = NULL;
1179 json_object *reply = NULL;
1180 char *session_key_hash = NULL;
1181 struct nc_cpblts* cpblts = NULL;
1182 unsigned int len, i;
1183
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001184 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001185 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001186
Tomas Cejka09629492014-07-10 15:58:06 +02001187 host = get_param_string(request, "host");
1188 port = get_param_string(request, "port");
1189 user = get_param_string(request, "user");
1190 pass = get_param_string(request, "pass");
1191
1192 if (json_object_object_get_ex(request, "capabilities", &capabilities) == TRUE) {
1193 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1194 cpblts = nc_cpblts_new(NULL);
1195 for (i=0; i<len; i++) {
1196 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1197 }
1198 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001199 ERROR("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001200 }
Tomas Cejka09629492014-07-10 15:58:06 +02001201 json_object_put(capabilities);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001202 }
Tomas Cejka09629492014-07-10 15:58:06 +02001203
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001204 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001205
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001206 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001207 if ((host == NULL) || (user == NULL)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001208 ERROR("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001209 session_key_hash = NULL;
1210 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001211 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1212 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001213 }
1214 if (cpblts != NULL) {
1215 nc_cpblts_free(cpblts);
1216 }
1217
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001218 GETSPEC_ERR_REPLY
1219
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001220 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001221 if (session_key_hash == NULL) {
1222 /* negative reply */
1223 if (err_reply == NULL) {
1224 reply = json_object_new_object();
1225 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1226 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkacf44e522015-04-24 17:29:21 +02001227 ERROR("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001228 } else {
1229 /* use filled err_reply from libnetconf's callback */
1230 reply = err_reply;
Tomas Cejkacf44e522015-04-24 17:29:21 +02001231 ERROR("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001232 }
1233 } else {
1234 /* positive reply */
1235 reply = json_object_new_object();
1236 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1237 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1238
1239 free(session_key_hash);
1240 }
Tomas Cejka09629492014-07-10 15:58:06 +02001241 memset(pass, 0, strlen(pass));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001242 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001243 CHECK_AND_FREE(host);
1244 CHECK_AND_FREE(user);
1245 CHECK_AND_FREE(port);
1246 CHECK_AND_FREE(pass);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001247 return reply;
1248}
1249
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001250json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001251{
Tomas Cejka09629492014-07-10 15:58:06 +02001252 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001253 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254 json_object *reply = NULL;
1255
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001256 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001258 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001259 filter = get_param_string(request, "filter");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001260 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001261
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001262 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001263 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001264 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001265 reply = create_data(data);
1266 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001267 }
1268 return reply;
1269}
1270
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001271json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001272{
1273 NC_DATASTORE ds_type_s = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001274 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001275 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001276 char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001277 json_object *reply = NULL;
1278
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001279 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001280
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001281 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001282 filter = get_param_string(request, "filter");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001283
Tomas Cejka09629492014-07-10 15:58:06 +02001284 source = get_param_string(request, "source");
1285 if (source != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001286 ds_type_s = parse_datastore(source);
1287 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001288 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001289
Tomas Cejkad5b53772013-06-08 23:01:07 +02001290 if (ds_type_s == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001291 reply = create_error("Invalid source repository type requested.");
1292 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001293 }
1294
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001295 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001296 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001297 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001298 reply = create_data(data);
1299 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001300 }
Tomas Cejka09629492014-07-10 15:58:06 +02001301finalize:
1302 CHECK_AND_FREE(filter);
1303 CHECK_AND_FREE(source);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001304 return reply;
1305}
1306
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001307json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001308{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001309 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001310 char *identifier = NULL;
1311 char *version = NULL;
1312 char *format = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001313 json_object *reply = NULL;
1314
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001315 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001316 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001317 identifier = get_param_string(request, "identifier");
1318 version = get_param_string(request, "version");
1319 format = get_param_string(request, "format");
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001320 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001321
Tomas Cejkad5b53772013-06-08 23:01:07 +02001322 if (identifier == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001323 reply = create_error("No identifier for get-schema supplied.");
1324 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001325 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001326
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001327 DEBUG("get-schema(version: %s, format: %s)", version, format);
1328 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001329 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001330 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001331 reply = create_data(data);
1332 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001333 }
Tomas Cejka09629492014-07-10 15:58:06 +02001334finalize:
1335 CHECK_AND_FREE(identifier);
1336 CHECK_AND_FREE(version);
1337 CHECK_AND_FREE(format);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001338 return reply;
1339}
1340
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001341json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001342{
1343 NC_DATASTORE ds_type_s = -1;
1344 NC_DATASTORE ds_type_t = -1;
1345 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1346 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001347 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejka09629492014-07-10 15:58:06 +02001348 char *defop = NULL;
1349 char *erropt = NULL;
1350 char *config = NULL;
1351 char *source = NULL;
1352 char *target = NULL;
1353 char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001354 json_object *reply = NULL;
1355
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001356 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001357
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001358 pthread_mutex_lock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001359 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001360 defop = get_param_string(request, "default-operation");
1361 erropt = get_param_string(request, "error-option");
1362 target = get_param_string(request, "target");
1363 source = get_param_string(request, "source");
1364 config = get_param_string(request, "config");
1365 testopt = get_param_string(request, "test-option");
1366 pthread_mutex_unlock(&json_lock);
1367
1368 if (target != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001369 ds_type_t = parse_datastore(target);
1370 }
Tomas Cejka09629492014-07-10 15:58:06 +02001371 if (source != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001372 ds_type_s = parse_datastore(source);
1373 } else {
1374 /* source is optional, default value is config */
1375 ds_type_s = NC_DATASTORE_CONFIG;
1376 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001377
Tomas Cejkad5b53772013-06-08 23:01:07 +02001378 if (defop != NULL) {
1379 if (strcmp(defop, "merge") == 0) {
1380 defop_type = NC_EDIT_DEFOP_MERGE;
1381 } else if (strcmp(defop, "replace") == 0) {
1382 defop_type = NC_EDIT_DEFOP_REPLACE;
1383 } else if (strcmp(defop, "none") == 0) {
1384 defop_type = NC_EDIT_DEFOP_NONE;
1385 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001386 reply = create_error("Invalid default-operation parameter.");
1387 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001388 }
1389 } else {
1390 defop_type = NC_EDIT_DEFOP_NOTSET;
1391 }
1392
Tomas Cejkad5b53772013-06-08 23:01:07 +02001393 if (erropt != NULL) {
1394 if (strcmp(erropt, "continue-on-error") == 0) {
1395 erropt_type = NC_EDIT_ERROPT_CONT;
1396 } else if (strcmp(erropt, "stop-on-error") == 0) {
1397 erropt_type = NC_EDIT_ERROPT_STOP;
1398 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1399 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1400 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001401 reply = create_error("Invalid error-option parameter.");
1402 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001403 }
1404 } else {
1405 erropt_type = 0;
1406 }
1407
Tomas Cejkad5b53772013-06-08 23:01:07 +02001408 if (ds_type_t == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001409 reply = create_error("Invalid target repository type requested.");
1410 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001411 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001412 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001413 if (config == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001414 reply = create_error("Invalid config data parameter.");
1415 goto finalize;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001416 }
1417 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001418 if (config == NULL) {
1419 config = "";
1420 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001421 }
1422
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001423 if (testopt != NULL) {
1424 testopt_type = parse_testopt(testopt);
1425 } else {
1426 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1427 }
1428
1429 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 +01001430
1431 CHECK_ERR_SET_REPLY
Tomas Cejka09629492014-07-10 15:58:06 +02001432finalize:
1433 CHECK_AND_FREE(defop);
1434 CHECK_AND_FREE(erropt);
1435 CHECK_AND_FREE(config);
1436 CHECK_AND_FREE(source);
1437 CHECK_AND_FREE(target);
1438 CHECK_AND_FREE(testopt);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001439 return reply;
1440}
1441
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001442json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001443{
1444 NC_DATASTORE ds_type_s = -1;
1445 NC_DATASTORE ds_type_t = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001446 char *config = NULL;
1447 char *target = NULL;
1448 char *source = NULL;
1449 char *uri_src = NULL;
1450 char *uri_trg = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001451
Tomas Cejkad5b53772013-06-08 23:01:07 +02001452 json_object *reply = NULL;
1453
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001454 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001455
1456 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001457 pthread_mutex_lock(&json_lock);
1458 target = get_param_string(request, "target");
1459 source = get_param_string(request, "source");
1460 config = get_param_string(request, "config");
1461 uri_src = get_param_string(request, "uri-source");
1462 uri_trg = get_param_string(request, "uri-target");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001463 pthread_mutex_unlock(&json_lock);
1464
Tomas Cejka09629492014-07-10 15:58:06 +02001465 if (target != NULL) {
1466 ds_type_t = parse_datastore(target);
1467 }
1468 if (source != NULL) {
1469 ds_type_s = parse_datastore(source);
1470 } else {
1471 /* source == NULL *//* no explicit source specified -> use config data */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejka09629492014-07-10 15:58:06 +02001473 }
1474 if (ds_type_s == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001475 /* source datastore specified, but it is invalid */
Tomas Cejka09629492014-07-10 15:58:06 +02001476 reply = create_error("Invalid source repository type requested.");
1477 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478 }
1479
1480 if (ds_type_t == -1) {
1481 /* invalid target datastore specified */
Tomas Cejka09629492014-07-10 15:58:06 +02001482 reply = create_error("Invalid target repository type requested.");
1483 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001484 }
1485
Tomas Cejka55c6df52014-02-20 12:59:33 +01001486 /* source can be missing when config is given */
1487 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001488 reply = create_error("invalid input parameters - source and config is required.");
Tomas Cejka09629492014-07-10 15:58:06 +02001489 goto finalize;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001490 }
1491
1492 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001493 if (uri_src == NULL) {
1494 uri_src = "";
1495 }
1496 }
1497 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001498 if (uri_trg == NULL) {
1499 uri_trg = "";
1500 }
1501 }
1502 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001503
1504 CHECK_ERR_SET_REPLY
1505
Tomas Cejka09629492014-07-10 15:58:06 +02001506finalize:
1507 CHECK_AND_FREE(config);
1508 CHECK_AND_FREE(target);
1509 CHECK_AND_FREE(source);
1510 CHECK_AND_FREE(uri_src);
1511 CHECK_AND_FREE(uri_trg);
1512
Tomas Cejkad5b53772013-06-08 23:01:07 +02001513 return reply;
1514}
1515
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001516json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001517{
1518 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001519 char *config = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001520 char *data = NULL;
1521
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001522 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001523
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001524 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001525 config = get_param_string(request, "content");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001526 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001527
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001528 reply = netconf_generic(session_key, config, &data);
1529 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001530 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001531 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001532 /* use filled err_reply from libnetconf's callback */
1533 reply = err_reply;
1534 }
1535 } else {
1536 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001537 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001538 reply = json_object_new_object();
1539 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001540 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001541 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001542 reply = create_data(data);
1543 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001544 }
1545 }
Tomas Cejka09629492014-07-10 15:58:06 +02001546 CHECK_AND_FREE(config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001547 return reply;
1548}
1549
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001550json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001551{
1552 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001553 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001554
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001555 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001556 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001557 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001558 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001559 }
1560 return reply;
1561}
1562
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001563json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001564{
1565 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001566 char *sid = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001567
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001568 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001569
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001570 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001571 sid = get_param_string(request, "session-id");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001572 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001573
1574 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001575 reply = create_error("Missing session-id parameter.");
1576 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001577 }
1578
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001579 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001580
1581 CHECK_ERR_SET_REPLY
1582
Tomas Cejka09629492014-07-10 15:58:06 +02001583finalize:
1584 CHECK_AND_FREE(sid);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001585 return reply;
1586}
1587
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001588json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001589{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001590 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001591 struct session_with_mutex * locked_session = NULL;
1592 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001593 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001594
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001595 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001596 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001597 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001598 return NULL;
1599 }
1600
Tomas Cejka09629492014-07-10 15:58:06 +02001601 locked_session = (struct session_with_mutex *) apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001602 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001603 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001604 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001605 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001606 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001607 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001608 prepare_status_message(locked_session, temp_session);
1609 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001610 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001611 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001612 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001613 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001614 reply = create_error("Reload was unsuccessful, connection failed.");
1615 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001616 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001617 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001618 DEBUG("UNLOCK wrlock %s", __func__);
1619 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001620 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka93b0e492014-03-26 15:47:45 +01001621 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001622 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001623 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001624 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001625 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001626 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001627 reply = create_error("Invalid session identifier.");
1628 }
1629
1630 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1631 reply = locked_session->hello_message;
1632 }
1633 return reply;
1634}
1635
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001636json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001637{
1638 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001639 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001640 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001641
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001642 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001643 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001644 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001645 }
1646
Tomas Cejka09629492014-07-10 15:58:06 +02001647 locked_session = (struct session_with_mutex *) apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001648 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001649 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001650 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001651 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001652 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001653 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001654 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001655 if (locked_session->hello_message != NULL) {
1656 reply = locked_session->hello_message;
1657 } else {
1658 reply = create_error("Invalid session identifier.");
1659 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001660 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001661 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001662 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001663 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001664 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001665 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001666 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001667 reply = create_error("Invalid session identifier.");
1668 }
1669
Tomas Cejka47387fd2013-06-10 20:37:46 +02001670
Tomas Cejkad5b53772013-06-08 23:01:07 +02001671 return reply;
1672}
1673
Tomas Cejka6b886e02013-07-05 09:53:17 +02001674void notification_history(time_t eventtime, const char *content)
1675{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001676 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1677 if (notif_history_array == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001678 ERROR("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001679 return;
1680 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001681 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001682 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001683 json_object *notif = json_object_new_object();
1684 if (notif == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001685 ERROR("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001686 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001687 }
1688 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1689 json_object_object_add(notif, "content", json_object_new_string(content));
1690 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001691failed:
1692 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001693}
1694
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001695json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001696{
1697 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001698 json_object *js_tmp = NULL;
1699 char *sid = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001700 struct session_with_mutex *locked_session = NULL;
1701 struct nc_session *temp_session = NULL;
1702 nc_rpc *rpc = NULL;
1703 time_t start = 0;
1704 time_t stop = 0;
Tomas Cejka09629492014-07-10 15:58:06 +02001705 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001706
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001707 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001708
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001709 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001710 sid = get_param_string(request, "session");
1711
1712 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
1713 from = json_object_get_int64(js_tmp);
1714 json_object_put(js_tmp);
1715 }
1716 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
1717 to = json_object_get_int64(js_tmp);
1718 json_object_put(js_tmp);
1719 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001720 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001721
1722 start = time(NULL) + from;
1723 stop = time(NULL) + to;
1724
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001725 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001726
1727 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001728 reply = create_error("Missing session parameter.");
1729 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001730 }
1731
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001732 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001733 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001734 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka09629492014-07-10 15:58:06 +02001735 reply = create_error("Internal lock failed.");
1736 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001737 }
1738
1739 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1740 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001741 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001742 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001743 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001744 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001745 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001746 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001747 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001748 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1749 if (temp_session != NULL) {
1750 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1751 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001752 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001753 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001754 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001755 reply = create_error("notifications: creating an rpc request failed.");
1756 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001757 }
1758
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001759 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001760 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001761 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001762 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001763 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001764 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001765 DEBUG("Subscription RPC failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001766 reply = res;
1767 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001768 }
1769 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1770
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001771 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001772 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001773 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001774 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001775 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001776 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001777 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001778 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001779 ERROR("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001780 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001781
1782 ncntf_dispatch_receive(temp_session, notification_history);
1783
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001784 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001785 reply = json_object_new_object();
1786 json_object_object_add(reply, "notifications", notif_history_array);
1787 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001788 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001789
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001790 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001791 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001792 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001793 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001794 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001795 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001796 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001797 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001798 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001799 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1800 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001801 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001802 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001803 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001804 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001805 }
1806 reply = create_error("Invalid session identifier.");
1807 }
1808
Tomas Cejka09629492014-07-10 15:58:06 +02001809finalize:
1810 CHECK_AND_FREE(sid);
Tomas Cejka4003a702013-10-01 00:02:45 +02001811 return reply;
1812}
1813
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001814json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001815{
1816 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001817 char *sid = NULL;
1818 char *target = NULL;
1819 char *url = NULL;
Tomas Cejka4003a702013-10-01 00:02:45 +02001820 nc_rpc *rpc = NULL;
1821 NC_DATASTORE target_ds;
1822
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001823 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001824
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001825 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001826 sid = get_param_string(request, "session");
1827 target = get_param_string(request, "target");
1828 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001829 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001830
1831
1832 if ((sid == NULL) || (target == NULL)) {
Tomas Cejka09629492014-07-10 15:58:06 +02001833 reply = create_error("Missing session parameter.");
1834 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001835 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001836
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001837 /* validation */
1838 target_ds = parse_datastore(target);
1839 if (target_ds == NC_DATASTORE_URL) {
1840 if (url != NULL) {
1841 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001842 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001843 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001844 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001845 rpc = nc_rpc_validate(target_ds);
1846 }
1847 if (rpc == NULL) {
1848 DEBUG("mod_netconf: creating rpc request failed");
1849 reply = create_error("Creation of RPC request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001850 goto finalize;
Tomas Cejka4003a702013-10-01 00:02:45 +02001851 }
1852
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001853 DEBUG("Request: validate datastore");
1854 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001855
1856 CHECK_ERR_SET_REPLY
1857
1858 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001859 DEBUG("Request: validation ok.");
1860 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001861 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001862 }
1863 nc_rpc_free (rpc);
Tomas Cejka09629492014-07-10 15:58:06 +02001864finalize:
1865 CHECK_AND_FREE(sid);
1866 CHECK_AND_FREE(target);
1867 CHECK_AND_FREE(url);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001868 return reply;
1869}
1870
David Kupka8e60a372012-09-04 09:15:20 +02001871void * thread_routine (void * arg)
1872{
1873 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001874 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001875 json_object *request = NULL;
1876 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001877 json_object *js_tmp = NULL;
1878 int operation = (-1);
1879 NC_DATASTORE ds_type_t = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001880 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001881 const char *msgtext;
Tomas Cejka09629492014-07-10 15:58:06 +02001882 char *session_key = NULL;
1883 char *target = NULL;
1884 char *url = NULL;
Tomas Cejka64b87482013-06-03 16:30:53 +02001885 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001886 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001887 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001888 int client = ((struct pass_to_thread*)arg)->client;
1889
Tomas Cejka00635972013-06-03 15:10:52 +02001890 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001891
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001892 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001893 create_err_reply_p();
1894
David Kupka8e60a372012-09-04 09:15:20 +02001895 while (!isterminated) {
1896 fds.fd = client;
1897 fds.events = POLLIN;
1898 fds.revents = 0;
1899
1900 status = poll(&fds, 1, 1000);
1901
1902 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1903 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
David Kupka8e60a372012-09-04 09:15:20 +02001904 continue;
1905 } else if (status < 0) {
1906 /* 0: poll time outed
1907 * close socket and ignore this request from the client, it can try it again
1908 * -1: poll failed
1909 * something wrong happend, close this socket and wait for another request
1910 */
David Kupka8e60a372012-09-04 09:15:20 +02001911 close(client);
1912 break;
1913 }
1914 /* status > 0 */
1915
1916 /* check the status of the socket */
1917
1918 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1919 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1920 /* close client's socket (it's probably already closed by client */
David Kupka8e60a372012-09-04 09:15:20 +02001921 close(client);
1922 break;
1923 }
1924
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001925 DEBUG("Get framed message...");
1926 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001927
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001928 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001929 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001930 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001931 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001932 request = json_tokener_parse_verbose(buffer, &jerr);
1933 if (jerr != json_tokener_success) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001934 ERROR("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001935 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001936 continue;
1937 }
David Kupka8e60a372012-09-04 09:15:20 +02001938
Tomas Cejka09629492014-07-10 15:58:06 +02001939 session_key = get_param_string(request, "session");
1940 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
1941 operation = json_object_get_int(js_tmp);
1942 json_object_put(js_tmp);
1943 js_tmp = NULL;
1944 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001945 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001946 if (operation == -1) {
1947 reply = create_error("Missing operation type form frontend.");
1948 goto send_reply;
1949 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001950
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001951 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001952 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1953 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001954 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001955 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001956 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001957 pthread_mutex_unlock(&json_lock);
1958
David Kupka8e60a372012-09-04 09:15:20 +02001959 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001960
1961 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001962 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001963 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001964 /* there is some stupid client, so close the connection to give a chance to some other client */
1965 close(client);
1966 break;
1967 }
1968
David Kupka8e60a372012-09-04 09:15:20 +02001969 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001970 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001971
1972 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001973 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001974 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001975 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001976 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001977 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001978 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001979
1980 /* process required operation */
1981 switch (operation) {
1982 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001983 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001984 break;
1985 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001986 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001987 break;
1988 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001989 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001990 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001991 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001992 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001993 break;
David Kupka8e60a372012-09-04 09:15:20 +02001994 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001995 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001996 break;
1997 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001998 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001999 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02002000
David Kupka8e60a372012-09-04 09:15:20 +02002001 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02002002 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02002003 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02002004 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002005 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002006 target = get_param_string(request, "target");
2007 pthread_mutex_unlock(&json_lock);
2008 if (target != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02002009 ds_type_t = parse_datastore(target);
2010 }
David Kupka8e60a372012-09-04 09:15:20 +02002011
2012 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02002013 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02002014 break;
2015 }
David Kupka8e60a372012-09-04 09:15:20 +02002016 switch(operation) {
2017 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002018 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002019 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002020 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002021 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002022 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02002023 break;
2024 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002025 DEBUG("Request: lock (session %s)", session_key);
2026 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002027 break;
2028 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002029 DEBUG("Request: unlock (session %s)", session_key);
2030 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002031 break;
2032 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02002033 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02002034 break;
2035 }
2036
Tomas Cejka442258e2014-04-01 18:17:18 +02002037 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02002038 if (reply == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02002039 reply = create_ok();
David Kupka8e60a372012-09-04 09:15:20 +02002040 }
2041 break;
2042 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002043 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002044 break;
2045 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002046 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002047 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002048 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002049 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002050 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002051 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002052 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002053 break;
2054 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002055 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002056 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02002057 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002058 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02002059 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02002060 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002061 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02002062 break;
David Kupka8e60a372012-09-04 09:15:20 +02002063 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002064 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002065 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02002066 break;
2067 }
Tomas Cejka09629492014-07-10 15:58:06 +02002068 /* free parameters */
2069 CHECK_AND_FREE(session_key);
2070 CHECK_AND_FREE(url);
2071 CHECK_AND_FREE(target);
2072 request = NULL;
2073 operation = (-1);
2074 ds_type_t = (-1);
2075
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002076 DEBUG("Clean request json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002077 if (request != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002078 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002079 json_object_put(request);
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002080 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002081 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002082 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002083
2084
2085send_reply:
David Kupka1e3e4c82012-09-04 09:32:15 +02002086 /* send reply to caller */
2087 if (reply != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002088 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002089 msgtext = json_object_to_json_string(reply);
Tomas Cejka866f2282014-09-18 15:20:26 +02002090 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int) strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02002091 if (buffer != NULL) {
2092 free(buffer);
2093 buffer = NULL;
2094 }
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002095 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02002096 break;
2097 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002098 pthread_mutex_unlock(&json_lock);
2099
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002100 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02002101 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002102 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002103 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002104 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002105 reply = NULL;
2106 DEBUG("Clean message buffer.");
Tomas Cejka09629492014-07-10 15:58:06 +02002107 CHECK_AND_FREE(chunked_out_msg);
Tomas Cejka64b87482013-06-03 16:30:53 +02002108 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02002109 if (buffer != NULL) {
2110 free(buffer);
2111 buffer = NULL;
2112 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002113 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02002114 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02002115 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002116 ERROR("Reply is NULL, shouldn't be...");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002117 continue;
David Kupka8e60a372012-09-04 09:15:20 +02002118 }
2119 }
2120 }
David Kupka8e60a372012-09-04 09:15:20 +02002121 free (arg);
2122
Tomas Cejka442258e2014-04-01 18:17:18 +02002123 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002124
David Kupka8e60a372012-09-04 09:15:20 +02002125 return retval;
2126}
2127
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002128/**
2129 * \brief Close all open NETCONF sessions.
2130 *
2131 * During termination of mod_netconf, it is useful to close all remaining
2132 * sessions. This function iterates over the list of sessions and close them
2133 * all.
2134 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002135 * \param[in] p apr pool needed for hash table iterating
2136 * \param[in] ht hash table of session_with_mutex structs
2137 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002138static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002139{
2140 apr_hash_index_t *hi;
2141 void *val = NULL;
2142 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002143 const char *hashed_key = NULL;
2144 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002145 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002146
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002147 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002148 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002149 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002150 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002151 return;
2152 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002153 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002154 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002155 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002156 swm = (struct session_with_mutex *) val;
2157 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002158 DEBUG("LOCK mutex %s", __func__);
2159 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002160 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002161 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002162 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002163
2164 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002165 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002166 }
2167 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002168 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002169 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002170 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002171 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002172 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002173}
2174
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002175static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002176{
2177 apr_hash_index_t *hi;
2178 void *val = NULL;
2179 struct nc_session *ns = NULL;
2180 struct session_with_mutex *swm = NULL;
2181 const char *hashed_key = NULL;
2182 apr_ssize_t hashed_key_length;
2183 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002184 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002185
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002186 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002187 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002188 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002189 return;
2190 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002191 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002192 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2193 swm = (struct session_with_mutex *) val;
2194 if (swm == NULL) {
2195 continue;
2196 }
2197 ns = swm->session;
2198 if (ns == NULL) {
2199 continue;
2200 }
2201 pthread_mutex_lock(&swm->lock);
2202 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002203 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002204 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002205 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
2206 pthread_mutex_unlock(&swm->lock);
2207
2208 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002209 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002210 } else {
2211 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002212 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002213 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002214 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002215 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002216 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002217 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002218}
2219
2220
2221/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002222 * This is actually implementation of NETCONF client
2223 * - requests are received from UNIX socket in the predefined format
2224 * - results are replied through the same way
2225 * - the daemon run as a separate process, but it is started and stopped
2226 * automatically by Apache.
2227 *
2228 */
Radek Krejci469aab82012-07-22 18:42:20 +02002229static void forked_proc(apr_pool_t * pool, server_rec * server)
2230{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002231 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002232 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002233 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002234 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002235 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002236 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002237 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002238 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002239 struct timespec maxtime;
2240 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002241 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002242 #ifdef WITH_NOTIFICATIONS
2243 char use_notifications = 0;
2244 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002245
Tomas Cejka6b886e02013-07-05 09:53:17 +02002246 http_server = server;
2247
Tomas Cejka404d37e2013-04-13 02:31:35 +02002248 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002249 maxtime.tv_sec = 5;
2250 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002251
Tomas Cejka04e08f42014-03-27 19:52:34 +01002252#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002253 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002254 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002255#else
2256# ifdef SU_GROUP
2257 if (strlen(SU_GROUP) > 0) {
2258 struct group *g = getgrnam(SU_GROUP);
2259 if (g == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002260 ERROR("GID (%s) was not found.", SU_GROUP);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002261 return;
2262 }
2263 if (setgid(g->gr_gid) != 0) {
2264
Tomas Cejkacf44e522015-04-24 17:29:21 +02002265 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002266 return;
2267 }
2268 }
2269# else
2270 DEBUG("no SU_GROUP");
2271# endif
2272# ifdef SU_USER
2273 if (strlen(SU_USER) > 0) {
2274 struct passwd *p = getpwnam(SU_USER);
2275 if (p == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002276 ERROR("UID (%s) was not found.", SU_USER);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002277 return;
2278 }
2279 if (setuid(p->pw_uid) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002280 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002281 return;
2282 }
2283 }
2284# else
2285 DEBUG("no SU_USER");
2286# endif
2287#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002288
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002289 if (server != NULL) {
2290 cfg = ap_get_module_config(server->module_config, &netconf_module);
2291 if (cfg == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002292 ERROR("Getting mod_netconf configuration failed");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002293 return;
2294 }
2295 sockname = cfg->sockname;
2296 } else {
2297 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002298 }
Radek Krejci469aab82012-07-22 18:42:20 +02002299
Tomas Cejka04e08f42014-03-27 19:52:34 +01002300 /* try to remove if exists */
2301 unlink(sockname);
2302
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002303 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002304 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002305 ERROR("Creating socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002306 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002307 }
2308
2309 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002310 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002311 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2312
2313 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2314 if (errno == EADDRINUSE) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002315 ERROR("mod_netconf socket address already in use");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002316 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002317 }
Tomas Cejkacf44e522015-04-24 17:29:21 +02002318 ERROR("Binding socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002319 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002320 }
2321
2322 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002323 ERROR("Setting up listen socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002324 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002325 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002326 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002327
Tomas Cejka04e08f42014-03-27 19:52:34 +01002328 uid_t user = -1;
2329 if (strlen(CHOWN_USER) > 0) {
2330 struct passwd *p = getpwnam(CHOWN_USER);
2331 if (p != NULL) {
2332 user = p->pw_uid;
2333 }
2334 }
2335 gid_t group = -1;
2336 if (strlen(CHOWN_GROUP) > 0) {
2337 struct group *g = getgrnam(CHOWN_GROUP);
2338 if (g != NULL) {
2339 group = g->gr_gid;
2340 }
2341 }
2342 if (chown(sockname, user, group) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002343 ERROR("Chown on socket file failed (%s).", strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002344 }
2345
Tomas Cejkaba21b382013-04-13 02:37:32 +02002346 /* prepare internal lists */
2347 netconf_sessions_list = apr_hash_make(pool);
2348
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002349 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002350 if (notification_init(pool, server) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002351 ERROR("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002352 use_notifications = 0;
2353 } else {
2354 use_notifications = 1;
2355 }
2356 #endif
2357
Radek Krejci469aab82012-07-22 18:42:20 +02002358 /* setup libnetconf's callbacks */
2359 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002360 nc_callback_print(clb_print);
2361 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2362 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2363 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002364 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002365
2366 /* disable publickey authentication */
2367 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2368
David Kupka8e60a372012-09-04 09:15:20 +02002369 /* create mutex protecting session list */
2370 pthread_rwlockattr_init(&lock_attrs);
2371 /* rwlock is shared only with threads in this process */
2372 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2373 /* create rw lock */
2374 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002375 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002376 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002377 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002378 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002379 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkacf44e522015-04-24 17:29:21 +02002380 DEBUG("Initialization of notification history.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002381 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002382 ERROR("Initialization of notification history failed.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002383 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002384 if (pthread_key_create(&err_reply_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002385 ERROR("Initialization of reply key failed.");
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002386 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002387
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002388 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002389 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002390 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002391 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002392 #ifdef WITH_NOTIFICATIONS
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002393 if (use_notifications == 1) {
2394 notification_handle();
2395 }
2396 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002397 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002398 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002399 }
Radek Krejci469aab82012-07-22 18:42:20 +02002400
2401 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002402 len = sizeof(remote);
2403 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002404 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2405 apr_sleep(SLEEP_TIME);
2406 continue;
2407 } else if (client == -1 && (errno == EINTR)) {
2408 continue;
2409 } else if (client == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002410 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002411 continue;
2412 }
Radek Krejci469aab82012-07-22 18:42:20 +02002413
2414 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002415 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002416
David Kupka8e60a372012-09-04 09:15:20 +02002417 arg = malloc (sizeof(struct pass_to_thread));
2418 arg->client = client;
2419 arg->pool = pool;
2420 arg->server = server;
2421 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002422
David Kupka8e60a372012-09-04 09:15:20 +02002423 /* start new thread. It will serve this particular request and then terminate */
2424 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002425 ERROR("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002426 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002427 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002428 pthread_count++;
2429 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2430 ptids[pthread_count] = 0;
2431 }
Radek Krejci469aab82012-07-22 18:42:20 +02002432
David Kupka8e60a372012-09-04 09:15:20 +02002433 /* check if some thread already terminated, free some resources by joining it */
2434 for (i=0; i<pthread_count; i++) {
2435 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002436 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002437 pthread_count--;
2438 if (pthread_count > 0) {
2439 /* place last Thread ID on the place of joined one */
2440 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002441 }
Radek Krejci469aab82012-07-22 18:42:20 +02002442 }
2443 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002444 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002445 }
2446
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002447 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002448 /* join all threads */
2449 for (i=0; i<pthread_count; i++) {
2450 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2451 }
Radek Krejci469aab82012-07-22 18:42:20 +02002452
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002453 #ifdef WITH_NOTIFICATIONS
2454 notification_close();
2455 #endif
2456
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002457 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002458 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002459
David Kupka8e60a372012-09-04 09:15:20 +02002460 /* destroy rwlock */
2461 pthread_rwlock_destroy(&session_lock);
2462 pthread_rwlockattr_destroy(&lock_attrs);
2463
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002464 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002465
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002466 free(ptids);
2467 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002468 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002469 return;
2470error_exit:
2471 close(lsock);
2472 free(ptids);
2473 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002474}
2475
Radek Krejcif23850c2012-07-23 16:14:17 +02002476static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002477{
Radek Krejcif23850c2012-07-23 16:14:17 +02002478 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2479 apr_pool_create(&config->pool, pool);
2480 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002481 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002482
2483 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002484}
2485
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002486#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002487static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2488 apr_pool_t * plog, server_rec * s)
2489{
Radek Krejcif23850c2012-07-23 16:14:17 +02002490 mod_netconf_cfg *config;
2491 apr_status_t res;
2492
Radek Krejci469aab82012-07-22 18:42:20 +02002493 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002494 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002495 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002496
2497 /*
2498 * The following checks if this routine has been called before.
2499 * This is necessary because the parent process gets initialized
2500 * a couple of times as the server starts up.
2501 */
2502 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2503 if (!data) {
2504 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2505 return (OK);
2506 }
2507
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002508 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002509 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002510
Radek Krejcif23850c2012-07-23 16:14:17 +02002511 if (config && config->forkproc == NULL) {
2512 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2513 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002514 switch (res) {
2515 case APR_INCHILD:
2516 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002517 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002518 apr_signal(SIGTERM, signal_handler);
2519
2520 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002521 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002522
2523 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002524 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002525
Radek Krejcif23850c2012-07-23 16:14:17 +02002526 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002527 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002528 exit(APR_EGENERAL);
2529 break;
2530 case APR_INPARENT:
2531 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002532 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002533 break;
2534 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +02002535 ERROR("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002536 break;
2537 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002538 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002539 ERROR("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002540 }
2541
2542 return OK;
2543}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002544#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002545
Radek Krejci469aab82012-07-22 18:42:20 +02002546/**
2547 * Register module hooks
2548 */
2549static void mod_netconf_register_hooks(apr_pool_t * p)
2550{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002551#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002552 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002553#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002554}
2555
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002556static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2557{
2558 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2559 return NULL;
2560}
2561
2562static const command_rec netconf_cmds[] = {
2563 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2564 {NULL}
2565};
2566
Radek Krejci469aab82012-07-22 18:42:20 +02002567/* Dispatch list for API hooks */
2568module AP_MODULE_DECLARE_DATA netconf_module = {
2569 STANDARD20_MODULE_STUFF,
2570 NULL, /* create per-dir config structures */
2571 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002572 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002573 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002574 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002575 mod_netconf_register_hooks /* register hooks */
2576};
Radek Krejcia332b692012-11-12 16:15:54 +01002577
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002578int main(int argc, char **argv)
2579{
2580 apr_pool_t *pool;
2581 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2582 apr_signal(SIGTERM, signal_handler);
2583 apr_signal(SIGINT, signal_handler);
2584 apr_pool_create(&pool, NULL);
2585 forked_proc(pool, NULL);
2586 apr_pool_destroy(pool);
2587 apr_terminate();
2588 DEBUG("Terminated");
2589 return 0;
2590}