blob: d4eeb9cd44a3bbf0152cff82180ac9b04f28c546 [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>
David Kupka8e60a372012-09-04 09:15:20 +020053#include <pthread.h>
54#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020055
56#include <unixd.h>
57#include <httpd.h>
58#include <http_log.h>
59#include <http_config.h>
60
61#include <apr_sha1.h>
62#include <apr_hash.h>
63#include <apr_signal.h>
64#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020065
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020066#include <json/json.h>
67
Radek Krejci469aab82012-07-22 18:42:20 +020068#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020069#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020070
Tomas Cejkad340dbf2013-03-24 20:36:57 +010071#ifdef WITH_NOTIFICATIONS
72#include "notification_module.h"
73#endif
74
Tomas Cejka94da2c52013-01-08 18:20:30 +010075#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020076#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020077
78#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020079#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020080#define MAX_SOCKET_CL 10
81#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020082#define NOTIFICATION_QUEUE_SIZE 10
83#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020084#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020085
86/* sleep in master process for non-blocking socket reading */
87#define SLEEP_TIME 200
88
89#ifndef offsetof
90#define offsetof(type, member) ((size_t) ((type *) 0)->member)
91#endif
92
Tomas Cejkaef531ee2013-11-12 16:07:00 +010093server_rec *http_server = NULL;
94
Tomas Cejka027f3bc2012-11-10 20:28:36 +010095/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020096struct timeval timeout = { 1, 0 };
97
Tomas Cejka5064c232013-01-17 09:30:58 +010098#define NCWITHDEFAULTS NCWD_MODE_NOTSET
99
100
Radek Krejci469aab82012-07-22 18:42:20 +0200101#define MSG_OK 0
102#define MSG_OPEN 1
103#define MSG_DATA 2
104#define MSG_CLOSE 3
105#define MSG_ERROR 4
106#define MSG_UNKNOWN 5
107
Radek Krejci469aab82012-07-22 18:42:20 +0200108module AP_MODULE_DECLARE_DATA netconf_module;
109
Tomas Cejka47387fd2013-06-10 20:37:46 +0200110pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200111pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100112pthread_mutex_t ntf_hist_clbc_mutex; /**< mutex protecting notification history list */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200113apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200114
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200115static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200116
Radek Krejci469aab82012-07-22 18:42:20 +0200117volatile int isterminated = 0;
118
119static char* password;
120
Radek Krejci469aab82012-07-22 18:42:20 +0200121static void signal_handler(int sign)
122{
123 switch (sign) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100124 case SIGINT:
Radek Krejci469aab82012-07-22 18:42:20 +0200125 case SIGTERM:
126 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200127 break;
128 }
129}
130
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200131static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200132{
Radek Krejcif23850c2012-07-23 16:14:17 +0200133 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
134 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200135 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200136
Radek Krejci469aab82012-07-22 18:42:20 +0200137 apr_sha1_ctx_t sha1_ctx;
138 apr_sha1_init(&sha1_ctx);
139 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
140 apr_sha1_update(&sha1_ctx, port, strlen(port));
141 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200143
Radek Krejcif23850c2012-07-23 16:14:17 +0200144 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200146 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200147 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200148 }
149 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200150
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200151 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200152}
153
154int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
155{
156 /* always approve */
157 return (EXIT_SUCCESS);
158}
159
160char* netconf_callback_sshauth_password (const char* username, const char* hostname)
161{
162 char* buf;
163
164 buf = malloc ((strlen(password) + 1) * sizeof(char));
165 apr_cpystrn(buf, password, strlen(password) + 1);
166
167 return (buf);
168}
169
170void netconf_callback_sshauth_interactive (const char* name,
171 int name_len,
172 const char* instruction,
173 int instruction_len,
174 int num_prompts,
175 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
176 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
177 void** abstract)
178{
179 int i;
180
181 for (i=0; i<num_prompts; i++) {
182 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
183 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
184 responses[i].length = strlen(responses[i].text) + 1;
185 }
186
187 return;
188}
189
Radek Krejcic11fd862012-07-26 12:41:21 +0200190static json_object *err_reply = NULL;
191void netconf_callback_error_process(const char* tag,
192 const char* type,
193 const char* severity,
194 const char* apptag,
195 const char* path,
196 const char* message,
197 const char* attribute,
198 const char* element,
199 const char* ns,
200 const char* sid)
201{
Tomas Cejka0858dbb2013-11-19 15:11:00 +0100202 json_object *array = NULL;
203 if (err_reply == NULL) {
204 err_reply = json_object_new_object();
205 array = json_object_new_array();
206 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
207 json_object_object_add(err_reply, "errors", array);
208 if (message != NULL) {
209 json_object_array_add(array, json_object_new_string(message));
210 }
211 return;
212 } else {
213 array = json_object_object_get(err_reply, "errors");
214 if (array != NULL) {
215 if (message != NULL) {
216 json_object_array_add(array, json_object_new_string(message));
217 }
218 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100219 }
Radek Krejcic11fd862012-07-26 12:41:21 +0200220}
221
Tomas Cejka47387fd2013-06-10 20:37:46 +0200222/**
223 * should be used in locked area
224 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100225void prepare_status_message(struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200226{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200227 json_object *json_obj = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100228 char *old_sid = NULL;
229 const char *j_old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200230 const char *cpbltstr;
231 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200232
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200233 if (s == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100234 DEBUG("No session given.");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200235 return;
236 }
237
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200238 if (s->hello_message != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100239 DEBUG("clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200240 //json_object_put(s->hello_message);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100241 j_old_sid = json_object_get_string(json_object_object_get(s->hello_message, "sid"));
242 if (j_old_sid != NULL) {
243 old_sid = strdup(j_old_sid);
244 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100245 json_object_put(s->hello_message);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200246 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200247 }
Tomas Cejkadd5cb452014-03-26 15:22:00 +0100248 s->hello_message = json_object_get(json_object_new_object());
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200249 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100250 if (old_sid != NULL) {
251 /* use previous sid */
252 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
253 free(old_sid);
254 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200255 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100256 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
257 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200258 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
259 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
260 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
261 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
262 cpblts = nc_session_get_cpblts (session);
263 if (cpblts != NULL) {
264 json_obj = json_object_new_array();
265 nc_cpblts_iter_start (cpblts);
266 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
267 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
268 }
269 json_object_object_add(s->hello_message, "capabilities", json_obj);
270 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100271 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200272 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100273 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200274 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
275 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
276 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100277 DEBUG("Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200278
279}
280
281
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200282/**
283 * \defgroup netconf_operations NETCONF operations
284 * The list of NETCONF operations that mod_netconf supports.
285 * @{
286 */
287
288/**
289 * \brief Connect to NETCONF server
290 *
291 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
292 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100293static 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 +0200294{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200295 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200296 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200297 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200298
299 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200300 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100301 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200302 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200303 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100304 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200305
Radek Krejci469aab82012-07-22 18:42:20 +0200306 /* if connected successful, add session to the list */
307 if (session != NULL) {
308 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200309 session_key = gen_ncsession_hash(
310 (host==NULL) ? "localhost" : host,
311 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200312 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200313
Tomas Cejkaba21b382013-04-13 02:37:32 +0200314 /** \todo allocate from apr_pool */
Tomas Cejka73496bf2014-03-26 15:31:09 +0100315 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 +0200316 nc_session_free(session);
Tomas Cejka73496bf2014-03-26 15:31:09 +0100317 session = NULL;
318 free(locked_session);
319 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100320 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200321 return NULL;
322 }
323 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200324 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200325 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200326 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200327 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100328 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200329 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100330 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200331 if (pthread_rwlock_wrlock (&session_lock) != 0) {
332 nc_session_free(session);
333 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100334 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200335 return NULL;
336 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200337 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200338 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100339 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200340 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 +0100341 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200342
Tomas Cejka47387fd2013-06-10 20:37:46 +0200343 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100344 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200345 pthread_mutex_lock(&locked_session->lock);
346
347 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100348 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200349 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100350 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200351 }
352
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200353 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100354 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200355
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100356 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200357 pthread_mutex_unlock(&locked_session->lock);
358
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100359 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200360 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200361 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100362 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200363 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200364 }
365
Radek Krejci469aab82012-07-22 18:42:20 +0200366}
367
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100368static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200369{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100370 DEBUG("lock private lock.");
371 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200372 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100373 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200374 }
375 locked_session->ntfc_subscribed = 0;
376 locked_session->closed = 1;
Tomas Cejka73496bf2014-03-26 15:31:09 +0100377 if (locked_session->session != NULL) {
378 nc_session_free(locked_session->session);
379 locked_session->session = NULL;
380 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100381 DEBUG("session closed.");
382 DEBUG("unlock private lock.");
383 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200384 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100385 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200386 }
387
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100388 DEBUG("unlock session lock.");
389 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200390 usleep(500000); /* let notification thread stop */
391
392 /* session shouldn't be used by now */
393 /** \todo free all notifications from queue */
394 apr_array_clear(locked_session->notifications);
395 pthread_mutex_destroy(&locked_session->lock);
396 if (locked_session->hello_message != NULL) {
397 /** \todo free hello_message */
398 //json_object_put(locked_session->hello_message);
399 locked_session->hello_message = NULL;
400 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200401 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200402 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200403 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100404 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200405 return (EXIT_SUCCESS);
406}
407
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100408static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200409{
David Kupka8e60a372012-09-04 09:15:20 +0200410 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200411
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100412 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200413
David Kupka8e60a372012-09-04 09:15:20 +0200414 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100415 DEBUG("lock session lock.");
416 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200417 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100418 DEBUG("Error while locking rwlock");
419 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200420 return EXIT_FAILURE;
421 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200422 /* get session to close */
423 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
424 /* remove session from the active sessions list -> nobody new can now work with session */
425 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200426
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100427 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200428 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100429 DEBUG("Error while unlocking rwlock");
430 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200431 }
432
433 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100434 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200435 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100436 DEBUG("Unknown session to close");
437 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200438 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200439 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100440 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200441}
442
Tomas Cejkac7929632013-10-24 19:25:15 +0200443/**
444 * Test reply message type and return error message.
445 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200446 * \param[in] session nc_session internal struct
447 * \param[in] session_key session key, NULL to disable disconnect on error
448 * \param[in] msgt RPC-REPLY message type
449 * \param[out] data
450 * \return NULL on success
451 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100452json_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 +0200453{
454 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100455 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200456
457 /* process the result of the operation */
458 switch (msgt) {
459 case NC_MSG_UNKNOWN:
460 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100461 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200462 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100463 netconf_close(session_key, &err);
464 }
465 if (err != NULL) {
466 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200467 }
468 return create_error("Internal: Receiving RPC-REPLY failed.");
469 }
470 case NC_MSG_NONE:
471 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100472 if (data != NULL) {
473 free(*data);
474 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200475 (*data) = NULL;
476 return NULL;
477 case NC_MSG_REPLY:
478 switch (replyt = nc_reply_get_type(reply)) {
479 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100480 if ((data != NULL) && (*data != NULL)) {
481 free(*data);
482 (*data) = NULL;
483 }
484 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200485 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100486 if (((*data) = nc_reply_get_data(reply)) == NULL) {
487 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200488 return create_error("Internal: No data from reply received.");
489 } else {
490 return NULL;
491 }
492 break;
493 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100494 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
495 if (data != NULL) {
496 free(*data);
497 (*data) = NULL;
498 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200499 return create_error(nc_reply_get_errormsg(reply));
500 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100501 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
502 if (data != NULL) {
503 free(*data);
504 (*data) = NULL;
505 }
Tomas Cejka60885252014-03-26 15:45:47 +0100506 return create_error("Unknown type of NETCONF reply.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200507 }
508 break;
509 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100510 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
511 if (data != NULL) {
512 free(*data);
513 (*data) = NULL;
514 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200515 return create_error("Internal: Unexpected RPC-REPLY message type.");
516 }
517}
518
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100519json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200520{
521 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200522 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200523
524 /* check requests */
525 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100526 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200527 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200528 }
529
530 if (session != NULL) {
531 /* send the request and get the reply */
532 msgt = nc_session_send_recv(session, rpc, &reply);
533 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100534 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200535 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100536 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200537 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200538 }
539}
540
Tomas Cejkac7929632013-10-24 19:25:15 +0200541/**
542 * Perform RPC method that returns data.
543 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200544 * \param[in] session_key session identifier
545 * \param[in] rpc RPC message to perform
546 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
547 * \return NULL on success, json object with error otherwise
548 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100549static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200550{
551 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200552 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200553 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200554 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100555 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100556 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200557
Radek Krejci8e4632a2012-07-26 13:40:34 +0200558 /* check requests */
559 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100560 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200561 res = create_error("Internal: RPC could not be created.");
562 data = NULL;
563 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200564 }
565
David Kupka8e60a372012-09-04 09:15:20 +0200566 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100567 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200568 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100569 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200570 res = create_error("Internal: Lock failed.");
571 data = NULL;
572 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200573 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200574 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200575 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 +0200576 if (locked_session != NULL) {
577 session = locked_session->session;
578 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200579 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200580 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100581 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200582 if (pthread_mutex_lock(&locked_session->lock) != 0) {
583 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100584 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200585 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200586
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100587 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200588 res = create_error("Internal: Could not unlock.");
589 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200590 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200591 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200592 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100593 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200594 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200595
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100596 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200597 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200598 }
599
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200600 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200601
Radek Krejci035bf4e2012-07-25 10:59:09 +0200602 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100603 msgt = nc_session_send_recv(session, rpc, &reply);
604
David Kupka8e60a372012-09-04 09:15:20 +0200605 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100606 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200607 pthread_mutex_unlock(&locked_session->lock);
608 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200609
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100610 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200611 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100612 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100613 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100614 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100615 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100616 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100617 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200618 res = create_error("Unknown session to process.");
619 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200620 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200621finished:
622 nc_reply_free(reply);
623 if (received_data != NULL) {
624 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100625 } else {
626 if (data != NULL) {
627 free(data);
628 data = NULL;
629 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200630 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200631 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200632}
633
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100634static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200635{
636 nc_rpc* rpc;
637 struct nc_filter *f = NULL;
638 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200639 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200640
641 /* create filter if set */
642 if (filter != NULL) {
643 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
644 }
645
646 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100647 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200648 nc_filter_free(f);
649 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100650 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200651 return (NULL);
652 }
653
Tomas Cejka94674662013-09-13 15:55:24 +0200654 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100655 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200657 }
658
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100659 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200660 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100661 if (res != NULL) {
662 (*err) = res;
663 } else {
664 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200665 }
666
Radek Krejci8e4632a2012-07-26 13:40:34 +0200667 return (data);
668}
669
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100670static 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 +0100671{
672 nc_rpc* rpc;
673 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200674 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100675
676 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100677 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100678 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100679 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100680 return (NULL);
681 }
682
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100683 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100684 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100685 if (res != NULL) {
686 (*err) = res;
687 } else {
688 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200689 }
690
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100691 return (data);
692}
693
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100694static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200695{
696 nc_rpc* rpc;
697 struct nc_filter *f = NULL;
698 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200699 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200700
701 /* create filter if set */
702 if (filter != NULL) {
703 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
704 }
705
706 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100707 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200708 nc_filter_free(f);
709 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100710 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200711 return (NULL);
712 }
713
Tomas Cejka94674662013-09-13 15:55:24 +0200714 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100715 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100716 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200717 }
718
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100719 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200720 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200721 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100722 (*err) = res;
723 } else {
724 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200725 }
726
Radek Krejci8e4632a2012-07-26 13:40:34 +0200727 return (data);
728}
729
Tomas Cejkab4d05872014-02-14 22:44:38 +0100730static 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 +0200731{
732 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200733 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200734
735 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100736 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100737 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100738 /* config, url */
739 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100740 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100741 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100742 rpc = nc_rpc_copyconfig(source, target, config);
743 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100744 } else if (source == NC_DATASTORE_URL) {
745 if (target == NC_DATASTORE_URL) {
746 /* url, url */
747 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
748 } else {
749 /* url, datastore */
750 rpc = nc_rpc_copyconfig(source, target, uri_src);
751 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100752 } else {
753 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100754 /* datastore, url */
755 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100756 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100757 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100758 rpc = nc_rpc_copyconfig(source, target);
759 }
760 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200761 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100762 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200763 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200764 }
765
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100766 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200767 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200768
769 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200770}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200771
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100772static 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 +0200773{
774 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200775 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200776
777 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100778 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200779 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100780 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200781 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200782 }
783
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100784 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200785 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200786
787 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200788}
789
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100790static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200791{
792 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200793 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200794
795 /* create requests */
796 rpc = nc_rpc_killsession(sid);
797 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100798 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200799 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200800 }
801
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100802 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200803 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200804 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200805}
806
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100807static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200808{
809 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200810 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200811
812 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200813 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200814 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100815 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200816 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200817 }
818
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100819 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200820 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200821 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200822}
823
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100824static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200825{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200826 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200827 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200828 if (target != NC_DATASTORE_URL) {
829 rpc = nc_rpc_deleteconfig(target);
830 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200831 rpc = nc_rpc_deleteconfig(target, url);
832 }
833 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100834 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200835 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200836 }
837
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100838 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200839 nc_rpc_free (rpc);
840 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200841}
842
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100843static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200844{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100845 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200846}
847
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100848static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200849{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100850 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200851}
852
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100853static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200854{
Tomas Cejka00635972013-06-03 15:10:52 +0200855 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200856 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200857
858 /* create requests */
859 rpc = nc_rpc_generic(content);
860 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100861 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200862 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200863 }
864
Radek Krejcia332b692012-11-12 16:15:54 +0100865 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200866 // TODO ?free(*data);
867 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100868 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200869
870 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100871 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200872 nc_rpc_free (rpc);
873 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200874}
875
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200876/**
877 * @}
878 *//* netconf_operations */
879
Radek Krejci7338bde2012-08-10 12:57:30 +0200880void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200881{
Radek Krejci7338bde2012-08-10 12:57:30 +0200882 switch (level) {
883 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100884 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200885 break;
886 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100887 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200888 break;
889 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100890 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200891 break;
892 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100893 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200894 break;
895 }
Radek Krejci469aab82012-07-22 18:42:20 +0200896}
897
Tomas Cejka64b87482013-06-03 16:30:53 +0200898/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200899 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200900 * Caller should free message memory.
901 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200902 * \return pointer to message
903 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100904char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200905{
906 /* read json in chunked framing */
907 unsigned int buffer_size = 0;
908 ssize_t buffer_len = 0;
909 char *buffer = NULL;
910 char c;
911 ssize_t ret;
912 int i, chunk_len;
913 char chunk_len_str[12];
914
915 while (1) {
916 /* read chunk length */
917 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
918 if (buffer != NULL) {
919 free (buffer);
920 buffer = NULL;
921 }
922 break;
923 }
924 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
925 if (buffer != NULL) {
926 free (buffer);
927 buffer = NULL;
928 }
929 break;
930 }
931 i=0;
932 memset (chunk_len_str, 0, 12);
933 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
934 if (i==0 && c == '#') {
935 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
936 /* end but invalid */
937 if (buffer != NULL) {
938 free (buffer);
939 buffer = NULL;
940 }
941 }
942 /* end of message, double-loop break */
943 goto msg_complete;
944 }
945 chunk_len_str[i++] = c;
946 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100947 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +0200948 break;
949 }
950 }
951 if (c != '\n') {
952 if (buffer != NULL) {
953 free (buffer);
954 buffer = NULL;
955 }
956 break;
957 }
958 chunk_len_str[i] = 0;
959 if ((chunk_len = atoi (chunk_len_str)) == 0) {
960 if (buffer != NULL) {
961 free (buffer);
962 buffer = NULL;
963 }
964 break;
965 }
966 buffer_size += chunk_len+1;
967 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100968 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +0200969 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
970 if (buffer != NULL) {
971 free (buffer);
972 buffer = NULL;
973 }
974 break;
975 }
976 buffer_len += ret;
977 }
978msg_complete:
979 return buffer;
980}
981
Tomas Cejkad5b53772013-06-08 23:01:07 +0200982NC_DATASTORE parse_datastore(const char *ds)
983{
984 if (strcmp(ds, "running") == 0) {
985 return NC_DATASTORE_RUNNING;
986 } else if (strcmp(ds, "startup") == 0) {
987 return NC_DATASTORE_STARTUP;
988 } else if (strcmp(ds, "candidate") == 0) {
989 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +0200990 } else if (strcmp(ds, "url") == 0) {
991 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100992 } else if (strcmp(ds, "config") == 0) {
993 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +0200994 }
995 return -1;
996}
997
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +0100998NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
999{
1000 if (strcmp(t, "notset") == 0) {
1001 return NC_EDIT_TESTOPT_NOTSET;
1002 } else if (strcmp(t, "testset") == 0) {
1003 return NC_EDIT_TESTOPT_TESTSET;
1004 } else if (strcmp(t, "set") == 0) {
1005 return NC_EDIT_TESTOPT_SET;
1006 } else if (strcmp(t, "test") == 0) {
1007 return NC_EDIT_TESTOPT_TEST;
1008 }
1009 return NC_EDIT_TESTOPT_ERROR;
1010}
1011
Tomas Cejkad5b53772013-06-08 23:01:07 +02001012json_object *create_error(const char *errmess)
1013{
1014 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001015 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001016 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001017 json_object_array_add(array, json_object_new_string(errmess));
1018 json_object_object_add(reply, "errors", array);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001019 return reply;
1020
1021}
1022
1023json_object *create_data(const char *data)
1024{
1025 json_object *reply = json_object_new_object();
1026 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1027 json_object_object_add(reply, "data", json_object_new_string(data));
1028 return reply;
1029}
1030
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001031json_object *create_ok()
1032{
1033 json_object *reply = json_object_new_object();
1034 reply = json_object_new_object();
1035 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1036 return reply;
1037}
1038
1039json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001040{
1041 const char *host = NULL;
1042 const char *port = NULL;
1043 const char *user = NULL;
1044 const char *pass = NULL;
1045 json_object *capabilities = NULL;
1046 json_object *reply = NULL;
1047 char *session_key_hash = NULL;
1048 struct nc_cpblts* cpblts = NULL;
1049 unsigned int len, i;
1050
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001051 DEBUG("Request: Connect");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001052 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1053 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1054 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1055 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1056
1057 capabilities = json_object_object_get((json_object *) request, "capabilities");
1058 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1059 cpblts = nc_cpblts_new(NULL);
1060 for (i=0; i<len; i++) {
1061 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1062 }
1063 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001064 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001065 }
1066
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001067 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001068 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001069 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001070 session_key_hash = NULL;
1071 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001072 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1073 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001074 }
1075 if (cpblts != NULL) {
1076 nc_cpblts_free(cpblts);
1077 }
1078
1079 if (session_key_hash == NULL) {
1080 /* negative reply */
1081 if (err_reply == NULL) {
1082 reply = json_object_new_object();
1083 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1084 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001085 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001086 } else {
1087 /* use filled err_reply from libnetconf's callback */
1088 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001089 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001090 }
1091 } else {
1092 /* positive reply */
1093 reply = json_object_new_object();
1094 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1095 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1096
1097 free(session_key_hash);
1098 }
1099 return reply;
1100}
1101
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001102json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001103{
1104 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001105 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001106 json_object *reply = NULL;
1107
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001108 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001109
1110 filter = json_object_get_string(json_object_object_get(request, "filter"));
1111
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001112 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
1113 if (reply == NULL) {
1114 if (err_reply == NULL) {
1115 reply = create_error("Get information failed.");
1116 } else {
1117 /* use filled err_reply from libnetconf's callback */
1118 reply = err_reply;
1119 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001120 }
1121 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001122 reply = create_data(data);
1123 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001124 }
1125 return reply;
1126}
1127
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001128json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001129{
1130 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001131 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001132 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001133 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001134 json_object *reply = NULL;
1135
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001136 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001137
1138 filter = json_object_get_string(json_object_object_get(request, "filter"));
1139
Tomas Cejkad5b53772013-06-08 23:01:07 +02001140 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1141 ds_type_s = parse_datastore(source);
1142 }
1143 if (ds_type_s == -1) {
1144 return create_error("Invalid source repository type requested.");
1145 }
1146
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001147 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
1148 if (reply == NULL) {
1149 if (err_reply == NULL) {
1150 reply = create_error("Get configuration operation failed.");
1151 } else {
1152 /* use filled err_reply from libnetconf's callback */
1153 reply = err_reply;
1154 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001155 }
1156 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001157 reply = create_data(data);
1158 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001159 }
1160 return reply;
1161}
1162
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001163json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001164{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001165 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001166 const char *identifier = NULL;
1167 const char *version = NULL;
1168 const char *format = NULL;
1169 json_object *reply = NULL;
1170
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001171 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001172 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1173 if (identifier == NULL) {
1174 return create_error("No identifier for get-schema supplied.");
1175 }
1176 version = json_object_get_string(json_object_object_get(request, "version"));
1177 format = json_object_get_string(json_object_object_get(request, "format"));
1178
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001179 DEBUG("get-schema(version: %s, format: %s)", version, format);
1180 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
1181 if (reply == NULL) {
1182 if (err_reply == NULL) {
1183 reply = create_error("Get models operation failed.");
1184 } else {
1185 /* use filled err_reply from libnetconf's callback */
1186 reply = err_reply;
1187 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001188 }
1189 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001190 reply = create_data(data);
1191 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001192 }
1193 return reply;
1194}
1195
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001196json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001197{
1198 NC_DATASTORE ds_type_s = -1;
1199 NC_DATASTORE ds_type_t = -1;
1200 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1201 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001202 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001203 const char *defop = NULL;
1204 const char *erropt = NULL;
1205 const char *config = NULL;
1206 const char *source = NULL;
1207 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001208 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001209 json_object *reply = NULL;
1210
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001211 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001212
1213 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1214 if (defop != NULL) {
1215 if (strcmp(defop, "merge") == 0) {
1216 defop_type = NC_EDIT_DEFOP_MERGE;
1217 } else if (strcmp(defop, "replace") == 0) {
1218 defop_type = NC_EDIT_DEFOP_REPLACE;
1219 } else if (strcmp(defop, "none") == 0) {
1220 defop_type = NC_EDIT_DEFOP_NONE;
1221 } else {
1222 return create_error("Invalid default-operation parameter.");
1223 }
1224 } else {
1225 defop_type = NC_EDIT_DEFOP_NOTSET;
1226 }
1227
1228 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1229 if (erropt != NULL) {
1230 if (strcmp(erropt, "continue-on-error") == 0) {
1231 erropt_type = NC_EDIT_ERROPT_CONT;
1232 } else if (strcmp(erropt, "stop-on-error") == 0) {
1233 erropt_type = NC_EDIT_ERROPT_STOP;
1234 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1235 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1236 } else {
1237 return create_error("Invalid error-option parameter.");
1238 }
1239 } else {
1240 erropt_type = 0;
1241 }
1242
1243 /* get parameters */
1244 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1245 ds_type_t = parse_datastore(target);
1246 }
1247 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1248 ds_type_s = parse_datastore(source);
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001249 } else {
1250 /* source is optional, default value is config */
1251 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001252 }
1253 if (ds_type_t == -1) {
1254 return create_error("Invalid target repository type requested.");
1255 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001256 if (ds_type_s == NC_DATASTORE_CONFIG) {
1257 config = json_object_get_string(json_object_object_get(request, "config"));
1258 if (config == NULL) {
1259 return create_error("Invalid config data parameter.");
1260 }
1261 } else if (ds_type_s == NC_DATASTORE_URL){
1262 config = json_object_get_string(json_object_object_get(request, "uri-source"));
1263 if (config == NULL) {
1264 config = "";
1265 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001266 }
1267
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001268 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1269 if (testopt != NULL) {
1270 testopt_type = parse_testopt(testopt);
1271 } else {
1272 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1273 }
1274
1275 reply = netconf_editconfig(session_key, ds_type_s, ds_type_t, defop_type, erropt_type, testopt_type, config);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001276 if (reply == NULL) {
1277 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001278 /* use filled err_reply from libnetconf's callback */
1279 reply = err_reply;
1280 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001281 }
1282 return reply;
1283}
1284
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001285json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001286{
1287 NC_DATASTORE ds_type_s = -1;
1288 NC_DATASTORE ds_type_t = -1;
1289 const char *config = NULL;
1290 const char *target = NULL;
1291 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001292 const char *uri_src = NULL;
1293 const char *uri_trg = NULL;
1294
Tomas Cejkad5b53772013-06-08 23:01:07 +02001295 json_object *reply = NULL;
1296
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001297 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001298
1299 /* get parameters */
1300 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1301 ds_type_t = parse_datastore(target);
1302 }
1303 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1304 ds_type_s = parse_datastore(source);
1305 }
1306 if (source == NULL) {
1307 /* no explicit source specified -> use config data */
1308 ds_type_s = NC_DATASTORE_CONFIG;
1309 config = json_object_get_string(json_object_object_get(request, "config"));
1310 } else if (ds_type_s == -1) {
1311 /* source datastore specified, but it is invalid */
1312 return create_error("Invalid source repository type requested.");
1313 }
1314
1315 if (ds_type_t == -1) {
1316 /* invalid target datastore specified */
1317 return create_error("Invalid target repository type requested.");
1318 }
1319
Tomas Cejka55c6df52014-02-20 12:59:33 +01001320 /* source can be missing when config is given */
1321 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001322 reply = create_error("invalid input parameters - source and config is required.");
1323 return reply;
1324 }
1325
1326 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkaa70d3032014-02-14 23:20:03 +01001327 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
Tomas Cejkab4d05872014-02-14 22:44:38 +01001328 if (uri_src == NULL) {
1329 uri_src = "";
1330 }
1331 }
1332 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkaa70d3032014-02-14 23:20:03 +01001333 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
Tomas Cejkab4d05872014-02-14 22:44:38 +01001334 if (uri_trg == NULL) {
1335 uri_trg = "";
1336 }
1337 }
1338 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
1339 if (reply == NULL) {
1340 if (err_reply != NULL) {
1341 /* use filled err_reply from libnetconf's callback */
1342 reply = err_reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001343 }
1344 }
1345 return reply;
1346}
1347
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001348json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001349{
1350 json_object *reply = NULL;
1351 const char *config = NULL;
1352 char *data = NULL;
1353
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001354 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001355
1356 config = json_object_get_string(json_object_object_get(request, "content"));
1357
1358 if (config == NULL) {
1359 return create_error("Missing content parameter.");
1360 }
1361
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001362 /* TODO */
1363 reply = netconf_generic(session_key, config, &data);
1364 if (reply == NULL) {
1365 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001366 /* use filled err_reply from libnetconf's callback */
1367 reply = err_reply;
1368 }
1369 } else {
1370 if (data == NULL) {
1371 reply = json_object_new_object();
1372 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1373 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001374 reply = create_data(data);
1375 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001376 }
1377 }
1378 return reply;
1379}
1380
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001381json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001382{
1383 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001384 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001385
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001386 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
1387 if (reply == NULL) {
1388 if (err_reply == NULL) {
1389 reply = create_error("Get configuration information from device failed.");
1390 } else {
1391 /* use filled err_reply from libnetconf's callback */
1392 reply = err_reply;
1393 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001394 }
1395 } else {
1396 reply = json_object_new_object();
1397 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1398 }
1399 return reply;
1400}
1401
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001402json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001403{
1404 json_object *reply = NULL;
1405 const char *sid = NULL;
1406
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001407 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001408
1409 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1410
1411 if (sid == NULL) {
1412 return create_error("Missing session-id parameter.");
1413 }
1414
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001415 reply = netconf_killsession(session_key, sid);
1416 if (reply == NULL) {
1417 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001418 /* use filled err_reply from libnetconf's callback */
1419 reply = err_reply;
1420 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001421 }
1422 return reply;
1423}
1424
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001425json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001427 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001428 struct session_with_mutex * locked_session = NULL;
1429 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001430 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001431
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001432 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001433 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001434 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001435 return NULL;
1436 }
1437
Tomas Cejkad5b53772013-06-08 23:01:07 +02001438 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1439 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001440 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001441 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001442 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001443 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001444 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001445 prepare_status_message(locked_session, temp_session);
1446 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001447 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001448 temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001450 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001451 reply = create_error("Reload was unsuccessful, connection failed.");
1452 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001453 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001454 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka93b0e492014-03-26 15:47:45 +01001455 DEBUG("UNLOCK wrlock %s", __func__);
1456 if (pthread_rwlock_unlock(&session_lock) != 0) {
1457 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1458 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001459 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001460 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001461 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001462 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001463 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001464 reply = create_error("Invalid session identifier.");
1465 }
1466
1467 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1468 reply = locked_session->hello_message;
1469 }
1470 return reply;
1471}
1472
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001473json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001474{
1475 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001476 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001477 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001479 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001480 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001481 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001482 }
1483
Tomas Cejkad5b53772013-06-08 23:01:07 +02001484 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1485 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001486 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001487 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001488 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001489 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001490 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001491 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001492 if (locked_session->hello_message != NULL) {
1493 reply = locked_session->hello_message;
1494 } else {
1495 reply = create_error("Invalid session identifier.");
1496 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001497 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001498 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001499 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001500 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001501 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001502 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001503 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001504 reply = create_error("Invalid session identifier.");
1505 }
1506
Tomas Cejka47387fd2013-06-10 20:37:46 +02001507
Tomas Cejkad5b53772013-06-08 23:01:07 +02001508 return reply;
1509}
1510
Tomas Cejka6b886e02013-07-05 09:53:17 +02001511void notification_history(time_t eventtime, const char *content)
1512{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001513 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1514 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001516 return;
1517 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001518 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001519 json_object *notif = json_object_new_object();
1520 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001521 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001522 return;
1523 }
1524 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1525 json_object_object_add(notif, "content", json_object_new_string(content));
1526 json_object_array_add(notif_history_array, notif);
1527}
1528
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001529json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001530{
1531 json_object *reply = NULL;
1532 const char *sid = NULL;
1533 struct session_with_mutex *locked_session = NULL;
1534 struct nc_session *temp_session = NULL;
1535 nc_rpc *rpc = NULL;
1536 time_t start = 0;
1537 time_t stop = 0;
1538 int64_t from, to;
1539
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001540 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001541
1542 sid = json_object_get_string(json_object_object_get(request, "session"));
1543 from = json_object_get_int64(json_object_object_get(request, "from"));
1544 to = json_object_get_int64(json_object_object_get(request, "to"));
1545
1546 start = time(NULL) + from;
1547 stop = time(NULL) + to;
1548
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001549 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001550
1551 if (sid == NULL) {
1552 return create_error("Missing session parameter.");
1553 }
1554
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001555 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001556 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001557 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001558 return NULL;
1559 }
1560
1561 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1562 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001563 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001564 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001565 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001566 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001567 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001568 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001569 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001570 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1571 if (temp_session != NULL) {
1572 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1573 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001574 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001575 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001576 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001577 return create_error("notifications: creating an rpc request failed.");
1578 }
1579
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001580 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001581 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001582 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001583 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001584 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001585 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001586 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001587 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001588 }
1589 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1590
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001591 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001592 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001593 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001594 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001595 json_object *notif_history_array = json_object_new_array();
1596 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001597 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001598 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001599
1600 ncntf_dispatch_receive(temp_session, notification_history);
1601
1602 reply = json_object_new_object();
1603 json_object_object_add(reply, "notifications", notif_history_array);
1604 //json_object_put(notif_history_array);
1605
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001606 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001607 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001608 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001609 nc_session_free(temp_session);
Tomas Cejka73496bf2014-03-26 15:31:09 +01001610 temp_session = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001611 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001612 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001613 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001614 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001615 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1616 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001617 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001618 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001619 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001620 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001621 }
1622 reply = create_error("Invalid session identifier.");
1623 }
1624
Tomas Cejka4003a702013-10-01 00:02:45 +02001625 return reply;
1626}
1627
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001628json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001629{
1630 json_object *reply = NULL;
1631 const char *sid = NULL;
1632 const char *target = NULL;
1633 const char *url = NULL;
1634 struct session_with_mutex *locked_session = NULL;
1635 nc_rpc *rpc = NULL;
1636 NC_DATASTORE target_ds;
1637
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001638 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001639
1640 sid = json_object_get_string(json_object_object_get(request, "session"));
1641 target = json_object_get_string(json_object_object_get(request, "target"));
1642 url = json_object_get_string(json_object_object_get(request, "url"));
1643
1644
1645 if ((sid == NULL) || (target == NULL)) {
1646 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001647 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001648
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001649 /* validation */
1650 target_ds = parse_datastore(target);
1651 if (target_ds == NC_DATASTORE_URL) {
1652 if (url != NULL) {
1653 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001654 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001655 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001656 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001657 rpc = nc_rpc_validate(target_ds);
1658 }
1659 if (rpc == NULL) {
1660 DEBUG("mod_netconf: creating rpc request failed");
1661 reply = create_error("Creation of RPC request failed.");
1662 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001663 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001664 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001665 }
1666
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001667 DEBUG("Request: validate datastore");
1668 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001669 if (err_reply == NULL) {
1670 DEBUG("Request: validation ok.");
1671 reply = create_ok();
1672 } else {
1673 /* use filled err_reply from libnetconf's callback */
1674 reply = err_reply;
1675 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001676 }
1677 nc_rpc_free (rpc);
1678
Tomas Cejka6b886e02013-07-05 09:53:17 +02001679 return reply;
1680}
1681
David Kupka8e60a372012-09-04 09:15:20 +02001682void * thread_routine (void * arg)
1683{
1684 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001685 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001686 json_object *request = NULL;
1687 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001688 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001689 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001690 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001691 const char *session_key;
1692 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001693 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001694 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001695 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001696 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001697 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001698 int client = ((struct pass_to_thread*)arg)->client;
1699
Tomas Cejka00635972013-06-03 15:10:52 +02001700 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001701
1702 while (!isterminated) {
1703 fds.fd = client;
1704 fds.events = POLLIN;
1705 fds.revents = 0;
1706
1707 status = poll(&fds, 1, 1000);
1708
1709 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1710 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001711 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001712 continue;
1713 } else if (status < 0) {
1714 /* 0: poll time outed
1715 * close socket and ignore this request from the client, it can try it again
1716 * -1: poll failed
1717 * something wrong happend, close this socket and wait for another request
1718 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001719 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001720 close(client);
1721 break;
1722 }
1723 /* status > 0 */
1724
1725 /* check the status of the socket */
1726
1727 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1728 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1729 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001730 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001731 close(client);
1732 break;
1733 }
1734
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001735 DEBUG("Get framed message...");
1736 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001737
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001738 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001739 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001740 enum json_tokener_error jerr;
1741 request = json_tokener_parse_verbose(buffer, &jerr);
1742 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001743 DEBUG("JSON parsing error");
Tomas Cejka00635972013-06-03 15:10:52 +02001744 continue;
1745 }
David Kupka8e60a372012-09-04 09:15:20 +02001746 operation = json_object_get_int(json_object_object_get(request, "type"));
1747
Tomas Cejka64b87482013-06-03 16:30:53 +02001748 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001749 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001750 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1751 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001752 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001753 msgtext = json_object_to_json_string(reply);
1754 send(client, msgtext, strlen(msgtext) + 1, 0);
1755 json_object_put(reply);
1756 /* there is some stupid client, so close the connection to give a chance to some other client */
1757 close(client);
1758 break;
1759 }
1760
David Kupka8e60a372012-09-04 09:15:20 +02001761 /* null global JSON error-reply */
1762 err_reply = NULL;
1763
1764 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001765 if (reply != NULL) {
1766 json_object_put(reply);
1767 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001768 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001769
1770 /* process required operation */
1771 switch (operation) {
1772 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001773 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001774 break;
1775 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001776 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001777 break;
1778 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001779 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001780 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001781 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001782 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001783 break;
David Kupka8e60a372012-09-04 09:15:20 +02001784 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001785 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001786 break;
1787 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001788 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001789 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001790
David Kupka8e60a372012-09-04 09:15:20 +02001791 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001792 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001793 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001794 /* get parameters */
1795 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1796 ds_type_t = parse_datastore(target);
1797 }
David Kupka8e60a372012-09-04 09:15:20 +02001798
1799 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001800 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001801 break;
1802 }
David Kupka8e60a372012-09-04 09:15:20 +02001803 switch(operation) {
1804 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001805 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001806 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001807 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001808 break;
1809 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001810 DEBUG("Request: lock (session %s)", session_key);
1811 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001812 break;
1813 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001814 DEBUG("Request: unlock (session %s)", session_key);
1815 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001816 break;
1817 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001818 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001819 break;
1820 }
1821
Tomas Cejkac7929632013-10-24 19:25:15 +02001822 if (reply == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001823 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001824 /** \todo more clever error message wanted */
Tomas Cejkac7929632013-10-24 19:25:15 +02001825 reply = json_object_new_object();
1826 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
David Kupka8e60a372012-09-04 09:15:20 +02001827 } else {
1828 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001829 reply = err_reply;
1830 }
David Kupka8e60a372012-09-04 09:15:20 +02001831 }
1832 break;
1833 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001834 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001835 break;
1836 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001837 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001838 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001839 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001840 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001841 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001842 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001843 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001844 break;
1845 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001846 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001847 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001848 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001849 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001850 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001851 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001852 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001853 break;
David Kupka8e60a372012-09-04 09:15:20 +02001854 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001855 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001856 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001857 break;
1858 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001859 DEBUG("Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001860 json_object_put(request);
1861
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001862 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001863 /* send reply to caller */
1864 if (reply != NULL) {
1865 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001866 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001867 if (buffer != NULL) {
1868 free(buffer);
1869 buffer = NULL;
1870 }
David Kupka8e60a372012-09-04 09:15:20 +02001871 break;
1872 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001873 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001874 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001875 DEBUG("Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001876 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001877 reply = NULL;
1878 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001879 free(chunked_out_msg);
1880 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001881 if (buffer != NULL) {
1882 free(buffer);
1883 buffer = NULL;
1884 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001885 if (err_reply != NULL) {
1886 json_object_put(err_reply);
1887 err_reply = NULL;
1888 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001889 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001890 DEBUG("Reply is NULL, shouldn't be...");
1891 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001892 }
1893 }
1894 }
David Kupka8e60a372012-09-04 09:15:20 +02001895 free (arg);
1896
1897 return retval;
1898}
1899
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001900/**
1901 * \brief Close all open NETCONF sessions.
1902 *
1903 * During termination of mod_netconf, it is useful to close all remaining
1904 * sessions. This function iterates over the list of sessions and close them
1905 * all.
1906 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001907 * \param[in] p apr pool needed for hash table iterating
1908 * \param[in] ht hash table of session_with_mutex structs
1909 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001910static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001911{
1912 apr_hash_index_t *hi;
1913 void *val = NULL;
1914 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001915 const char *hashed_key = NULL;
1916 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001917 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001918
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001919 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001920 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001921 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001922 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001923 return;
1924 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001925 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001926 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001927 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001928 swm = (struct session_with_mutex *) val;
1929 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001930 DEBUG("LOCK mutex %s", __func__);
1931 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001932 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001933 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001934 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001935
1936 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001937 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001938 }
1939 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001940 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001941 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001942 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001943 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001944 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001945}
1946
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001947static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001948{
1949 apr_hash_index_t *hi;
1950 void *val = NULL;
1951 struct nc_session *ns = NULL;
1952 struct session_with_mutex *swm = NULL;
1953 const char *hashed_key = NULL;
1954 apr_ssize_t hashed_key_length;
1955 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001956 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001957
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001958 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001959//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001960 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001961 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001962 return;
1963 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001964 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001965 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1966 swm = (struct session_with_mutex *) val;
1967 if (swm == NULL) {
1968 continue;
1969 }
1970 ns = swm->session;
1971 if (ns == NULL) {
1972 continue;
1973 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001974//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001975 pthread_mutex_lock(&swm->lock);
1976 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001977 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001978 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001979 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001980//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001981 pthread_mutex_unlock(&swm->lock);
1982
1983 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001984 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001985 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001986//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001987 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001988 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001989 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001990 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001991//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001992 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001993 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001994 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001995}
1996
1997
1998/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001999 * This is actually implementation of NETCONF client
2000 * - requests are received from UNIX socket in the predefined format
2001 * - results are replied through the same way
2002 * - the daemon run as a separate process, but it is started and stopped
2003 * automatically by Apache.
2004 *
2005 */
Radek Krejci469aab82012-07-22 18:42:20 +02002006static void forked_proc(apr_pool_t * pool, server_rec * server)
2007{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002008 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002009 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002010 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002011 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002012 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002013 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002014 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002015 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002016 struct timespec maxtime;
2017 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002018 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002019 #ifdef WITH_NOTIFICATIONS
2020 char use_notifications = 0;
2021 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002022
Tomas Cejka6b886e02013-07-05 09:53:17 +02002023 http_server = server;
2024
Tomas Cejka404d37e2013-04-13 02:31:35 +02002025 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002026 maxtime.tv_sec = 5;
2027 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002028
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002029 #ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002030 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002031 unixd_setup_child();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002032 #endif
Radek Krejci469aab82012-07-22 18:42:20 +02002033
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002034 if (server != NULL) {
2035 cfg = ap_get_module_config(server->module_config, &netconf_module);
2036 if (cfg == NULL) {
2037 DEBUG("Getting mod_netconf configuration failed");
2038 return;
2039 }
2040 sockname = cfg->sockname;
2041 } else {
2042 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002043 }
Radek Krejci469aab82012-07-22 18:42:20 +02002044
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002045 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002046 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002047 DEBUG("Creating socket failed (%s)", strerror(errno));
2048 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002049 }
2050
2051 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002052 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002053 unlink(local.sun_path);
2054 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2055
2056 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2057 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002058 DEBUG("mod_netconf socket address already in use");
2059 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002060 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002061 DEBUG("Binding socket failed (%s)", strerror(errno));
2062 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002063 }
2064
2065 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002066 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2067 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002068 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002069 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002070
Tomas Cejkaba21b382013-04-13 02:37:32 +02002071 /* prepare internal lists */
2072 netconf_sessions_list = apr_hash_make(pool);
2073
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002074 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002075 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002076 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002077 use_notifications = 0;
2078 } else {
2079 use_notifications = 1;
2080 }
2081 #endif
2082
Radek Krejci469aab82012-07-22 18:42:20 +02002083 /* setup libnetconf's callbacks */
2084 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002085 nc_callback_print(clb_print);
2086 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2087 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2088 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002089 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002090
2091 /* disable publickey authentication */
2092 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2093
David Kupka8e60a372012-09-04 09:15:20 +02002094 /* create mutex protecting session list */
2095 pthread_rwlockattr_init(&lock_attrs);
2096 /* rwlock is shared only with threads in this process */
2097 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2098 /* create rw lock */
2099 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002100 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2101 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002102 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002103 pthread_mutex_init(&ntf_history_lock, NULL);
2104 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002105 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002106 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002107 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002108
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002109 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002110 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002111 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002112 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002113 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002114 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002115 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002116 }
2117 if (use_notifications == 1) {
2118 notification_handle();
2119 }
2120 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002121 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002122 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002123 }
Radek Krejci469aab82012-07-22 18:42:20 +02002124
2125 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002126 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002127 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002128 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002129 olds = tv.tv_sec;
2130 }
David Kupka8e60a372012-09-04 09:15:20 +02002131 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002132 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2133 apr_sleep(SLEEP_TIME);
2134 continue;
2135 } else if (client == -1 && (errno == EINTR)) {
2136 continue;
2137 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002138 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002139 continue;
2140 }
Radek Krejci469aab82012-07-22 18:42:20 +02002141
2142 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002143 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002144
David Kupka8e60a372012-09-04 09:15:20 +02002145 arg = malloc (sizeof(struct pass_to_thread));
2146 arg->client = client;
2147 arg->pool = pool;
2148 arg->server = server;
2149 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002150
David Kupka8e60a372012-09-04 09:15:20 +02002151 /* start new thread. It will serve this particular request and then terminate */
2152 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002153 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002154 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002155 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002156 pthread_count++;
2157 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2158 ptids[pthread_count] = 0;
2159 }
Radek Krejci469aab82012-07-22 18:42:20 +02002160
David Kupka8e60a372012-09-04 09:15:20 +02002161 /* check if some thread already terminated, free some resources by joining it */
2162 for (i=0; i<pthread_count; i++) {
2163 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002164 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002165 pthread_count--;
2166 if (pthread_count > 0) {
2167 /* place last Thread ID on the place of joined one */
2168 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002169 }
Radek Krejci469aab82012-07-22 18:42:20 +02002170 }
2171 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002172 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002173 }
2174
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002175 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002176 /* join all threads */
2177 for (i=0; i<pthread_count; i++) {
2178 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2179 }
Radek Krejci469aab82012-07-22 18:42:20 +02002180
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002181 #ifdef WITH_NOTIFICATIONS
2182 notification_close();
2183 #endif
2184
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002185 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002186 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002187
David Kupka8e60a372012-09-04 09:15:20 +02002188 /* destroy rwlock */
2189 pthread_rwlock_destroy(&session_lock);
2190 pthread_rwlockattr_destroy(&lock_attrs);
2191
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002192 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002193
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002194 free(ptids);
2195 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002196 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002197 return;
2198error_exit:
2199 close(lsock);
2200 free(ptids);
2201 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002202}
2203
Radek Krejcif23850c2012-07-23 16:14:17 +02002204static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002205{
Radek Krejcif23850c2012-07-23 16:14:17 +02002206 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2207 apr_pool_create(&config->pool, pool);
2208 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002209 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002210
2211 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002212}
2213
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002214#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002215static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2216 apr_pool_t * plog, server_rec * s)
2217{
Radek Krejcif23850c2012-07-23 16:14:17 +02002218 mod_netconf_cfg *config;
2219 apr_status_t res;
2220
Radek Krejci469aab82012-07-22 18:42:20 +02002221 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002222 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002223 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002224
2225 /*
2226 * The following checks if this routine has been called before.
2227 * This is necessary because the parent process gets initialized
2228 * a couple of times as the server starts up.
2229 */
2230 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2231 if (!data) {
2232 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2233 return (OK);
2234 }
2235
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002236 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002237 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002238
Radek Krejcif23850c2012-07-23 16:14:17 +02002239 if (config && config->forkproc == NULL) {
2240 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2241 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002242 switch (res) {
2243 case APR_INCHILD:
2244 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002245 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002246 apr_signal(SIGTERM, signal_handler);
2247
2248 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002249 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002250
2251 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002252 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002253
Radek Krejcif23850c2012-07-23 16:14:17 +02002254 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002255 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002256 exit(APR_EGENERAL);
2257 break;
2258 case APR_INPARENT:
2259 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002260 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002261 break;
2262 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002263 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002264 break;
2265 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002266 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002267 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002268 }
2269
2270 return OK;
2271}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002272#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002273
Radek Krejci469aab82012-07-22 18:42:20 +02002274/**
2275 * Register module hooks
2276 */
2277static void mod_netconf_register_hooks(apr_pool_t * p)
2278{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002279#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002280 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002281#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002282}
2283
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002284static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2285{
2286 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2287 return NULL;
2288}
2289
2290static const command_rec netconf_cmds[] = {
2291 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2292 {NULL}
2293};
2294
Radek Krejci469aab82012-07-22 18:42:20 +02002295/* Dispatch list for API hooks */
2296module AP_MODULE_DECLARE_DATA netconf_module = {
2297 STANDARD20_MODULE_STUFF,
2298 NULL, /* create per-dir config structures */
2299 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002300 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002301 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002302 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002303 mod_netconf_register_hooks /* register hooks */
2304};
Radek Krejcia332b692012-11-12 16:15:54 +01002305
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002306int main(int argc, char **argv)
2307{
2308 apr_pool_t *pool;
2309 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2310 apr_signal(SIGTERM, signal_handler);
2311 apr_signal(SIGINT, signal_handler);
2312 apr_pool_create(&pool, NULL);
2313 forked_proc(pool, NULL);
2314 apr_pool_destroy(pool);
2315 apr_terminate();
2316 DEBUG("Terminated");
2317 return 0;
2318}