blob: eb05259c3eeb1dae0b3820232e52519ddc1acd8d [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;
358 nc_session_close(locked_session->session, NC_SESSION_TERM_CLOSED);
359 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 }
378 nc_session_free(locked_session->session);
379 locked_session->session = NULL;
380 free (locked_session);
381 locked_session = NULL;
382 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed, everything cleared.");
383 return (EXIT_SUCCESS);
384}
385
386static int netconf_close(server_rec* server, const char* session_key)
387{
David Kupka8e60a372012-09-04 09:15:20 +0200388 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200389
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200390 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200391
David Kupka8e60a372012-09-04 09:15:20 +0200392 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200393 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200394 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200395 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
David Kupka8e60a372012-09-04 09:15:20 +0200396 return EXIT_FAILURE;
397 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200398 /* get session to close */
399 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
400 /* remove session from the active sessions list -> nobody new can now work with session */
401 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200402
Tomas Cejka47387fd2013-06-10 20:37:46 +0200403 if (pthread_rwlock_unlock (&session_lock) != 0) {
404 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock");
405 }
406
407 if ((locked_session != NULL) && (locked_session->session != NULL)) {
408 return close_and_free_session(server, locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200409 } else {
410 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200411 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200412 }
413}
414
Tomas Cejka6b886e02013-07-05 09:53:17 +0200415int netconf_process_op(server_rec* server, struct nc_session *session, nc_rpc* rpc)
416{
417 nc_reply* reply = NULL;
418 int retval = EXIT_SUCCESS;
419 NC_MSG_TYPE msgt;
420 NC_REPLY_TYPE replyt;
421
422 /* check requests */
423 if (rpc == NULL) {
424 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
425 return (EXIT_FAILURE);
426 }
427
428 if (session != NULL) {
429 /* send the request and get the reply */
430 msgt = nc_session_send_recv(session, rpc, &reply);
431 /* process the result of the operation */
432 switch (msgt) {
433 case NC_MSG_UNKNOWN:
434 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
435 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
436 return (EXIT_FAILURE);
437 }
438 /* no break */
439 case NC_MSG_NONE:
440 /* there is error handled by callback */
441 return (EXIT_FAILURE);
442 break;
443 case NC_MSG_REPLY:
444 switch (replyt = nc_reply_get_type(reply)) {
445 case NC_REPLY_OK:
446 retval = EXIT_SUCCESS;
447 break;
448 default:
449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
450 retval = EXIT_FAILURE;
451 break;
452 }
453 break;
454 default:
455 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
456 retval = EXIT_FAILURE;
457 break;
458 }
459 nc_reply_free(reply);
460 return (retval);
461 } else {
462 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
463 return (EXIT_FAILURE);
464 }
465}
466
Tomas Cejka47387fd2013-06-10 20:37:46 +0200467int netconf_op(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200468{
469 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200470 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200471 nc_reply* reply = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200472 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100473 NC_MSG_TYPE msgt;
474 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200475
Radek Krejci8e4632a2012-07-26 13:40:34 +0200476 /* check requests */
477 if (rpc == NULL) {
478 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
479 return (EXIT_FAILURE);
480 }
481
David Kupka8e60a372012-09-04 09:15:20 +0200482 /* get non-exclusive (read) access to sessions_list (conns) */
483 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200484 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 +0200485 return EXIT_FAILURE;
486 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200487 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200488 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 +0200489 if (locked_session != NULL) {
490 session = locked_session->session;
491 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200492 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200493 /* get exclusive access to session */
494 if (pthread_mutex_lock(&locked_session->lock) != 0) {
495 /* unlock before returning error */
496 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200497 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 +0200498 return EXIT_FAILURE;
499 }
500 return EXIT_FAILURE;
501 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200502 if (pthread_rwlock_unlock(&session_lock) != 0) {
503 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
504 }
505
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200506 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200507 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100508 msgt = nc_session_send_recv(session, rpc, &reply);
509
David Kupka8e60a372012-09-04 09:15:20 +0200510 /* first release exclusive lock for this session */
511 pthread_mutex_unlock(&locked_session->lock);
512 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200513
Radek Krejcia332b692012-11-12 16:15:54 +0100514 /* process the result of the operation */
515 switch (msgt) {
516 case NC_MSG_UNKNOWN:
517 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
518 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200519 netconf_close(server, session_key);
Radek Krejcia332b692012-11-12 16:15:54 +0100520 return (EXIT_FAILURE);
521 }
522 /* no break */
523 case NC_MSG_NONE:
524 /* there is error handled by callback */
525 return (EXIT_FAILURE);
526 break;
527 case NC_MSG_REPLY:
528 switch (replyt = nc_reply_get_type(reply)) {
529 case NC_REPLY_OK:
530 retval = EXIT_SUCCESS;
531 break;
532 default:
533 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
534 retval = EXIT_FAILURE;
535 break;
536 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200537 break;
538 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100539 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200540 retval = EXIT_FAILURE;
541 break;
542 }
543 nc_reply_free(reply);
544 return (retval);
545 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100546 /* release lock on failure */
547 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200548 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 +0100549 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200550 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
551 return (EXIT_FAILURE);
552 }
553}
Radek Krejci80c10d92012-07-30 08:38:50 +0200554
Tomas Cejka47387fd2013-06-10 20:37:46 +0200555static char* netconf_opdata(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200556{
557 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200558 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100559 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200560 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100561 NC_MSG_TYPE msgt;
562 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200563
564 /* check requests */
565 if (rpc == NULL) {
566 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
567 return (NULL);
568 }
569
David Kupka8e60a372012-09-04 09:15:20 +0200570 /* get non-exclusive (read) access to sessions_list (conns) */
571 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200572 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 +0200573 return NULL;
574 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200575 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200576 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 +0200577 if (locked_session != NULL) {
578 session = locked_session->session;
579 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200580 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200581 /* get exclusive access to session */
582 if (pthread_mutex_lock(&locked_session->lock) != 0) {
583 /* unlock before returning error */
584 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200585 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 +0200586 }
587 return NULL;
588 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200589 if (pthread_rwlock_unlock (&session_lock) != 0) {
590 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
591 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200592 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200593 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100594 msgt = nc_session_send_recv(session, rpc, &reply);
595
David Kupka8e60a372012-09-04 09:15:20 +0200596 /* first release exclusive lock for this session */
597 pthread_mutex_unlock(&locked_session->lock);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200598
Radek Krejcia332b692012-11-12 16:15:54 +0100599 /* process the result of the operation */
600 switch (msgt) {
601 case NC_MSG_UNKNOWN:
602 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
603 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200604 netconf_close(server, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200605 return (NULL);
606 }
Radek Krejcia332b692012-11-12 16:15:54 +0100607 /* no break */
608 case NC_MSG_NONE:
609 /* there is error handled by callback */
610 return (NULL);
611 break;
612 case NC_MSG_REPLY:
613 switch (replyt = nc_reply_get_type(reply)) {
614 case NC_REPLY_DATA:
615 if ((data = nc_reply_get_data (reply)) == NULL) {
616 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
617 data = NULL;
618 }
619 break;
620 default:
621 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
622 data = NULL;
623 break;
624 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200625 break;
626 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100627 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
628 data = NULL;
629 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200630 }
631 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200632 return (data);
633 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100634 /* release lock on failure */
635 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200636 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 +0100637 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200638 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
639 return (NULL);
640 }
641}
642
Tomas Cejka47387fd2013-06-10 20:37:46 +0200643static char* netconf_getconfig(server_rec* server, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200644{
645 nc_rpc* rpc;
646 struct nc_filter *f = NULL;
647 char* data = NULL;
648
649 /* create filter if set */
650 if (filter != NULL) {
651 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
652 }
653
654 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100655 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200656 nc_filter_free(f);
657 if (rpc == NULL) {
658 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
659 return (NULL);
660 }
661
Tomas Cejka47387fd2013-06-10 20:37:46 +0200662 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200663 nc_rpc_free (rpc);
664 return (data);
665}
666
Tomas Cejka47387fd2013-06-10 20:37:46 +0200667static 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 +0100668{
669 nc_rpc* rpc;
670 char* data = NULL;
671
672 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100673 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100674 if (rpc == NULL) {
675 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
676 return (NULL);
677 }
678
Tomas Cejka47387fd2013-06-10 20:37:46 +0200679 data = netconf_opdata(server, session_key, rpc);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100680 nc_rpc_free (rpc);
681 return (data);
682}
683
Tomas Cejka47387fd2013-06-10 20:37:46 +0200684static char* netconf_get(server_rec* server, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200685{
686 nc_rpc* rpc;
687 struct nc_filter *f = NULL;
688 char* data = NULL;
689
690 /* create filter if set */
691 if (filter != NULL) {
692 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
693 }
694
695 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100696 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200697 nc_filter_free(f);
698 if (rpc == NULL) {
699 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
700 return (NULL);
701 }
702
Tomas Cejka47387fd2013-06-10 20:37:46 +0200703 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200704 nc_rpc_free (rpc);
705 return (data);
706}
707
Tomas Cejka47387fd2013-06-10 20:37:46 +0200708static 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 +0200709{
710 nc_rpc* rpc;
711 int retval = EXIT_SUCCESS;
712
713 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100714 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
715 if (target == NC_DATASTORE_URL) {
716 rpc = nc_rpc_copyconfig(source, target, config, url);
717 } else {
718 rpc = nc_rpc_copyconfig(source, target, config);
719 }
720 } else {
721 if (target == NC_DATASTORE_URL) {
722 rpc = nc_rpc_copyconfig(source, target, url);
723 } else {
724 rpc = nc_rpc_copyconfig(source, target);
725 }
726 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200727 if (rpc == NULL) {
728 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
729 return (EXIT_FAILURE);
730 }
731
Tomas Cejka47387fd2013-06-10 20:37:46 +0200732 retval = netconf_op(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200733 nc_rpc_free (rpc);
734 return (retval);
735}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200736
Tomas Cejka47387fd2013-06-10 20:37:46 +0200737static 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 +0200738{
739 nc_rpc* rpc;
740 int retval = EXIT_SUCCESS;
741
742 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100743 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
744 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200745 if (rpc == NULL) {
746 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
747 return (EXIT_FAILURE);
748 }
749
Tomas Cejka47387fd2013-06-10 20:37:46 +0200750 retval = netconf_op(server, session_key, rpc);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200751 nc_rpc_free (rpc);
752 return (retval);
753}
754
Tomas Cejka47387fd2013-06-10 20:37:46 +0200755static int netconf_killsession(server_rec* server, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200756{
757 nc_rpc* rpc;
758 int retval = EXIT_SUCCESS;
759
760 /* create requests */
761 rpc = nc_rpc_killsession(sid);
762 if (rpc == NULL) {
763 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
764 return (EXIT_FAILURE);
765 }
766
Tomas Cejka47387fd2013-06-10 20:37:46 +0200767 retval = netconf_op(server, session_key, rpc);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200768 nc_rpc_free (rpc);
769 return (retval);
770}
771
Tomas Cejka47387fd2013-06-10 20:37:46 +0200772static 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 +0200773{
774 nc_rpc* rpc;
775 int retval = EXIT_SUCCESS;
776
777 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200778 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200779 if (rpc == NULL) {
780 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
781 return (EXIT_FAILURE);
782 }
783
Tomas Cejka47387fd2013-06-10 20:37:46 +0200784 retval = netconf_op(server, session_key, rpc);
Radek Krejci2f318372012-07-26 14:22:35 +0200785 nc_rpc_free (rpc);
786 return (retval);
787}
788
Tomas Cejka47387fd2013-06-10 20:37:46 +0200789static int netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200790{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200791 nc_rpc *rpc = NULL;
792 if (target != NC_DATASTORE_URL) {
793 rpc = nc_rpc_deleteconfig(target);
794 } else {
795 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
796 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
797 return (EXIT_FAILURE);
798 }
799
Tomas Cejka47387fd2013-06-10 20:37:46 +0200800 return netconf_op(server, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200801}
802
Tomas Cejka47387fd2013-06-10 20:37:46 +0200803static int netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200804{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200805 return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200806}
807
Tomas Cejka47387fd2013-06-10 20:37:46 +0200808static int netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200809{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200810 return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200811}
812
Radek Krejci80c10d92012-07-30 08:38:50 +0200813/**
814 * @return REPLY_OK: 0, *data == NULL
815 * REPLY_DATA: 0, *data != NULL
816 * REPLY_ERROR: 1, *data == NULL
817 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200818static int netconf_generic(server_rec* server, const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200819{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200820 struct session_with_mutex *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200821 nc_reply* reply = NULL;
822 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200823 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100824 NC_MSG_TYPE msgt;
825 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200826
827 /* create requests */
828 rpc = nc_rpc_generic(content);
829 if (rpc == NULL) {
830 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
831 return (EXIT_FAILURE);
832 }
833
Radek Krejcia332b692012-11-12 16:15:54 +0100834 if (data != NULL) {
835 *data = NULL;
836 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200837
Tomas Cejka47387fd2013-06-10 20:37:46 +0200838 if (pthread_rwlock_rdlock(&session_lock) != 0) {
839 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
840 return EXIT_FAILURE;
841 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200842 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200843 session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Radek Krejci80c10d92012-07-30 08:38:50 +0200844 if (session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200845 pthread_mutex_lock(&session->lock);
846 if (pthread_rwlock_unlock(&session_lock) != 0) {
847 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
848 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200849 /* send the request and get the reply */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200850 msgt = nc_session_send_recv(session->session, rpc, &reply);
851 nc_rpc_free(rpc);
852 pthread_mutex_unlock(&session->lock);
Radek Krejcia332b692012-11-12 16:15:54 +0100853
854 /* process the result of the operation */
855 switch (msgt) {
856 case NC_MSG_UNKNOWN:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200857 if (nc_session_get_status(session->session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci80c10d92012-07-30 08:38:50 +0200858 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200859 netconf_close(server, session_key);
Radek Krejci80c10d92012-07-30 08:38:50 +0200860 return (EXIT_FAILURE);
861 }
Radek Krejcia332b692012-11-12 16:15:54 +0100862 /* no break */
863 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200864 /* there is error handled by callback */
865 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200866 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100867 case NC_MSG_REPLY:
868 switch (replyt = nc_reply_get_type(reply)) {
869 case NC_REPLY_DATA:
870 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
871 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
872 nc_reply_free(reply);
873 return (EXIT_FAILURE);
874 }
875 retval = EXIT_SUCCESS;
876 break;
877 case NC_REPLY_OK:
878 retval = EXIT_SUCCESS;
879 break;
880 default:
881 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
882 retval = EXIT_FAILURE;
883 break;
884 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200885 break;
886 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100887 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200888 retval = EXIT_FAILURE;
889 break;
890 }
891 nc_reply_free(reply);
892
893 return (retval);
894 } else {
895 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200896 if (pthread_rwlock_unlock(&session_lock) != 0) {
897 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
898 return EXIT_FAILURE;
899 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200900 return (EXIT_FAILURE);
901 }
902}
903
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200904/**
905 * @}
906 *//* netconf_operations */
907
Radek Krejci469aab82012-07-22 18:42:20 +0200908server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200909void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200910{
Radek Krejci7338bde2012-08-10 12:57:30 +0200911 switch (level) {
912 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200913 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200914 break;
915 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200916 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200917 break;
918 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200919 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200920 break;
921 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200922 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200923 break;
924 }
Radek Krejci469aab82012-07-22 18:42:20 +0200925}
926
Tomas Cejka64b87482013-06-03 16:30:53 +0200927/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200928 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200929 * Caller should free message memory.
930 * \param[in] client socket descriptor of client
931 * \param[in] server httpd server for logging
932 * \return pointer to message
933 */
934char *get_framed_message(server_rec *server, int client)
935{
936 /* read json in chunked framing */
937 unsigned int buffer_size = 0;
938 ssize_t buffer_len = 0;
939 char *buffer = NULL;
940 char c;
941 ssize_t ret;
942 int i, chunk_len;
943 char chunk_len_str[12];
944
945 while (1) {
946 /* read chunk length */
947 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
948 if (buffer != NULL) {
949 free (buffer);
950 buffer = NULL;
951 }
952 break;
953 }
954 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
955 if (buffer != NULL) {
956 free (buffer);
957 buffer = NULL;
958 }
959 break;
960 }
961 i=0;
962 memset (chunk_len_str, 0, 12);
963 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
964 if (i==0 && c == '#') {
965 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
966 /* end but invalid */
967 if (buffer != NULL) {
968 free (buffer);
969 buffer = NULL;
970 }
971 }
972 /* end of message, double-loop break */
973 goto msg_complete;
974 }
975 chunk_len_str[i++] = c;
976 if (i==11) {
977 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
978 break;
979 }
980 }
981 if (c != '\n') {
982 if (buffer != NULL) {
983 free (buffer);
984 buffer = NULL;
985 }
986 break;
987 }
988 chunk_len_str[i] = 0;
989 if ((chunk_len = atoi (chunk_len_str)) == 0) {
990 if (buffer != NULL) {
991 free (buffer);
992 buffer = NULL;
993 }
994 break;
995 }
996 buffer_size += chunk_len+1;
997 buffer = realloc (buffer, sizeof(char)*buffer_size);
998 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
999 if (buffer != NULL) {
1000 free (buffer);
1001 buffer = NULL;
1002 }
1003 break;
1004 }
1005 buffer_len += ret;
1006 }
1007msg_complete:
1008 return buffer;
1009}
1010
Tomas Cejkad5b53772013-06-08 23:01:07 +02001011NC_DATASTORE parse_datastore(const char *ds)
1012{
1013 if (strcmp(ds, "running") == 0) {
1014 return NC_DATASTORE_RUNNING;
1015 } else if (strcmp(ds, "startup") == 0) {
1016 return NC_DATASTORE_STARTUP;
1017 } else if (strcmp(ds, "candidate") == 0) {
1018 return NC_DATASTORE_CANDIDATE;
1019 }
1020 return -1;
1021}
1022
1023json_object *create_error(const char *errmess)
1024{
1025 json_object *reply = json_object_new_object();
1026 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1027 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
1028 return reply;
1029
1030}
1031
1032json_object *create_data(const char *data)
1033{
1034 json_object *reply = json_object_new_object();
1035 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1036 json_object_object_add(reply, "data", json_object_new_string(data));
1037 return reply;
1038}
1039
Tomas Cejka47387fd2013-06-10 20:37:46 +02001040json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001041{
1042 const char *host = NULL;
1043 const char *port = NULL;
1044 const char *user = NULL;
1045 const char *pass = NULL;
1046 json_object *capabilities = NULL;
1047 json_object *reply = NULL;
1048 char *session_key_hash = NULL;
1049 struct nc_cpblts* cpblts = NULL;
1050 unsigned int len, i;
1051
1052 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
1053 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1054 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1055 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1056 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1057
1058 capabilities = json_object_object_get((json_object *) request, "capabilities");
1059 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1060 cpblts = nc_cpblts_new(NULL);
1061 for (i=0; i<len; i++) {
1062 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1063 }
1064 } else {
1065 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1066 }
1067
1068 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1069 if ((host == NULL) || (user == NULL)) {
1070 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1071 session_key_hash = NULL;
1072 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001073 session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001074 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1075 }
1076 if (cpblts != NULL) {
1077 nc_cpblts_free(cpblts);
1078 }
1079
1080 if (session_key_hash == NULL) {
1081 /* negative reply */
1082 if (err_reply == NULL) {
1083 reply = json_object_new_object();
1084 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1085 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
1086 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
1087 } else {
1088 /* use filled err_reply from libnetconf's callback */
1089 reply = err_reply;
1090 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
1091 }
1092 } else {
1093 /* positive reply */
1094 reply = json_object_new_object();
1095 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1096 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1097
1098 free(session_key_hash);
1099 }
1100 return reply;
1101}
1102
Tomas Cejka47387fd2013-06-10 20:37:46 +02001103json_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 +02001104{
1105 const char *filter = NULL;
1106 const char *data = NULL;
1107 json_object *reply = NULL;
1108
1109 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1110
1111 filter = json_object_get_string(json_object_object_get(request, "filter"));
1112
Tomas Cejka47387fd2013-06-10 20:37:46 +02001113 if ((data = netconf_get(server, session_key, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001114 if (err_reply == NULL) {
1115 reply = create_error("Get information from device failed.");
1116 } else {
1117 /* use filled err_reply from libnetconf's callback */
1118 reply = err_reply;
1119 }
1120 } else {
1121 return create_data(data);
1122 }
1123 return reply;
1124}
1125
Tomas Cejka47387fd2013-06-10 20:37:46 +02001126json_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 +02001127{
1128 NC_DATASTORE ds_type_s = -1;
1129 NC_DATASTORE ds_type_t = -1;
1130 const char *filter = NULL;
1131 const char *data = NULL;
1132 const char *source = NULL;
1133 const char *target = NULL;
1134 json_object *reply = NULL;
1135
1136 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1137
1138 filter = json_object_get_string(json_object_object_get(request, "filter"));
1139
1140 /* get parameters */
1141 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1142 ds_type_t = parse_datastore(target);
1143 }
1144 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1145 ds_type_s = parse_datastore(source);
1146 }
1147 if (ds_type_s == -1) {
1148 return create_error("Invalid source repository type requested.");
1149 }
1150
Tomas Cejka47387fd2013-06-10 20:37:46 +02001151 if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001152 if (err_reply == NULL) {
1153 reply = create_error("Get configuration information from device failed.");
1154 } else {
1155 /* use filled err_reply from libnetconf's callback */
1156 reply = err_reply;
1157 }
1158 } else {
1159 return create_data(data);
1160 }
1161 return reply;
1162}
1163
Tomas Cejka47387fd2013-06-10 20:37:46 +02001164json_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 +02001165{
1166 const char *data = NULL;
1167 const char *identifier = NULL;
1168 const char *version = NULL;
1169 const char *format = NULL;
1170 json_object *reply = NULL;
1171
1172 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1173 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1174 if (identifier == NULL) {
1175 return create_error("No identifier for get-schema supplied.");
1176 }
1177 version = json_object_get_string(json_object_object_get(request, "version"));
1178 format = json_object_get_string(json_object_object_get(request, "format"));
1179
1180 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001181 if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001182 if (err_reply == NULL) {
1183 reply = create_error("Get schema failed.");
1184 } else {
1185 /* use filled err_reply from libnetconf's callback */
1186 reply = err_reply;
1187 }
1188 } else {
1189 return create_data(data);
1190 }
1191 return reply;
1192}
1193
Tomas Cejka47387fd2013-06-10 20:37:46 +02001194json_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 +02001195{
1196 NC_DATASTORE ds_type_s = -1;
1197 NC_DATASTORE ds_type_t = -1;
1198 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1199 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1200 const char *defop = NULL;
1201 const char *erropt = NULL;
1202 const char *config = NULL;
1203 const char *source = NULL;
1204 const char *target = NULL;
1205 json_object *reply = NULL;
1206
1207 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1208
1209 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1210 if (defop != NULL) {
1211 if (strcmp(defop, "merge") == 0) {
1212 defop_type = NC_EDIT_DEFOP_MERGE;
1213 } else if (strcmp(defop, "replace") == 0) {
1214 defop_type = NC_EDIT_DEFOP_REPLACE;
1215 } else if (strcmp(defop, "none") == 0) {
1216 defop_type = NC_EDIT_DEFOP_NONE;
1217 } else {
1218 return create_error("Invalid default-operation parameter.");
1219 }
1220 } else {
1221 defop_type = NC_EDIT_DEFOP_NOTSET;
1222 }
1223
1224 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1225 if (erropt != NULL) {
1226 if (strcmp(erropt, "continue-on-error") == 0) {
1227 erropt_type = NC_EDIT_ERROPT_CONT;
1228 } else if (strcmp(erropt, "stop-on-error") == 0) {
1229 erropt_type = NC_EDIT_ERROPT_STOP;
1230 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1231 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1232 } else {
1233 return create_error("Invalid error-option parameter.");
1234 }
1235 } else {
1236 erropt_type = 0;
1237 }
1238
1239 /* get parameters */
1240 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1241 ds_type_t = parse_datastore(target);
1242 }
1243 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1244 ds_type_s = parse_datastore(source);
1245 }
1246 if (ds_type_t == -1) {
1247 return create_error("Invalid target repository type requested.");
1248 }
1249
1250 config = json_object_get_string(json_object_object_get(request, "config"));
1251 if (config == NULL) {
1252 return create_error("Invalid config data parameter.");
1253 }
1254
Tomas Cejka47387fd2013-06-10 20:37:46 +02001255 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 +02001256 if (err_reply == NULL) {
1257 reply = create_error("Edit-config failed.");
1258 } else {
1259 /* use filled err_reply from libnetconf's callback */
1260 reply = err_reply;
1261 }
1262 } else {
1263 reply = json_object_new_object();
1264 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1265 }
1266 return reply;
1267}
1268
Tomas Cejka47387fd2013-06-10 20:37:46 +02001269json_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 +02001270{
1271 NC_DATASTORE ds_type_s = -1;
1272 NC_DATASTORE ds_type_t = -1;
1273 const char *config = NULL;
1274 const char *target = NULL;
1275 const char *source = NULL;
1276 json_object *reply = NULL;
1277
1278 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1279
1280 /* get parameters */
1281 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1282 ds_type_t = parse_datastore(target);
1283 }
1284 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1285 ds_type_s = parse_datastore(source);
1286 }
1287 if (source == NULL) {
1288 /* no explicit source specified -> use config data */
1289 ds_type_s = NC_DATASTORE_CONFIG;
1290 config = json_object_get_string(json_object_object_get(request, "config"));
1291 } else if (ds_type_s == -1) {
1292 /* source datastore specified, but it is invalid */
1293 return create_error("Invalid source repository type requested.");
1294 }
1295
1296 if (ds_type_t == -1) {
1297 /* invalid target datastore specified */
1298 return create_error("Invalid target repository type requested.");
1299 }
1300
1301 if (source == NULL && config == NULL) {
1302 reply = create_error("invalid input parameters - one of source and config is required.");
1303 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001304 if (netconf_copyconfig(server, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001305 if (err_reply == NULL) {
1306 reply = create_error("Copying of configuration failed.");
1307 } else {
1308 /* use filled err_reply from libnetconf's callback */
1309 reply = err_reply;
1310 }
1311 } else {
1312 reply = json_object_new_object();
1313 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1314 }
1315 }
1316 return reply;
1317}
1318
Tomas Cejka47387fd2013-06-10 20:37:46 +02001319json_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 +02001320{
1321 json_object *reply = NULL;
1322 const char *config = NULL;
1323 char *data = NULL;
1324
1325 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1326
1327 config = json_object_get_string(json_object_object_get(request, "content"));
1328
1329 if (config == NULL) {
1330 return create_error("Missing content parameter.");
1331 }
1332
Tomas Cejka47387fd2013-06-10 20:37:46 +02001333 if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001334 if (err_reply == NULL) {
1335 reply = create_error("Killing of session failed.");
1336 } else {
1337 /* use filled err_reply from libnetconf's callback */
1338 reply = err_reply;
1339 }
1340 } else {
1341 if (data == NULL) {
1342 reply = json_object_new_object();
1343 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1344 } else {
1345 return create_data(data);
1346 }
1347 }
1348 return reply;
1349}
1350
Tomas Cejka47387fd2013-06-10 20:37:46 +02001351json_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 +02001352{
1353 json_object *reply = NULL;
1354 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1355
Tomas Cejka47387fd2013-06-10 20:37:46 +02001356 if (netconf_close(server, session_key) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001357 if (err_reply == NULL) {
1358 reply = create_error("Invalid session identifier.");
1359 } else {
1360 /* use filled err_reply from libnetconf's callback */
1361 reply = err_reply;
1362 }
1363 } else {
1364 reply = json_object_new_object();
1365 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1366 }
1367 return reply;
1368}
1369
Tomas Cejka47387fd2013-06-10 20:37:46 +02001370json_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 +02001371{
1372 json_object *reply = NULL;
1373 const char *sid = NULL;
1374
1375 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1376
1377 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1378
1379 if (sid == NULL) {
1380 return create_error("Missing session-id parameter.");
1381 }
1382
Tomas Cejka47387fd2013-06-10 20:37:46 +02001383 if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001384 if (err_reply == NULL) {
1385 reply = create_error("Killing of session failed.");
1386 } else {
1387 /* use filled err_reply from libnetconf's callback */
1388 reply = err_reply;
1389 }
1390 } else {
1391 reply = json_object_new_object();
1392 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1393 }
1394 return reply;
1395}
1396
Tomas Cejka47387fd2013-06-10 20:37:46 +02001397json_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 +02001398{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001399 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001400 struct session_with_mutex * locked_session = NULL;
1401 json_object *reply = NULL;
1402 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1403
Tomas Cejka47387fd2013-06-10 20:37:46 +02001404 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1405 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1406 return NULL;
1407 }
1408
Tomas Cejkad5b53772013-06-08 23:01:07 +02001409 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1410 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001411 pthread_mutex_lock(&locked_session->lock);
1412 if (pthread_rwlock_unlock(&session_lock) != 0) {
1413 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1414 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001415 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001416 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001417 if (temp_session != NULL) {
1418 prepare_status_message(server, locked_session, temp_session);
1419 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1420 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1421 } else {
1422 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1423 reply = create_error("Reload was unsuccessful, connection failed.");
1424 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001425 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001426 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001427 if (pthread_rwlock_unlock(&session_lock) != 0) {
1428 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1429 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001430 reply = create_error("Invalid session identifier.");
1431 }
1432
1433 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1434 reply = locked_session->hello_message;
1435 }
1436 return reply;
1437}
1438
Tomas Cejka47387fd2013-06-10 20:37:46 +02001439json_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 +02001440{
1441 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001442 struct session_with_mutex * locked_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001443 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1444
Tomas Cejka47387fd2013-06-10 20:37:46 +02001445 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1446 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1447 }
1448
Tomas Cejkad5b53772013-06-08 23:01:07 +02001449 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1450 if (locked_session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001451 pthread_mutex_lock(&locked_session->lock);
1452 if (pthread_rwlock_unlock(&session_lock) != 0) {
1453 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1454 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001455 if (locked_session->hello_message != NULL) {
1456 reply = locked_session->hello_message;
1457 } else {
1458 reply = create_error("Invalid session identifier.");
1459 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001460 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001461 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001462 if (pthread_rwlock_unlock(&session_lock) != 0) {
1463 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1464 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001465 reply = create_error("Invalid session identifier.");
1466 }
1467
Tomas Cejka47387fd2013-06-10 20:37:46 +02001468
Tomas Cejkad5b53772013-06-08 23:01:07 +02001469 return reply;
1470}
1471
Tomas Cejka6b886e02013-07-05 09:53:17 +02001472void notification_history(time_t eventtime, const char *content)
1473{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001474 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1475 if (notif_history_array == NULL) {
1476 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "No list of notification history found.");
1477 return;
1478 }
1479 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 +02001480 json_object *notif = json_object_new_object();
1481 if (notif == NULL) {
1482 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Could not allocate memory for notification (json).");
1483 return;
1484 }
1485 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1486 json_object_object_add(notif, "content", json_object_new_string(content));
1487 json_object_array_add(notif_history_array, notif);
1488}
1489
1490json_object *handle_op_ntfgethistory(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1491{
1492 json_object *reply = NULL;
1493 const char *sid = NULL;
1494 struct session_with_mutex *locked_session = NULL;
1495 struct nc_session *temp_session = NULL;
1496 nc_rpc *rpc = NULL;
1497 time_t start = 0;
1498 time_t stop = 0;
1499 int64_t from, to;
1500
1501 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get notification history, session %s", session_key);
1502
1503 sid = json_object_get_string(json_object_object_get(request, "session"));
1504 from = json_object_get_int64(json_object_object_get(request, "from"));
1505 to = json_object_get_int64(json_object_object_get(request, "to"));
1506
1507 start = time(NULL) + from;
1508 stop = time(NULL) + to;
1509
1510 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "notification history interval %li %li", (long int) from, (long int) to);
1511
1512 if (sid == NULL) {
1513 return create_error("Missing session parameter.");
1514 }
1515
1516 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1517 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1518 return NULL;
1519 }
1520
1521 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1522 if (locked_session != NULL) {
1523 pthread_mutex_lock(&locked_session->lock);
1524 if (pthread_rwlock_unlock(&session_lock) != 0) {
1525 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1526 }
1527 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1528 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1529 if (temp_session != NULL) {
1530 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1531 if (rpc == NULL) {
1532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "notifications: creating an rpc request failed.");
1533 return create_error("notifications: creating an rpc request failed.");
1534 }
1535
1536 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Send NC subscribe.");
1537 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejka6e8f4262013-07-10 09:20:19 +02001538 if (netconf_process_op(server, temp_session, rpc) != EXIT_SUCCESS) {
Tomas Cejka6b886e02013-07-05 09:53:17 +02001539 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Subscription RPC failed.");
1540 return create_error("Subscription RPC failed.");
1541 }
1542 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1543
1544 pthread_mutex_unlock(&locked_session->lock);
1545 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001546 json_object *notif_history_array = json_object_new_array();
1547 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
1548 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notif_history: cannot set thread-specific hash value.");
1549 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001550
1551 ncntf_dispatch_receive(temp_session, notification_history);
1552
1553 reply = json_object_new_object();
1554 json_object_object_add(reply, "notifications", notif_history_array);
1555 //json_object_put(notif_history_array);
1556
1557 pthread_mutex_unlock(&ntf_history_lock);
1558 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1559 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1560 } else {
1561 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get history of notification failed due to channel establishment");
1562 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1563 }
1564 pthread_mutex_unlock(&locked_session->lock);
1565 } else {
1566 if (pthread_rwlock_unlock(&session_lock) != 0) {
1567 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1568 }
1569 reply = create_error("Invalid session identifier.");
1570 }
1571
1572 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1573 reply = locked_session->hello_message;
1574 }
1575 return reply;
1576}
1577
David Kupka8e60a372012-09-04 09:15:20 +02001578void * thread_routine (void * arg)
1579{
1580 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001581 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001582 json_object *request = NULL;
1583 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001584 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001585 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001586 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001587 const char *session_key;
1588 const char *target = NULL;
1589 const char *source = NULL;
1590 NC_DATASTORE ds_type_t = -1;
1591 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001592 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001593 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
David Kupka8e60a372012-09-04 09:15:20 +02001594 server_rec * server = ((struct pass_to_thread*)arg)->server;
1595 int client = ((struct pass_to_thread*)arg)->client;
1596
Tomas Cejka00635972013-06-03 15:10:52 +02001597 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001598
1599 while (!isterminated) {
1600 fds.fd = client;
1601 fds.events = POLLIN;
1602 fds.revents = 0;
1603
1604 status = poll(&fds, 1, 1000);
1605
1606 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1607 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1608 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1609 continue;
1610 } else if (status < 0) {
1611 /* 0: poll time outed
1612 * close socket and ignore this request from the client, it can try it again
1613 * -1: poll failed
1614 * something wrong happend, close this socket and wait for another request
1615 */
1616 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1617 close(client);
1618 break;
1619 }
1620 /* status > 0 */
1621
1622 /* check the status of the socket */
1623
1624 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1625 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1626 /* close client's socket (it's probably already closed by client */
1627 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1628 close(client);
1629 break;
1630 }
1631
Tomas Cejka47387fd2013-06-10 20:37:46 +02001632 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message...");
Tomas Cejka64b87482013-06-03 16:30:53 +02001633 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001634
Tomas Cejka47387fd2013-06-10 20:37:46 +02001635 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001636 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001637 enum json_tokener_error jerr;
1638 request = json_tokener_parse_verbose(buffer, &jerr);
1639 if (jerr != json_tokener_success) {
1640 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1641 continue;
1642 }
David Kupka8e60a372012-09-04 09:15:20 +02001643 operation = json_object_get_int(json_object_object_get(request, "type"));
1644
Tomas Cejka64b87482013-06-03 16:30:53 +02001645 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001646 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001647 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1648 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001649 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001650 msgtext = json_object_to_json_string(reply);
1651 send(client, msgtext, strlen(msgtext) + 1, 0);
1652 json_object_put(reply);
1653 /* there is some stupid client, so close the connection to give a chance to some other client */
1654 close(client);
1655 break;
1656 }
1657
David Kupka8e60a372012-09-04 09:15:20 +02001658 /* null global JSON error-reply */
1659 err_reply = NULL;
1660
1661 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001662 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001663
1664 /* process required operation */
1665 switch (operation) {
1666 case MSG_CONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001667 reply = handle_op_connect(server, pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001668 break;
1669 case MSG_GET:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001670 reply = handle_op_get(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001671 break;
1672 case MSG_GETCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001673 reply = handle_op_getconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001674 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001675 case MSG_GETSCHEMA:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001676 reply = handle_op_getschema(server, pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001677 break;
David Kupka8e60a372012-09-04 09:15:20 +02001678 case MSG_EDITCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001679 reply = handle_op_editconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001680 break;
1681 case MSG_COPYCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001682 reply = handle_op_copyconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001683 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001684
David Kupka8e60a372012-09-04 09:15:20 +02001685 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001686 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001687 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001688 /* get parameters */
1689 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1690 ds_type_t = parse_datastore(target);
1691 }
1692 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1693 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001694 }
1695
1696 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001697 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001698 break;
1699 }
David Kupka8e60a372012-09-04 09:15:20 +02001700 switch(operation) {
1701 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001702 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001703 status = netconf_deleteconfig(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001704 break;
1705 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001706 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001707 status = netconf_lock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001708 break;
1709 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001710 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001711 status = netconf_unlock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001712 break;
1713 default:
1714 status = -1;
1715 break;
1716 }
1717
1718 if (status != EXIT_SUCCESS) {
1719 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001720 /** \todo more clever error message wanted */
1721 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001722 } else {
1723 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001724 reply = err_reply;
1725 }
1726 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001727 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001728 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1729 }
1730 break;
1731 case MSG_KILL:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001732 reply = handle_op_kill(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001733 break;
1734 case MSG_DISCONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001735 reply = handle_op_disconnect(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001736 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001737 case MSG_RELOADHELLO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001738 reply = handle_op_reloadhello(server, pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001739 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001740 case MSG_INFO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001741 reply = handle_op_info(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001742 break;
1743 case MSG_GENERIC:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001744 reply = handle_op_generic(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001745 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001746 case MSG_NTF_GETHISTORY:
1747 reply = handle_op_ntfgethistory(server, pool, request, session_key);
1748 break;
David Kupka8e60a372012-09-04 09:15:20 +02001749 default:
1750 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001751 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001752 break;
1753 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001754 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001755 json_object_put(request);
1756
Tomas Cejkad5b53772013-06-08 23:01:07 +02001757 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001758 /* send reply to caller */
1759 if (reply != NULL) {
1760 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001761 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001762 if (buffer != NULL) {
1763 free(buffer);
1764 buffer = NULL;
1765 }
David Kupka8e60a372012-09-04 09:15:20 +02001766 break;
1767 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001768 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001769 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001770 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001771 json_object_put(reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001772 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001773 free(chunked_out_msg);
1774 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001775 if (buffer != NULL) {
1776 free(buffer);
1777 buffer = NULL;
1778 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001779 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001780 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001781 break;
David Kupka8e60a372012-09-04 09:15:20 +02001782 }
1783 }
1784 }
David Kupka8e60a372012-09-04 09:15:20 +02001785 free (arg);
1786
1787 return retval;
1788}
1789
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001790/**
1791 * \brief Close all open NETCONF sessions.
1792 *
1793 * During termination of mod_netconf, it is useful to close all remaining
1794 * sessions. This function iterates over the list of sessions and close them
1795 * all.
1796 *
1797 * \param[in] server pointer to server_rec for logging
1798 * \param[in] p apr pool needed for hash table iterating
1799 * \param[in] ht hash table of session_with_mutex structs
1800 */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001801static void close_all_nc_sessions(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001802{
1803 apr_hash_index_t *hi;
1804 void *val = NULL;
1805 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001806 const char *hashed_key = NULL;
1807 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001808 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001809
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001810 /* get exclusive access to sessions_list (conns) */
1811 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1812 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1813 return;
1814 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001815 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001816 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1817 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1818 swm = (struct session_with_mutex *) val;
1819 if (swm != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001820 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001821 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001822
1823 /* close_and_free_session handles locking on its own */
1824 close_and_free_session(server, swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001825 }
1826 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001827 /* get exclusive access to sessions_list (conns) */
1828 if (pthread_rwlock_unlock (&session_lock) != 0) {
1829 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1830 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001831}
1832
Tomas Cejka47387fd2013-06-10 20:37:46 +02001833static void check_timeout_and_close(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001834{
1835 apr_hash_index_t *hi;
1836 void *val = NULL;
1837 struct nc_session *ns = NULL;
1838 struct session_with_mutex *swm = NULL;
1839 const char *hashed_key = NULL;
1840 apr_ssize_t hashed_key_length;
1841 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001842 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001843
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001844 /* get exclusive access to sessions_list (conns) */
1845 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1846 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1847 return;
1848 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001849 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001850 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1851 swm = (struct session_with_mutex *) val;
1852 if (swm == NULL) {
1853 continue;
1854 }
1855 ns = swm->session;
1856 if (ns == NULL) {
1857 continue;
1858 }
1859 pthread_mutex_lock(&swm->lock);
1860 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1861 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001862 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001863 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
1864 pthread_mutex_unlock(&swm->lock);
1865
1866 /* close_and_free_session handles locking on its own */
1867 close_and_free_session(server, swm);
1868 } else {
1869 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001870 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001871 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001872 /* get exclusive access to sessions_list (conns) */
1873 if (pthread_rwlock_unlock (&session_lock) != 0) {
1874 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1875 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001876}
1877
1878
1879/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001880 * This is actually implementation of NETCONF client
1881 * - requests are received from UNIX socket in the predefined format
1882 * - results are replied through the same way
1883 * - the daemon run as a separate process, but it is started and stopped
1884 * automatically by Apache.
1885 *
1886 */
Radek Krejci469aab82012-07-22 18:42:20 +02001887static void forked_proc(apr_pool_t * pool, server_rec * server)
1888{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001889 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001890 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001891 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001892 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001893 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001894 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001895 struct pass_to_thread * arg;
1896 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1897 struct timespec maxtime;
1898 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001899 #ifdef WITH_NOTIFICATIONS
1900 char use_notifications = 0;
1901 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001902
Tomas Cejka6b886e02013-07-05 09:53:17 +02001903 http_server = server;
1904
Tomas Cejka404d37e2013-04-13 02:31:35 +02001905 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001906 maxtime.tv_sec = 5;
1907 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001908
Radek Krejcif23850c2012-07-23 16:14:17 +02001909 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001910 unixd_setup_child();
1911
Radek Krejciae021c12012-07-25 18:03:52 +02001912 cfg = ap_get_module_config(server->module_config, &netconf_module);
1913 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001914 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1915 return;
1916 }
Radek Krejci469aab82012-07-22 18:42:20 +02001917
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001918 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001919 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1920 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1921 return;
1922 }
1923
1924 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001925 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001926 unlink(local.sun_path);
1927 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1928
1929 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1930 if (errno == EADDRINUSE) {
1931 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1932 close(lsock);
1933 exit(0);
1934 }
1935 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1936 close(lsock);
1937 return;
1938 }
1939
1940 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1941 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1942 close(lsock);
1943 return;
1944 }
1945
Tomas Cejkaba21b382013-04-13 02:37:32 +02001946 /* prepare internal lists */
1947 netconf_sessions_list = apr_hash_make(pool);
1948
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001949 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02001950 if (notification_init(pool, server) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001951 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1952 use_notifications = 0;
1953 } else {
1954 use_notifications = 1;
1955 }
1956 #endif
1957
Radek Krejci469aab82012-07-22 18:42:20 +02001958 /* setup libnetconf's callbacks */
1959 nc_verbosity(NC_VERB_DEBUG);
1960 clb_print_server = server;
1961 nc_callback_print(clb_print);
1962 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1963 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1964 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001965 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001966
1967 /* disable publickey authentication */
1968 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1969
Tomas Cejka44e71db2013-04-21 15:50:47 +02001970 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1971
David Kupka8e60a372012-09-04 09:15:20 +02001972 /* create mutex protecting session list */
1973 pthread_rwlockattr_init(&lock_attrs);
1974 /* rwlock is shared only with threads in this process */
1975 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1976 /* create rw lock */
1977 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1978 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1979 close (lsock);
1980 return;
1981 }
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001982 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "init of notif_history_key.");
1983 if (pthread_key_create(&notif_history_key, NULL) != 0) {
1984 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "init of notif_history_key failed");
1985 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001986
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001987 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001988 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001989 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001990 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001991 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001992 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001993 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1994 }
1995 if (use_notifications == 1) {
1996 notification_handle();
1997 }
1998 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001999 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02002000 check_timeout_and_close(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002001 }
Radek Krejci469aab82012-07-22 18:42:20 +02002002
2003 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002004 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002005 if (((unsigned int)tv.tv_sec - olds) > 60) {
2006 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
2007 olds = tv.tv_sec;
2008 }
David Kupka8e60a372012-09-04 09:15:20 +02002009 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002010 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2011 apr_sleep(SLEEP_TIME);
2012 continue;
2013 } else if (client == -1 && (errno == EINTR)) {
2014 continue;
2015 } else if (client == -1) {
2016 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
2017 continue;
2018 }
Radek Krejci469aab82012-07-22 18:42:20 +02002019
2020 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002021 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002022
David Kupka8e60a372012-09-04 09:15:20 +02002023 arg = malloc (sizeof(struct pass_to_thread));
2024 arg->client = client;
2025 arg->pool = pool;
2026 arg->server = server;
2027 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002028
David Kupka8e60a372012-09-04 09:15:20 +02002029 /* start new thread. It will serve this particular request and then terminate */
2030 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
2031 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
2032 } else {
2033 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
2034 pthread_count++;
2035 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2036 ptids[pthread_count] = 0;
2037 }
Radek Krejci469aab82012-07-22 18:42:20 +02002038
David Kupka8e60a372012-09-04 09:15:20 +02002039 /* check if some thread already terminated, free some resources by joining it */
2040 for (i=0; i<pthread_count; i++) {
2041 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
2042 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
2043 pthread_count--;
2044 if (pthread_count > 0) {
2045 /* place last Thread ID on the place of joined one */
2046 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002047 }
Radek Krejci469aab82012-07-22 18:42:20 +02002048 }
2049 }
David Kupka8e60a372012-09-04 09:15:20 +02002050 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002051 }
2052
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002053 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002054 /* join all threads */
2055 for (i=0; i<pthread_count; i++) {
2056 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2057 }
2058 free (ptids);
2059
Radek Krejci469aab82012-07-22 18:42:20 +02002060 close(lsock);
2061
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002062 #ifdef WITH_NOTIFICATIONS
2063 notification_close();
2064 #endif
2065
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002066 /* close all NETCONF sessions */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002067 close_all_nc_sessions(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002068
David Kupka8e60a372012-09-04 09:15:20 +02002069 /* destroy rwlock */
2070 pthread_rwlock_destroy(&session_lock);
2071 pthread_rwlockattr_destroy(&lock_attrs);
2072
Radek Krejci469aab82012-07-22 18:42:20 +02002073 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
2074
2075 exit(APR_SUCCESS);
2076}
2077
Radek Krejcif23850c2012-07-23 16:14:17 +02002078static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002079{
Radek Krejcif23850c2012-07-23 16:14:17 +02002080 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
2081
2082 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2083 apr_pool_create(&config->pool, pool);
2084 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002085 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002086
2087 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002088}
2089
2090static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2091 apr_pool_t * plog, server_rec * s)
2092{
Radek Krejcif23850c2012-07-23 16:14:17 +02002093 mod_netconf_cfg *config;
2094 apr_status_t res;
2095
Radek Krejci469aab82012-07-22 18:42:20 +02002096 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002097 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002098 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002099
2100 /*
2101 * The following checks if this routine has been called before.
2102 * This is necessary because the parent process gets initialized
2103 * a couple of times as the server starts up.
2104 */
2105 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2106 if (!data) {
2107 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2108 return (OK);
2109 }
2110
Radek Krejcif23850c2012-07-23 16:14:17 +02002111 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
2112 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002113
Radek Krejcif23850c2012-07-23 16:14:17 +02002114 if (config && config->forkproc == NULL) {
2115 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2116 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002117 switch (res) {
2118 case APR_INCHILD:
2119 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002120 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002121 apr_signal(SIGTERM, signal_handler);
2122
2123 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02002124 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002125
2126 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002127 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002128
Radek Krejcif23850c2012-07-23 16:14:17 +02002129 /* I never should be here, wtf?!? */
2130 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002131 exit(APR_EGENERAL);
2132 break;
2133 case APR_INPARENT:
2134 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002135 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002136 break;
2137 default:
2138 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
2139 break;
2140 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002141 } else {
2142 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002143 }
2144
2145 return OK;
2146}
2147
Radek Krejci469aab82012-07-22 18:42:20 +02002148/**
2149 * Register module hooks
2150 */
2151static void mod_netconf_register_hooks(apr_pool_t * p)
2152{
Radek Krejcif23850c2012-07-23 16:14:17 +02002153 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02002154}
2155
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002156static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2157{
2158 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2159 return NULL;
2160}
2161
2162static const command_rec netconf_cmds[] = {
2163 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2164 {NULL}
2165};
2166
Radek Krejci469aab82012-07-22 18:42:20 +02002167/* Dispatch list for API hooks */
2168module AP_MODULE_DECLARE_DATA netconf_module = {
2169 STANDARD20_MODULE_STUFF,
2170 NULL, /* create per-dir config structures */
2171 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002172 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002173 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002174 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002175 mod_netconf_register_hooks /* register hooks */
2176};
Radek Krejcia332b692012-11-12 16:15:54 +01002177