blob: 0abff310f49ee9d6ee9cbc7e72a69a2ccacdc02b [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 */
Tomas Cejka689a1042013-01-16 15:08:25 +010046static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $";
Radek Krejci469aab82012-07-22 18:42:20 +020047
Radek Krejci7b4ddd02012-07-30 08:09:58 +020048#include <unistd.h>
49#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020050#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010053#include <sys/fcntl.h>
David Kupka8e60a372012-09-04 09:15:20 +020054#include <pthread.h>
55#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020056
57#include <unixd.h>
58#include <httpd.h>
59#include <http_log.h>
60#include <http_config.h>
61
62#include <apr_sha1.h>
63#include <apr_hash.h>
64#include <apr_signal.h>
65#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020066
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020067#include <json/json.h>
68
Radek Krejci469aab82012-07-22 18:42:20 +020069#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020070#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020071
Tomas Cejkad340dbf2013-03-24 20:36:57 +010072#ifdef WITH_NOTIFICATIONS
73#include "notification_module.h"
74#endif
75
Tomas Cejka94da2c52013-01-08 18:20:30 +010076#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020077#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020078
79#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020080#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020081#define MAX_SOCKET_CL 10
82#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020083#define NOTIFICATION_QUEUE_SIZE 10
84#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020085#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020086
87/* sleep in master process for non-blocking socket reading */
88#define SLEEP_TIME 200
89
90#ifndef offsetof
91#define offsetof(type, member) ((size_t) ((type *) 0)->member)
92#endif
93
Tomas Cejka027f3bc2012-11-10 20:28:36 +010094/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020095struct timeval timeout = { 1, 0 };
96
Tomas Cejka5064c232013-01-17 09:30:58 +010097#define NCWITHDEFAULTS NCWD_MODE_NOTSET
98
99
Radek Krejci469aab82012-07-22 18:42:20 +0200100#define MSG_OK 0
101#define MSG_OPEN 1
102#define MSG_DATA 2
103#define MSG_CLOSE 3
104#define MSG_ERROR 4
105#define MSG_UNKNOWN 5
106
Radek Krejci469aab82012-07-22 18:42:20 +0200107module AP_MODULE_DECLARE_DATA netconf_module;
108
Tomas Cejka47387fd2013-06-10 20:37:46 +0200109pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200110pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200111apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200112
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200113static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200114server_rec *http_server = NULL;
115
Radek Krejci469aab82012-07-22 18:42:20 +0200116volatile int isterminated = 0;
117
118static char* password;
119
Radek Krejci469aab82012-07-22 18:42:20 +0200120static void signal_handler(int sign)
121{
122 switch (sign) {
123 case SIGTERM:
124 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200125 break;
126 }
127}
128
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200129static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200130{
Radek Krejcif23850c2012-07-23 16:14:17 +0200131 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
132 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200133 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200134
Radek Krejci469aab82012-07-22 18:42:20 +0200135 apr_sha1_ctx_t sha1_ctx;
136 apr_sha1_init(&sha1_ctx);
137 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
138 apr_sha1_update(&sha1_ctx, port, strlen(port));
139 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200140 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200141
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200144 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200146 }
147 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200148
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200150}
151
152int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
153{
154 /* always approve */
155 return (EXIT_SUCCESS);
156}
157
158char* netconf_callback_sshauth_password (const char* username, const char* hostname)
159{
160 char* buf;
161
162 buf = malloc ((strlen(password) + 1) * sizeof(char));
163 apr_cpystrn(buf, password, strlen(password) + 1);
164
165 return (buf);
166}
167
168void netconf_callback_sshauth_interactive (const char* name,
169 int name_len,
170 const char* instruction,
171 int instruction_len,
172 int num_prompts,
173 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
174 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
175 void** abstract)
176{
177 int i;
178
179 for (i=0; i<num_prompts; i++) {
180 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
181 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
182 responses[i].length = strlen(responses[i].text) + 1;
183 }
184
185 return;
186}
187
Radek Krejcic11fd862012-07-26 12:41:21 +0200188static json_object *err_reply = NULL;
189void netconf_callback_error_process(const char* tag,
190 const char* type,
191 const char* severity,
192 const char* apptag,
193 const char* path,
194 const char* message,
195 const char* attribute,
196 const char* element,
197 const char* ns,
198 const char* sid)
199{
200 err_reply = json_object_new_object();
201 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
202 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
203 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
204 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
205 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
206 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
207 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
208 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
209 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
210 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
211 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
212}
213
Tomas Cejka47387fd2013-06-10 20:37:46 +0200214/**
215 * should be used in locked area
216 */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200217void prepare_status_message(server_rec* server, struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200218{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200219 json_object *json_obj = NULL;
Tomas Cejka73286932013-05-27 22:54:35 +0200220 //json_object *old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200221 const char *cpbltstr;
222 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200223
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200224 if (s == NULL) {
225 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "No session given.");
226 return;
227 }
228
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200229 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200230 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200231 //json_object_put(s->hello_message);
232 s->hello_message = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200233
234 //old_sid = json_object_object_get(s->hello_message, "sid");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200235 }
236 s->hello_message = json_object_new_object();
237 if (session != NULL) {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200238 /** \todo reload hello - save old sid */
239 //if (old_sid != NULL) {
240 // /* use previous sid */
241 // json_object_object_add(s->hello_message, "sid", old_sid);
242 //} else {
243 /* we don't have old sid */
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200244 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200245 //}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200246 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
247 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
248 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
249 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
250 cpblts = nc_session_get_cpblts (session);
251 if (cpblts != NULL) {
252 json_obj = json_object_new_array();
253 nc_cpblts_iter_start (cpblts);
254 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
255 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
256 }
257 json_object_object_add(s->hello_message, "capabilities", json_obj);
258 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200259 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200260 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200261 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200262 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
263 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
264 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200265 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200266
267}
268
269
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200270/**
271 * \defgroup netconf_operations NETCONF operations
272 * The list of NETCONF operations that mod_netconf supports.
273 * @{
274 */
275
276/**
277 * \brief Connect to NETCONF server
278 *
279 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
280 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200281static char* netconf_connect(server_rec* server, 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 +0200282{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200283 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200284 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200285 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200286
287 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200288 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200289 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
290 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200291 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200292 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200293
Radek Krejci469aab82012-07-22 18:42:20 +0200294 /* if connected successful, add session to the list */
295 if (session != NULL) {
296 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200297 session_key = gen_ncsession_hash(
298 (host==NULL) ? "localhost" : host,
299 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200300 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200301
Tomas Cejkaba21b382013-04-13 02:37:32 +0200302 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200303 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
304 nc_session_free(session);
305 free (locked_session);
306 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
307 return NULL;
308 }
309 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200310 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200311 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200312 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200313 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100314 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200315 /* get exclusive access to sessions_list (conns) */
316 if (pthread_rwlock_wrlock (&session_lock) != 0) {
317 nc_session_free(session);
318 free (locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200319 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200320 return NULL;
321 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200322 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200323 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100324 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200325 apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100326 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200327
Tomas Cejka47387fd2013-06-10 20:37:46 +0200328 /* lock session */
329 pthread_mutex_lock(&locked_session->lock);
330
331 /* unlock session list */
332 if (pthread_rwlock_unlock (&session_lock) != 0) {
333 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
334 }
335
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200336 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200337 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200338
Tomas Cejka47387fd2013-06-10 20:37:46 +0200339 pthread_mutex_unlock(&locked_session->lock);
340
Radek Krejci469aab82012-07-22 18:42:20 +0200341 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200342 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200343 } else {
344 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200345 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200346 }
347
Radek Krejci469aab82012-07-22 18:42:20 +0200348}
349
Tomas Cejka47387fd2013-06-10 20:37:46 +0200350static int close_and_free_session(server_rec *server, struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200351{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200352 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock private lock.");
353 if (pthread_mutex_lock(&locked_session->lock) != 0) {
354 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
355 }
356 locked_session->ntfc_subscribed = 0;
357 locked_session->closed = 1;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200358 nc_session_free(locked_session->session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200359 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "session closed.");
360 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock private lock.");
361 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
362 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
363 }
364
365 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
366 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "closed session, disabled notif(?), wait 2s");
367 usleep(500000); /* let notification thread stop */
368
369 /* session shouldn't be used by now */
370 /** \todo free all notifications from queue */
371 apr_array_clear(locked_session->notifications);
372 pthread_mutex_destroy(&locked_session->lock);
373 if (locked_session->hello_message != NULL) {
374 /** \todo free hello_message */
375 //json_object_put(locked_session->hello_message);
376 locked_session->hello_message = NULL;
377 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200378 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200379 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200380 locked_session = NULL;
381 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed, everything cleared.");
382 return (EXIT_SUCCESS);
383}
384
385static int netconf_close(server_rec* server, const char* session_key)
386{
David Kupka8e60a372012-09-04 09:15:20 +0200387 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200388
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200389 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200390
David Kupka8e60a372012-09-04 09:15:20 +0200391 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200392 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200393 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200394 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
David Kupka8e60a372012-09-04 09:15:20 +0200395 return EXIT_FAILURE;
396 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200397 /* get session to close */
398 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
399 /* remove session from the active sessions list -> nobody new can now work with session */
400 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200401
Tomas Cejka47387fd2013-06-10 20:37:46 +0200402 if (pthread_rwlock_unlock (&session_lock) != 0) {
403 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock");
404 }
405
406 if ((locked_session != NULL) && (locked_session->session != NULL)) {
407 return close_and_free_session(server, locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200408 } else {
409 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200410 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200411 }
412}
413
Tomas Cejka6b886e02013-07-05 09:53:17 +0200414int netconf_process_op(server_rec* server, struct nc_session *session, nc_rpc* rpc)
415{
416 nc_reply* reply = NULL;
417 int retval = EXIT_SUCCESS;
418 NC_MSG_TYPE msgt;
419 NC_REPLY_TYPE replyt;
420
421 /* check requests */
422 if (rpc == NULL) {
423 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
424 return (EXIT_FAILURE);
425 }
426
427 if (session != NULL) {
428 /* send the request and get the reply */
429 msgt = nc_session_send_recv(session, rpc, &reply);
430 /* process the result of the operation */
431 switch (msgt) {
432 case NC_MSG_UNKNOWN:
433 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
434 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
435 return (EXIT_FAILURE);
436 }
437 /* no break */
438 case NC_MSG_NONE:
439 /* there is error handled by callback */
440 return (EXIT_FAILURE);
441 break;
442 case NC_MSG_REPLY:
443 switch (replyt = nc_reply_get_type(reply)) {
444 case NC_REPLY_OK:
445 retval = EXIT_SUCCESS;
446 break;
447 default:
448 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
449 retval = EXIT_FAILURE;
450 break;
451 }
452 break;
453 default:
454 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
455 retval = EXIT_FAILURE;
456 break;
457 }
458 nc_reply_free(reply);
459 return (retval);
460 } else {
461 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
462 return (EXIT_FAILURE);
463 }
464}
465
Tomas Cejka47387fd2013-06-10 20:37:46 +0200466int netconf_op(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200467{
468 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200469 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200470 nc_reply* reply = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200471 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100472 NC_MSG_TYPE msgt;
473 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200474
Radek Krejci8e4632a2012-07-26 13:40:34 +0200475 /* check requests */
476 if (rpc == NULL) {
477 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
478 return (EXIT_FAILURE);
479 }
480
David Kupka8e60a372012-09-04 09:15:20 +0200481 /* get non-exclusive (read) access to sessions_list (conns) */
482 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200483 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200484 return EXIT_FAILURE;
485 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200486 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200487 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 +0200488 if (locked_session != NULL) {
489 session = locked_session->session;
490 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200491 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200492 /* get exclusive access to session */
493 if (pthread_mutex_lock(&locked_session->lock) != 0) {
494 /* unlock before returning error */
495 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200496 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200497 return EXIT_FAILURE;
498 }
499 return EXIT_FAILURE;
500 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200501 if (pthread_rwlock_unlock(&session_lock) != 0) {
502 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
503 }
504
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200505 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200506 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100507 msgt = nc_session_send_recv(session, rpc, &reply);
508
David Kupka8e60a372012-09-04 09:15:20 +0200509 /* first release exclusive lock for this session */
510 pthread_mutex_unlock(&locked_session->lock);
511 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200512
Radek Krejcia332b692012-11-12 16:15:54 +0100513 /* process the result of the operation */
514 switch (msgt) {
515 case NC_MSG_UNKNOWN:
516 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
517 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200518 netconf_close(server, session_key);
Radek Krejcia332b692012-11-12 16:15:54 +0100519 return (EXIT_FAILURE);
520 }
521 /* no break */
522 case NC_MSG_NONE:
523 /* there is error handled by callback */
524 return (EXIT_FAILURE);
525 break;
526 case NC_MSG_REPLY:
527 switch (replyt = nc_reply_get_type(reply)) {
528 case NC_REPLY_OK:
529 retval = EXIT_SUCCESS;
530 break;
531 default:
532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
533 retval = EXIT_FAILURE;
534 break;
535 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200536 break;
537 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100538 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200539 retval = EXIT_FAILURE;
540 break;
541 }
542 nc_reply_free(reply);
543 return (retval);
544 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100545 /* release lock on failure */
546 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200547 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100548 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200549 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
550 return (EXIT_FAILURE);
551 }
552}
Radek Krejci80c10d92012-07-30 08:38:50 +0200553
Tomas Cejka47387fd2013-06-10 20:37:46 +0200554static char* netconf_opdata(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200555{
556 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200557 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100558 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200559 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100560 NC_MSG_TYPE msgt;
561 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200562
563 /* check requests */
564 if (rpc == NULL) {
565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
566 return (NULL);
567 }
568
David Kupka8e60a372012-09-04 09:15:20 +0200569 /* get non-exclusive (read) access to sessions_list (conns) */
570 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200571 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200572 return NULL;
573 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200574 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200575 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
David Kupka8e60a372012-09-04 09:15:20 +0200576 if (locked_session != NULL) {
577 session = locked_session->session;
578 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200579 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200580 /* get exclusive access to session */
581 if (pthread_mutex_lock(&locked_session->lock) != 0) {
582 /* unlock before returning error */
583 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200584 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200585 }
586 return NULL;
587 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200588 if (pthread_rwlock_unlock (&session_lock) != 0) {
589 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
590 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200591 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200592 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100593 msgt = nc_session_send_recv(session, rpc, &reply);
594
David Kupka8e60a372012-09-04 09:15:20 +0200595 /* first release exclusive lock for this session */
596 pthread_mutex_unlock(&locked_session->lock);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200597
Radek Krejcia332b692012-11-12 16:15:54 +0100598 /* process the result of the operation */
599 switch (msgt) {
600 case NC_MSG_UNKNOWN:
601 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
602 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200603 netconf_close(server, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200604 return (NULL);
605 }
Radek Krejcia332b692012-11-12 16:15:54 +0100606 /* no break */
607 case NC_MSG_NONE:
608 /* there is error handled by callback */
609 return (NULL);
610 break;
611 case NC_MSG_REPLY:
612 switch (replyt = nc_reply_get_type(reply)) {
613 case NC_REPLY_DATA:
614 if ((data = nc_reply_get_data (reply)) == NULL) {
615 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
616 data = NULL;
617 }
618 break;
619 default:
620 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
621 data = NULL;
622 break;
623 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200624 break;
625 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100626 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
627 data = NULL;
628 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200629 }
630 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200631 return (data);
632 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100633 /* release lock on failure */
634 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200635 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100636 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200637 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
638 return (NULL);
639 }
640}
641
Tomas Cejka47387fd2013-06-10 20:37:46 +0200642static char* netconf_getconfig(server_rec* server, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200643{
644 nc_rpc* rpc;
645 struct nc_filter *f = NULL;
646 char* data = NULL;
647
648 /* create filter if set */
649 if (filter != NULL) {
650 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
651 }
652
653 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100654 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200655 nc_filter_free(f);
656 if (rpc == NULL) {
657 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
658 return (NULL);
659 }
660
Tomas Cejka94674662013-09-13 15:55:24 +0200661 /* tell server to show all elements even if they have default values */
662 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
663 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
664 }
665
Tomas Cejka47387fd2013-06-10 20:37:46 +0200666 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200667 nc_rpc_free (rpc);
668 return (data);
669}
670
Tomas Cejka47387fd2013-06-10 20:37:46 +0200671static char* netconf_getschema(server_rec* server, const char* session_key, const char* identifier, const char* version, const char* format)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100672{
673 nc_rpc* rpc;
674 char* data = NULL;
675
676 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100677 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100678 if (rpc == NULL) {
679 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
680 return (NULL);
681 }
682
Tomas Cejka47387fd2013-06-10 20:37:46 +0200683 data = netconf_opdata(server, session_key, rpc);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100684 nc_rpc_free (rpc);
685 return (data);
686}
687
Tomas Cejka47387fd2013-06-10 20:37:46 +0200688static char* netconf_get(server_rec* server, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200689{
690 nc_rpc* rpc;
691 struct nc_filter *f = NULL;
692 char* data = NULL;
693
694 /* create filter if set */
695 if (filter != NULL) {
696 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
697 }
698
699 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100700 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200701 nc_filter_free(f);
702 if (rpc == NULL) {
703 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
704 return (NULL);
705 }
706
Tomas Cejka94674662013-09-13 15:55:24 +0200707 /* tell server to show all elements even if they have default values */
708 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
709 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
710 }
711
Tomas Cejka47387fd2013-06-10 20:37:46 +0200712 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200713 nc_rpc_free (rpc);
714 return (data);
715}
716
Tomas Cejka47387fd2013-06-10 20:37:46 +0200717static int netconf_copyconfig(server_rec* server, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *url)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200718{
719 nc_rpc* rpc;
720 int retval = EXIT_SUCCESS;
721
722 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100723 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
724 if (target == NC_DATASTORE_URL) {
725 rpc = nc_rpc_copyconfig(source, target, config, url);
726 } else {
727 rpc = nc_rpc_copyconfig(source, target, config);
728 }
729 } else {
730 if (target == NC_DATASTORE_URL) {
731 rpc = nc_rpc_copyconfig(source, target, url);
732 } else {
733 rpc = nc_rpc_copyconfig(source, target);
734 }
735 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200736 if (rpc == NULL) {
737 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
738 return (EXIT_FAILURE);
739 }
740
Tomas Cejka47387fd2013-06-10 20:37:46 +0200741 retval = netconf_op(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200742 nc_rpc_free (rpc);
743 return (retval);
744}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200745
Tomas Cejka47387fd2013-06-10 20:37:46 +0200746static int netconf_editconfig(server_rec* server, const char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char* config)
Radek Krejci62ab34b2012-07-26 13:42:05 +0200747{
748 nc_rpc* rpc;
749 int retval = EXIT_SUCCESS;
750
751 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100752 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
753 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200754 if (rpc == NULL) {
755 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
756 return (EXIT_FAILURE);
757 }
758
Tomas Cejka47387fd2013-06-10 20:37:46 +0200759 retval = netconf_op(server, session_key, rpc);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200760 nc_rpc_free (rpc);
761 return (retval);
762}
763
Tomas Cejka47387fd2013-06-10 20:37:46 +0200764static int netconf_killsession(server_rec* server, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200765{
766 nc_rpc* rpc;
767 int retval = EXIT_SUCCESS;
768
769 /* create requests */
770 rpc = nc_rpc_killsession(sid);
771 if (rpc == NULL) {
772 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
773 return (EXIT_FAILURE);
774 }
775
Tomas Cejka47387fd2013-06-10 20:37:46 +0200776 retval = netconf_op(server, session_key, rpc);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200777 nc_rpc_free (rpc);
778 return (retval);
779}
780
Tomas Cejka47387fd2013-06-10 20:37:46 +0200781static int netconf_onlytargetop(server_rec* server, const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200782{
783 nc_rpc* rpc;
784 int retval = EXIT_SUCCESS;
785
786 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200787 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200788 if (rpc == NULL) {
789 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
790 return (EXIT_FAILURE);
791 }
792
Tomas Cejka47387fd2013-06-10 20:37:46 +0200793 retval = netconf_op(server, session_key, rpc);
Radek Krejci2f318372012-07-26 14:22:35 +0200794 nc_rpc_free (rpc);
795 return (retval);
796}
797
Tomas Cejka47387fd2013-06-10 20:37:46 +0200798static int netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200799{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200800 nc_rpc *rpc = NULL;
801 if (target != NC_DATASTORE_URL) {
802 rpc = nc_rpc_deleteconfig(target);
803 } else {
804 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
805 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
806 return (EXIT_FAILURE);
807 }
808
Tomas Cejka47387fd2013-06-10 20:37:46 +0200809 return netconf_op(server, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200810}
811
Tomas Cejka47387fd2013-06-10 20:37:46 +0200812static int netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200813{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200814 return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200815}
816
Tomas Cejka47387fd2013-06-10 20:37:46 +0200817static int netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200818{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200819 return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200820}
821
Radek Krejci80c10d92012-07-30 08:38:50 +0200822/**
823 * @return REPLY_OK: 0, *data == NULL
824 * REPLY_DATA: 0, *data != NULL
825 * REPLY_ERROR: 1, *data == NULL
826 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200827static int netconf_generic(server_rec* server, const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200828{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200829 struct session_with_mutex *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200830 nc_reply* reply = NULL;
831 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200832 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100833 NC_MSG_TYPE msgt;
834 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200835
836 /* create requests */
837 rpc = nc_rpc_generic(content);
838 if (rpc == NULL) {
839 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
840 return (EXIT_FAILURE);
841 }
842
Radek Krejcia332b692012-11-12 16:15:54 +0100843 if (data != NULL) {
844 *data = NULL;
845 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200846
Tomas Cejka47387fd2013-06-10 20:37:46 +0200847 if (pthread_rwlock_rdlock(&session_lock) != 0) {
848 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
849 return EXIT_FAILURE;
850 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200851 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200852 session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Radek Krejci80c10d92012-07-30 08:38:50 +0200853 if (session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200854 pthread_mutex_lock(&session->lock);
855 if (pthread_rwlock_unlock(&session_lock) != 0) {
856 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
857 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200858 /* send the request and get the reply */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200859 msgt = nc_session_send_recv(session->session, rpc, &reply);
860 nc_rpc_free(rpc);
861 pthread_mutex_unlock(&session->lock);
Radek Krejcia332b692012-11-12 16:15:54 +0100862
863 /* process the result of the operation */
864 switch (msgt) {
865 case NC_MSG_UNKNOWN:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200866 if (nc_session_get_status(session->session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci80c10d92012-07-30 08:38:50 +0200867 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200868 netconf_close(server, session_key);
Radek Krejci80c10d92012-07-30 08:38:50 +0200869 return (EXIT_FAILURE);
870 }
Radek Krejcia332b692012-11-12 16:15:54 +0100871 /* no break */
872 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200873 /* there is error handled by callback */
874 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200875 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100876 case NC_MSG_REPLY:
877 switch (replyt = nc_reply_get_type(reply)) {
878 case NC_REPLY_DATA:
879 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
880 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
881 nc_reply_free(reply);
882 return (EXIT_FAILURE);
883 }
884 retval = EXIT_SUCCESS;
885 break;
886 case NC_REPLY_OK:
887 retval = EXIT_SUCCESS;
888 break;
889 default:
890 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
891 retval = EXIT_FAILURE;
892 break;
893 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200894 break;
895 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100896 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200897 retval = EXIT_FAILURE;
898 break;
899 }
900 nc_reply_free(reply);
901
902 return (retval);
903 } else {
904 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200905 if (pthread_rwlock_unlock(&session_lock) != 0) {
906 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
907 return EXIT_FAILURE;
908 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200909 return (EXIT_FAILURE);
910 }
911}
912
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200913/**
914 * @}
915 *//* netconf_operations */
916
Radek Krejci469aab82012-07-22 18:42:20 +0200917server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200918void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200919{
Radek Krejci7338bde2012-08-10 12:57:30 +0200920 switch (level) {
921 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200922 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200923 break;
924 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200925 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200926 break;
927 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200928 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200929 break;
930 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200931 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200932 break;
933 }
Radek Krejci469aab82012-07-22 18:42:20 +0200934}
935
Tomas Cejka64b87482013-06-03 16:30:53 +0200936/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200937 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200938 * Caller should free message memory.
939 * \param[in] client socket descriptor of client
940 * \param[in] server httpd server for logging
941 * \return pointer to message
942 */
943char *get_framed_message(server_rec *server, int client)
944{
945 /* read json in chunked framing */
946 unsigned int buffer_size = 0;
947 ssize_t buffer_len = 0;
948 char *buffer = NULL;
949 char c;
950 ssize_t ret;
951 int i, chunk_len;
952 char chunk_len_str[12];
953
954 while (1) {
955 /* read chunk length */
956 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
957 if (buffer != NULL) {
958 free (buffer);
959 buffer = NULL;
960 }
961 break;
962 }
963 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
964 if (buffer != NULL) {
965 free (buffer);
966 buffer = NULL;
967 }
968 break;
969 }
970 i=0;
971 memset (chunk_len_str, 0, 12);
972 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
973 if (i==0 && c == '#') {
974 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
975 /* end but invalid */
976 if (buffer != NULL) {
977 free (buffer);
978 buffer = NULL;
979 }
980 }
981 /* end of message, double-loop break */
982 goto msg_complete;
983 }
984 chunk_len_str[i++] = c;
985 if (i==11) {
986 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
987 break;
988 }
989 }
990 if (c != '\n') {
991 if (buffer != NULL) {
992 free (buffer);
993 buffer = NULL;
994 }
995 break;
996 }
997 chunk_len_str[i] = 0;
998 if ((chunk_len = atoi (chunk_len_str)) == 0) {
999 if (buffer != NULL) {
1000 free (buffer);
1001 buffer = NULL;
1002 }
1003 break;
1004 }
1005 buffer_size += chunk_len+1;
1006 buffer = realloc (buffer, sizeof(char)*buffer_size);
1007 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1008 if (buffer != NULL) {
1009 free (buffer);
1010 buffer = NULL;
1011 }
1012 break;
1013 }
1014 buffer_len += ret;
1015 }
1016msg_complete:
1017 return buffer;
1018}
1019
Tomas Cejkad5b53772013-06-08 23:01:07 +02001020NC_DATASTORE parse_datastore(const char *ds)
1021{
1022 if (strcmp(ds, "running") == 0) {
1023 return NC_DATASTORE_RUNNING;
1024 } else if (strcmp(ds, "startup") == 0) {
1025 return NC_DATASTORE_STARTUP;
1026 } else if (strcmp(ds, "candidate") == 0) {
1027 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +02001028 } else if (strcmp(ds, "url") == 0) {
1029 return NC_DATASTORE_URL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001030 }
1031 return -1;
1032}
1033
1034json_object *create_error(const char *errmess)
1035{
1036 json_object *reply = json_object_new_object();
1037 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1038 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
1039 return reply;
1040
1041}
1042
1043json_object *create_data(const char *data)
1044{
1045 json_object *reply = json_object_new_object();
1046 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1047 json_object_object_add(reply, "data", json_object_new_string(data));
1048 return reply;
1049}
1050
Tomas Cejka47387fd2013-06-10 20:37:46 +02001051json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001052{
1053 const char *host = NULL;
1054 const char *port = NULL;
1055 const char *user = NULL;
1056 const char *pass = NULL;
1057 json_object *capabilities = NULL;
1058 json_object *reply = NULL;
1059 char *session_key_hash = NULL;
1060 struct nc_cpblts* cpblts = NULL;
1061 unsigned int len, i;
1062
1063 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
1064 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1065 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1066 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1067 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1068
1069 capabilities = json_object_object_get((json_object *) request, "capabilities");
1070 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1071 cpblts = nc_cpblts_new(NULL);
1072 for (i=0; i<len; i++) {
1073 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1074 }
1075 } else {
1076 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1077 }
1078
1079 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1080 if ((host == NULL) || (user == NULL)) {
1081 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1082 session_key_hash = NULL;
1083 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001084 session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001085 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1086 }
1087 if (cpblts != NULL) {
1088 nc_cpblts_free(cpblts);
1089 }
1090
1091 if (session_key_hash == NULL) {
1092 /* negative reply */
1093 if (err_reply == NULL) {
1094 reply = json_object_new_object();
1095 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1096 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
1097 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
1098 } else {
1099 /* use filled err_reply from libnetconf's callback */
1100 reply = err_reply;
1101 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
1102 }
1103 } else {
1104 /* positive reply */
1105 reply = json_object_new_object();
1106 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1107 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1108
1109 free(session_key_hash);
1110 }
1111 return reply;
1112}
1113
Tomas Cejka47387fd2013-06-10 20:37:46 +02001114json_object *handle_op_get(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001115{
1116 const char *filter = NULL;
1117 const char *data = NULL;
1118 json_object *reply = NULL;
1119
1120 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1121
1122 filter = json_object_get_string(json_object_object_get(request, "filter"));
1123
Tomas Cejka47387fd2013-06-10 20:37:46 +02001124 if ((data = netconf_get(server, session_key, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001125 if (err_reply == NULL) {
1126 reply = create_error("Get information from device failed.");
1127 } else {
1128 /* use filled err_reply from libnetconf's callback */
1129 reply = err_reply;
1130 }
1131 } else {
1132 return create_data(data);
1133 }
1134 return reply;
1135}
1136
Tomas Cejka47387fd2013-06-10 20:37:46 +02001137json_object *handle_op_getconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001138{
1139 NC_DATASTORE ds_type_s = -1;
1140 NC_DATASTORE ds_type_t = -1;
1141 const char *filter = NULL;
1142 const char *data = NULL;
1143 const char *source = NULL;
1144 const char *target = NULL;
1145 json_object *reply = NULL;
1146
1147 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1148
1149 filter = json_object_get_string(json_object_object_get(request, "filter"));
1150
1151 /* get parameters */
1152 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1153 ds_type_t = parse_datastore(target);
1154 }
1155 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1156 ds_type_s = parse_datastore(source);
1157 }
1158 if (ds_type_s == -1) {
1159 return create_error("Invalid source repository type requested.");
1160 }
1161
Tomas Cejka47387fd2013-06-10 20:37:46 +02001162 if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001163 if (err_reply == NULL) {
1164 reply = create_error("Get configuration information from device failed.");
1165 } else {
1166 /* use filled err_reply from libnetconf's callback */
1167 reply = err_reply;
1168 }
1169 } else {
1170 return create_data(data);
1171 }
1172 return reply;
1173}
1174
Tomas Cejka47387fd2013-06-10 20:37:46 +02001175json_object *handle_op_getschema(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001176{
1177 const char *data = NULL;
1178 const char *identifier = NULL;
1179 const char *version = NULL;
1180 const char *format = NULL;
1181 json_object *reply = NULL;
1182
1183 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1184 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1185 if (identifier == NULL) {
1186 return create_error("No identifier for get-schema supplied.");
1187 }
1188 version = json_object_get_string(json_object_object_get(request, "version"));
1189 format = json_object_get_string(json_object_object_get(request, "format"));
1190
1191 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001192 if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001193 if (err_reply == NULL) {
1194 reply = create_error("Get schema failed.");
1195 } else {
1196 /* use filled err_reply from libnetconf's callback */
1197 reply = err_reply;
1198 }
1199 } else {
1200 return create_data(data);
1201 }
1202 return reply;
1203}
1204
Tomas Cejka47387fd2013-06-10 20:37:46 +02001205json_object *handle_op_editconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001206{
1207 NC_DATASTORE ds_type_s = -1;
1208 NC_DATASTORE ds_type_t = -1;
1209 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1210 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1211 const char *defop = NULL;
1212 const char *erropt = NULL;
1213 const char *config = NULL;
1214 const char *source = NULL;
1215 const char *target = NULL;
1216 json_object *reply = NULL;
1217
1218 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1219
1220 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1221 if (defop != NULL) {
1222 if (strcmp(defop, "merge") == 0) {
1223 defop_type = NC_EDIT_DEFOP_MERGE;
1224 } else if (strcmp(defop, "replace") == 0) {
1225 defop_type = NC_EDIT_DEFOP_REPLACE;
1226 } else if (strcmp(defop, "none") == 0) {
1227 defop_type = NC_EDIT_DEFOP_NONE;
1228 } else {
1229 return create_error("Invalid default-operation parameter.");
1230 }
1231 } else {
1232 defop_type = NC_EDIT_DEFOP_NOTSET;
1233 }
1234
1235 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1236 if (erropt != NULL) {
1237 if (strcmp(erropt, "continue-on-error") == 0) {
1238 erropt_type = NC_EDIT_ERROPT_CONT;
1239 } else if (strcmp(erropt, "stop-on-error") == 0) {
1240 erropt_type = NC_EDIT_ERROPT_STOP;
1241 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1242 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1243 } else {
1244 return create_error("Invalid error-option parameter.");
1245 }
1246 } else {
1247 erropt_type = 0;
1248 }
1249
1250 /* get parameters */
1251 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1252 ds_type_t = parse_datastore(target);
1253 }
1254 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1255 ds_type_s = parse_datastore(source);
1256 }
1257 if (ds_type_t == -1) {
1258 return create_error("Invalid target repository type requested.");
1259 }
1260
1261 config = json_object_get_string(json_object_object_get(request, "config"));
1262 if (config == NULL) {
1263 return create_error("Invalid config data parameter.");
1264 }
1265
Tomas Cejka47387fd2013-06-10 20:37:46 +02001266 if (netconf_editconfig(server, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001267 if (err_reply == NULL) {
1268 reply = create_error("Edit-config failed.");
1269 } else {
1270 /* use filled err_reply from libnetconf's callback */
1271 reply = err_reply;
1272 }
1273 } else {
1274 reply = json_object_new_object();
1275 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1276 }
1277 return reply;
1278}
1279
Tomas Cejka47387fd2013-06-10 20:37:46 +02001280json_object *handle_op_copyconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001281{
1282 NC_DATASTORE ds_type_s = -1;
1283 NC_DATASTORE ds_type_t = -1;
1284 const char *config = NULL;
1285 const char *target = NULL;
1286 const char *source = NULL;
1287 json_object *reply = NULL;
1288
1289 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1290
1291 /* get parameters */
1292 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1293 ds_type_t = parse_datastore(target);
1294 }
1295 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1296 ds_type_s = parse_datastore(source);
1297 }
1298 if (source == NULL) {
1299 /* no explicit source specified -> use config data */
1300 ds_type_s = NC_DATASTORE_CONFIG;
1301 config = json_object_get_string(json_object_object_get(request, "config"));
1302 } else if (ds_type_s == -1) {
1303 /* source datastore specified, but it is invalid */
1304 return create_error("Invalid source repository type requested.");
1305 }
1306
1307 if (ds_type_t == -1) {
1308 /* invalid target datastore specified */
1309 return create_error("Invalid target repository type requested.");
1310 }
1311
1312 if (source == NULL && config == NULL) {
1313 reply = create_error("invalid input parameters - one of source and config is required.");
1314 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001315 if (netconf_copyconfig(server, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001316 if (err_reply == NULL) {
1317 reply = create_error("Copying of configuration failed.");
1318 } else {
1319 /* use filled err_reply from libnetconf's callback */
1320 reply = err_reply;
1321 }
1322 } else {
1323 reply = json_object_new_object();
1324 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1325 }
1326 }
1327 return reply;
1328}
1329
Tomas Cejka47387fd2013-06-10 20:37:46 +02001330json_object *handle_op_generic(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001331{
1332 json_object *reply = NULL;
1333 const char *config = NULL;
1334 char *data = NULL;
1335
1336 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1337
1338 config = json_object_get_string(json_object_object_get(request, "content"));
1339
1340 if (config == NULL) {
1341 return create_error("Missing content parameter.");
1342 }
1343
Tomas Cejka47387fd2013-06-10 20:37:46 +02001344 if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001345 if (err_reply == NULL) {
1346 reply = create_error("Killing of session failed.");
1347 } else {
1348 /* use filled err_reply from libnetconf's callback */
1349 reply = err_reply;
1350 }
1351 } else {
1352 if (data == NULL) {
1353 reply = json_object_new_object();
1354 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1355 } else {
1356 return create_data(data);
1357 }
1358 }
1359 return reply;
1360}
1361
Tomas Cejka47387fd2013-06-10 20:37:46 +02001362json_object *handle_op_disconnect(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001363{
1364 json_object *reply = NULL;
1365 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1366
Tomas Cejka47387fd2013-06-10 20:37:46 +02001367 if (netconf_close(server, session_key) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001368 if (err_reply == NULL) {
1369 reply = create_error("Invalid session identifier.");
1370 } else {
1371 /* use filled err_reply from libnetconf's callback */
1372 reply = err_reply;
1373 }
1374 } else {
1375 reply = json_object_new_object();
1376 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1377 }
1378 return reply;
1379}
1380
Tomas Cejka47387fd2013-06-10 20:37:46 +02001381json_object *handle_op_kill(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001382{
1383 json_object *reply = NULL;
1384 const char *sid = NULL;
1385
1386 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1387
1388 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1389
1390 if (sid == NULL) {
1391 return create_error("Missing session-id parameter.");
1392 }
1393
Tomas Cejka47387fd2013-06-10 20:37:46 +02001394 if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001395 if (err_reply == NULL) {
1396 reply = create_error("Killing of session failed.");
1397 } else {
1398 /* use filled err_reply from libnetconf's callback */
1399 reply = err_reply;
1400 }
1401 } else {
1402 reply = json_object_new_object();
1403 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1404 }
1405 return reply;
1406}
1407
Tomas Cejka47387fd2013-06-10 20:37:46 +02001408json_object *handle_op_reloadhello(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001409{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001410 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001411 struct session_with_mutex * locked_session = NULL;
1412 json_object *reply = NULL;
1413 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1414
Tomas Cejka47387fd2013-06-10 20:37:46 +02001415 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1416 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1417 return NULL;
1418 }
1419
Tomas Cejkad5b53772013-06-08 23:01:07 +02001420 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1421 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001422 pthread_mutex_lock(&locked_session->lock);
1423 if (pthread_rwlock_unlock(&session_lock) != 0) {
1424 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1425 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001427 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001428 if (temp_session != NULL) {
1429 prepare_status_message(server, locked_session, temp_session);
1430 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001431 nc_session_free(temp_session);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001432 } else {
1433 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1434 reply = create_error("Reload was unsuccessful, connection failed.");
1435 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001436 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001437 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001438 if (pthread_rwlock_unlock(&session_lock) != 0) {
1439 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1440 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001441 reply = create_error("Invalid session identifier.");
1442 }
1443
1444 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1445 reply = locked_session->hello_message;
1446 }
1447 return reply;
1448}
1449
Tomas Cejka47387fd2013-06-10 20:37:46 +02001450json_object *handle_op_info(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001451{
1452 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001453 struct session_with_mutex * locked_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001454 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1455
Tomas Cejka47387fd2013-06-10 20:37:46 +02001456 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1457 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1458 }
1459
Tomas Cejkad5b53772013-06-08 23:01:07 +02001460 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1461 if (locked_session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001462 pthread_mutex_lock(&locked_session->lock);
1463 if (pthread_rwlock_unlock(&session_lock) != 0) {
1464 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1465 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001466 if (locked_session->hello_message != NULL) {
1467 reply = locked_session->hello_message;
1468 } else {
1469 reply = create_error("Invalid session identifier.");
1470 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001471 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001473 if (pthread_rwlock_unlock(&session_lock) != 0) {
1474 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1475 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001476 reply = create_error("Invalid session identifier.");
1477 }
1478
Tomas Cejka47387fd2013-06-10 20:37:46 +02001479
Tomas Cejkad5b53772013-06-08 23:01:07 +02001480 return reply;
1481}
1482
Tomas Cejka6b886e02013-07-05 09:53:17 +02001483void notification_history(time_t eventtime, const char *content)
1484{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001485 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1486 if (notif_history_array == NULL) {
1487 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "No list of notification history found.");
1488 return;
1489 }
1490 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Got notification from history %lu.", (long unsigned) eventtime);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001491 json_object *notif = json_object_new_object();
1492 if (notif == NULL) {
1493 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Could not allocate memory for notification (json).");
1494 return;
1495 }
1496 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1497 json_object_object_add(notif, "content", json_object_new_string(content));
1498 json_object_array_add(notif_history_array, notif);
1499}
1500
1501json_object *handle_op_ntfgethistory(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1502{
1503 json_object *reply = NULL;
1504 const char *sid = NULL;
1505 struct session_with_mutex *locked_session = NULL;
1506 struct nc_session *temp_session = NULL;
1507 nc_rpc *rpc = NULL;
1508 time_t start = 0;
1509 time_t stop = 0;
1510 int64_t from, to;
1511
1512 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get notification history, session %s", session_key);
1513
1514 sid = json_object_get_string(json_object_object_get(request, "session"));
1515 from = json_object_get_int64(json_object_object_get(request, "from"));
1516 to = json_object_get_int64(json_object_object_get(request, "to"));
1517
1518 start = time(NULL) + from;
1519 stop = time(NULL) + to;
1520
1521 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "notification history interval %li %li", (long int) from, (long int) to);
1522
1523 if (sid == NULL) {
1524 return create_error("Missing session parameter.");
1525 }
1526
1527 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1528 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1529 return NULL;
1530 }
1531
1532 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1533 if (locked_session != NULL) {
1534 pthread_mutex_lock(&locked_session->lock);
1535 if (pthread_rwlock_unlock(&session_lock) != 0) {
1536 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1537 }
1538 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1539 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1540 if (temp_session != NULL) {
1541 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1542 if (rpc == NULL) {
1543 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "notifications: creating an rpc request failed.");
1544 return create_error("notifications: creating an rpc request failed.");
1545 }
1546
1547 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Send NC subscribe.");
1548 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejka6e8f4262013-07-10 09:20:19 +02001549 if (netconf_process_op(server, temp_session, rpc) != EXIT_SUCCESS) {
Tomas Cejka6b886e02013-07-05 09:53:17 +02001550 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Subscription RPC failed.");
1551 return create_error("Subscription RPC failed.");
1552 }
1553 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1554
1555 pthread_mutex_unlock(&locked_session->lock);
1556 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001557 json_object *notif_history_array = json_object_new_array();
1558 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
1559 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notif_history: cannot set thread-specific hash value.");
1560 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001561
1562 ncntf_dispatch_receive(temp_session, notification_history);
1563
1564 reply = json_object_new_object();
1565 json_object_object_add(reply, "notifications", notif_history_array);
1566 //json_object_put(notif_history_array);
1567
1568 pthread_mutex_unlock(&ntf_history_lock);
1569 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001570 nc_session_free(temp_session);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001571 } else {
1572 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get history of notification failed due to channel establishment");
1573 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1574 }
1575 pthread_mutex_unlock(&locked_session->lock);
1576 } else {
1577 if (pthread_rwlock_unlock(&session_lock) != 0) {
1578 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1579 }
1580 reply = create_error("Invalid session identifier.");
1581 }
1582
Tomas Cejka4003a702013-10-01 00:02:45 +02001583 return reply;
1584}
1585
1586json_object *handle_op_validate(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1587{
1588 json_object *reply = NULL;
1589 const char *sid = NULL;
1590 const char *target = NULL;
1591 const char *url = NULL;
1592 struct session_with_mutex *locked_session = NULL;
1593 nc_rpc *rpc = NULL;
1594 NC_DATASTORE target_ds;
1595
1596 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: validate datastore, session %s", session_key);
1597
1598 sid = json_object_get_string(json_object_object_get(request, "session"));
1599 target = json_object_get_string(json_object_object_get(request, "target"));
1600 url = json_object_get_string(json_object_object_get(request, "url"));
1601
1602
1603 if ((sid == NULL) || (target == NULL)) {
1604 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001605 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001606
1607 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1608 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1609 return NULL;
1610 }
1611
1612 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1613 if (locked_session != NULL) {
1614 pthread_mutex_lock(&locked_session->lock);
1615 if (pthread_rwlock_unlock(&session_lock) != 0) {
1616 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1617 }
1618 /* validation */
1619 target_ds = parse_datastore(target);
1620 if (target_ds == NC_DATASTORE_URL) {
1621 if (url != NULL) {
1622 rpc = nc_rpc_validate(target_ds, url);
1623 }
1624 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
1625 || (target_ds == NC_DATASTORE_CANDIDATE)) {
1626 rpc = nc_rpc_validate(target_ds);
1627 }
1628 if (rpc == NULL) {
1629 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
1630 reply = create_error("Creation of RPC request failed.");
1631 pthread_mutex_unlock(&locked_session->lock);
1632 return reply;
1633 }
1634
1635 if (netconf_op(server, session_key, rpc) == EXIT_SUCCESS) {
1636 reply = json_object_new_object();
1637 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1638 } else {
1639 reply = create_error("Validation was not successful.");
1640 }
1641 nc_rpc_free (rpc);
1642
1643 pthread_mutex_unlock(&locked_session->lock);
1644 } else {
1645 if (pthread_rwlock_unlock(&session_lock) != 0) {
1646 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1647 }
1648 reply = create_error("Invalid session identifier.");
1649 }
1650
Tomas Cejka6b886e02013-07-05 09:53:17 +02001651 return reply;
1652}
1653
David Kupka8e60a372012-09-04 09:15:20 +02001654void * thread_routine (void * arg)
1655{
1656 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001657 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001658 json_object *request = NULL;
1659 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001660 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001661 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001662 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001663 const char *session_key;
1664 const char *target = NULL;
1665 const char *source = NULL;
1666 NC_DATASTORE ds_type_t = -1;
1667 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001668 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001669 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
David Kupka8e60a372012-09-04 09:15:20 +02001670 server_rec * server = ((struct pass_to_thread*)arg)->server;
1671 int client = ((struct pass_to_thread*)arg)->client;
1672
Tomas Cejka00635972013-06-03 15:10:52 +02001673 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001674
1675 while (!isterminated) {
1676 fds.fd = client;
1677 fds.events = POLLIN;
1678 fds.revents = 0;
1679
1680 status = poll(&fds, 1, 1000);
1681
1682 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1683 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1684 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1685 continue;
1686 } else if (status < 0) {
1687 /* 0: poll time outed
1688 * close socket and ignore this request from the client, it can try it again
1689 * -1: poll failed
1690 * something wrong happend, close this socket and wait for another request
1691 */
1692 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1693 close(client);
1694 break;
1695 }
1696 /* status > 0 */
1697
1698 /* check the status of the socket */
1699
1700 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1701 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1702 /* close client's socket (it's probably already closed by client */
1703 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1704 close(client);
1705 break;
1706 }
1707
Tomas Cejka47387fd2013-06-10 20:37:46 +02001708 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message...");
Tomas Cejka64b87482013-06-03 16:30:53 +02001709 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001710
Tomas Cejka47387fd2013-06-10 20:37:46 +02001711 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001712 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001713 enum json_tokener_error jerr;
1714 request = json_tokener_parse_verbose(buffer, &jerr);
1715 if (jerr != json_tokener_success) {
1716 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1717 continue;
1718 }
David Kupka8e60a372012-09-04 09:15:20 +02001719 operation = json_object_get_int(json_object_object_get(request, "type"));
1720
Tomas Cejka64b87482013-06-03 16:30:53 +02001721 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001722 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001723 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1724 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001725 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001726 msgtext = json_object_to_json_string(reply);
1727 send(client, msgtext, strlen(msgtext) + 1, 0);
1728 json_object_put(reply);
1729 /* there is some stupid client, so close the connection to give a chance to some other client */
1730 close(client);
1731 break;
1732 }
1733
David Kupka8e60a372012-09-04 09:15:20 +02001734 /* null global JSON error-reply */
1735 err_reply = NULL;
1736
1737 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001738 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001739
1740 /* process required operation */
1741 switch (operation) {
1742 case MSG_CONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001743 reply = handle_op_connect(server, pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001744 break;
1745 case MSG_GET:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001746 reply = handle_op_get(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001747 break;
1748 case MSG_GETCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001749 reply = handle_op_getconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001750 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001751 case MSG_GETSCHEMA:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001752 reply = handle_op_getschema(server, pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001753 break;
David Kupka8e60a372012-09-04 09:15:20 +02001754 case MSG_EDITCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001755 reply = handle_op_editconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001756 break;
1757 case MSG_COPYCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001758 reply = handle_op_copyconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001759 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001760
David Kupka8e60a372012-09-04 09:15:20 +02001761 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001762 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001763 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001764 /* get parameters */
1765 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1766 ds_type_t = parse_datastore(target);
1767 }
1768 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1769 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001770 }
1771
1772 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001773 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001774 break;
1775 }
David Kupka8e60a372012-09-04 09:15:20 +02001776 switch(operation) {
1777 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001778 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001779 status = netconf_deleteconfig(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001780 break;
1781 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001782 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001783 status = netconf_lock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001784 break;
1785 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001786 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001787 status = netconf_unlock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001788 break;
1789 default:
1790 status = -1;
1791 break;
1792 }
1793
1794 if (status != EXIT_SUCCESS) {
1795 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001796 /** \todo more clever error message wanted */
1797 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001798 } else {
1799 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001800 reply = err_reply;
1801 }
1802 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001803 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001804 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1805 }
1806 break;
1807 case MSG_KILL:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001808 reply = handle_op_kill(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001809 break;
1810 case MSG_DISCONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001811 reply = handle_op_disconnect(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001812 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001813 case MSG_RELOADHELLO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001814 reply = handle_op_reloadhello(server, pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001815 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001816 case MSG_INFO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001817 reply = handle_op_info(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001818 break;
1819 case MSG_GENERIC:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001820 reply = handle_op_generic(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001821 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001822 case MSG_NTF_GETHISTORY:
1823 reply = handle_op_ntfgethistory(server, pool, request, session_key);
1824 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001825 case MSG_VALIDATE:
1826 reply = handle_op_validate(server, pool, request, session_key);
1827 break;
David Kupka8e60a372012-09-04 09:15:20 +02001828 default:
1829 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001830 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001831 break;
1832 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001833 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001834 json_object_put(request);
1835
Tomas Cejkad5b53772013-06-08 23:01:07 +02001836 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001837 /* send reply to caller */
1838 if (reply != NULL) {
1839 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001840 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001841 if (buffer != NULL) {
1842 free(buffer);
1843 buffer = NULL;
1844 }
David Kupka8e60a372012-09-04 09:15:20 +02001845 break;
1846 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001847 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001848 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001849 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001850 json_object_put(reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001851 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001852 free(chunked_out_msg);
1853 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001854 if (buffer != NULL) {
1855 free(buffer);
1856 buffer = NULL;
1857 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001858 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001859 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001860 break;
David Kupka8e60a372012-09-04 09:15:20 +02001861 }
1862 }
1863 }
David Kupka8e60a372012-09-04 09:15:20 +02001864 free (arg);
1865
1866 return retval;
1867}
1868
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001869/**
1870 * \brief Close all open NETCONF sessions.
1871 *
1872 * During termination of mod_netconf, it is useful to close all remaining
1873 * sessions. This function iterates over the list of sessions and close them
1874 * all.
1875 *
1876 * \param[in] server pointer to server_rec for logging
1877 * \param[in] p apr pool needed for hash table iterating
1878 * \param[in] ht hash table of session_with_mutex structs
1879 */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001880static void close_all_nc_sessions(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001881{
1882 apr_hash_index_t *hi;
1883 void *val = NULL;
1884 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001885 const char *hashed_key = NULL;
1886 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001887 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001888
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001889 /* get exclusive access to sessions_list (conns) */
1890 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1891 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1892 return;
1893 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001894 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001895 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1896 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1897 swm = (struct session_with_mutex *) val;
1898 if (swm != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001899 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001900 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001901
1902 /* close_and_free_session handles locking on its own */
1903 close_and_free_session(server, swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001904 }
1905 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001906 /* get exclusive access to sessions_list (conns) */
1907 if (pthread_rwlock_unlock (&session_lock) != 0) {
1908 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1909 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001910}
1911
Tomas Cejka47387fd2013-06-10 20:37:46 +02001912static void check_timeout_and_close(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001913{
1914 apr_hash_index_t *hi;
1915 void *val = NULL;
1916 struct nc_session *ns = NULL;
1917 struct session_with_mutex *swm = NULL;
1918 const char *hashed_key = NULL;
1919 apr_ssize_t hashed_key_length;
1920 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001921 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001922
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001923 /* get exclusive access to sessions_list (conns) */
1924 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1925 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1926 return;
1927 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001928 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001929 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1930 swm = (struct session_with_mutex *) val;
1931 if (swm == NULL) {
1932 continue;
1933 }
1934 ns = swm->session;
1935 if (ns == NULL) {
1936 continue;
1937 }
1938 pthread_mutex_lock(&swm->lock);
1939 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1940 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001941 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001942 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
1943 pthread_mutex_unlock(&swm->lock);
1944
1945 /* close_and_free_session handles locking on its own */
1946 close_and_free_session(server, swm);
1947 } else {
1948 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001949 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001950 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001951 /* get exclusive access to sessions_list (conns) */
1952 if (pthread_rwlock_unlock (&session_lock) != 0) {
1953 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1954 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001955}
1956
1957
1958/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001959 * This is actually implementation of NETCONF client
1960 * - requests are received from UNIX socket in the predefined format
1961 * - results are replied through the same way
1962 * - the daemon run as a separate process, but it is started and stopped
1963 * automatically by Apache.
1964 *
1965 */
Radek Krejci469aab82012-07-22 18:42:20 +02001966static void forked_proc(apr_pool_t * pool, server_rec * server)
1967{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001968 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001969 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001970 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001971 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001972 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001973 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001974 struct pass_to_thread * arg;
1975 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1976 struct timespec maxtime;
1977 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001978 #ifdef WITH_NOTIFICATIONS
1979 char use_notifications = 0;
1980 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001981
Tomas Cejka6b886e02013-07-05 09:53:17 +02001982 http_server = server;
1983
Tomas Cejka404d37e2013-04-13 02:31:35 +02001984 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001985 maxtime.tv_sec = 5;
1986 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001987
Radek Krejcif23850c2012-07-23 16:14:17 +02001988 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001989 unixd_setup_child();
1990
Radek Krejciae021c12012-07-25 18:03:52 +02001991 cfg = ap_get_module_config(server->module_config, &netconf_module);
1992 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001993 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1994 return;
1995 }
Radek Krejci469aab82012-07-22 18:42:20 +02001996
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001997 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001998 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1999 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
2000 return;
2001 }
2002
2003 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02002004 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02002005 unlink(local.sun_path);
2006 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
2007
2008 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
2009 if (errno == EADDRINUSE) {
2010 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
2011 close(lsock);
2012 exit(0);
2013 }
2014 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
2015 close(lsock);
2016 return;
2017 }
2018
2019 if (listen(lsock, MAX_SOCKET_CL) == -1) {
2020 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
2021 close(lsock);
2022 return;
2023 }
2024
Tomas Cejkaba21b382013-04-13 02:37:32 +02002025 /* prepare internal lists */
2026 netconf_sessions_list = apr_hash_make(pool);
2027
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002028 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02002029 if (notification_init(pool, server) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002030 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
2031 use_notifications = 0;
2032 } else {
2033 use_notifications = 1;
2034 }
2035 #endif
2036
Radek Krejci469aab82012-07-22 18:42:20 +02002037 /* setup libnetconf's callbacks */
2038 nc_verbosity(NC_VERB_DEBUG);
2039 clb_print_server = server;
2040 nc_callback_print(clb_print);
2041 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
2042 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
2043 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02002044 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02002045
2046 /* disable publickey authentication */
2047 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
2048
David Kupka8e60a372012-09-04 09:15:20 +02002049 /* create mutex protecting session list */
2050 pthread_rwlockattr_init(&lock_attrs);
2051 /* rwlock is shared only with threads in this process */
2052 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
2053 /* create rw lock */
2054 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
2055 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
2056 close (lsock);
2057 return;
2058 }
Tomas Cejkad016f9c2013-07-10 09:16:16 +02002059 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "init of notif_history_key.");
2060 if (pthread_key_create(&notif_history_key, NULL) != 0) {
2061 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "init of notif_history_key failed");
2062 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02002063
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002064 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002065 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002066 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002067 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002068 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002069 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002070 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
2071 }
2072 if (use_notifications == 1) {
2073 notification_handle();
2074 }
2075 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002076 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02002077 check_timeout_and_close(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002078 }
Radek Krejci469aab82012-07-22 18:42:20 +02002079
2080 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002081 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002082 if (((unsigned int)tv.tv_sec - olds) > 60) {
2083 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
2084 olds = tv.tv_sec;
2085 }
David Kupka8e60a372012-09-04 09:15:20 +02002086 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002087 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2088 apr_sleep(SLEEP_TIME);
2089 continue;
2090 } else if (client == -1 && (errno == EINTR)) {
2091 continue;
2092 } else if (client == -1) {
2093 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
2094 continue;
2095 }
Radek Krejci469aab82012-07-22 18:42:20 +02002096
2097 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002098 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002099
David Kupka8e60a372012-09-04 09:15:20 +02002100 arg = malloc (sizeof(struct pass_to_thread));
2101 arg->client = client;
2102 arg->pool = pool;
2103 arg->server = server;
2104 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002105
David Kupka8e60a372012-09-04 09:15:20 +02002106 /* start new thread. It will serve this particular request and then terminate */
2107 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
2108 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
2109 } else {
2110 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
2111 pthread_count++;
2112 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2113 ptids[pthread_count] = 0;
2114 }
Radek Krejci469aab82012-07-22 18:42:20 +02002115
David Kupka8e60a372012-09-04 09:15:20 +02002116 /* check if some thread already terminated, free some resources by joining it */
2117 for (i=0; i<pthread_count; i++) {
2118 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
2119 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
2120 pthread_count--;
2121 if (pthread_count > 0) {
2122 /* place last Thread ID on the place of joined one */
2123 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002124 }
Radek Krejci469aab82012-07-22 18:42:20 +02002125 }
2126 }
David Kupka8e60a372012-09-04 09:15:20 +02002127 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002128 }
2129
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002130 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002131 /* join all threads */
2132 for (i=0; i<pthread_count; i++) {
2133 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2134 }
2135 free (ptids);
2136
Radek Krejci469aab82012-07-22 18:42:20 +02002137 close(lsock);
2138
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002139 #ifdef WITH_NOTIFICATIONS
2140 notification_close();
2141 #endif
2142
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002143 /* close all NETCONF sessions */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002144 close_all_nc_sessions(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002145
David Kupka8e60a372012-09-04 09:15:20 +02002146 /* destroy rwlock */
2147 pthread_rwlock_destroy(&session_lock);
2148 pthread_rwlockattr_destroy(&lock_attrs);
2149
Radek Krejci469aab82012-07-22 18:42:20 +02002150 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
2151
2152 exit(APR_SUCCESS);
2153}
2154
Radek Krejcif23850c2012-07-23 16:14:17 +02002155static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002156{
Radek Krejcif23850c2012-07-23 16:14:17 +02002157 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
2158
2159 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2160 apr_pool_create(&config->pool, pool);
2161 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002162 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002163
2164 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002165}
2166
2167static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2168 apr_pool_t * plog, server_rec * s)
2169{
Radek Krejcif23850c2012-07-23 16:14:17 +02002170 mod_netconf_cfg *config;
2171 apr_status_t res;
2172
Radek Krejci469aab82012-07-22 18:42:20 +02002173 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002174 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002175 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002176
2177 /*
2178 * The following checks if this routine has been called before.
2179 * This is necessary because the parent process gets initialized
2180 * a couple of times as the server starts up.
2181 */
2182 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2183 if (!data) {
2184 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2185 return (OK);
2186 }
2187
Radek Krejcif23850c2012-07-23 16:14:17 +02002188 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
2189 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002190
Radek Krejcif23850c2012-07-23 16:14:17 +02002191 if (config && config->forkproc == NULL) {
2192 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2193 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002194 switch (res) {
2195 case APR_INCHILD:
2196 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002197 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002198 apr_signal(SIGTERM, signal_handler);
2199
2200 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02002201 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002202
2203 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002204 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002205
Radek Krejcif23850c2012-07-23 16:14:17 +02002206 /* I never should be here, wtf?!? */
2207 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002208 exit(APR_EGENERAL);
2209 break;
2210 case APR_INPARENT:
2211 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002212 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002213 break;
2214 default:
2215 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
2216 break;
2217 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002218 } else {
2219 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002220 }
2221
2222 return OK;
2223}
2224
Radek Krejci469aab82012-07-22 18:42:20 +02002225/**
2226 * Register module hooks
2227 */
2228static void mod_netconf_register_hooks(apr_pool_t * p)
2229{
Radek Krejcif23850c2012-07-23 16:14:17 +02002230 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02002231}
2232
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002233static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2234{
2235 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2236 return NULL;
2237}
2238
2239static const command_rec netconf_cmds[] = {
2240 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2241 {NULL}
2242};
2243
Radek Krejci469aab82012-07-22 18:42:20 +02002244/* Dispatch list for API hooks */
2245module AP_MODULE_DECLARE_DATA netconf_module = {
2246 STANDARD20_MODULE_STUFF,
2247 NULL, /* create per-dir config structures */
2248 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002249 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002250 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002251 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002252 mod_netconf_register_hooks /* register hooks */
2253};
Radek Krejcia332b692012-11-12 16:15:54 +01002254