blob: 22d213d7907f542062adaa2063c99d1de8ae2790 [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 }
378 ret = nc_session_recv_reply(session, timeout, reply);
379 return ret;
380}
381
382/**
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200383 * \brief Connect to NETCONF server
384 *
385 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
386 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100387static 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 +0200388{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200389 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200390 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200391 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200392
393 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200394 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100395 DEBUG("prepare to connect %s@%s:%s", user, host, port);
David Kupka8e60a372012-09-04 09:15:20 +0200396 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100397 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200398
Radek Krejci469aab82012-07-22 18:42:20 +0200399 /* if connected successful, add session to the list */
400 if (session != NULL) {
401 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200402 session_key = gen_ncsession_hash(
403 (host==NULL) ? "localhost" : host,
404 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200405 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200406
Tomas Cejkaba21b382013-04-13 02:37:32 +0200407 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100408 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 +0200409 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100410 session = NULL;
411 free(locked_session);
412 locked_session = NULL;
Tomas Cejkacf44e522015-04-24 17:29:21 +0200413 ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200414 return NULL;
415 }
416 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200417 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200418 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200419 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200420 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100421 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200422 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100423 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200424 if (pthread_rwlock_wrlock(&session_lock) != 0) {
David Kupka8e60a372012-09-04 09:15:20 +0200425 nc_session_free(session);
426 free (locked_session);
Tomas Cejkacf44e522015-04-24 17:29:21 +0200427 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200428 return NULL;
429 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200430 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200431 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100432 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200433 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 +0100434 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200435
Tomas Cejka47387fd2013-06-10 20:37:46 +0200436 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100437 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200438 pthread_mutex_lock(&locked_session->lock);
439
440 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100441 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200442 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200443 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200444 }
445
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200446 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200448
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100449 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200450 pthread_mutex_unlock(&locked_session->lock);
451
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100452 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200453 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200454 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200455 ERROR("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200456 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200457 }
458
Radek Krejci469aab82012-07-22 18:42:20 +0200459}
460
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100461static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200462{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100463 DEBUG("lock private lock.");
464 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200465 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200466 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200467 }
468 locked_session->ntfc_subscribed = 0;
469 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100470 if (locked_session->session != NULL) {
471 nc_session_free(locked_session->session);
472 locked_session->session = NULL;
473 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100474 DEBUG("session closed.");
475 DEBUG("unlock private lock.");
476 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200477 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200478 ERROR("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200479 }
480
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100481 DEBUG("unlock session lock.");
482 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200483 usleep(500000); /* let notification thread stop */
484
485 /* session shouldn't be used by now */
486 /** \todo free all notifications from queue */
487 apr_array_clear(locked_session->notifications);
488 pthread_mutex_destroy(&locked_session->lock);
489 if (locked_session->hello_message != NULL) {
490 /** \todo free hello_message */
491 //json_object_put(locked_session->hello_message);
492 locked_session->hello_message = NULL;
493 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200494 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200495 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200496 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100497 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200498 return (EXIT_SUCCESS);
499}
500
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100501static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200502{
David Kupka8e60a372012-09-04 09:15:20 +0200503 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200504
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100505 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200506
David Kupka8e60a372012-09-04 09:15:20 +0200507 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100508 DEBUG("lock session lock.");
509 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200510 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200511 ERROR("Error while locking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100512 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200513 return EXIT_FAILURE;
514 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200515 /* get session to close */
516 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
517 /* remove session from the active sessions list -> nobody new can now work with session */
518 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200519
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100520 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200521 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200522 ERROR("Error while unlocking rwlock");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100523 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200524 }
525
526 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100527 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200528 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200529 ERROR("Unknown session to close");
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100530 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200531 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200532 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100533 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200534}
535
Tomas Cejkac7929632013-10-24 19:25:15 +0200536/**
537 * Test reply message type and return error message.
538 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200539 * \param[in] session nc_session internal struct
540 * \param[in] session_key session key, NULL to disable disconnect on error
541 * \param[in] msgt RPC-REPLY message type
542 * \param[out] data
543 * \return NULL on success
544 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100545json_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 +0200546{
547 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100548 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200549
550 /* process the result of the operation */
551 switch (msgt) {
552 case NC_MSG_UNKNOWN:
553 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200554 ERROR("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200555 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100556 netconf_close(session_key, &err);
557 }
558 if (err != NULL) {
559 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200560 }
561 return create_error("Internal: Receiving RPC-REPLY failed.");
562 }
563 case NC_MSG_NONE:
564 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100565 if (data != NULL) {
566 free(*data);
Tomas Cejka7b1e3bd2014-04-08 14:34:28 +0200567 (*data) = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100568 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200569 return NULL;
570 case NC_MSG_REPLY:
571 switch (replyt = nc_reply_get_type(reply)) {
572 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100573 if ((data != NULL) && (*data != NULL)) {
574 free(*data);
575 (*data) = NULL;
576 }
577 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200578 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100579 if (((*data) = nc_reply_get_data(reply)) == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200580 ERROR("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200581 return create_error("Internal: No data from reply received.");
582 } else {
583 return NULL;
584 }
585 break;
586 case NC_REPLY_ERROR:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200587 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100588 if (data != NULL) {
589 free(*data);
590 (*data) = NULL;
591 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200592 return create_error(nc_reply_get_errormsg(reply));
593 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200594 ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100595 if (data != NULL) {
596 free(*data);
597 (*data) = NULL;
598 }
Tomas Cejka60885252014-03-26 15:45:47 +0100599 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200600 }
601 break;
602 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +0200603 ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100604 if (data != NULL) {
605 free(*data);
606 (*data) = NULL;
607 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200608 return create_error("Internal: Unexpected RPC-REPLY message type.");
609 }
610}
611
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100612json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200613{
614 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200615 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200616
617 /* check requests */
618 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200619 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200620 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200621 }
622
623 if (session != NULL) {
624 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200625 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200626 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200628 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200629 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200630 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200631 }
632}
633
Tomas Cejkac7929632013-10-24 19:25:15 +0200634/**
635 * Perform RPC method that returns data.
636 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200637 * \param[in] session_key session identifier
638 * \param[in] rpc RPC message to perform
639 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
640 * \return NULL on success, json object with error otherwise
641 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100642static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200643{
644 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200645 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200646 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200647 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100648 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100649 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200650
Radek Krejci8e4632a2012-07-26 13:40:34 +0200651 /* check requests */
652 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200653 ERROR("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200654 res = create_error("Internal: RPC could not be created.");
655 data = NULL;
656 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200657 }
658
David Kupka8e60a372012-09-04 09:15:20 +0200659 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100660 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200661 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200662 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200663 res = create_error("Internal: Lock failed.");
664 data = NULL;
665 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200666 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200667 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200668 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 +0200669 if (locked_session != NULL) {
670 session = locked_session->session;
671 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200672 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200673 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100674 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200675 if (pthread_mutex_lock(&locked_session->lock) != 0) {
676 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100677 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200678 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200679
Tomas Cejkacf44e522015-04-24 17:29:21 +0200680 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200681 res = create_error("Internal: Could not unlock.");
682 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200683 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200684 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200685 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100686 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200687 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200688
Tomas Cejkacf44e522015-04-24 17:29:21 +0200689 ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200690 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200691 }
692
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200693 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200694
Radek Krejci035bf4e2012-07-25 10:59:09 +0200695 /* send the request and get the reply */
Tomas Cejka8ce138c2015-04-27 23:25:25 +0200696 msgt = netconf_send_recv_timed(session, rpc, 5000, &reply);
Radek Krejcia332b692012-11-12 16:15:54 +0100697
David Kupka8e60a372012-09-04 09:15:20 +0200698 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100699 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200700 pthread_mutex_unlock(&locked_session->lock);
701 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200702
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100703 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200704 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100705 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100706 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka866f2282014-09-18 15:20:26 +0200707 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200708 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100709 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200710 ERROR("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200711 res = create_error("Unknown session to process.");
712 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200713 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200714finished:
715 nc_reply_free(reply);
716 if (received_data != NULL) {
717 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100718 } else {
719 if (data != NULL) {
720 free(data);
721 data = NULL;
722 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200723 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200724 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200725}
726
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100727static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200728{
729 nc_rpc* rpc;
730 struct nc_filter *f = NULL;
731 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200732 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200733
734 /* create filter if set */
735 if (filter != NULL) {
736 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
737 }
738
739 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100740 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200741 nc_filter_free(f);
742 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200743 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200744 return (NULL);
745 }
746
Tomas Cejka94674662013-09-13 15:55:24 +0200747 /* tell server to show all elements even if they have default values */
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100748#ifdef HAVE_WITHDEFAULTS_TAGGED
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100749 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100750#else
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200751 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
752 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
Tomas Cejkae8bd27c2014-03-27 15:16:31 +0100753#endif
Tomas Cejkacf44e522015-04-24 17:29:21 +0200754 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200755 }
756
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100757 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200758 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100759 if (res != NULL) {
760 (*err) = res;
761 } else {
762 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200763 }
764
Radek Krejci8e4632a2012-07-26 13:40:34 +0200765 return (data);
766}
767
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100768static 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 +0100769{
770 nc_rpc* rpc;
771 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200772 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100773
774 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100775 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100776 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200777 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100778 return (NULL);
779 }
780
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100781 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100782 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100783 if (res != NULL) {
784 (*err) = res;
785 } else {
786 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200787 }
788
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100789 return (data);
790}
791
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100792static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200793{
794 nc_rpc* rpc;
795 struct nc_filter *f = NULL;
796 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200797 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200798
799 /* create filter if set */
800 if (filter != NULL) {
801 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
802 }
803
804 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100805 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200806 nc_filter_free(f);
807 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200808 ERROR("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200809 return (NULL);
810 }
811
Tomas Cejka94674662013-09-13 15:55:24 +0200812 /* tell server to show all elements even if they have default values */
Tomas Cejkac0e166b2014-07-08 16:04:58 +0200813 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
814 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
815 //if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200816 ERROR("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200817 }
818
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100819 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200820 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200821 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100822 (*err) = res;
823 } else {
824 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200825 }
826
Radek Krejci8e4632a2012-07-26 13:40:34 +0200827 return (data);
828}
829
Tomas Cejkab4d05872014-02-14 22:44:38 +0100830static 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 +0200831{
832 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200833 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200834
835 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100836 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100837 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100838 /* config, url */
839 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100840 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100841 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100842 rpc = nc_rpc_copyconfig(source, target, config);
843 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100844 } else if (source == NC_DATASTORE_URL) {
845 if (target == NC_DATASTORE_URL) {
846 /* url, url */
847 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
848 } else {
849 /* url, datastore */
850 rpc = nc_rpc_copyconfig(source, target, uri_src);
851 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100852 } else {
853 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100854 /* datastore, url */
855 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100856 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100857 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100858 rpc = nc_rpc_copyconfig(source, target);
859 }
860 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200861 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200862 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200863 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200864 }
865
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100866 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200867 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200868
869 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200870}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200871
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100872static 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 +0200873{
874 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200875 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200876
877 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100878 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200879 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200880 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200881 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200882 }
883
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100884 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200885 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200886
887 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200888}
889
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100890static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200891{
892 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200893 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200894
895 /* create requests */
896 rpc = nc_rpc_killsession(sid);
897 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200898 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200899 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200900 }
901
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100902 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200903 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200904 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200905}
906
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100907static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200908{
909 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200910 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200911
912 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200913 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200914 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200915 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200916 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200917 }
918
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100919 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200920 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200921 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200922}
923
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100924static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200925{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200926 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200927 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200928 if (target != NC_DATASTORE_URL) {
929 rpc = nc_rpc_deleteconfig(target);
930 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200931 rpc = nc_rpc_deleteconfig(target, url);
932 }
933 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200934 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200935 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200936 }
937
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100938 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200939 nc_rpc_free (rpc);
940 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200941}
942
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100943static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200944{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100945 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200946}
947
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100948static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200949{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100950 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200951}
952
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100953static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200954{
Tomas Cejka00635972013-06-03 15:10:52 +0200955 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200956 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200957
958 /* create requests */
959 rpc = nc_rpc_generic(content);
960 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200961 ERROR("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200962 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200963 }
964
Radek Krejcia332b692012-11-12 16:15:54 +0100965 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200966 // TODO ?free(*data);
967 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100968 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200969
970 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100971 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200972 nc_rpc_free (rpc);
973 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200974}
975
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200976/**
977 * @}
978 *//* netconf_operations */
979
Radek Krejci7338bde2012-08-10 12:57:30 +0200980void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200981{
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200982#define FOREACH(I) \
Tomas Cejkacf44e522015-04-24 17:29:21 +0200983 I(NC_VERB_ERROR) I(NC_VERB_WARNING)
984
985#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200986 break;
987
Radek Krejci7338bde2012-08-10 12:57:30 +0200988 switch (level) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200989 FOREACH(CASE);
990 case NC_VERB_VERBOSE:
991 case NC_VERB_DEBUG:
992 DEBUG("DEBUG: %s", msg);
993 break;
Tomas Cejka8a86cc22014-09-18 15:35:07 +0200994 }
995 if (level == NC_VERB_ERROR) {
996 /* return global error */
997 netconf_callback_error_process(NULL /* tag */, NULL /* type */,
998 NULL /* severity */, NULL /* apptag */,
999 NULL /* path */, msg, NULL /* attribute */,
1000 NULL /* element */, NULL /* ns */, NULL /* sid */);
Radek Krejci7338bde2012-08-10 12:57:30 +02001001 }
Radek Krejci469aab82012-07-22 18:42:20 +02001002}
1003
Tomas Cejka64b87482013-06-03 16:30:53 +02001004/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +02001005 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +02001006 * Caller should free message memory.
1007 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +02001008 * \return pointer to message
1009 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001010char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +02001011{
1012 /* read json in chunked framing */
1013 unsigned int buffer_size = 0;
1014 ssize_t buffer_len = 0;
1015 char *buffer = NULL;
1016 char c;
1017 ssize_t ret;
1018 int i, chunk_len;
1019 char chunk_len_str[12];
1020
1021 while (1) {
1022 /* read chunk length */
1023 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
1024 if (buffer != NULL) {
1025 free (buffer);
1026 buffer = NULL;
1027 }
1028 break;
1029 }
1030 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
1031 if (buffer != NULL) {
1032 free (buffer);
1033 buffer = NULL;
1034 }
1035 break;
1036 }
1037 i=0;
1038 memset (chunk_len_str, 0, 12);
1039 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
1040 if (i==0 && c == '#') {
1041 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
1042 /* end but invalid */
1043 if (buffer != NULL) {
1044 free (buffer);
1045 buffer = NULL;
1046 }
1047 }
1048 /* end of message, double-loop break */
1049 goto msg_complete;
1050 }
1051 chunk_len_str[i++] = c;
1052 if (i==11) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001053 ERROR("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +02001054 break;
1055 }
1056 }
1057 if (c != '\n') {
1058 if (buffer != NULL) {
1059 free (buffer);
1060 buffer = NULL;
1061 }
1062 break;
1063 }
1064 chunk_len_str[i] = 0;
1065 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1066 if (buffer != NULL) {
1067 free (buffer);
1068 buffer = NULL;
1069 }
1070 break;
1071 }
1072 buffer_size += chunk_len+1;
1073 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001074 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +02001075 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1076 if (buffer != NULL) {
1077 free (buffer);
1078 buffer = NULL;
1079 }
1080 break;
1081 }
1082 buffer_len += ret;
1083 }
1084msg_complete:
1085 return buffer;
1086}
1087
Tomas Cejkad5b53772013-06-08 23:01:07 +02001088NC_DATASTORE parse_datastore(const char *ds)
1089{
1090 if (strcmp(ds, "running") == 0) {
1091 return NC_DATASTORE_RUNNING;
1092 } else if (strcmp(ds, "startup") == 0) {
1093 return NC_DATASTORE_STARTUP;
1094 } else if (strcmp(ds, "candidate") == 0) {
1095 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001096 } else if (strcmp(ds, "url") == 0) {
1097 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001098 } else if (strcmp(ds, "config") == 0) {
1099 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001100 }
1101 return -1;
1102}
1103
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001104NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
1105{
1106 if (strcmp(t, "notset") == 0) {
1107 return NC_EDIT_TESTOPT_NOTSET;
1108 } else if (strcmp(t, "testset") == 0) {
1109 return NC_EDIT_TESTOPT_TESTSET;
1110 } else if (strcmp(t, "set") == 0) {
1111 return NC_EDIT_TESTOPT_SET;
1112 } else if (strcmp(t, "test") == 0) {
1113 return NC_EDIT_TESTOPT_TEST;
1114 }
1115 return NC_EDIT_TESTOPT_ERROR;
1116}
1117
Tomas Cejkad5b53772013-06-08 23:01:07 +02001118json_object *create_error(const char *errmess)
1119{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001120 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001121 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001122 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001123 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001124 json_object_array_add(array, json_object_new_string(errmess));
1125 json_object_object_add(reply, "errors", array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001126 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001127 return reply;
1128
1129}
1130
1131json_object *create_data(const char *data)
1132{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001133 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001134 json_object *reply = json_object_new_object();
1135 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1136 json_object_object_add(reply, "data", json_object_new_string(data));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001137 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001138 return reply;
1139}
1140
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001141json_object *create_ok()
1142{
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001143 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001144 json_object *reply = json_object_new_object();
1145 reply = json_object_new_object();
1146 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001147 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001148 return reply;
1149}
1150
Tomas Cejka09629492014-07-10 15:58:06 +02001151char *get_param_string(json_object *data, const char *name)
1152{
1153 json_object *js_tmp = NULL;
1154 char *res = NULL;
1155 if (json_object_object_get_ex(data, name, &js_tmp) == TRUE) {
1156 res = strdup(json_object_get_string(js_tmp));
1157 json_object_put(js_tmp);
1158 }
1159 return res;
1160}
1161
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001162json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001163{
Tomas Cejka09629492014-07-10 15:58:06 +02001164 char *host = NULL;
1165 char *port = NULL;
1166 char *user = NULL;
1167 char *pass = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001168 json_object *capabilities = NULL;
1169 json_object *reply = NULL;
1170 char *session_key_hash = NULL;
1171 struct nc_cpblts* cpblts = NULL;
1172 unsigned int len, i;
1173
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001174 DEBUG("Request: Connect");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001175 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001176
Tomas Cejka09629492014-07-10 15:58:06 +02001177 host = get_param_string(request, "host");
1178 port = get_param_string(request, "port");
1179 user = get_param_string(request, "user");
1180 pass = get_param_string(request, "pass");
1181
1182 if (json_object_object_get_ex(request, "capabilities", &capabilities) == TRUE) {
1183 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1184 cpblts = nc_cpblts_new(NULL);
1185 for (i=0; i<len; i++) {
1186 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1187 }
1188 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001189 ERROR("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001190 }
Tomas Cejka09629492014-07-10 15:58:06 +02001191 json_object_put(capabilities);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001192 }
Tomas Cejka09629492014-07-10 15:58:06 +02001193
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001194 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001195
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001196 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001197 if ((host == NULL) || (user == NULL)) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001198 ERROR("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001199 session_key_hash = NULL;
1200 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001201 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1202 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001203 }
1204 if (cpblts != NULL) {
1205 nc_cpblts_free(cpblts);
1206 }
1207
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001208 GETSPEC_ERR_REPLY
1209
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001210 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001211 if (session_key_hash == NULL) {
1212 /* negative reply */
1213 if (err_reply == NULL) {
1214 reply = json_object_new_object();
1215 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1216 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkacf44e522015-04-24 17:29:21 +02001217 ERROR("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001218 } else {
1219 /* use filled err_reply from libnetconf's callback */
1220 reply = err_reply;
Tomas Cejkacf44e522015-04-24 17:29:21 +02001221 ERROR("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001222 }
1223 } else {
1224 /* positive reply */
1225 reply = json_object_new_object();
1226 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1227 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1228
1229 free(session_key_hash);
1230 }
Tomas Cejka09629492014-07-10 15:58:06 +02001231 memset(pass, 0, strlen(pass));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001232 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001233 CHECK_AND_FREE(host);
1234 CHECK_AND_FREE(user);
1235 CHECK_AND_FREE(port);
1236 CHECK_AND_FREE(pass);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001237 return reply;
1238}
1239
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001240json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001241{
Tomas Cejka09629492014-07-10 15:58:06 +02001242 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001243 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001244 json_object *reply = NULL;
1245
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001246 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001247
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001248 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001249 filter = get_param_string(request, "filter");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001250 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001251
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001252 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001253 CHECK_ERR_SET_REPLY_ERR("Get information failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001254 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001255 reply = create_data(data);
1256 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257 }
1258 return reply;
1259}
1260
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001261json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001262{
1263 NC_DATASTORE ds_type_s = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001264 char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001265 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001266 char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001267 json_object *reply = NULL;
1268
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001269 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001270
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001271 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001272 filter = get_param_string(request, "filter");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001273
Tomas Cejka09629492014-07-10 15:58:06 +02001274 source = get_param_string(request, "source");
1275 if (source != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001276 ds_type_s = parse_datastore(source);
1277 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001278 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001279
Tomas Cejkad5b53772013-06-08 23:01:07 +02001280 if (ds_type_s == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001281 reply = create_error("Invalid source repository type requested.");
1282 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001283 }
1284
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001285 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001286 CHECK_ERR_SET_REPLY_ERR("Get configuration operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001287 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001288 reply = create_data(data);
1289 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001290 }
Tomas Cejka09629492014-07-10 15:58:06 +02001291finalize:
1292 CHECK_AND_FREE(filter);
1293 CHECK_AND_FREE(source);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001294 return reply;
1295}
1296
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001297json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001298{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001299 char *data = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001300 char *identifier = NULL;
1301 char *version = NULL;
1302 char *format = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001303 json_object *reply = NULL;
1304
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001305 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001306 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001307 identifier = get_param_string(request, "identifier");
1308 version = get_param_string(request, "version");
1309 format = get_param_string(request, "format");
Tomas Cejkab9e7efe2014-03-28 21:09:04 +01001310 pthread_mutex_unlock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001311
Tomas Cejkad5b53772013-06-08 23:01:07 +02001312 if (identifier == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001313 reply = create_error("No identifier for get-schema supplied.");
1314 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001315 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001316
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001317 DEBUG("get-schema(version: %s, format: %s)", version, format);
1318 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001319 CHECK_ERR_SET_REPLY_ERR("Get models operation failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001320 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001321 reply = create_data(data);
1322 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001323 }
Tomas Cejka09629492014-07-10 15:58:06 +02001324finalize:
1325 CHECK_AND_FREE(identifier);
1326 CHECK_AND_FREE(version);
1327 CHECK_AND_FREE(format);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001328 return reply;
1329}
1330
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001331json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001332{
1333 NC_DATASTORE ds_type_s = -1;
1334 NC_DATASTORE ds_type_t = -1;
1335 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1336 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001337 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejka09629492014-07-10 15:58:06 +02001338 char *defop = NULL;
1339 char *erropt = NULL;
1340 char *config = NULL;
1341 char *source = NULL;
1342 char *target = NULL;
1343 char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001344 json_object *reply = NULL;
1345
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001346 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001347
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001348 pthread_mutex_lock(&json_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001349 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001350 defop = get_param_string(request, "default-operation");
1351 erropt = get_param_string(request, "error-option");
1352 target = get_param_string(request, "target");
1353 source = get_param_string(request, "source");
1354 config = get_param_string(request, "config");
1355 testopt = get_param_string(request, "test-option");
1356 pthread_mutex_unlock(&json_lock);
1357
1358 if (target != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001359 ds_type_t = parse_datastore(target);
1360 }
Tomas Cejka09629492014-07-10 15:58:06 +02001361 if (source != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001362 ds_type_s = parse_datastore(source);
1363 } else {
1364 /* source is optional, default value is config */
1365 ds_type_s = NC_DATASTORE_CONFIG;
1366 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001367
Tomas Cejkad5b53772013-06-08 23:01:07 +02001368 if (defop != NULL) {
1369 if (strcmp(defop, "merge") == 0) {
1370 defop_type = NC_EDIT_DEFOP_MERGE;
1371 } else if (strcmp(defop, "replace") == 0) {
1372 defop_type = NC_EDIT_DEFOP_REPLACE;
1373 } else if (strcmp(defop, "none") == 0) {
1374 defop_type = NC_EDIT_DEFOP_NONE;
1375 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001376 reply = create_error("Invalid default-operation parameter.");
1377 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001378 }
1379 } else {
1380 defop_type = NC_EDIT_DEFOP_NOTSET;
1381 }
1382
Tomas Cejkad5b53772013-06-08 23:01:07 +02001383 if (erropt != NULL) {
1384 if (strcmp(erropt, "continue-on-error") == 0) {
1385 erropt_type = NC_EDIT_ERROPT_CONT;
1386 } else if (strcmp(erropt, "stop-on-error") == 0) {
1387 erropt_type = NC_EDIT_ERROPT_STOP;
1388 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1389 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1390 } else {
Tomas Cejka09629492014-07-10 15:58:06 +02001391 reply = create_error("Invalid error-option parameter.");
1392 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001393 }
1394 } else {
1395 erropt_type = 0;
1396 }
1397
Tomas Cejkad5b53772013-06-08 23:01:07 +02001398 if (ds_type_t == -1) {
Tomas Cejka09629492014-07-10 15:58:06 +02001399 reply = create_error("Invalid target repository type requested.");
1400 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001401 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001402 if (ds_type_s == NC_DATASTORE_CONFIG) {
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001403 if (config == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001404 reply = create_error("Invalid config data parameter.");
1405 goto finalize;
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001406 }
1407 } else if (ds_type_s == NC_DATASTORE_URL){
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001408 if (config == NULL) {
1409 config = "";
1410 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001411 }
1412
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001413 if (testopt != NULL) {
1414 testopt_type = parse_testopt(testopt);
1415 } else {
1416 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1417 }
1418
1419 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 +01001420
1421 CHECK_ERR_SET_REPLY
Tomas Cejka09629492014-07-10 15:58:06 +02001422finalize:
1423 CHECK_AND_FREE(defop);
1424 CHECK_AND_FREE(erropt);
1425 CHECK_AND_FREE(config);
1426 CHECK_AND_FREE(source);
1427 CHECK_AND_FREE(target);
1428 CHECK_AND_FREE(testopt);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001429 return reply;
1430}
1431
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001432json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001433{
1434 NC_DATASTORE ds_type_s = -1;
1435 NC_DATASTORE ds_type_t = -1;
Tomas Cejka09629492014-07-10 15:58:06 +02001436 char *config = NULL;
1437 char *target = NULL;
1438 char *source = NULL;
1439 char *uri_src = NULL;
1440 char *uri_trg = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001441
Tomas Cejkad5b53772013-06-08 23:01:07 +02001442 json_object *reply = NULL;
1443
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001444 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001445
1446 /* get parameters */
Tomas Cejka09629492014-07-10 15:58:06 +02001447 pthread_mutex_lock(&json_lock);
1448 target = get_param_string(request, "target");
1449 source = get_param_string(request, "source");
1450 config = get_param_string(request, "config");
1451 uri_src = get_param_string(request, "uri-source");
1452 uri_trg = get_param_string(request, "uri-target");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001453 pthread_mutex_unlock(&json_lock);
1454
Tomas Cejka09629492014-07-10 15:58:06 +02001455 if (target != NULL) {
1456 ds_type_t = parse_datastore(target);
1457 }
1458 if (source != NULL) {
1459 ds_type_s = parse_datastore(source);
1460 } else {
1461 /* source == NULL *//* no explicit source specified -> use config data */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001462 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejka09629492014-07-10 15:58:06 +02001463 }
1464 if (ds_type_s == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001465 /* source datastore specified, but it is invalid */
Tomas Cejka09629492014-07-10 15:58:06 +02001466 reply = create_error("Invalid source repository type requested.");
1467 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001468 }
1469
1470 if (ds_type_t == -1) {
1471 /* invalid target datastore specified */
Tomas Cejka09629492014-07-10 15:58:06 +02001472 reply = create_error("Invalid target repository type requested.");
1473 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001474 }
1475
Tomas Cejka55c6df52014-02-20 12:59:33 +01001476 /* source can be missing when config is given */
1477 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001478 reply = create_error("invalid input parameters - source and config is required.");
Tomas Cejka09629492014-07-10 15:58:06 +02001479 goto finalize;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001480 }
1481
1482 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001483 if (uri_src == NULL) {
1484 uri_src = "";
1485 }
1486 }
1487 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001488 if (uri_trg == NULL) {
1489 uri_trg = "";
1490 }
1491 }
1492 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001493
1494 CHECK_ERR_SET_REPLY
1495
Tomas Cejka09629492014-07-10 15:58:06 +02001496finalize:
1497 CHECK_AND_FREE(config);
1498 CHECK_AND_FREE(target);
1499 CHECK_AND_FREE(source);
1500 CHECK_AND_FREE(uri_src);
1501 CHECK_AND_FREE(uri_trg);
1502
Tomas Cejkad5b53772013-06-08 23:01:07 +02001503 return reply;
1504}
1505
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001506json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001507{
1508 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001509 char *config = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001510 char *data = NULL;
1511
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001512 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001513
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001514 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001515 config = get_param_string(request, "content");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001516 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001517
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001518 reply = netconf_generic(session_key, config, &data);
1519 if (reply == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001520 GETSPEC_ERR_REPLY
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001521 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001522 /* use filled err_reply from libnetconf's callback */
1523 reply = err_reply;
1524 }
1525 } else {
1526 if (data == NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001527 pthread_mutex_lock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001528 reply = json_object_new_object();
1529 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001530 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001531 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001532 reply = create_data(data);
1533 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001534 }
1535 }
Tomas Cejka09629492014-07-10 15:58:06 +02001536 CHECK_AND_FREE(config);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001537 return reply;
1538}
1539
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001540json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001541{
1542 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001543 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001544
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001545 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001546 CHECK_ERR_SET_REPLY_ERR("Get configuration information from device failed.")
Tomas Cejkad5b53772013-06-08 23:01:07 +02001547 } else {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001548 reply = create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001549 }
1550 return reply;
1551}
1552
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001553json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001554{
1555 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001556 char *sid = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001557
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001558 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001559
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001560 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001561 sid = get_param_string(request, "session-id");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001562 pthread_mutex_unlock(&json_lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001563
1564 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001565 reply = create_error("Missing session-id parameter.");
1566 goto finalize;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001567 }
1568
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001569 reply = netconf_killsession(session_key, sid);
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001570
1571 CHECK_ERR_SET_REPLY
1572
Tomas Cejka09629492014-07-10 15:58:06 +02001573finalize:
1574 CHECK_AND_FREE(sid);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001575 return reply;
1576}
1577
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001578json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001579{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001580 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001581 struct session_with_mutex * locked_session = NULL;
1582 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001583 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001584
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001585 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001586 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001587 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001588 return NULL;
1589 }
1590
Tomas Cejka09629492014-07-10 15:58:06 +02001591 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 +02001592 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001593 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001594 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001595 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001596 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001597 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001598 prepare_status_message(locked_session, temp_session);
1599 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001600 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001601 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001602 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001603 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001604 reply = create_error("Reload was unsuccessful, connection failed.");
1605 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001606 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001607 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001608 DEBUG("UNLOCK wrlock %s", __func__);
1609 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001610 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka93b0e492014-03-26 15:47:45 +01001611 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001612 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001613 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001614 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001615 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001616 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001617 reply = create_error("Invalid session identifier.");
1618 }
1619
1620 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1621 reply = locked_session->hello_message;
1622 }
1623 return reply;
1624}
1625
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001626json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001627{
1628 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001629 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001630 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001631
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001632 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001633 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001634 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001635 }
1636
Tomas Cejka09629492014-07-10 15:58:06 +02001637 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 +02001638 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001639 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001640 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001641 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001642 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001643 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001644 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001645 if (locked_session->hello_message != NULL) {
1646 reply = locked_session->hello_message;
1647 } else {
1648 reply = create_error("Invalid session identifier.");
1649 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001650 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001651 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001652 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001653 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001654 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001655 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001656 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001657 reply = create_error("Invalid session identifier.");
1658 }
1659
Tomas Cejka47387fd2013-06-10 20:37:46 +02001660
Tomas Cejkad5b53772013-06-08 23:01:07 +02001661 return reply;
1662}
1663
Tomas Cejka6b886e02013-07-05 09:53:17 +02001664void notification_history(time_t eventtime, const char *content)
1665{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001666 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1667 if (notif_history_array == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001668 ERROR("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001669 return;
1670 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001671 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001672 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001673 json_object *notif = json_object_new_object();
1674 if (notif == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001675 ERROR("Could not allocate memory for notification (json).");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001676 goto failed;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001677 }
1678 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1679 json_object_object_add(notif, "content", json_object_new_string(content));
1680 json_object_array_add(notif_history_array, notif);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001681failed:
1682 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001683}
1684
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001685json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001686{
1687 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001688 json_object *js_tmp = NULL;
1689 char *sid = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001690 struct session_with_mutex *locked_session = NULL;
1691 struct nc_session *temp_session = NULL;
1692 nc_rpc *rpc = NULL;
1693 time_t start = 0;
1694 time_t stop = 0;
Tomas Cejka09629492014-07-10 15:58:06 +02001695 int64_t from = 0, to = 0;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001696
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001697 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001698
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001699 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001700 sid = get_param_string(request, "session");
1701
1702 if (json_object_object_get_ex(request, "from", &js_tmp) == TRUE) {
1703 from = json_object_get_int64(js_tmp);
1704 json_object_put(js_tmp);
1705 }
1706 if (json_object_object_get_ex(request, "to", &js_tmp) == TRUE) {
1707 to = json_object_get_int64(js_tmp);
1708 json_object_put(js_tmp);
1709 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001710 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001711
1712 start = time(NULL) + from;
1713 stop = time(NULL) + to;
1714
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001715 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001716
1717 if (sid == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02001718 reply = create_error("Missing session parameter.");
1719 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001720 }
1721
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001722 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001723 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001724 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka09629492014-07-10 15:58:06 +02001725 reply = create_error("Internal lock failed.");
1726 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001727 }
1728
1729 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1730 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001731 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001732 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001733 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001734 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001735 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001736 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001737 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001738 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1739 if (temp_session != NULL) {
1740 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1741 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001742 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001743 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001744 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001745 reply = create_error("notifications: creating an rpc request failed.");
1746 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001747 }
1748
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001749 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001750 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001751 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001752 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001753 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001754 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001755 DEBUG("Subscription RPC failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001756 reply = res;
1757 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001758 }
1759 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1760
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001761 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001762 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001763 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001764 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001765 pthread_mutex_lock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001766 json_object *notif_history_array = json_object_new_array();
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001767 pthread_mutex_unlock(&json_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001768 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001769 ERROR("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001770 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001771
1772 ncntf_dispatch_receive(temp_session, notification_history);
1773
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001774 pthread_mutex_lock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001775 reply = json_object_new_object();
1776 json_object_object_add(reply, "notifications", notif_history_array);
1777 //json_object_put(notif_history_array);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001778 pthread_mutex_unlock(&json_lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001779
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001780 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001781 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001782 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001783 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001784 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001785 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001786 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001787 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001788 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001789 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1790 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001791 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001792 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001793 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001794 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001795 }
1796 reply = create_error("Invalid session identifier.");
1797 }
1798
Tomas Cejka09629492014-07-10 15:58:06 +02001799finalize:
1800 CHECK_AND_FREE(sid);
Tomas Cejka4003a702013-10-01 00:02:45 +02001801 return reply;
1802}
1803
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001804json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001805{
1806 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001807 char *sid = NULL;
1808 char *target = NULL;
1809 char *url = NULL;
Tomas Cejka4003a702013-10-01 00:02:45 +02001810 nc_rpc *rpc = NULL;
1811 NC_DATASTORE target_ds;
1812
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001813 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001814
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001815 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001816 sid = get_param_string(request, "session");
1817 target = get_param_string(request, "target");
1818 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001819 pthread_mutex_unlock(&json_lock);
Tomas Cejka4003a702013-10-01 00:02:45 +02001820
1821
1822 if ((sid == NULL) || (target == NULL)) {
Tomas Cejka09629492014-07-10 15:58:06 +02001823 reply = create_error("Missing session parameter.");
1824 goto finalize;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001825 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001826
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001827 /* validation */
1828 target_ds = parse_datastore(target);
1829 if (target_ds == NC_DATASTORE_URL) {
1830 if (url != NULL) {
1831 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001832 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001833 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001834 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001835 rpc = nc_rpc_validate(target_ds);
1836 }
1837 if (rpc == NULL) {
1838 DEBUG("mod_netconf: creating rpc request failed");
1839 reply = create_error("Creation of RPC request failed.");
Tomas Cejka09629492014-07-10 15:58:06 +02001840 goto finalize;
Tomas Cejka4003a702013-10-01 00:02:45 +02001841 }
1842
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001843 DEBUG("Request: validate datastore");
1844 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001845
1846 CHECK_ERR_SET_REPLY
1847
1848 if (reply == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001849 DEBUG("Request: validation ok.");
1850 reply = create_ok();
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001851 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001852 }
1853 nc_rpc_free (rpc);
Tomas Cejka09629492014-07-10 15:58:06 +02001854finalize:
1855 CHECK_AND_FREE(sid);
1856 CHECK_AND_FREE(target);
1857 CHECK_AND_FREE(url);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001858 return reply;
1859}
1860
David Kupka8e60a372012-09-04 09:15:20 +02001861void * thread_routine (void * arg)
1862{
1863 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001864 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001865 json_object *request = NULL;
1866 json_object *reply = NULL;
Tomas Cejka09629492014-07-10 15:58:06 +02001867 json_object *js_tmp = NULL;
1868 int operation = (-1);
1869 NC_DATASTORE ds_type_t = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001870 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001871 const char *msgtext;
Tomas Cejka09629492014-07-10 15:58:06 +02001872 char *session_key = NULL;
1873 char *target = NULL;
1874 char *url = NULL;
Tomas Cejka64b87482013-06-03 16:30:53 +02001875 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001876 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001877 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001878 int client = ((struct pass_to_thread*)arg)->client;
1879
Tomas Cejka00635972013-06-03 15:10:52 +02001880 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001881
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01001882 /* init thread specific err_reply memory */
Tomas Cejka442258e2014-04-01 18:17:18 +02001883 create_err_reply_p();
1884
David Kupka8e60a372012-09-04 09:15:20 +02001885 while (!isterminated) {
1886 fds.fd = client;
1887 fds.events = POLLIN;
1888 fds.revents = 0;
1889
1890 status = poll(&fds, 1, 1000);
1891
1892 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1893 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
David Kupka8e60a372012-09-04 09:15:20 +02001894 continue;
1895 } else if (status < 0) {
1896 /* 0: poll time outed
1897 * close socket and ignore this request from the client, it can try it again
1898 * -1: poll failed
1899 * something wrong happend, close this socket and wait for another request
1900 */
David Kupka8e60a372012-09-04 09:15:20 +02001901 close(client);
1902 break;
1903 }
1904 /* status > 0 */
1905
1906 /* check the status of the socket */
1907
1908 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1909 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1910 /* close client's socket (it's probably already closed by client */
David Kupka8e60a372012-09-04 09:15:20 +02001911 close(client);
1912 break;
1913 }
1914
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001915 DEBUG("Get framed message...");
1916 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001917
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001918 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001919 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001920 enum json_tokener_error jerr;
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001921 pthread_mutex_lock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001922 request = json_tokener_parse_verbose(buffer, &jerr);
1923 if (jerr != json_tokener_success) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02001924 ERROR("JSON parsing error");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001925 pthread_mutex_unlock(&json_lock);
Tomas Cejka00635972013-06-03 15:10:52 +02001926 continue;
1927 }
David Kupka8e60a372012-09-04 09:15:20 +02001928
Tomas Cejka09629492014-07-10 15:58:06 +02001929 session_key = get_param_string(request, "session");
1930 if (json_object_object_get_ex(request, "type", &js_tmp) == TRUE) {
1931 operation = json_object_get_int(js_tmp);
1932 json_object_put(js_tmp);
1933 js_tmp = NULL;
1934 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001935 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001936 if (operation == -1) {
1937 reply = create_error("Missing operation type form frontend.");
1938 goto send_reply;
1939 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001940
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001941 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001942 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1943 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001944 reply = create_error("Missing session specification.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001945 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001946 msgtext = json_object_to_json_string(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001947 pthread_mutex_unlock(&json_lock);
1948
David Kupka8e60a372012-09-04 09:15:20 +02001949 send(client, msgtext, strlen(msgtext) + 1, 0);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001950
1951 pthread_mutex_lock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001952 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001953 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02001954 /* there is some stupid client, so close the connection to give a chance to some other client */
1955 close(client);
1956 break;
1957 }
1958
David Kupka8e60a372012-09-04 09:15:20 +02001959 /* null global JSON error-reply */
Tomas Cejka442258e2014-04-01 18:17:18 +02001960 clean_err_reply();
David Kupka8e60a372012-09-04 09:15:20 +02001961
1962 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001963 if (reply != NULL) {
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001964 pthread_mutex_lock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001965 json_object_put(reply);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001966 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001967 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001968 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001969
1970 /* process required operation */
1971 switch (operation) {
1972 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001973 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001974 break;
1975 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001976 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001977 break;
1978 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001979 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001980 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001981 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001982 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001983 break;
David Kupka8e60a372012-09-04 09:15:20 +02001984 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001985 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001986 break;
1987 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001988 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001989 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001990
David Kupka8e60a372012-09-04 09:15:20 +02001991 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001992 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001993 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001994 /* get parameters */
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01001995 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02001996 target = get_param_string(request, "target");
1997 pthread_mutex_unlock(&json_lock);
1998 if (target != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001999 ds_type_t = parse_datastore(target);
2000 }
David Kupka8e60a372012-09-04 09:15:20 +02002001
2002 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02002003 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02002004 break;
2005 }
David Kupka8e60a372012-09-04 09:15:20 +02002006 switch(operation) {
2007 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002008 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002009 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002010 url = get_param_string(request, "url");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002011 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002012 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02002013 break;
2014 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002015 DEBUG("Request: lock (session %s)", session_key);
2016 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002017 break;
2018 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002019 DEBUG("Request: unlock (session %s)", session_key);
2020 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02002021 break;
2022 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02002023 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02002024 break;
2025 }
2026
Tomas Cejka442258e2014-04-01 18:17:18 +02002027 CHECK_ERR_SET_REPLY
Tomas Cejkac7929632013-10-24 19:25:15 +02002028 if (reply == NULL) {
Tomas Cejka09629492014-07-10 15:58:06 +02002029 reply = create_ok();
David Kupka8e60a372012-09-04 09:15:20 +02002030 }
2031 break;
2032 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002033 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002034 break;
2035 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002036 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002037 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002038 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002039 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002040 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02002041 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002042 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002043 break;
2044 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002045 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02002046 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02002047 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002048 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02002049 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02002050 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002051 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02002052 break;
David Kupka8e60a372012-09-04 09:15:20 +02002053 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002054 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02002055 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02002056 break;
2057 }
Tomas Cejka09629492014-07-10 15:58:06 +02002058 /* free parameters */
2059 CHECK_AND_FREE(session_key);
2060 CHECK_AND_FREE(url);
2061 CHECK_AND_FREE(target);
2062 request = NULL;
2063 operation = (-1);
2064 ds_type_t = (-1);
2065
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002066 DEBUG("Clean request json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002067 if (request != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002068 pthread_mutex_lock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002069 json_object_put(request);
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002070 pthread_mutex_unlock(&json_lock);
Tomas Cejka09629492014-07-10 15:58:06 +02002071 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002072 DEBUG("Send reply json object.");
Tomas Cejka09629492014-07-10 15:58:06 +02002073
2074
2075send_reply:
David Kupka1e3e4c82012-09-04 09:32:15 +02002076 /* send reply to caller */
2077 if (reply != NULL) {
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002078 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002079 msgtext = json_object_to_json_string(reply);
Tomas Cejka866f2282014-09-18 15:20:26 +02002080 if (asprintf(&chunked_out_msg, "\n#%d\n%s\n##\n", (int) strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02002081 if (buffer != NULL) {
2082 free(buffer);
2083 buffer = NULL;
2084 }
Tomas Cejka74dd7a92014-09-18 15:58:45 +02002085 pthread_mutex_unlock(&json_lock);
David Kupka8e60a372012-09-04 09:15:20 +02002086 break;
2087 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002088 pthread_mutex_unlock(&json_lock);
2089
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002090 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02002091 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002092 DEBUG("Clean reply json object.");
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002093 pthread_mutex_lock(&json_lock);
David Kupka1e3e4c82012-09-04 09:32:15 +02002094 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002095 reply = NULL;
2096 DEBUG("Clean message buffer.");
Tomas Cejka09629492014-07-10 15:58:06 +02002097 CHECK_AND_FREE(chunked_out_msg);
Tomas Cejka64b87482013-06-03 16:30:53 +02002098 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02002099 if (buffer != NULL) {
2100 free(buffer);
2101 buffer = NULL;
2102 }
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002103 pthread_mutex_unlock(&json_lock);
Tomas Cejka442258e2014-04-01 18:17:18 +02002104 clean_err_reply();
David Kupka1e3e4c82012-09-04 09:32:15 +02002105 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002106 ERROR("Reply is NULL, shouldn't be...");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002107 continue;
David Kupka8e60a372012-09-04 09:15:20 +02002108 }
2109 }
2110 }
David Kupka8e60a372012-09-04 09:15:20 +02002111 free (arg);
2112
Tomas Cejka442258e2014-04-01 18:17:18 +02002113 free_err_reply();
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002114
David Kupka8e60a372012-09-04 09:15:20 +02002115 return retval;
2116}
2117
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002118/**
2119 * \brief Close all open NETCONF sessions.
2120 *
2121 * During termination of mod_netconf, it is useful to close all remaining
2122 * sessions. This function iterates over the list of sessions and close them
2123 * all.
2124 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002125 * \param[in] p apr pool needed for hash table iterating
2126 * \param[in] ht hash table of session_with_mutex structs
2127 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002128static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002129{
2130 apr_hash_index_t *hi;
2131 void *val = NULL;
2132 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002133 const char *hashed_key = NULL;
2134 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002135 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002136
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002137 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002138 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002139 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002140 ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002141 return;
2142 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002143 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002144 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002145 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002146 swm = (struct session_with_mutex *) val;
2147 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002148 DEBUG("LOCK mutex %s", __func__);
2149 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002150 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002151 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002152 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002153
2154 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002155 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002156 }
2157 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002158 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002159 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002160 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002161 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002162 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002163}
2164
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002165static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002166{
2167 apr_hash_index_t *hi;
2168 void *val = NULL;
2169 struct nc_session *ns = NULL;
2170 struct session_with_mutex *swm = NULL;
2171 const char *hashed_key = NULL;
2172 apr_ssize_t hashed_key_length;
2173 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002174 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002175
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002176 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002177 if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002178 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002179 return;
2180 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02002181 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002182 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
2183 swm = (struct session_with_mutex *) val;
2184 if (swm == NULL) {
2185 continue;
2186 }
2187 ns = swm->session;
2188 if (ns == NULL) {
2189 continue;
2190 }
2191 pthread_mutex_lock(&swm->lock);
2192 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002193 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002194 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002195 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
2196 pthread_mutex_unlock(&swm->lock);
2197
2198 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002199 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02002200 } else {
2201 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002202 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002203 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002204 /* get exclusive access to sessions_list (conns) */
Tomas Cejka866f2282014-09-18 15:20:26 +02002205 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002206 ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02002207 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002208}
2209
2210
2211/**
Radek Krejcif23850c2012-07-23 16:14:17 +02002212 * This is actually implementation of NETCONF client
2213 * - requests are received from UNIX socket in the predefined format
2214 * - results are replied through the same way
2215 * - the daemon run as a separate process, but it is started and stopped
2216 * automatically by Apache.
2217 *
2218 */
Radek Krejci469aab82012-07-22 18:42:20 +02002219static void forked_proc(apr_pool_t * pool, server_rec * server)
2220{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002221 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002222 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002223 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002224 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002225 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002226 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002227 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002228 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002229 struct timespec maxtime;
2230 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002231 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002232 #ifdef WITH_NOTIFICATIONS
2233 char use_notifications = 0;
2234 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002235
Tomas Cejka6b886e02013-07-05 09:53:17 +02002236 http_server = server;
2237
Tomas Cejka404d37e2013-04-13 02:31:35 +02002238 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002239 maxtime.tv_sec = 5;
2240 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002241
Tomas Cejka04e08f42014-03-27 19:52:34 +01002242#ifdef HAVE_UNIXD_SETUP_CHILD
Radek Krejcif23850c2012-07-23 16:14:17 +02002243 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002244 unixd_setup_child();
Tomas Cejka04e08f42014-03-27 19:52:34 +01002245#else
2246# ifdef SU_GROUP
2247 if (strlen(SU_GROUP) > 0) {
2248 struct group *g = getgrnam(SU_GROUP);
2249 if (g == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002250 ERROR("GID (%s) was not found.", SU_GROUP);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002251 return;
2252 }
2253 if (setgid(g->gr_gid) != 0) {
2254
Tomas Cejkacf44e522015-04-24 17:29:21 +02002255 ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002256 return;
2257 }
2258 }
2259# else
2260 DEBUG("no SU_GROUP");
2261# endif
2262# ifdef SU_USER
2263 if (strlen(SU_USER) > 0) {
2264 struct passwd *p = getpwnam(SU_USER);
2265 if (p == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002266 ERROR("UID (%s) was not found.", SU_USER);
Tomas Cejka04e08f42014-03-27 19:52:34 +01002267 return;
2268 }
2269 if (setuid(p->pw_uid) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002270 ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002271 return;
2272 }
2273 }
2274# else
2275 DEBUG("no SU_USER");
2276# endif
2277#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002278
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002279 if (server != NULL) {
2280 cfg = ap_get_module_config(server->module_config, &netconf_module);
2281 if (cfg == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002282 ERROR("Getting mod_netconf configuration failed");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002283 return;
2284 }
2285 sockname = cfg->sockname;
2286 } else {
2287 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002288 }
Radek Krejci469aab82012-07-22 18:42:20 +02002289
Tomas Cejka04e08f42014-03-27 19:52:34 +01002290 /* try to remove if exists */
2291 unlink(sockname);
2292
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002293 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002294 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002295 ERROR("Creating socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002296 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002297 }
2298
2299 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002300 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002301 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2302
2303 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2304 if (errno == EADDRINUSE) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002305 ERROR("mod_netconf socket address already in use");
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002306 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002307 }
Tomas Cejkacf44e522015-04-24 17:29:21 +02002308 ERROR("Binding socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002309 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002310 }
2311
2312 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002313 ERROR("Setting up listen socket failed (%s)", strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002314 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002315 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002316 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002317
Tomas Cejka04e08f42014-03-27 19:52:34 +01002318 uid_t user = -1;
2319 if (strlen(CHOWN_USER) > 0) {
2320 struct passwd *p = getpwnam(CHOWN_USER);
2321 if (p != NULL) {
2322 user = p->pw_uid;
2323 }
2324 }
2325 gid_t group = -1;
2326 if (strlen(CHOWN_GROUP) > 0) {
2327 struct group *g = getgrnam(CHOWN_GROUP);
2328 if (g != NULL) {
2329 group = g->gr_gid;
2330 }
2331 }
2332 if (chown(sockname, user, group) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002333 ERROR("Chown on socket file failed (%s).", strerror(errno));
Tomas Cejka04e08f42014-03-27 19:52:34 +01002334 }
2335
Tomas Cejkaba21b382013-04-13 02:37:32 +02002336 /* prepare internal lists */
2337 netconf_sessions_list = apr_hash_make(pool);
2338
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002339 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002340 if (notification_init(pool, server) == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002341 ERROR("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002342 use_notifications = 0;
2343 } else {
2344 use_notifications = 1;
2345 }
2346 #endif
2347
Radek Krejci469aab82012-07-22 18:42:20 +02002348 /* setup libnetconf's callbacks */
2349 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002350 nc_callback_print(clb_print);
2351 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2352 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2353 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002354 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002355
2356 /* disable publickey authentication */
2357 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2358
David Kupka8e60a372012-09-04 09:15:20 +02002359 /* create mutex protecting session list */
2360 pthread_rwlockattr_init(&lock_attrs);
2361 /* rwlock is shared only with threads in this process */
2362 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2363 /* create rw lock */
2364 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002365 ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002366 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002367 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002368 pthread_mutex_init(&ntf_history_lock, NULL);
Tomas Cejka9a23f6e2014-03-27 14:57:00 +01002369 pthread_mutex_init(&json_lock, NULL);
Tomas Cejkacf44e522015-04-24 17:29:21 +02002370 DEBUG("Initialization of notification history.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002371 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002372 ERROR("Initialization of notification history failed.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002373 }
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002374 if (pthread_key_create(&err_reply_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002375 ERROR("Initialization of reply key failed.");
Tomas Cejkaedb3ab42014-03-27 15:04:00 +01002376 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002377
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002378 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002379 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002380 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002381 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002382 #ifdef WITH_NOTIFICATIONS
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002383 if (use_notifications == 1) {
2384 notification_handle();
2385 }
2386 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002387 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002388 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002389 }
Radek Krejci469aab82012-07-22 18:42:20 +02002390
2391 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002392 len = sizeof(remote);
2393 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002394 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2395 apr_sleep(SLEEP_TIME);
2396 continue;
2397 } else if (client == -1 && (errno == EINTR)) {
2398 continue;
2399 } else if (client == -1) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002400 ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002401 continue;
2402 }
Radek Krejci469aab82012-07-22 18:42:20 +02002403
2404 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002405 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002406
David Kupka8e60a372012-09-04 09:15:20 +02002407 arg = malloc (sizeof(struct pass_to_thread));
2408 arg->client = client;
2409 arg->pool = pool;
2410 arg->server = server;
2411 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002412
David Kupka8e60a372012-09-04 09:15:20 +02002413 /* start new thread. It will serve this particular request and then terminate */
2414 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002415 ERROR("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002416 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002417 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002418 pthread_count++;
2419 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2420 ptids[pthread_count] = 0;
2421 }
Radek Krejci469aab82012-07-22 18:42:20 +02002422
David Kupka8e60a372012-09-04 09:15:20 +02002423 /* check if some thread already terminated, free some resources by joining it */
2424 for (i=0; i<pthread_count; i++) {
2425 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002426 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002427 pthread_count--;
2428 if (pthread_count > 0) {
2429 /* place last Thread ID on the place of joined one */
2430 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002431 }
Radek Krejci469aab82012-07-22 18:42:20 +02002432 }
2433 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002434 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002435 }
2436
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002437 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002438 /* join all threads */
2439 for (i=0; i<pthread_count; i++) {
2440 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2441 }
Radek Krejci469aab82012-07-22 18:42:20 +02002442
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002443 #ifdef WITH_NOTIFICATIONS
2444 notification_close();
2445 #endif
2446
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002447 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002448 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002449
David Kupka8e60a372012-09-04 09:15:20 +02002450 /* destroy rwlock */
2451 pthread_rwlock_destroy(&session_lock);
2452 pthread_rwlockattr_destroy(&lock_attrs);
2453
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002454 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002455
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002456 free(ptids);
2457 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002458 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002459 return;
2460error_exit:
2461 close(lsock);
2462 free(ptids);
2463 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002464}
2465
Radek Krejcif23850c2012-07-23 16:14:17 +02002466static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002467{
Radek Krejcif23850c2012-07-23 16:14:17 +02002468 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2469 apr_pool_create(&config->pool, pool);
2470 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002471 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002472
2473 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002474}
2475
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002476#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002477static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2478 apr_pool_t * plog, server_rec * s)
2479{
Radek Krejcif23850c2012-07-23 16:14:17 +02002480 mod_netconf_cfg *config;
2481 apr_status_t res;
2482
Radek Krejci469aab82012-07-22 18:42:20 +02002483 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002484 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002485 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002486
2487 /*
2488 * The following checks if this routine has been called before.
2489 * This is necessary because the parent process gets initialized
2490 * a couple of times as the server starts up.
2491 */
2492 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2493 if (!data) {
2494 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2495 return (OK);
2496 }
2497
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002498 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002499 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002500
Radek Krejcif23850c2012-07-23 16:14:17 +02002501 if (config && config->forkproc == NULL) {
2502 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2503 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002504 switch (res) {
2505 case APR_INCHILD:
2506 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002507 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002508 apr_signal(SIGTERM, signal_handler);
2509
2510 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002511 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002512
2513 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002514 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002515
Radek Krejcif23850c2012-07-23 16:14:17 +02002516 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002517 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002518 exit(APR_EGENERAL);
2519 break;
2520 case APR_INPARENT:
2521 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002522 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002523 break;
2524 default:
Tomas Cejkacf44e522015-04-24 17:29:21 +02002525 ERROR("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002526 break;
2527 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002528 } else {
Tomas Cejkacf44e522015-04-24 17:29:21 +02002529 ERROR("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002530 }
2531
2532 return OK;
2533}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002534#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002535
Radek Krejci469aab82012-07-22 18:42:20 +02002536/**
2537 * Register module hooks
2538 */
2539static void mod_netconf_register_hooks(apr_pool_t * p)
2540{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002541#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002542 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002543#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002544}
2545
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002546static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2547{
2548 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2549 return NULL;
2550}
2551
2552static const command_rec netconf_cmds[] = {
2553 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2554 {NULL}
2555};
2556
Radek Krejci469aab82012-07-22 18:42:20 +02002557/* Dispatch list for API hooks */
2558module AP_MODULE_DECLARE_DATA netconf_module = {
2559 STANDARD20_MODULE_STUFF,
2560 NULL, /* create per-dir config structures */
2561 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002562 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002563 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002564 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002565 mod_netconf_register_hooks /* register hooks */
2566};
Radek Krejcia332b692012-11-12 16:15:54 +01002567
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002568int main(int argc, char **argv)
2569{
2570 apr_pool_t *pool;
2571 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2572 apr_signal(SIGTERM, signal_handler);
2573 apr_signal(SIGINT, signal_handler);
2574 apr_pool_create(&pool, NULL);
2575 forked_proc(pool, NULL);
2576 apr_pool_destroy(pool);
2577 apr_terminate();
2578 DEBUG("Terminated");
2579 return 0;
2580}