blob: f307930e3feb864a4a3d4ecf832ac28168fa53a1 [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 Cejka8a82dab2013-05-30 23:37:23 +0200245 s->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200246 }
247 s->hello_message = json_object_new_object();
248 if (session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100249 if (old_sid != NULL) {
250 /* use previous sid */
251 json_object_object_add(s->hello_message, "sid", json_object_new_string(old_sid));
252 free(old_sid);
253 } else {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200254 /* we don't have old sid */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100255 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
256 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200257 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
258 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
259 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
260 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
261 cpblts = nc_session_get_cpblts (session);
262 if (cpblts != NULL) {
263 json_obj = json_object_new_array();
264 nc_cpblts_iter_start (cpblts);
265 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
266 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
267 }
268 json_object_object_add(s->hello_message, "capabilities", json_obj);
269 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100270 DEBUG("%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200271 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100272 DEBUG("Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200273 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
274 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
275 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100276 DEBUG("Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200277
278}
279
280
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200281/**
282 * \defgroup netconf_operations NETCONF operations
283 * The list of NETCONF operations that mod_netconf supports.
284 * @{
285 */
286
287/**
288 * \brief Connect to NETCONF server
289 *
290 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
291 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100292static 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 +0200293{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200294 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200295 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200296 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200297
298 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200299 password = (char*)pass;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100300 DEBUG("prepare to connect %s@%s:%s", user, host, port);
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200301 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200302 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100303 DEBUG("nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200304
Radek Krejci469aab82012-07-22 18:42:20 +0200305 /* if connected successful, add session to the list */
306 if (session != NULL) {
307 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200308 session_key = gen_ncsession_hash(
309 (host==NULL) ? "localhost" : host,
310 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200311 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200312
Tomas Cejkaba21b382013-04-13 02:37:32 +0200313 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200314 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
315 nc_session_free(session);
316 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100317 DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200318 return NULL;
319 }
320 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200321 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200322 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200323 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200324 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100325 DEBUG("Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200326 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100327 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200328 if (pthread_rwlock_wrlock (&session_lock) != 0) {
329 nc_session_free(session);
330 free (locked_session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100331 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200332 return NULL;
333 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200334 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200335 locked_session->ntfc_subscribed = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100336 DEBUG("Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200337 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 +0100338 DEBUG("Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200339
Tomas Cejka47387fd2013-06-10 20:37:46 +0200340 /* lock session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100341 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200342 pthread_mutex_lock(&locked_session->lock);
343
344 /* unlock session list */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100345 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200346 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100347 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200348 }
349
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200350 /* store information about session from hello message for future usage */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100351 prepare_status_message(locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200352
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100353 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200354 pthread_mutex_unlock(&locked_session->lock);
355
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100356 DEBUG("NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200357 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200358 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100359 DEBUG("Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200360 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200361 }
362
Radek Krejci469aab82012-07-22 18:42:20 +0200363}
364
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100365static int close_and_free_session(struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200366{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100367 DEBUG("lock private lock.");
368 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200369 if (pthread_mutex_lock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100370 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200371 }
372 locked_session->ntfc_subscribed = 0;
373 locked_session->closed = 1;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200374 nc_session_free(locked_session->session);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100375 DEBUG("session closed.");
376 DEBUG("unlock private lock.");
377 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200378 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100379 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200380 }
381
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100382 DEBUG("unlock session lock.");
383 DEBUG("closed session, disabled notif(?), wait 2s");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200384 usleep(500000); /* let notification thread stop */
385
386 /* session shouldn't be used by now */
387 /** \todo free all notifications from queue */
388 apr_array_clear(locked_session->notifications);
389 pthread_mutex_destroy(&locked_session->lock);
390 if (locked_session->hello_message != NULL) {
391 /** \todo free hello_message */
392 //json_object_put(locked_session->hello_message);
393 locked_session->hello_message = NULL;
394 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200395 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200396 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200397 locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100398 DEBUG("NETCONF session closed, everything cleared.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200399 return (EXIT_SUCCESS);
400}
401
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100402static int netconf_close(const char* session_key, json_object **reply)
Tomas Cejka47387fd2013-06-10 20:37:46 +0200403{
David Kupka8e60a372012-09-04 09:15:20 +0200404 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200405
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100406 DEBUG("Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200407
David Kupka8e60a372012-09-04 09:15:20 +0200408 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100409 DEBUG("lock session lock.");
410 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200411 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100412 DEBUG("Error while locking rwlock");
413 (*reply) = create_error("Internal: Error while locking.");
David Kupka8e60a372012-09-04 09:15:20 +0200414 return EXIT_FAILURE;
415 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200416 /* get session to close */
417 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
418 /* remove session from the active sessions list -> nobody new can now work with session */
419 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200420
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100421 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200422 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100423 DEBUG("Error while unlocking rwlock");
424 (*reply) = create_error("Internal: Error while unlocking.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200425 }
426
427 if ((locked_session != NULL) && (locked_session->session != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100428 return close_and_free_session(locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200429 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100430 DEBUG("Unknown session to close");
431 (*reply) = create_error("Internal: Unkown session to close.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200432 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200433 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100434 (*reply) = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200435}
436
Tomas Cejkac7929632013-10-24 19:25:15 +0200437/**
438 * Test reply message type and return error message.
439 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200440 * \param[in] session nc_session internal struct
441 * \param[in] session_key session key, NULL to disable disconnect on error
442 * \param[in] msgt RPC-REPLY message type
443 * \param[out] data
444 * \return NULL on success
445 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100446json_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 +0200447{
448 NC_REPLY_TYPE replyt;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100449 json_object *err = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200450
451 /* process the result of the operation */
452 switch (msgt) {
453 case NC_MSG_UNKNOWN:
454 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100455 DEBUG("mod_netconf: receiving rpc-reply failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200456 if (session_key != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100457 netconf_close(session_key, &err);
458 }
459 if (err != NULL) {
460 return err;
Tomas Cejkac7929632013-10-24 19:25:15 +0200461 }
462 return create_error("Internal: Receiving RPC-REPLY failed.");
463 }
464 case NC_MSG_NONE:
465 /* there is error handled by callback */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100466 if (data != NULL) {
467 free(*data);
468 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200469 (*data) = NULL;
470 return NULL;
471 case NC_MSG_REPLY:
472 switch (replyt = nc_reply_get_type(reply)) {
473 case NC_REPLY_OK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100474 if ((data != NULL) && (*data != NULL)) {
475 free(*data);
476 (*data) = NULL;
477 }
478 return create_ok();
Tomas Cejkac7929632013-10-24 19:25:15 +0200479 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100480 if (((*data) = nc_reply_get_data(reply)) == NULL) {
481 DEBUG("mod_netconf: no data from reply");
Tomas Cejkac7929632013-10-24 19:25:15 +0200482 return create_error("Internal: No data from reply received.");
483 } else {
484 return NULL;
485 }
486 break;
487 case NC_REPLY_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100488 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
489 if (data != NULL) {
490 free(*data);
491 (*data) = NULL;
492 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200493 return create_error(nc_reply_get_errormsg(reply));
494 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100495 DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
496 if (data != NULL) {
497 free(*data);
498 (*data) = NULL;
499 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200500 return create_error(nc_reply_get_errormsg(reply));
501 }
502 break;
503 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100504 DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
505 if (data != NULL) {
506 free(*data);
507 (*data) = NULL;
508 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200509 return create_error("Internal: Unexpected RPC-REPLY message type.");
510 }
511}
512
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100513json_object *netconf_unlocked_op(struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200514{
515 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200516 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200517
518 /* check requests */
519 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100520 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200521 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200522 }
523
524 if (session != NULL) {
525 /* send the request and get the reply */
526 msgt = nc_session_send_recv(session, rpc, &reply);
527 /* process the result of the operation */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100528 return netconf_test_reply(session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200529 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100530 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200531 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200532 }
533}
534
Tomas Cejkac7929632013-10-24 19:25:15 +0200535/**
536 * Perform RPC method that returns data.
537 *
Tomas Cejkac7929632013-10-24 19:25:15 +0200538 * \param[in] session_key session identifier
539 * \param[in] rpc RPC message to perform
540 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
541 * \return NULL on success, json object with error otherwise
542 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100543static json_object *netconf_op(const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200544{
545 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200546 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200547 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200548 json_object *res = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100549 char *data = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100550 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200551
Radek Krejci8e4632a2012-07-26 13:40:34 +0200552 /* check requests */
553 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100554 DEBUG("mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200555 res = create_error("Internal: RPC could not be created.");
556 data = NULL;
557 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200558 }
559
David Kupka8e60a372012-09-04 09:15:20 +0200560 /* get non-exclusive (read) access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100561 DEBUG("LOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200562 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100563 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200564 res = create_error("Internal: Lock failed.");
565 data = NULL;
566 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200567 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200568 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200569 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 +0200570 if (locked_session != NULL) {
571 session = locked_session->session;
572 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200573 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200574 /* get exclusive access to session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100575 DEBUG("LOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200576 if (pthread_mutex_lock(&locked_session->lock) != 0) {
577 /* unlock before returning error */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100578 DEBUG("UNLOCK wrlock %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200579 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200580
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100581 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200582 res = create_error("Internal: Could not unlock.");
583 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200584 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200585 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200586 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100587 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200588 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200589
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100590 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200591 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200592 }
593
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200594 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200595
Radek Krejci035bf4e2012-07-25 10:59:09 +0200596 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100597 msgt = nc_session_send_recv(session, rpc, &reply);
598
David Kupka8e60a372012-09-04 09:15:20 +0200599 /* first release exclusive lock for this session */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100600 DEBUG("UNLOCK mutex %s", __func__);
David Kupka8e60a372012-09-04 09:15:20 +0200601 pthread_mutex_unlock(&locked_session->lock);
602 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200603
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100604 res = netconf_test_reply(session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200605 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100606 /* release lock on failure */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100607 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100608 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100609 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100610 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100611 DEBUG("Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200612 res = create_error("Unknown session to process.");
613 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200614 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200615finished:
616 nc_reply_free(reply);
617 if (received_data != NULL) {
618 (*received_data) = data;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100619 } else {
620 if (data != NULL) {
621 free(data);
622 data = NULL;
623 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200624 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200625 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200626}
627
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100628static char* netconf_getconfig(const char* session_key, NC_DATASTORE source, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200629{
630 nc_rpc* rpc;
631 struct nc_filter *f = NULL;
632 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200633 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200634
635 /* create filter if set */
636 if (filter != NULL) {
637 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
638 }
639
640 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100641 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200642 nc_filter_free(f);
643 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100644 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200645 return (NULL);
646 }
647
Tomas Cejka94674662013-09-13 15:55:24 +0200648 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100649 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100650 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200651 }
652
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100653 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200654 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100655 if (res != NULL) {
656 (*err) = res;
657 } else {
658 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200659 }
660
Radek Krejci8e4632a2012-07-26 13:40:34 +0200661 return (data);
662}
663
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100664static 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 +0100665{
666 nc_rpc* rpc;
667 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200668 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100669
670 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100671 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100672 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100673 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100674 return (NULL);
675 }
676
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100677 res = netconf_op(session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100678 nc_rpc_free (rpc);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100679 if (res != NULL) {
680 (*err) = res;
681 } else {
682 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200683 }
684
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100685 return (data);
686}
687
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100688static char* netconf_get(const char* session_key, const char* filter, json_object **err)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200689{
690 nc_rpc* rpc;
691 struct nc_filter *f = NULL;
692 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200693 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200694
695 /* create filter if set */
696 if (filter != NULL) {
697 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
698 }
699
700 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100701 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200702 nc_filter_free(f);
703 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100704 DEBUG("mod_netconf: creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200705 return (NULL);
706 }
707
Tomas Cejka94674662013-09-13 15:55:24 +0200708 /* tell server to show all elements even if they have default values */
Tomas Cejka1c3840d2014-01-29 21:08:23 +0100709 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100710 DEBUG("mod_netconf: setting withdefaults failed");
Tomas Cejka94674662013-09-13 15:55:24 +0200711 }
712
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100713 res = netconf_op(session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200714 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200715 if (res == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100716 (*err) = res;
717 } else {
718 (*err) = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200719 }
720
Radek Krejci8e4632a2012-07-26 13:40:34 +0200721 return (data);
722}
723
Tomas Cejkab4d05872014-02-14 22:44:38 +0100724static 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 +0200725{
726 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200727 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200728
729 /* create requests */
Tomas Cejkab4d05872014-02-14 22:44:38 +0100730 if (source == NC_DATASTORE_CONFIG) {
Tomas Cejka5064c232013-01-17 09:30:58 +0100731 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100732 /* config, url */
733 rpc = nc_rpc_copyconfig(source, target, config, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100734 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100735 /* config, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100736 rpc = nc_rpc_copyconfig(source, target, config);
737 }
Tomas Cejkab4d05872014-02-14 22:44:38 +0100738 } else if (source == NC_DATASTORE_URL) {
739 if (target == NC_DATASTORE_URL) {
740 /* url, url */
741 rpc = nc_rpc_copyconfig(source, target, uri_src, uri_trg);
742 } else {
743 /* url, datastore */
744 rpc = nc_rpc_copyconfig(source, target, uri_src);
745 }
Tomas Cejka5064c232013-01-17 09:30:58 +0100746 } else {
747 if (target == NC_DATASTORE_URL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100748 /* datastore, url */
749 rpc = nc_rpc_copyconfig(source, target, uri_trg);
Tomas Cejka5064c232013-01-17 09:30:58 +0100750 } else {
Tomas Cejkab4d05872014-02-14 22:44:38 +0100751 /* datastore, datastore */
Tomas Cejka5064c232013-01-17 09:30:58 +0100752 rpc = nc_rpc_copyconfig(source, target);
753 }
754 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200755 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100756 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200757 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200758 }
759
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100760 res = netconf_op(session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200761 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200762
763 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200764}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200765
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100766static 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 +0200767{
768 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200769 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200770
771 /* create requests */
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100772 rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200773 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100774 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200775 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200776 }
777
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100778 res = netconf_op(session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200779 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200780
781 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200782}
783
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100784static json_object *netconf_killsession(const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200785{
786 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200787 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200788
789 /* create requests */
790 rpc = nc_rpc_killsession(sid);
791 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100792 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200793 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200794 }
795
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100796 res = netconf_op(session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200797 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200798 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200799}
800
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100801static json_object *netconf_onlytargetop(const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200802{
803 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200804 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200805
806 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200807 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200808 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100809 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200810 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200811 }
812
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100813 res = netconf_op(session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200814 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200815 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200816}
817
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100818static json_object *netconf_deleteconfig(const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200819{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200820 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200821 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200822 if (target != NC_DATASTORE_URL) {
823 rpc = nc_rpc_deleteconfig(target);
824 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200825 rpc = nc_rpc_deleteconfig(target, url);
826 }
827 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100828 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200829 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200830 }
831
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100832 res = netconf_op(session_key, rpc, NULL);
Tomas Cejkac7929632013-10-24 19:25:15 +0200833 nc_rpc_free (rpc);
834 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200835}
836
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100837static json_object *netconf_lock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200838{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100839 return (netconf_onlytargetop(session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200840}
841
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100842static json_object *netconf_unlock(const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200843{
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100844 return (netconf_onlytargetop(session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200845}
846
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100847static json_object *netconf_generic(const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200848{
Tomas Cejka00635972013-06-03 15:10:52 +0200849 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200850 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200851
852 /* create requests */
853 rpc = nc_rpc_generic(content);
854 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100855 DEBUG("mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200856 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200857 }
858
Radek Krejcia332b692012-11-12 16:15:54 +0100859 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200860 // TODO ?free(*data);
861 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100862 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200863
864 /* get session where send the RPC */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100865 res = netconf_op(session_key, rpc, data);
Tomas Cejkac7929632013-10-24 19:25:15 +0200866 nc_rpc_free (rpc);
867 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200868}
869
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200870/**
871 * @}
872 *//* netconf_operations */
873
Radek Krejci7338bde2012-08-10 12:57:30 +0200874void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200875{
Radek Krejci7338bde2012-08-10 12:57:30 +0200876 switch (level) {
877 case NC_VERB_ERROR:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100878 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200879 break;
880 case NC_VERB_WARNING:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100881 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200882 break;
883 case NC_VERB_VERBOSE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100884 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200885 break;
886 case NC_VERB_DEBUG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100887 DEBUG("%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200888 break;
889 }
Radek Krejci469aab82012-07-22 18:42:20 +0200890}
891
Tomas Cejka64b87482013-06-03 16:30:53 +0200892/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200893 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200894 * Caller should free message memory.
895 * \param[in] client socket descriptor of client
Tomas Cejka64b87482013-06-03 16:30:53 +0200896 * \return pointer to message
897 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100898char *get_framed_message(int client)
Tomas Cejka64b87482013-06-03 16:30:53 +0200899{
900 /* read json in chunked framing */
901 unsigned int buffer_size = 0;
902 ssize_t buffer_len = 0;
903 char *buffer = NULL;
904 char c;
905 ssize_t ret;
906 int i, chunk_len;
907 char chunk_len_str[12];
908
909 while (1) {
910 /* read chunk length */
911 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
912 if (buffer != NULL) {
913 free (buffer);
914 buffer = NULL;
915 }
916 break;
917 }
918 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
919 if (buffer != NULL) {
920 free (buffer);
921 buffer = NULL;
922 }
923 break;
924 }
925 i=0;
926 memset (chunk_len_str, 0, 12);
927 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
928 if (i==0 && c == '#') {
929 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
930 /* end but invalid */
931 if (buffer != NULL) {
932 free (buffer);
933 buffer = NULL;
934 }
935 }
936 /* end of message, double-loop break */
937 goto msg_complete;
938 }
939 chunk_len_str[i++] = c;
940 if (i==11) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100941 DEBUG("Message is too long, buffer for length is not big enought!!!!");
Tomas Cejka64b87482013-06-03 16:30:53 +0200942 break;
943 }
944 }
945 if (c != '\n') {
946 if (buffer != NULL) {
947 free (buffer);
948 buffer = NULL;
949 }
950 break;
951 }
952 chunk_len_str[i] = 0;
953 if ((chunk_len = atoi (chunk_len_str)) == 0) {
954 if (buffer != NULL) {
955 free (buffer);
956 buffer = NULL;
957 }
958 break;
959 }
960 buffer_size += chunk_len+1;
961 buffer = realloc (buffer, sizeof(char)*buffer_size);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100962 memset(buffer + (buffer_size-chunk_len-1), 0, chunk_len+1);
Tomas Cejka64b87482013-06-03 16:30:53 +0200963 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
964 if (buffer != NULL) {
965 free (buffer);
966 buffer = NULL;
967 }
968 break;
969 }
970 buffer_len += ret;
971 }
972msg_complete:
973 return buffer;
974}
975
Tomas Cejkad5b53772013-06-08 23:01:07 +0200976NC_DATASTORE parse_datastore(const char *ds)
977{
978 if (strcmp(ds, "running") == 0) {
979 return NC_DATASTORE_RUNNING;
980 } else if (strcmp(ds, "startup") == 0) {
981 return NC_DATASTORE_STARTUP;
982 } else if (strcmp(ds, "candidate") == 0) {
983 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +0200984 } else if (strcmp(ds, "url") == 0) {
985 return NC_DATASTORE_URL;
Tomas Cejka8f3031e2014-02-14 23:15:08 +0100986 } else if (strcmp(ds, "config") == 0) {
987 return NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +0200988 }
989 return -1;
990}
991
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +0100992NC_EDIT_TESTOPT_TYPE parse_testopt(const char *t)
993{
994 if (strcmp(t, "notset") == 0) {
995 return NC_EDIT_TESTOPT_NOTSET;
996 } else if (strcmp(t, "testset") == 0) {
997 return NC_EDIT_TESTOPT_TESTSET;
998 } else if (strcmp(t, "set") == 0) {
999 return NC_EDIT_TESTOPT_SET;
1000 } else if (strcmp(t, "test") == 0) {
1001 return NC_EDIT_TESTOPT_TEST;
1002 }
1003 return NC_EDIT_TESTOPT_ERROR;
1004}
1005
Tomas Cejkad5b53772013-06-08 23:01:07 +02001006json_object *create_error(const char *errmess)
1007{
1008 json_object *reply = json_object_new_object();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001009 json_object *array = json_object_new_array();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001010 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001011 json_object_array_add(array, json_object_new_string(errmess));
1012 json_object_object_add(reply, "errors", array);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001013 return reply;
1014
1015}
1016
1017json_object *create_data(const char *data)
1018{
1019 json_object *reply = json_object_new_object();
1020 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1021 json_object_object_add(reply, "data", json_object_new_string(data));
1022 return reply;
1023}
1024
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001025json_object *create_ok()
1026{
1027 json_object *reply = json_object_new_object();
1028 reply = json_object_new_object();
1029 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1030 return reply;
1031}
1032
1033json_object *handle_op_connect(apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001034{
1035 const char *host = NULL;
1036 const char *port = NULL;
1037 const char *user = NULL;
1038 const char *pass = NULL;
1039 json_object *capabilities = NULL;
1040 json_object *reply = NULL;
1041 char *session_key_hash = NULL;
1042 struct nc_cpblts* cpblts = NULL;
1043 unsigned int len, i;
1044
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001045 DEBUG("Request: Connect");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001046 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1047 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1048 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1049 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1050
1051 capabilities = json_object_object_get((json_object *) request, "capabilities");
1052 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1053 cpblts = nc_cpblts_new(NULL);
1054 for (i=0; i<len; i++) {
1055 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1056 }
1057 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001058 DEBUG("no capabilities specified");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001059 }
1060
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001061 DEBUG("host: %s, port: %s, user: %s", host, port, user);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001062 if ((host == NULL) || (user == NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001063 DEBUG("Cannot connect - insufficient input.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001064 session_key_hash = NULL;
1065 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001066 session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
1067 DEBUG("hash: %s", session_key_hash);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001068 }
1069 if (cpblts != NULL) {
1070 nc_cpblts_free(cpblts);
1071 }
1072
1073 if (session_key_hash == NULL) {
1074 /* negative reply */
1075 if (err_reply == NULL) {
1076 reply = json_object_new_object();
1077 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1078 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001079 DEBUG("Connection failed.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001080 } else {
1081 /* use filled err_reply from libnetconf's callback */
1082 reply = err_reply;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001083 DEBUG("Connect - error from libnetconf's callback.");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001084 }
1085 } else {
1086 /* positive reply */
1087 reply = json_object_new_object();
1088 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1089 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1090
1091 free(session_key_hash);
1092 }
1093 return reply;
1094}
1095
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001096json_object *handle_op_get(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001097{
1098 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001099 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001100 json_object *reply = NULL;
1101
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001102 DEBUG("Request: get (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001103
1104 filter = json_object_get_string(json_object_object_get(request, "filter"));
1105
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001106 if ((data = netconf_get(session_key, filter, &reply)) == NULL) {
1107 if (reply == NULL) {
1108 if (err_reply == NULL) {
1109 reply = create_error("Get information failed.");
1110 } else {
1111 /* use filled err_reply from libnetconf's callback */
1112 reply = err_reply;
1113 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001114 }
1115 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001116 reply = create_data(data);
1117 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001118 }
1119 return reply;
1120}
1121
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001122json_object *handle_op_getconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001123{
1124 NC_DATASTORE ds_type_s = -1;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001125 const char *filter = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001126 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001127 const char *source = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001128 json_object *reply = NULL;
1129
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001130 DEBUG("Request: get-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001131
1132 filter = json_object_get_string(json_object_object_get(request, "filter"));
1133
Tomas Cejkad5b53772013-06-08 23:01:07 +02001134 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1135 ds_type_s = parse_datastore(source);
1136 }
1137 if (ds_type_s == -1) {
1138 return create_error("Invalid source repository type requested.");
1139 }
1140
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001141 if ((data = netconf_getconfig(session_key, ds_type_s, filter, &reply)) == NULL) {
1142 if (reply == NULL) {
1143 if (err_reply == NULL) {
1144 reply = create_error("Get configuration operation failed.");
1145 } else {
1146 /* use filled err_reply from libnetconf's callback */
1147 reply = err_reply;
1148 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001149 }
1150 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001151 reply = create_data(data);
1152 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001153 }
1154 return reply;
1155}
1156
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001157json_object *handle_op_getschema(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001158{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001159 char *data = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001160 const char *identifier = NULL;
1161 const char *version = NULL;
1162 const char *format = NULL;
1163 json_object *reply = NULL;
1164
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001165 DEBUG("Request: get-schema (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001166 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1167 if (identifier == NULL) {
1168 return create_error("No identifier for get-schema supplied.");
1169 }
1170 version = json_object_get_string(json_object_object_get(request, "version"));
1171 format = json_object_get_string(json_object_object_get(request, "format"));
1172
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001173 DEBUG("get-schema(version: %s, format: %s)", version, format);
1174 if ((data = netconf_getschema(session_key, identifier, version, format, &reply)) == NULL) {
1175 if (reply == NULL) {
1176 if (err_reply == NULL) {
1177 reply = create_error("Get models operation failed.");
1178 } else {
1179 /* use filled err_reply from libnetconf's callback */
1180 reply = err_reply;
1181 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001182 }
1183 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001184 reply = create_data(data);
1185 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001186 }
1187 return reply;
1188}
1189
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001190json_object *handle_op_editconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001191{
1192 NC_DATASTORE ds_type_s = -1;
1193 NC_DATASTORE ds_type_t = -1;
1194 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1195 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001196 NC_EDIT_TESTOPT_TYPE testopt_type = NC_EDIT_TESTOPT_TESTSET;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001197 const char *defop = NULL;
1198 const char *erropt = NULL;
1199 const char *config = NULL;
1200 const char *source = NULL;
1201 const char *target = NULL;
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001202 const char *testopt = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001203 json_object *reply = NULL;
1204
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001205 DEBUG("Request: edit-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001206
1207 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1208 if (defop != NULL) {
1209 if (strcmp(defop, "merge") == 0) {
1210 defop_type = NC_EDIT_DEFOP_MERGE;
1211 } else if (strcmp(defop, "replace") == 0) {
1212 defop_type = NC_EDIT_DEFOP_REPLACE;
1213 } else if (strcmp(defop, "none") == 0) {
1214 defop_type = NC_EDIT_DEFOP_NONE;
1215 } else {
1216 return create_error("Invalid default-operation parameter.");
1217 }
1218 } else {
1219 defop_type = NC_EDIT_DEFOP_NOTSET;
1220 }
1221
1222 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1223 if (erropt != NULL) {
1224 if (strcmp(erropt, "continue-on-error") == 0) {
1225 erropt_type = NC_EDIT_ERROPT_CONT;
1226 } else if (strcmp(erropt, "stop-on-error") == 0) {
1227 erropt_type = NC_EDIT_ERROPT_STOP;
1228 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1229 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1230 } else {
1231 return create_error("Invalid error-option parameter.");
1232 }
1233 } else {
1234 erropt_type = 0;
1235 }
1236
1237 /* get parameters */
1238 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1239 ds_type_t = parse_datastore(target);
1240 }
1241 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1242 ds_type_s = parse_datastore(source);
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001243 } else {
1244 /* source is optional, default value is config */
1245 ds_type_s = NC_DATASTORE_CONFIG;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001246 }
1247 if (ds_type_t == -1) {
1248 return create_error("Invalid target repository type requested.");
1249 }
Tomas Cejka8f3031e2014-02-14 23:15:08 +01001250 if (ds_type_s == NC_DATASTORE_CONFIG) {
1251 config = json_object_get_string(json_object_object_get(request, "config"));
1252 if (config == NULL) {
1253 return create_error("Invalid config data parameter.");
1254 }
1255 } else if (ds_type_s == NC_DATASTORE_URL){
1256 config = json_object_get_string(json_object_object_get(request, "uri-source"));
1257 if (config == NULL) {
1258 config = "";
1259 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001260 }
1261
Tomas Cejka5ae8dfb2014-02-14 23:42:17 +01001262 testopt = json_object_get_string(json_object_object_get(request, "test-option"));
1263 if (testopt != NULL) {
1264 testopt_type = parse_testopt(testopt);
1265 } else {
1266 testopt_type = NC_EDIT_TESTOPT_TESTSET;
1267 }
1268
1269 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 +01001270 if (reply == NULL) {
1271 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001272 /* use filled err_reply from libnetconf's callback */
1273 reply = err_reply;
1274 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001275 }
1276 return reply;
1277}
1278
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001279json_object *handle_op_copyconfig(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001280{
1281 NC_DATASTORE ds_type_s = -1;
1282 NC_DATASTORE ds_type_t = -1;
1283 const char *config = NULL;
1284 const char *target = NULL;
1285 const char *source = NULL;
Tomas Cejkab4d05872014-02-14 22:44:38 +01001286 const char *uri_src = NULL;
1287 const char *uri_trg = NULL;
1288
Tomas Cejkad5b53772013-06-08 23:01:07 +02001289 json_object *reply = NULL;
1290
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001291 DEBUG("Request: copy-config (session %s)", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001292
1293 /* get parameters */
1294 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1295 ds_type_t = parse_datastore(target);
1296 }
1297 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1298 ds_type_s = parse_datastore(source);
1299 }
1300 if (source == NULL) {
1301 /* no explicit source specified -> use config data */
1302 ds_type_s = NC_DATASTORE_CONFIG;
1303 config = json_object_get_string(json_object_object_get(request, "config"));
1304 } else if (ds_type_s == -1) {
1305 /* source datastore specified, but it is invalid */
1306 return create_error("Invalid source repository type requested.");
1307 }
1308
1309 if (ds_type_t == -1) {
1310 /* invalid target datastore specified */
1311 return create_error("Invalid target repository type requested.");
1312 }
1313
Tomas Cejka55c6df52014-02-20 12:59:33 +01001314 /* source can be missing when config is given */
1315 if (source == NULL && config == NULL) {
Tomas Cejkab4d05872014-02-14 22:44:38 +01001316 reply = create_error("invalid input parameters - source and config is required.");
1317 return reply;
1318 }
1319
1320 if (ds_type_s == NC_DATASTORE_URL) {
Tomas Cejkaa70d3032014-02-14 23:20:03 +01001321 uri_src = json_object_get_string(json_object_object_get(request, "uri-source"));
Tomas Cejkab4d05872014-02-14 22:44:38 +01001322 if (uri_src == NULL) {
1323 uri_src = "";
1324 }
1325 }
1326 if (ds_type_t == NC_DATASTORE_URL) {
Tomas Cejkaa70d3032014-02-14 23:20:03 +01001327 uri_trg = json_object_get_string(json_object_object_get(request, "uri-target"));
Tomas Cejkab4d05872014-02-14 22:44:38 +01001328 if (uri_trg == NULL) {
1329 uri_trg = "";
1330 }
1331 }
1332 reply = netconf_copyconfig(session_key, ds_type_s, ds_type_t, config, uri_src, uri_trg);
1333 if (reply == NULL) {
1334 if (err_reply != NULL) {
1335 /* use filled err_reply from libnetconf's callback */
1336 reply = err_reply;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001337 }
1338 }
1339 return reply;
1340}
1341
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001342json_object *handle_op_generic(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001343{
1344 json_object *reply = NULL;
1345 const char *config = NULL;
1346 char *data = NULL;
1347
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001348 DEBUG("Request: generic request for session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001349
1350 config = json_object_get_string(json_object_object_get(request, "content"));
1351
1352 if (config == NULL) {
1353 return create_error("Missing content parameter.");
1354 }
1355
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001356 /* TODO */
1357 reply = netconf_generic(session_key, config, &data);
1358 if (reply == NULL) {
1359 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001360 /* use filled err_reply from libnetconf's callback */
1361 reply = err_reply;
1362 }
1363 } else {
1364 if (data == NULL) {
1365 reply = json_object_new_object();
1366 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1367 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001368 reply = create_data(data);
1369 free(data);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001370 }
1371 }
1372 return reply;
1373}
1374
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001375json_object *handle_op_disconnect(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001376{
1377 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001378 DEBUG("Request: Disconnect session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001379
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001380 if (netconf_close(session_key, &reply) != EXIT_SUCCESS) {
1381 if (reply == NULL) {
1382 if (err_reply == NULL) {
1383 reply = create_error("Get configuration information from device failed.");
1384 } else {
1385 /* use filled err_reply from libnetconf's callback */
1386 reply = err_reply;
1387 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001388 }
1389 } else {
1390 reply = json_object_new_object();
1391 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1392 }
1393 return reply;
1394}
1395
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001396json_object *handle_op_kill(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001397{
1398 json_object *reply = NULL;
1399 const char *sid = NULL;
1400
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001401 DEBUG("Request: kill-session, session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001402
1403 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1404
1405 if (sid == NULL) {
1406 return create_error("Missing session-id parameter.");
1407 }
1408
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001409 reply = netconf_killsession(session_key, sid);
1410 if (reply == NULL) {
1411 if (err_reply != NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001412 /* use filled err_reply from libnetconf's callback */
1413 reply = err_reply;
1414 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001415 }
1416 return reply;
1417}
1418
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001419json_object *handle_op_reloadhello(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001420{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001421 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001422 struct session_with_mutex * locked_session = NULL;
1423 json_object *reply = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001424 DEBUG("Request: get info about session %s", session_key);
1425 return create_ok();
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001427 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001428 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001429 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001430 return NULL;
1431 }
1432
Tomas Cejkad5b53772013-06-08 23:01:07 +02001433 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1434 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001435 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001436 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001437 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001438 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001439 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001440 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001441 DEBUG("creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001442 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001443 if (temp_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001444 prepare_status_message(locked_session, temp_session);
1445 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001446 nc_session_free(temp_session);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001447 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001448 DEBUG("Reload hello failed due to channel establishment");
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449 reply = create_error("Reload was unsuccessful, connection failed.");
1450 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001451 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001452 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001453 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001454 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001455 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001456 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001457 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001458 reply = create_error("Invalid session identifier.");
1459 }
1460
1461 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1462 reply = locked_session->hello_message;
1463 }
1464 return reply;
1465}
1466
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001467json_object *handle_op_info(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001468{
1469 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001470 struct session_with_mutex * locked_session = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001471 DEBUG("Request: get info about session %s", session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001473 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001474 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001475 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001476 }
1477
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1479 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001480 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001481 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001482 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001483 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001484 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001485 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001486 if (locked_session->hello_message != NULL) {
1487 reply = locked_session->hello_message;
1488 } else {
1489 reply = create_error("Invalid session identifier.");
1490 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001491 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001492 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001493 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001494 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001495 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001496 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +02001497 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001498 reply = create_error("Invalid session identifier.");
1499 }
1500
Tomas Cejka47387fd2013-06-10 20:37:46 +02001501
Tomas Cejkad5b53772013-06-08 23:01:07 +02001502 return reply;
1503}
1504
Tomas Cejka6b886e02013-07-05 09:53:17 +02001505void notification_history(time_t eventtime, const char *content)
1506{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001507 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1508 if (notif_history_array == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001509 DEBUG("No list of notification history found.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001510 return;
1511 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001512 DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001513 json_object *notif = json_object_new_object();
1514 if (notif == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001515 DEBUG("Could not allocate memory for notification (json).");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001516 return;
1517 }
1518 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1519 json_object_object_add(notif, "content", json_object_new_string(content));
1520 json_object_array_add(notif_history_array, notif);
1521}
1522
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001523json_object *handle_op_ntfgethistory(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka6b886e02013-07-05 09:53:17 +02001524{
1525 json_object *reply = NULL;
1526 const char *sid = NULL;
1527 struct session_with_mutex *locked_session = NULL;
1528 struct nc_session *temp_session = NULL;
1529 nc_rpc *rpc = NULL;
1530 time_t start = 0;
1531 time_t stop = 0;
1532 int64_t from, to;
1533
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001534 DEBUG("Request: get notification history, session %s", session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001535
1536 sid = json_object_get_string(json_object_object_get(request, "session"));
1537 from = json_object_get_int64(json_object_object_get(request, "from"));
1538 to = json_object_get_int64(json_object_object_get(request, "to"));
1539
1540 start = time(NULL) + from;
1541 stop = time(NULL) + to;
1542
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001543 DEBUG("notification history interval %li %li", (long int) from, (long int) to);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001544
1545 if (sid == NULL) {
1546 return create_error("Missing session parameter.");
1547 }
1548
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001549 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001550 if (pthread_rwlock_rdlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001551 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001552 return NULL;
1553 }
1554
1555 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1556 if (locked_session != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001557 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001558 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001559 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001560 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001561 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001562 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001563 DEBUG("creating temporal NC session.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001564 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1565 if (temp_session != NULL) {
1566 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1567 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001568 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001569 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001570 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001571 return create_error("notifications: creating an rpc request failed.");
1572 }
1573
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001574 DEBUG("Send NC subscribe.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001575 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001576 json_object *res = netconf_unlocked_op(temp_session, rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +02001577 if (res != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001578 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001579 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001580 DEBUG("Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001581 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001582 }
1583 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1584
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001585 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001586 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001587 DEBUG("LOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001588 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001589 json_object *notif_history_array = json_object_new_array();
1590 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001591 DEBUG("notif_history: cannot set thread-specific hash value.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001592 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001593
1594 ncntf_dispatch_receive(temp_session, notification_history);
1595
1596 reply = json_object_new_object();
1597 json_object_object_add(reply, "notifications", notif_history_array);
1598 //json_object_put(notif_history_array);
1599
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001600 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001601 pthread_mutex_unlock(&ntf_history_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001602 DEBUG("closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001603 nc_session_free(temp_session);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001604 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001605 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkac7929632013-10-24 19:25:15 +02001606 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001607 DEBUG("Get history of notification failed due to channel establishment");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001608 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1609 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001610 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001611 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001612 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001613 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6b886e02013-07-05 09:53:17 +02001614 }
1615 reply = create_error("Invalid session identifier.");
1616 }
1617
Tomas Cejka4003a702013-10-01 00:02:45 +02001618 return reply;
1619}
1620
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001621json_object *handle_op_validate(apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejka4003a702013-10-01 00:02:45 +02001622{
1623 json_object *reply = NULL;
1624 const char *sid = NULL;
1625 const char *target = NULL;
1626 const char *url = NULL;
1627 struct session_with_mutex *locked_session = NULL;
1628 nc_rpc *rpc = NULL;
1629 NC_DATASTORE target_ds;
1630
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001631 DEBUG("Request: validate datastore, session %s", session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001632
1633 sid = json_object_get_string(json_object_object_get(request, "session"));
1634 target = json_object_get_string(json_object_object_get(request, "target"));
1635 url = json_object_get_string(json_object_object_get(request, "url"));
1636
1637
1638 if ((sid == NULL) || (target == NULL)) {
1639 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001640 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001641
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001642 /* validation */
1643 target_ds = parse_datastore(target);
1644 if (target_ds == NC_DATASTORE_URL) {
1645 if (url != NULL) {
1646 rpc = nc_rpc_validate(target_ds, url);
Tomas Cejka4003a702013-10-01 00:02:45 +02001647 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001648 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
Tomas Cejka4003a702013-10-01 00:02:45 +02001649 || (target_ds == NC_DATASTORE_CANDIDATE)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001650 rpc = nc_rpc_validate(target_ds);
1651 }
1652 if (rpc == NULL) {
1653 DEBUG("mod_netconf: creating rpc request failed");
1654 reply = create_error("Creation of RPC request failed.");
1655 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka4003a702013-10-01 00:02:45 +02001656 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001657 return reply;
Tomas Cejka4003a702013-10-01 00:02:45 +02001658 }
1659
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001660 DEBUG("Request: validate datastore");
1661 if ((reply = netconf_op(session_key, rpc, NULL)) == NULL) {
Tomas Cejka4ad470b2014-03-20 15:30:50 +01001662 if (err_reply == NULL) {
1663 DEBUG("Request: validation ok.");
1664 reply = create_ok();
1665 } else {
1666 /* use filled err_reply from libnetconf's callback */
1667 reply = err_reply;
1668 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001669 }
1670 nc_rpc_free (rpc);
1671
Tomas Cejka6b886e02013-07-05 09:53:17 +02001672 return reply;
1673}
1674
David Kupka8e60a372012-09-04 09:15:20 +02001675void * thread_routine (void * arg)
1676{
1677 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001678 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001679 json_object *request = NULL;
1680 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001681 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001682 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001683 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001684 const char *session_key;
1685 const char *target = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001686 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001687 NC_DATASTORE ds_type_t = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001688 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001689 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001690 //server_rec * server = ((struct pass_to_thread*)arg)->server;
David Kupka8e60a372012-09-04 09:15:20 +02001691 int client = ((struct pass_to_thread*)arg)->client;
1692
Tomas Cejka00635972013-06-03 15:10:52 +02001693 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001694
1695 while (!isterminated) {
1696 fds.fd = client;
1697 fds.events = POLLIN;
1698 fds.revents = 0;
1699
1700 status = poll(&fds, 1, 1000);
1701
1702 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1703 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001704 //DEBUG("poll interrupted");
David Kupka8e60a372012-09-04 09:15:20 +02001705 continue;
1706 } else if (status < 0) {
1707 /* 0: poll time outed
1708 * close socket and ignore this request from the client, it can try it again
1709 * -1: poll failed
1710 * something wrong happend, close this socket and wait for another request
1711 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001712 //DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +02001713 close(client);
1714 break;
1715 }
1716 /* status > 0 */
1717
1718 /* check the status of the socket */
1719
1720 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1721 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1722 /* close client's socket (it's probably already closed by client */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001723 //DEBUG("socket error (%d)", fds.revents);
David Kupka8e60a372012-09-04 09:15:20 +02001724 close(client);
1725 break;
1726 }
1727
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001728 DEBUG("Get framed message...");
1729 buffer = get_framed_message(client);
David Kupka8e60a372012-09-04 09:15:20 +02001730
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001731 DEBUG("Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001732 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001733 enum json_tokener_error jerr;
1734 request = json_tokener_parse_verbose(buffer, &jerr);
1735 if (jerr != json_tokener_success) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001736 DEBUG("JSON parsing error");
Tomas Cejka00635972013-06-03 15:10:52 +02001737 continue;
1738 }
David Kupka8e60a372012-09-04 09:15:20 +02001739 operation = json_object_get_int(json_object_object_get(request, "type"));
1740
Tomas Cejka64b87482013-06-03 16:30:53 +02001741 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001742 DEBUG("operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001743 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1744 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001745 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001746 msgtext = json_object_to_json_string(reply);
1747 send(client, msgtext, strlen(msgtext) + 1, 0);
1748 json_object_put(reply);
1749 /* there is some stupid client, so close the connection to give a chance to some other client */
1750 close(client);
1751 break;
1752 }
1753
David Kupka8e60a372012-09-04 09:15:20 +02001754 /* null global JSON error-reply */
1755 err_reply = NULL;
1756
1757 /* prepare reply envelope */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001758 if (reply != NULL) {
1759 json_object_put(reply);
1760 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001761 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001762
1763 /* process required operation */
1764 switch (operation) {
1765 case MSG_CONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001766 reply = handle_op_connect(pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001767 break;
1768 case MSG_GET:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001769 reply = handle_op_get(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001770 break;
1771 case MSG_GETCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001772 reply = handle_op_getconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001773 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001774 case MSG_GETSCHEMA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001775 reply = handle_op_getschema(pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001776 break;
David Kupka8e60a372012-09-04 09:15:20 +02001777 case MSG_EDITCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001778 reply = handle_op_editconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001779 break;
1780 case MSG_COPYCONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001781 reply = handle_op_copyconfig(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001782 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001783
David Kupka8e60a372012-09-04 09:15:20 +02001784 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001785 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001786 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001787 /* get parameters */
1788 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1789 ds_type_t = parse_datastore(target);
1790 }
David Kupka8e60a372012-09-04 09:15:20 +02001791
1792 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001793 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001794 break;
1795 }
David Kupka8e60a372012-09-04 09:15:20 +02001796 switch(operation) {
1797 case MSG_DELETECONFIG:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001798 DEBUG("Request: delete-config (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001799 url = json_object_get_string(json_object_object_get(request, "url"));
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001800 reply = netconf_deleteconfig(session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001801 break;
1802 case MSG_LOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001803 DEBUG("Request: lock (session %s)", session_key);
1804 reply = netconf_lock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001805 break;
1806 case MSG_UNLOCK:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001807 DEBUG("Request: unlock (session %s)", session_key);
1808 reply = netconf_unlock(session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001809 break;
1810 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001811 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001812 break;
1813 }
1814
Tomas Cejkac7929632013-10-24 19:25:15 +02001815 if (reply == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001816 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001817 /** \todo more clever error message wanted */
Tomas Cejkac7929632013-10-24 19:25:15 +02001818 reply = json_object_new_object();
1819 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
David Kupka8e60a372012-09-04 09:15:20 +02001820 } else {
1821 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001822 reply = err_reply;
1823 }
David Kupka8e60a372012-09-04 09:15:20 +02001824 }
1825 break;
1826 case MSG_KILL:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001827 reply = handle_op_kill(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001828 break;
1829 case MSG_DISCONNECT:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001830 reply = handle_op_disconnect(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001831 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001832 case MSG_RELOADHELLO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001833 reply = handle_op_reloadhello(pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001834 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001835 case MSG_INFO:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001836 reply = handle_op_info(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001837 break;
1838 case MSG_GENERIC:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001839 reply = handle_op_generic(pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001840 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001841 case MSG_NTF_GETHISTORY:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001842 reply = handle_op_ntfgethistory(pool, request, session_key);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001843 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001844 case MSG_VALIDATE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001845 reply = handle_op_validate(pool, request, session_key);
Tomas Cejka4003a702013-10-01 00:02:45 +02001846 break;
David Kupka8e60a372012-09-04 09:15:20 +02001847 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001848 DEBUG("Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001849 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001850 break;
1851 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001852 DEBUG("Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001853 json_object_put(request);
1854
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001855 DEBUG("Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001856 /* send reply to caller */
1857 if (reply != NULL) {
1858 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001859 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001860 if (buffer != NULL) {
1861 free(buffer);
1862 buffer = NULL;
1863 }
David Kupka8e60a372012-09-04 09:15:20 +02001864 break;
1865 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001866 DEBUG("Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001867 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001868 DEBUG("Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001869 json_object_put(reply);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001870 reply = NULL;
1871 DEBUG("Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001872 free(chunked_out_msg);
1873 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001874 if (buffer != NULL) {
1875 free(buffer);
1876 buffer = NULL;
1877 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001878 if (err_reply != NULL) {
1879 json_object_put(err_reply);
1880 err_reply = NULL;
1881 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001882 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001883 DEBUG("Reply is NULL, shouldn't be...");
1884 continue;
David Kupka8e60a372012-09-04 09:15:20 +02001885 }
1886 }
1887 }
David Kupka8e60a372012-09-04 09:15:20 +02001888 free (arg);
1889
1890 return retval;
1891}
1892
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001893/**
1894 * \brief Close all open NETCONF sessions.
1895 *
1896 * During termination of mod_netconf, it is useful to close all remaining
1897 * sessions. This function iterates over the list of sessions and close them
1898 * all.
1899 *
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001900 * \param[in] p apr pool needed for hash table iterating
1901 * \param[in] ht hash table of session_with_mutex structs
1902 */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001903static void close_all_nc_sessions(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001904{
1905 apr_hash_index_t *hi;
1906 void *val = NULL;
1907 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001908 const char *hashed_key = NULL;
1909 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001910 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001911
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001912 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001913 DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001914 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001915 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001916 return;
1917 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001918 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001919 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001920 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001921 swm = (struct session_with_mutex *) val;
1922 if (swm != NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001923 DEBUG("LOCK mutex %s", __func__);
1924 pthread_mutex_lock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001925 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001926 DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001927 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001928
1929 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001930 close_and_free_session(swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001931 }
1932 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001933 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001934 DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001935 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001936 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001937 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001938}
1939
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001940static void check_timeout_and_close(apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001941{
1942 apr_hash_index_t *hi;
1943 void *val = NULL;
1944 struct nc_session *ns = NULL;
1945 struct session_with_mutex *swm = NULL;
1946 const char *hashed_key = NULL;
1947 apr_ssize_t hashed_key_length;
1948 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001949 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001950
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001951 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001952//DEBUG("LOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001953 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001954 DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001955 return;
1956 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001957 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001958 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1959 swm = (struct session_with_mutex *) val;
1960 if (swm == NULL) {
1961 continue;
1962 }
1963 ns = swm->session;
1964 if (ns == NULL) {
1965 continue;
1966 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001967//DEBUG("LOCK mutex %s", __func__);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001968 pthread_mutex_lock(&swm->lock);
1969 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001970 DEBUG("Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001971 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001972 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001973//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001974 pthread_mutex_unlock(&swm->lock);
1975
1976 /* close_and_free_session handles locking on its own */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001977 close_and_free_session(swm);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001978 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001979//DEBUG("UNLOCK mutex %s", __func__);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001980 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001981 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001982 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001983 /* get exclusive access to sessions_list (conns) */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001984//DEBUG("UNLOCK wrlock %s", __func__);
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001985 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01001986 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001987 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001988}
1989
1990
1991/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001992 * This is actually implementation of NETCONF client
1993 * - requests are received from UNIX socket in the predefined format
1994 * - results are replied through the same way
1995 * - the daemon run as a separate process, but it is started and stopped
1996 * automatically by Apache.
1997 *
1998 */
Radek Krejci469aab82012-07-22 18:42:20 +02001999static void forked_proc(apr_pool_t * pool, server_rec * server)
2000{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002001 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02002002 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02002003 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002004 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02002005 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02002006 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02002007 struct pass_to_thread * arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002008 pthread_t * ptids = calloc(1, sizeof(pthread_t));
David Kupka8e60a372012-09-04 09:15:20 +02002009 struct timespec maxtime;
2010 pthread_rwlockattr_t lock_attrs;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002011 char *sockname = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +02002012 #ifdef WITH_NOTIFICATIONS
2013 char use_notifications = 0;
2014 #endif
David Kupka8e60a372012-09-04 09:15:20 +02002015
Tomas Cejka6b886e02013-07-05 09:53:17 +02002016 http_server = server;
2017
Tomas Cejka404d37e2013-04-13 02:31:35 +02002018 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02002019 maxtime.tv_sec = 5;
2020 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02002021
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002022 #ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002023 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02002024 unixd_setup_child();
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002025 #endif
Radek Krejci469aab82012-07-22 18:42:20 +02002026
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002027 if (server != NULL) {
2028 cfg = ap_get_module_config(server->module_config, &netconf_module);
2029 if (cfg == NULL) {
2030 DEBUG("Getting mod_netconf configuration failed");
2031 return;
2032 }
2033 sockname = cfg->sockname;
2034 } else {
2035 sockname = SOCKET_FILENAME;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002036 }
Radek Krejci469aab82012-07-22 18:42:20 +02002037
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002038 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02002039 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002040 DEBUG("Creating socket failed (%s)", strerror(errno));
2041 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002042 }
2043
2044 local.sun_family = AF_UNIX;
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002045 strncpy(local.sun_path, sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002046 unlink(local.sun_path);
2047 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2048
2049 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2050 if (errno == EADDRINUSE) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002051 DEBUG("mod_netconf socket address already in use");
2052 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002053 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002054 DEBUG("Binding socket failed (%s)", strerror(errno));
2055 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002056 }
2057
2058 if (listen(lsock, MAX_SOCKET_CL) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002059 DEBUG("Setting up listen socket failed (%s)", strerror(errno));
2060 goto error_exit;
Radek Krejci469aab82012-07-22 18:42:20 +02002061 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002062 chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
Radek Krejci469aab82012-07-22 18:42:20 +02002063
Tomas Cejkaba21b382013-04-13 02:37:32 +02002064 /* prepare internal lists */
2065 netconf_sessions_list = apr_hash_make(pool);
2066
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002067 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002068 if (notification_init(pool, server) == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002069 DEBUG("libwebsockets initialization failed");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002070 use_notifications = 0;
2071 } else {
2072 use_notifications = 1;
2073 }
2074 #endif
2075
Radek Krejci469aab82012-07-22 18:42:20 +02002076 /* setup libnetconf's callbacks */
2077 nc_verbosity(NC_VERB_DEBUG);
Radek Krejci469aab82012-07-22 18:42:20 +02002078 nc_callback_print(clb_print);
2079 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2080 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2081 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002082 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002083
2084 /* disable publickey authentication */
2085 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2086
David Kupka8e60a372012-09-04 09:15:20 +02002087 /* create mutex protecting session list */
2088 pthread_rwlockattr_init(&lock_attrs);
2089 /* rwlock is shared only with threads in this process */
2090 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2091 /* create rw lock */
2092 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002093 DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2094 goto error_exit;
David Kupka8e60a372012-09-04 09:15:20 +02002095 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002096 pthread_mutex_init(&ntf_history_lock, NULL);
2097 DEBUG("init of notif_history_key.");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002098 if (pthread_key_create(&notif_history_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002099 DEBUG("init of notif_history_key failed");
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002100 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002101
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002102 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002103 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002104 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002105 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002106 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002107 if (timediff > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002108 DEBUG("handling notifications");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002109 }
2110 if (use_notifications == 1) {
2111 notification_handle();
2112 }
2113 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002114 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002115 check_timeout_and_close(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002116 }
Radek Krejci469aab82012-07-22 18:42:20 +02002117
2118 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002119 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002120 if (((unsigned int)tv.tv_sec - olds) > 60) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002121 DEBUG("accepting another client");
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002122 olds = tv.tv_sec;
2123 }
David Kupka8e60a372012-09-04 09:15:20 +02002124 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002125 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2126 apr_sleep(SLEEP_TIME);
2127 continue;
2128 } else if (client == -1 && (errno == EINTR)) {
2129 continue;
2130 } else if (client == -1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002131 DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +02002132 continue;
2133 }
Radek Krejci469aab82012-07-22 18:42:20 +02002134
2135 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002136 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002137
David Kupka8e60a372012-09-04 09:15:20 +02002138 arg = malloc (sizeof(struct pass_to_thread));
2139 arg->client = client;
2140 arg->pool = pool;
2141 arg->server = server;
2142 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002143
David Kupka8e60a372012-09-04 09:15:20 +02002144 /* start new thread. It will serve this particular request and then terminate */
2145 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002146 DEBUG("Creating POSIX thread failed: %d\n", ret);
David Kupka8e60a372012-09-04 09:15:20 +02002147 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002148 DEBUG("Thread %lu created", ptids[pthread_count]);
David Kupka8e60a372012-09-04 09:15:20 +02002149 pthread_count++;
2150 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2151 ptids[pthread_count] = 0;
2152 }
Radek Krejci469aab82012-07-22 18:42:20 +02002153
David Kupka8e60a372012-09-04 09:15:20 +02002154 /* check if some thread already terminated, free some resources by joining it */
2155 for (i=0; i<pthread_count; i++) {
2156 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002157 DEBUG("Thread %lu joined with retval %p", ptids[i], arg);
David Kupka8e60a372012-09-04 09:15:20 +02002158 pthread_count--;
2159 if (pthread_count > 0) {
2160 /* place last Thread ID on the place of joined one */
2161 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002162 }
Radek Krejci469aab82012-07-22 18:42:20 +02002163 }
2164 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002165 DEBUG("Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002166 }
2167
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002168 DEBUG("mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002169 /* join all threads */
2170 for (i=0; i<pthread_count; i++) {
2171 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2172 }
Radek Krejci469aab82012-07-22 18:42:20 +02002173
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002174 #ifdef WITH_NOTIFICATIONS
2175 notification_close();
2176 #endif
2177
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002178 /* close all NETCONF sessions */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002179 close_all_nc_sessions(pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002180
David Kupka8e60a372012-09-04 09:15:20 +02002181 /* destroy rwlock */
2182 pthread_rwlock_destroy(&session_lock);
2183 pthread_rwlockattr_destroy(&lock_attrs);
2184
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002185 DEBUG("Exiting from the mod_netconf daemon");
Radek Krejci469aab82012-07-22 18:42:20 +02002186
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002187 free(ptids);
2188 close(lsock);
Radek Krejci469aab82012-07-22 18:42:20 +02002189 exit(APR_SUCCESS);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002190 return;
2191error_exit:
2192 close(lsock);
2193 free(ptids);
2194 return;
Radek Krejci469aab82012-07-22 18:42:20 +02002195}
2196
Radek Krejcif23850c2012-07-23 16:14:17 +02002197static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002198{
Radek Krejcif23850c2012-07-23 16:14:17 +02002199 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2200 apr_pool_create(&config->pool, pool);
2201 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002202 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002203
2204 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002205}
2206
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002207#ifndef HTTPD_INDEPENDENT
Radek Krejci469aab82012-07-22 18:42:20 +02002208static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2209 apr_pool_t * plog, server_rec * s)
2210{
Radek Krejcif23850c2012-07-23 16:14:17 +02002211 mod_netconf_cfg *config;
2212 apr_status_t res;
2213
Radek Krejci469aab82012-07-22 18:42:20 +02002214 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002215 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002216 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002217
2218 /*
2219 * The following checks if this routine has been called before.
2220 * This is necessary because the parent process gets initialized
2221 * a couple of times as the server starts up.
2222 */
2223 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2224 if (!data) {
2225 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2226 return (OK);
2227 }
2228
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002229 DEBUG("creating mod_netconf daemon");
Radek Krejcif23850c2012-07-23 16:14:17 +02002230 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002231
Radek Krejcif23850c2012-07-23 16:14:17 +02002232 if (config && config->forkproc == NULL) {
2233 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2234 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002235 switch (res) {
2236 case APR_INCHILD:
2237 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002238 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002239 apr_signal(SIGTERM, signal_handler);
2240
2241 /* log start of the separated NETCONF communication process */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002242 DEBUG("mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002243
2244 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002245 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002246
Radek Krejcif23850c2012-07-23 16:14:17 +02002247 /* I never should be here, wtf?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002248 DEBUG("mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002249 exit(APR_EGENERAL);
2250 break;
2251 case APR_INPARENT:
2252 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002253 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002254 break;
2255 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002256 DEBUG("apr_proc_fork() failed");
Radek Krejci469aab82012-07-22 18:42:20 +02002257 break;
2258 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002259 } else {
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002260 DEBUG("mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002261 }
2262
2263 return OK;
2264}
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002265#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002266
Radek Krejci469aab82012-07-22 18:42:20 +02002267/**
2268 * Register module hooks
2269 */
2270static void mod_netconf_register_hooks(apr_pool_t * p)
2271{
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002272#ifndef HTTPD_INDEPENDENT
Radek Krejcif23850c2012-07-23 16:14:17 +02002273 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002274#endif
Radek Krejci469aab82012-07-22 18:42:20 +02002275}
2276
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002277static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2278{
2279 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2280 return NULL;
2281}
2282
2283static const command_rec netconf_cmds[] = {
2284 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2285 {NULL}
2286};
2287
Radek Krejci469aab82012-07-22 18:42:20 +02002288/* Dispatch list for API hooks */
2289module AP_MODULE_DECLARE_DATA netconf_module = {
2290 STANDARD20_MODULE_STUFF,
2291 NULL, /* create per-dir config structures */
2292 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002293 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002294 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002295 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002296 mod_netconf_register_hooks /* register hooks */
2297};
Radek Krejcia332b692012-11-12 16:15:54 +01002298
Tomas Cejkaef531ee2013-11-12 16:07:00 +01002299int main(int argc, char **argv)
2300{
2301 apr_pool_t *pool;
2302 apr_app_initialize(&argc, (char const *const **) &argv, NULL);
2303 apr_signal(SIGTERM, signal_handler);
2304 apr_signal(SIGINT, signal_handler);
2305 apr_pool_create(&pool, NULL);
2306 forked_proc(pool, NULL);
2307 apr_pool_destroy(pool);
2308 apr_terminate();
2309 DEBUG("Terminated");
2310 return 0;
2311}