blob: 36d07549dd07c06091bd67a2f80aa766dff51b89 [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 Cejka94674662013-09-13 15:55:24 +0200662 /* tell server to show all elements even if they have default values */
663 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
664 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
665 }
666
Tomas Cejka47387fd2013-06-10 20:37:46 +0200667 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200668 nc_rpc_free (rpc);
669 return (data);
670}
671
Tomas Cejka47387fd2013-06-10 20:37:46 +0200672static 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 +0100673{
674 nc_rpc* rpc;
675 char* data = NULL;
676
677 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100678 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100679 if (rpc == NULL) {
680 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
681 return (NULL);
682 }
683
Tomas Cejka47387fd2013-06-10 20:37:46 +0200684 data = netconf_opdata(server, session_key, rpc);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100685 nc_rpc_free (rpc);
686 return (data);
687}
688
Tomas Cejka47387fd2013-06-10 20:37:46 +0200689static char* netconf_get(server_rec* server, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200690{
691 nc_rpc* rpc;
692 struct nc_filter *f = NULL;
693 char* data = NULL;
694
695 /* create filter if set */
696 if (filter != NULL) {
697 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
698 }
699
700 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100701 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200702 nc_filter_free(f);
703 if (rpc == NULL) {
704 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
705 return (NULL);
706 }
707
Tomas Cejka94674662013-09-13 15:55:24 +0200708 /* tell server to show all elements even if they have default values */
709 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
710 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
711 }
712
Tomas Cejka47387fd2013-06-10 20:37:46 +0200713 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200714 nc_rpc_free (rpc);
715 return (data);
716}
717
Tomas Cejka47387fd2013-06-10 20:37:46 +0200718static 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 +0200719{
720 nc_rpc* rpc;
721 int retval = EXIT_SUCCESS;
722
723 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100724 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
725 if (target == NC_DATASTORE_URL) {
726 rpc = nc_rpc_copyconfig(source, target, config, url);
727 } else {
728 rpc = nc_rpc_copyconfig(source, target, config);
729 }
730 } else {
731 if (target == NC_DATASTORE_URL) {
732 rpc = nc_rpc_copyconfig(source, target, url);
733 } else {
734 rpc = nc_rpc_copyconfig(source, target);
735 }
736 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200737 if (rpc == NULL) {
738 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
739 return (EXIT_FAILURE);
740 }
741
Tomas Cejka47387fd2013-06-10 20:37:46 +0200742 retval = netconf_op(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200743 nc_rpc_free (rpc);
744 return (retval);
745}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200746
Tomas Cejka47387fd2013-06-10 20:37:46 +0200747static 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 +0200748{
749 nc_rpc* rpc;
750 int retval = EXIT_SUCCESS;
751
752 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100753 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
754 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200755 if (rpc == NULL) {
756 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
757 return (EXIT_FAILURE);
758 }
759
Tomas Cejka47387fd2013-06-10 20:37:46 +0200760 retval = netconf_op(server, session_key, rpc);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200761 nc_rpc_free (rpc);
762 return (retval);
763}
764
Tomas Cejka47387fd2013-06-10 20:37:46 +0200765static int netconf_killsession(server_rec* server, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200766{
767 nc_rpc* rpc;
768 int retval = EXIT_SUCCESS;
769
770 /* create requests */
771 rpc = nc_rpc_killsession(sid);
772 if (rpc == NULL) {
773 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
774 return (EXIT_FAILURE);
775 }
776
Tomas Cejka47387fd2013-06-10 20:37:46 +0200777 retval = netconf_op(server, session_key, rpc);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200778 nc_rpc_free (rpc);
779 return (retval);
780}
781
Tomas Cejka47387fd2013-06-10 20:37:46 +0200782static 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 +0200783{
784 nc_rpc* rpc;
785 int retval = EXIT_SUCCESS;
786
787 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200788 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200789 if (rpc == NULL) {
790 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
791 return (EXIT_FAILURE);
792 }
793
Tomas Cejka47387fd2013-06-10 20:37:46 +0200794 retval = netconf_op(server, session_key, rpc);
Radek Krejci2f318372012-07-26 14:22:35 +0200795 nc_rpc_free (rpc);
796 return (retval);
797}
798
Tomas Cejka47387fd2013-06-10 20:37:46 +0200799static int netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200800{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200801 nc_rpc *rpc = NULL;
802 if (target != NC_DATASTORE_URL) {
803 rpc = nc_rpc_deleteconfig(target);
804 } else {
805 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
806 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
807 return (EXIT_FAILURE);
808 }
809
Tomas Cejka47387fd2013-06-10 20:37:46 +0200810 return netconf_op(server, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200811}
812
Tomas Cejka47387fd2013-06-10 20:37:46 +0200813static int netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200814{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200815 return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200816}
817
Tomas Cejka47387fd2013-06-10 20:37:46 +0200818static int netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200819{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200820 return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200821}
822
Radek Krejci80c10d92012-07-30 08:38:50 +0200823/**
824 * @return REPLY_OK: 0, *data == NULL
825 * REPLY_DATA: 0, *data != NULL
826 * REPLY_ERROR: 1, *data == NULL
827 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200828static int netconf_generic(server_rec* server, const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200829{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200830 struct session_with_mutex *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200831 nc_reply* reply = NULL;
832 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200833 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100834 NC_MSG_TYPE msgt;
835 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200836
837 /* create requests */
838 rpc = nc_rpc_generic(content);
839 if (rpc == NULL) {
840 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
841 return (EXIT_FAILURE);
842 }
843
Radek Krejcia332b692012-11-12 16:15:54 +0100844 if (data != NULL) {
845 *data = NULL;
846 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200847
Tomas Cejka47387fd2013-06-10 20:37:46 +0200848 if (pthread_rwlock_rdlock(&session_lock) != 0) {
849 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
850 return EXIT_FAILURE;
851 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200852 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200853 session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Radek Krejci80c10d92012-07-30 08:38:50 +0200854 if (session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200855 pthread_mutex_lock(&session->lock);
856 if (pthread_rwlock_unlock(&session_lock) != 0) {
857 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
858 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200859 /* send the request and get the reply */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200860 msgt = nc_session_send_recv(session->session, rpc, &reply);
861 nc_rpc_free(rpc);
862 pthread_mutex_unlock(&session->lock);
Radek Krejcia332b692012-11-12 16:15:54 +0100863
864 /* process the result of the operation */
865 switch (msgt) {
866 case NC_MSG_UNKNOWN:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200867 if (nc_session_get_status(session->session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci80c10d92012-07-30 08:38:50 +0200868 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200869 netconf_close(server, session_key);
Radek Krejci80c10d92012-07-30 08:38:50 +0200870 return (EXIT_FAILURE);
871 }
Radek Krejcia332b692012-11-12 16:15:54 +0100872 /* no break */
873 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200874 /* there is error handled by callback */
875 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200876 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100877 case NC_MSG_REPLY:
878 switch (replyt = nc_reply_get_type(reply)) {
879 case NC_REPLY_DATA:
880 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
881 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
882 nc_reply_free(reply);
883 return (EXIT_FAILURE);
884 }
885 retval = EXIT_SUCCESS;
886 break;
887 case NC_REPLY_OK:
888 retval = EXIT_SUCCESS;
889 break;
890 default:
891 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
892 retval = EXIT_FAILURE;
893 break;
894 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200895 break;
896 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100897 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200898 retval = EXIT_FAILURE;
899 break;
900 }
901 nc_reply_free(reply);
902
903 return (retval);
904 } else {
905 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200906 if (pthread_rwlock_unlock(&session_lock) != 0) {
907 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
908 return EXIT_FAILURE;
909 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200910 return (EXIT_FAILURE);
911 }
912}
913
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200914/**
915 * @}
916 *//* netconf_operations */
917
Radek Krejci469aab82012-07-22 18:42:20 +0200918server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200919void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200920{
Radek Krejci7338bde2012-08-10 12:57:30 +0200921 switch (level) {
922 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200923 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200924 break;
925 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200926 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200927 break;
928 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200929 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200930 break;
931 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200932 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200933 break;
934 }
Radek Krejci469aab82012-07-22 18:42:20 +0200935}
936
Tomas Cejka64b87482013-06-03 16:30:53 +0200937/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200938 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200939 * Caller should free message memory.
940 * \param[in] client socket descriptor of client
941 * \param[in] server httpd server for logging
942 * \return pointer to message
943 */
944char *get_framed_message(server_rec *server, int client)
945{
946 /* read json in chunked framing */
947 unsigned int buffer_size = 0;
948 ssize_t buffer_len = 0;
949 char *buffer = NULL;
950 char c;
951 ssize_t ret;
952 int i, chunk_len;
953 char chunk_len_str[12];
954
955 while (1) {
956 /* read chunk length */
957 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
958 if (buffer != NULL) {
959 free (buffer);
960 buffer = NULL;
961 }
962 break;
963 }
964 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
965 if (buffer != NULL) {
966 free (buffer);
967 buffer = NULL;
968 }
969 break;
970 }
971 i=0;
972 memset (chunk_len_str, 0, 12);
973 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
974 if (i==0 && c == '#') {
975 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
976 /* end but invalid */
977 if (buffer != NULL) {
978 free (buffer);
979 buffer = NULL;
980 }
981 }
982 /* end of message, double-loop break */
983 goto msg_complete;
984 }
985 chunk_len_str[i++] = c;
986 if (i==11) {
987 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
988 break;
989 }
990 }
991 if (c != '\n') {
992 if (buffer != NULL) {
993 free (buffer);
994 buffer = NULL;
995 }
996 break;
997 }
998 chunk_len_str[i] = 0;
999 if ((chunk_len = atoi (chunk_len_str)) == 0) {
1000 if (buffer != NULL) {
1001 free (buffer);
1002 buffer = NULL;
1003 }
1004 break;
1005 }
1006 buffer_size += chunk_len+1;
1007 buffer = realloc (buffer, sizeof(char)*buffer_size);
1008 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
1009 if (buffer != NULL) {
1010 free (buffer);
1011 buffer = NULL;
1012 }
1013 break;
1014 }
1015 buffer_len += ret;
1016 }
1017msg_complete:
1018 return buffer;
1019}
1020
Tomas Cejkad5b53772013-06-08 23:01:07 +02001021NC_DATASTORE parse_datastore(const char *ds)
1022{
1023 if (strcmp(ds, "running") == 0) {
1024 return NC_DATASTORE_RUNNING;
1025 } else if (strcmp(ds, "startup") == 0) {
1026 return NC_DATASTORE_STARTUP;
1027 } else if (strcmp(ds, "candidate") == 0) {
1028 return NC_DATASTORE_CANDIDATE;
1029 }
1030 return -1;
1031}
1032
1033json_object *create_error(const char *errmess)
1034{
1035 json_object *reply = json_object_new_object();
1036 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1037 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
1038 return reply;
1039
1040}
1041
1042json_object *create_data(const char *data)
1043{
1044 json_object *reply = json_object_new_object();
1045 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1046 json_object_object_add(reply, "data", json_object_new_string(data));
1047 return reply;
1048}
1049
Tomas Cejka47387fd2013-06-10 20:37:46 +02001050json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001051{
1052 const char *host = NULL;
1053 const char *port = NULL;
1054 const char *user = NULL;
1055 const char *pass = NULL;
1056 json_object *capabilities = NULL;
1057 json_object *reply = NULL;
1058 char *session_key_hash = NULL;
1059 struct nc_cpblts* cpblts = NULL;
1060 unsigned int len, i;
1061
1062 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
1063 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
1064 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
1065 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1066 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1067
1068 capabilities = json_object_object_get((json_object *) request, "capabilities");
1069 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1070 cpblts = nc_cpblts_new(NULL);
1071 for (i=0; i<len; i++) {
1072 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1073 }
1074 } else {
1075 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1076 }
1077
1078 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1079 if ((host == NULL) || (user == NULL)) {
1080 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1081 session_key_hash = NULL;
1082 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001083 session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001084 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1085 }
1086 if (cpblts != NULL) {
1087 nc_cpblts_free(cpblts);
1088 }
1089
1090 if (session_key_hash == NULL) {
1091 /* negative reply */
1092 if (err_reply == NULL) {
1093 reply = json_object_new_object();
1094 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1095 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
1096 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
1097 } else {
1098 /* use filled err_reply from libnetconf's callback */
1099 reply = err_reply;
1100 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
1101 }
1102 } else {
1103 /* positive reply */
1104 reply = json_object_new_object();
1105 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1106 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1107
1108 free(session_key_hash);
1109 }
1110 return reply;
1111}
1112
Tomas Cejka47387fd2013-06-10 20:37:46 +02001113json_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 +02001114{
1115 const char *filter = NULL;
1116 const char *data = NULL;
1117 json_object *reply = NULL;
1118
1119 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1120
1121 filter = json_object_get_string(json_object_object_get(request, "filter"));
1122
Tomas Cejka47387fd2013-06-10 20:37:46 +02001123 if ((data = netconf_get(server, session_key, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001124 if (err_reply == NULL) {
1125 reply = create_error("Get information from device failed.");
1126 } else {
1127 /* use filled err_reply from libnetconf's callback */
1128 reply = err_reply;
1129 }
1130 } else {
1131 return create_data(data);
1132 }
1133 return reply;
1134}
1135
Tomas Cejka47387fd2013-06-10 20:37:46 +02001136json_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 +02001137{
1138 NC_DATASTORE ds_type_s = -1;
1139 NC_DATASTORE ds_type_t = -1;
1140 const char *filter = NULL;
1141 const char *data = NULL;
1142 const char *source = NULL;
1143 const char *target = NULL;
1144 json_object *reply = NULL;
1145
1146 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1147
1148 filter = json_object_get_string(json_object_object_get(request, "filter"));
1149
1150 /* get parameters */
1151 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1152 ds_type_t = parse_datastore(target);
1153 }
1154 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1155 ds_type_s = parse_datastore(source);
1156 }
1157 if (ds_type_s == -1) {
1158 return create_error("Invalid source repository type requested.");
1159 }
1160
Tomas Cejka47387fd2013-06-10 20:37:46 +02001161 if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001162 if (err_reply == NULL) {
1163 reply = create_error("Get configuration information from device failed.");
1164 } else {
1165 /* use filled err_reply from libnetconf's callback */
1166 reply = err_reply;
1167 }
1168 } else {
1169 return create_data(data);
1170 }
1171 return reply;
1172}
1173
Tomas Cejka47387fd2013-06-10 20:37:46 +02001174json_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 +02001175{
1176 const char *data = NULL;
1177 const char *identifier = NULL;
1178 const char *version = NULL;
1179 const char *format = NULL;
1180 json_object *reply = NULL;
1181
1182 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1183 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1184 if (identifier == NULL) {
1185 return create_error("No identifier for get-schema supplied.");
1186 }
1187 version = json_object_get_string(json_object_object_get(request, "version"));
1188 format = json_object_get_string(json_object_object_get(request, "format"));
1189
1190 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001191 if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001192 if (err_reply == NULL) {
1193 reply = create_error("Get schema failed.");
1194 } else {
1195 /* use filled err_reply from libnetconf's callback */
1196 reply = err_reply;
1197 }
1198 } else {
1199 return create_data(data);
1200 }
1201 return reply;
1202}
1203
Tomas Cejka47387fd2013-06-10 20:37:46 +02001204json_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 +02001205{
1206 NC_DATASTORE ds_type_s = -1;
1207 NC_DATASTORE ds_type_t = -1;
1208 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1209 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1210 const char *defop = NULL;
1211 const char *erropt = NULL;
1212 const char *config = NULL;
1213 const char *source = NULL;
1214 const char *target = NULL;
1215 json_object *reply = NULL;
1216
1217 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1218
1219 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1220 if (defop != NULL) {
1221 if (strcmp(defop, "merge") == 0) {
1222 defop_type = NC_EDIT_DEFOP_MERGE;
1223 } else if (strcmp(defop, "replace") == 0) {
1224 defop_type = NC_EDIT_DEFOP_REPLACE;
1225 } else if (strcmp(defop, "none") == 0) {
1226 defop_type = NC_EDIT_DEFOP_NONE;
1227 } else {
1228 return create_error("Invalid default-operation parameter.");
1229 }
1230 } else {
1231 defop_type = NC_EDIT_DEFOP_NOTSET;
1232 }
1233
1234 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1235 if (erropt != NULL) {
1236 if (strcmp(erropt, "continue-on-error") == 0) {
1237 erropt_type = NC_EDIT_ERROPT_CONT;
1238 } else if (strcmp(erropt, "stop-on-error") == 0) {
1239 erropt_type = NC_EDIT_ERROPT_STOP;
1240 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1241 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1242 } else {
1243 return create_error("Invalid error-option parameter.");
1244 }
1245 } else {
1246 erropt_type = 0;
1247 }
1248
1249 /* get parameters */
1250 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1251 ds_type_t = parse_datastore(target);
1252 }
1253 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1254 ds_type_s = parse_datastore(source);
1255 }
1256 if (ds_type_t == -1) {
1257 return create_error("Invalid target repository type requested.");
1258 }
1259
1260 config = json_object_get_string(json_object_object_get(request, "config"));
1261 if (config == NULL) {
1262 return create_error("Invalid config data parameter.");
1263 }
1264
Tomas Cejka47387fd2013-06-10 20:37:46 +02001265 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 +02001266 if (err_reply == NULL) {
1267 reply = create_error("Edit-config failed.");
1268 } else {
1269 /* use filled err_reply from libnetconf's callback */
1270 reply = err_reply;
1271 }
1272 } else {
1273 reply = json_object_new_object();
1274 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1275 }
1276 return reply;
1277}
1278
Tomas Cejka47387fd2013-06-10 20:37:46 +02001279json_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 +02001280{
1281 NC_DATASTORE ds_type_s = -1;
1282 NC_DATASTORE ds_type_t = -1;
1283 const char *config = NULL;
1284 const char *target = NULL;
1285 const char *source = NULL;
1286 json_object *reply = NULL;
1287
1288 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1289
1290 /* get parameters */
1291 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1292 ds_type_t = parse_datastore(target);
1293 }
1294 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1295 ds_type_s = parse_datastore(source);
1296 }
1297 if (source == NULL) {
1298 /* no explicit source specified -> use config data */
1299 ds_type_s = NC_DATASTORE_CONFIG;
1300 config = json_object_get_string(json_object_object_get(request, "config"));
1301 } else if (ds_type_s == -1) {
1302 /* source datastore specified, but it is invalid */
1303 return create_error("Invalid source repository type requested.");
1304 }
1305
1306 if (ds_type_t == -1) {
1307 /* invalid target datastore specified */
1308 return create_error("Invalid target repository type requested.");
1309 }
1310
1311 if (source == NULL && config == NULL) {
1312 reply = create_error("invalid input parameters - one of source and config is required.");
1313 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001314 if (netconf_copyconfig(server, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001315 if (err_reply == NULL) {
1316 reply = create_error("Copying of configuration failed.");
1317 } else {
1318 /* use filled err_reply from libnetconf's callback */
1319 reply = err_reply;
1320 }
1321 } else {
1322 reply = json_object_new_object();
1323 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1324 }
1325 }
1326 return reply;
1327}
1328
Tomas Cejka47387fd2013-06-10 20:37:46 +02001329json_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 +02001330{
1331 json_object *reply = NULL;
1332 const char *config = NULL;
1333 char *data = NULL;
1334
1335 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1336
1337 config = json_object_get_string(json_object_object_get(request, "content"));
1338
1339 if (config == NULL) {
1340 return create_error("Missing content parameter.");
1341 }
1342
Tomas Cejka47387fd2013-06-10 20:37:46 +02001343 if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001344 if (err_reply == NULL) {
1345 reply = create_error("Killing of session failed.");
1346 } else {
1347 /* use filled err_reply from libnetconf's callback */
1348 reply = err_reply;
1349 }
1350 } else {
1351 if (data == NULL) {
1352 reply = json_object_new_object();
1353 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1354 } else {
1355 return create_data(data);
1356 }
1357 }
1358 return reply;
1359}
1360
Tomas Cejka47387fd2013-06-10 20:37:46 +02001361json_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 +02001362{
1363 json_object *reply = NULL;
1364 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1365
Tomas Cejka47387fd2013-06-10 20:37:46 +02001366 if (netconf_close(server, session_key) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001367 if (err_reply == NULL) {
1368 reply = create_error("Invalid session identifier.");
1369 } else {
1370 /* use filled err_reply from libnetconf's callback */
1371 reply = err_reply;
1372 }
1373 } else {
1374 reply = json_object_new_object();
1375 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1376 }
1377 return reply;
1378}
1379
Tomas Cejka47387fd2013-06-10 20:37:46 +02001380json_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 +02001381{
1382 json_object *reply = NULL;
1383 const char *sid = NULL;
1384
1385 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1386
1387 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1388
1389 if (sid == NULL) {
1390 return create_error("Missing session-id parameter.");
1391 }
1392
Tomas Cejka47387fd2013-06-10 20:37:46 +02001393 if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001394 if (err_reply == NULL) {
1395 reply = create_error("Killing of session failed.");
1396 } else {
1397 /* use filled err_reply from libnetconf's callback */
1398 reply = err_reply;
1399 }
1400 } else {
1401 reply = json_object_new_object();
1402 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1403 }
1404 return reply;
1405}
1406
Tomas Cejka47387fd2013-06-10 20:37:46 +02001407json_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 +02001408{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001409 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001410 struct session_with_mutex * locked_session = NULL;
1411 json_object *reply = NULL;
1412 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1413
Tomas Cejka47387fd2013-06-10 20:37:46 +02001414 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1415 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1416 return NULL;
1417 }
1418
Tomas Cejkad5b53772013-06-08 23:01:07 +02001419 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1420 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001421 pthread_mutex_lock(&locked_session->lock);
1422 if (pthread_rwlock_unlock(&session_lock) != 0) {
1423 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1424 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001425 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001426 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001427 if (temp_session != NULL) {
1428 prepare_status_message(server, locked_session, temp_session);
1429 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1430 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1431 } else {
1432 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1433 reply = create_error("Reload was unsuccessful, connection failed.");
1434 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001435 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001436 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001437 if (pthread_rwlock_unlock(&session_lock) != 0) {
1438 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1439 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001440 reply = create_error("Invalid session identifier.");
1441 }
1442
1443 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1444 reply = locked_session->hello_message;
1445 }
1446 return reply;
1447}
1448
Tomas Cejka47387fd2013-06-10 20:37:46 +02001449json_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 +02001450{
1451 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001452 struct session_with_mutex * locked_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001453 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1454
Tomas Cejka47387fd2013-06-10 20:37:46 +02001455 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1456 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1457 }
1458
Tomas Cejkad5b53772013-06-08 23:01:07 +02001459 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1460 if (locked_session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001461 pthread_mutex_lock(&locked_session->lock);
1462 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 if (locked_session->hello_message != NULL) {
1466 reply = locked_session->hello_message;
1467 } else {
1468 reply = create_error("Invalid session identifier.");
1469 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001470 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001471 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001472 if (pthread_rwlock_unlock(&session_lock) != 0) {
1473 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1474 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001475 reply = create_error("Invalid session identifier.");
1476 }
1477
Tomas Cejka47387fd2013-06-10 20:37:46 +02001478
Tomas Cejkad5b53772013-06-08 23:01:07 +02001479 return reply;
1480}
1481
Tomas Cejka6b886e02013-07-05 09:53:17 +02001482void notification_history(time_t eventtime, const char *content)
1483{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001484 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1485 if (notif_history_array == NULL) {
1486 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "No list of notification history found.");
1487 return;
1488 }
1489 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 +02001490 json_object *notif = json_object_new_object();
1491 if (notif == NULL) {
1492 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Could not allocate memory for notification (json).");
1493 return;
1494 }
1495 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1496 json_object_object_add(notif, "content", json_object_new_string(content));
1497 json_object_array_add(notif_history_array, notif);
1498}
1499
1500json_object *handle_op_ntfgethistory(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1501{
1502 json_object *reply = NULL;
1503 const char *sid = NULL;
1504 struct session_with_mutex *locked_session = NULL;
1505 struct nc_session *temp_session = NULL;
1506 nc_rpc *rpc = NULL;
1507 time_t start = 0;
1508 time_t stop = 0;
1509 int64_t from, to;
1510
1511 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get notification history, session %s", session_key);
1512
1513 sid = json_object_get_string(json_object_object_get(request, "session"));
1514 from = json_object_get_int64(json_object_object_get(request, "from"));
1515 to = json_object_get_int64(json_object_object_get(request, "to"));
1516
1517 start = time(NULL) + from;
1518 stop = time(NULL) + to;
1519
1520 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "notification history interval %li %li", (long int) from, (long int) to);
1521
1522 if (sid == NULL) {
1523 return create_error("Missing session parameter.");
1524 }
1525
1526 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1527 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1528 return NULL;
1529 }
1530
1531 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1532 if (locked_session != NULL) {
1533 pthread_mutex_lock(&locked_session->lock);
1534 if (pthread_rwlock_unlock(&session_lock) != 0) {
1535 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1536 }
1537 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1538 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1539 if (temp_session != NULL) {
1540 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1541 if (rpc == NULL) {
1542 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "notifications: creating an rpc request failed.");
1543 return create_error("notifications: creating an rpc request failed.");
1544 }
1545
1546 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Send NC subscribe.");
1547 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejka6e8f4262013-07-10 09:20:19 +02001548 if (netconf_process_op(server, temp_session, rpc) != EXIT_SUCCESS) {
Tomas Cejka6b886e02013-07-05 09:53:17 +02001549 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Subscription RPC failed.");
1550 return create_error("Subscription RPC failed.");
1551 }
1552 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1553
1554 pthread_mutex_unlock(&locked_session->lock);
1555 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001556 json_object *notif_history_array = json_object_new_array();
1557 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
1558 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notif_history: cannot set thread-specific hash value.");
1559 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001560
1561 ncntf_dispatch_receive(temp_session, notification_history);
1562
1563 reply = json_object_new_object();
1564 json_object_object_add(reply, "notifications", notif_history_array);
1565 //json_object_put(notif_history_array);
1566
1567 pthread_mutex_unlock(&ntf_history_lock);
1568 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1569 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1570 } else {
1571 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get history of notification failed due to channel establishment");
1572 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1573 }
1574 pthread_mutex_unlock(&locked_session->lock);
1575 } else {
1576 if (pthread_rwlock_unlock(&session_lock) != 0) {
1577 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1578 }
1579 reply = create_error("Invalid session identifier.");
1580 }
1581
1582 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1583 reply = locked_session->hello_message;
1584 }
1585 return reply;
1586}
1587
David Kupka8e60a372012-09-04 09:15:20 +02001588void * thread_routine (void * arg)
1589{
1590 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001591 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001592 json_object *request = NULL;
1593 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001594 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001595 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001596 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001597 const char *session_key;
1598 const char *target = NULL;
1599 const char *source = NULL;
1600 NC_DATASTORE ds_type_t = -1;
1601 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001602 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001603 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
David Kupka8e60a372012-09-04 09:15:20 +02001604 server_rec * server = ((struct pass_to_thread*)arg)->server;
1605 int client = ((struct pass_to_thread*)arg)->client;
1606
Tomas Cejka00635972013-06-03 15:10:52 +02001607 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001608
1609 while (!isterminated) {
1610 fds.fd = client;
1611 fds.events = POLLIN;
1612 fds.revents = 0;
1613
1614 status = poll(&fds, 1, 1000);
1615
1616 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1617 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1618 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1619 continue;
1620 } else if (status < 0) {
1621 /* 0: poll time outed
1622 * close socket and ignore this request from the client, it can try it again
1623 * -1: poll failed
1624 * something wrong happend, close this socket and wait for another request
1625 */
1626 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1627 close(client);
1628 break;
1629 }
1630 /* status > 0 */
1631
1632 /* check the status of the socket */
1633
1634 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1635 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1636 /* close client's socket (it's probably already closed by client */
1637 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1638 close(client);
1639 break;
1640 }
1641
Tomas Cejka47387fd2013-06-10 20:37:46 +02001642 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message...");
Tomas Cejka64b87482013-06-03 16:30:53 +02001643 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001644
Tomas Cejka47387fd2013-06-10 20:37:46 +02001645 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001646 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001647 enum json_tokener_error jerr;
1648 request = json_tokener_parse_verbose(buffer, &jerr);
1649 if (jerr != json_tokener_success) {
1650 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1651 continue;
1652 }
David Kupka8e60a372012-09-04 09:15:20 +02001653 operation = json_object_get_int(json_object_object_get(request, "type"));
1654
Tomas Cejka64b87482013-06-03 16:30:53 +02001655 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001656 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001657 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1658 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001659 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001660 msgtext = json_object_to_json_string(reply);
1661 send(client, msgtext, strlen(msgtext) + 1, 0);
1662 json_object_put(reply);
1663 /* there is some stupid client, so close the connection to give a chance to some other client */
1664 close(client);
1665 break;
1666 }
1667
David Kupka8e60a372012-09-04 09:15:20 +02001668 /* null global JSON error-reply */
1669 err_reply = NULL;
1670
1671 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001672 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001673
1674 /* process required operation */
1675 switch (operation) {
1676 case MSG_CONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001677 reply = handle_op_connect(server, pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001678 break;
1679 case MSG_GET:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001680 reply = handle_op_get(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001681 break;
1682 case MSG_GETCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001683 reply = handle_op_getconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001684 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001685 case MSG_GETSCHEMA:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001686 reply = handle_op_getschema(server, pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001687 break;
David Kupka8e60a372012-09-04 09:15:20 +02001688 case MSG_EDITCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001689 reply = handle_op_editconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001690 break;
1691 case MSG_COPYCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001692 reply = handle_op_copyconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001693 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001694
David Kupka8e60a372012-09-04 09:15:20 +02001695 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001696 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001697 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001698 /* get parameters */
1699 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1700 ds_type_t = parse_datastore(target);
1701 }
1702 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1703 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001704 }
1705
1706 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001707 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001708 break;
1709 }
David Kupka8e60a372012-09-04 09:15:20 +02001710 switch(operation) {
1711 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001712 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001713 status = netconf_deleteconfig(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001714 break;
1715 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001716 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001717 status = netconf_lock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001718 break;
1719 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001720 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001721 status = netconf_unlock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001722 break;
1723 default:
1724 status = -1;
1725 break;
1726 }
1727
1728 if (status != EXIT_SUCCESS) {
1729 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001730 /** \todo more clever error message wanted */
1731 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001732 } else {
1733 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001734 reply = err_reply;
1735 }
1736 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001737 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001738 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1739 }
1740 break;
1741 case MSG_KILL:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001742 reply = handle_op_kill(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001743 break;
1744 case MSG_DISCONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001745 reply = handle_op_disconnect(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001746 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001747 case MSG_RELOADHELLO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001748 reply = handle_op_reloadhello(server, pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001749 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001750 case MSG_INFO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001751 reply = handle_op_info(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001752 break;
1753 case MSG_GENERIC:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001754 reply = handle_op_generic(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001755 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001756 case MSG_NTF_GETHISTORY:
1757 reply = handle_op_ntfgethistory(server, pool, request, session_key);
1758 break;
David Kupka8e60a372012-09-04 09:15:20 +02001759 default:
1760 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001761 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001762 break;
1763 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001764 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001765 json_object_put(request);
1766
Tomas Cejkad5b53772013-06-08 23:01:07 +02001767 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001768 /* send reply to caller */
1769 if (reply != NULL) {
1770 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001771 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001772 if (buffer != NULL) {
1773 free(buffer);
1774 buffer = NULL;
1775 }
David Kupka8e60a372012-09-04 09:15:20 +02001776 break;
1777 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001778 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001779 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001780 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001781 json_object_put(reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001782 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001783 free(chunked_out_msg);
1784 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001785 if (buffer != NULL) {
1786 free(buffer);
1787 buffer = NULL;
1788 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001789 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001790 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001791 break;
David Kupka8e60a372012-09-04 09:15:20 +02001792 }
1793 }
1794 }
David Kupka8e60a372012-09-04 09:15:20 +02001795 free (arg);
1796
1797 return retval;
1798}
1799
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001800/**
1801 * \brief Close all open NETCONF sessions.
1802 *
1803 * During termination of mod_netconf, it is useful to close all remaining
1804 * sessions. This function iterates over the list of sessions and close them
1805 * all.
1806 *
1807 * \param[in] server pointer to server_rec for logging
1808 * \param[in] p apr pool needed for hash table iterating
1809 * \param[in] ht hash table of session_with_mutex structs
1810 */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001811static void close_all_nc_sessions(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001812{
1813 apr_hash_index_t *hi;
1814 void *val = NULL;
1815 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001816 const char *hashed_key = NULL;
1817 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001818 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001819
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001820 /* get exclusive access to sessions_list (conns) */
1821 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1822 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1823 return;
1824 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001825 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001826 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1827 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1828 swm = (struct session_with_mutex *) val;
1829 if (swm != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001830 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001831 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001832
1833 /* close_and_free_session handles locking on its own */
1834 close_and_free_session(server, swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001835 }
1836 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001837 /* get exclusive access to sessions_list (conns) */
1838 if (pthread_rwlock_unlock (&session_lock) != 0) {
1839 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1840 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001841}
1842
Tomas Cejka47387fd2013-06-10 20:37:46 +02001843static void check_timeout_and_close(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001844{
1845 apr_hash_index_t *hi;
1846 void *val = NULL;
1847 struct nc_session *ns = NULL;
1848 struct session_with_mutex *swm = NULL;
1849 const char *hashed_key = NULL;
1850 apr_ssize_t hashed_key_length;
1851 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001852 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001853
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001854 /* get exclusive access to sessions_list (conns) */
1855 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1856 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1857 return;
1858 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001859 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001860 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1861 swm = (struct session_with_mutex *) val;
1862 if (swm == NULL) {
1863 continue;
1864 }
1865 ns = swm->session;
1866 if (ns == NULL) {
1867 continue;
1868 }
1869 pthread_mutex_lock(&swm->lock);
1870 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1871 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001872 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001873 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
1874 pthread_mutex_unlock(&swm->lock);
1875
1876 /* close_and_free_session handles locking on its own */
1877 close_and_free_session(server, swm);
1878 } else {
1879 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001880 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001881 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001882 /* get exclusive access to sessions_list (conns) */
1883 if (pthread_rwlock_unlock (&session_lock) != 0) {
1884 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1885 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001886}
1887
1888
1889/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001890 * This is actually implementation of NETCONF client
1891 * - requests are received from UNIX socket in the predefined format
1892 * - results are replied through the same way
1893 * - the daemon run as a separate process, but it is started and stopped
1894 * automatically by Apache.
1895 *
1896 */
Radek Krejci469aab82012-07-22 18:42:20 +02001897static void forked_proc(apr_pool_t * pool, server_rec * server)
1898{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001899 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001900 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001901 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001902 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001903 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001904 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001905 struct pass_to_thread * arg;
1906 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1907 struct timespec maxtime;
1908 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001909 #ifdef WITH_NOTIFICATIONS
1910 char use_notifications = 0;
1911 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001912
Tomas Cejka6b886e02013-07-05 09:53:17 +02001913 http_server = server;
1914
Tomas Cejka404d37e2013-04-13 02:31:35 +02001915 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001916 maxtime.tv_sec = 5;
1917 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001918
Radek Krejcif23850c2012-07-23 16:14:17 +02001919 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001920 unixd_setup_child();
1921
Radek Krejciae021c12012-07-25 18:03:52 +02001922 cfg = ap_get_module_config(server->module_config, &netconf_module);
1923 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001924 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1925 return;
1926 }
Radek Krejci469aab82012-07-22 18:42:20 +02001927
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001928 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001929 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1930 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1931 return;
1932 }
1933
1934 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001935 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001936 unlink(local.sun_path);
1937 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1938
1939 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1940 if (errno == EADDRINUSE) {
1941 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1942 close(lsock);
1943 exit(0);
1944 }
1945 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1946 close(lsock);
1947 return;
1948 }
1949
1950 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1951 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1952 close(lsock);
1953 return;
1954 }
1955
Tomas Cejkaba21b382013-04-13 02:37:32 +02001956 /* prepare internal lists */
1957 netconf_sessions_list = apr_hash_make(pool);
1958
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001959 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02001960 if (notification_init(pool, server) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001961 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1962 use_notifications = 0;
1963 } else {
1964 use_notifications = 1;
1965 }
1966 #endif
1967
Radek Krejci469aab82012-07-22 18:42:20 +02001968 /* setup libnetconf's callbacks */
1969 nc_verbosity(NC_VERB_DEBUG);
1970 clb_print_server = server;
1971 nc_callback_print(clb_print);
1972 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1973 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1974 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001975 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001976
1977 /* disable publickey authentication */
1978 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1979
David Kupka8e60a372012-09-04 09:15:20 +02001980 /* create mutex protecting session list */
1981 pthread_rwlockattr_init(&lock_attrs);
1982 /* rwlock is shared only with threads in this process */
1983 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1984 /* create rw lock */
1985 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1986 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1987 close (lsock);
1988 return;
1989 }
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001990 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "init of notif_history_key.");
1991 if (pthread_key_create(&notif_history_key, NULL) != 0) {
1992 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "init of notif_history_key failed");
1993 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001994
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001995 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001996 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001997 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001998 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001999 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002000 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002001 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
2002 }
2003 if (use_notifications == 1) {
2004 notification_handle();
2005 }
2006 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002007 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02002008 check_timeout_and_close(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002009 }
Radek Krejci469aab82012-07-22 18:42:20 +02002010
2011 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02002012 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002013 if (((unsigned int)tv.tv_sec - olds) > 60) {
2014 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
2015 olds = tv.tv_sec;
2016 }
David Kupka8e60a372012-09-04 09:15:20 +02002017 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02002018 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
2019 apr_sleep(SLEEP_TIME);
2020 continue;
2021 } else if (client == -1 && (errno == EINTR)) {
2022 continue;
2023 } else if (client == -1) {
2024 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
2025 continue;
2026 }
Radek Krejci469aab82012-07-22 18:42:20 +02002027
2028 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02002029 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02002030
David Kupka8e60a372012-09-04 09:15:20 +02002031 arg = malloc (sizeof(struct pass_to_thread));
2032 arg->client = client;
2033 arg->pool = pool;
2034 arg->server = server;
2035 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02002036
David Kupka8e60a372012-09-04 09:15:20 +02002037 /* start new thread. It will serve this particular request and then terminate */
2038 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
2039 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
2040 } else {
2041 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
2042 pthread_count++;
2043 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2044 ptids[pthread_count] = 0;
2045 }
Radek Krejci469aab82012-07-22 18:42:20 +02002046
David Kupka8e60a372012-09-04 09:15:20 +02002047 /* check if some thread already terminated, free some resources by joining it */
2048 for (i=0; i<pthread_count; i++) {
2049 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
2050 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
2051 pthread_count--;
2052 if (pthread_count > 0) {
2053 /* place last Thread ID on the place of joined one */
2054 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002055 }
Radek Krejci469aab82012-07-22 18:42:20 +02002056 }
2057 }
David Kupka8e60a372012-09-04 09:15:20 +02002058 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002059 }
2060
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002061 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002062 /* join all threads */
2063 for (i=0; i<pthread_count; i++) {
2064 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2065 }
2066 free (ptids);
2067
Radek Krejci469aab82012-07-22 18:42:20 +02002068 close(lsock);
2069
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002070 #ifdef WITH_NOTIFICATIONS
2071 notification_close();
2072 #endif
2073
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002074 /* close all NETCONF sessions */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002075 close_all_nc_sessions(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002076
David Kupka8e60a372012-09-04 09:15:20 +02002077 /* destroy rwlock */
2078 pthread_rwlock_destroy(&session_lock);
2079 pthread_rwlockattr_destroy(&lock_attrs);
2080
Radek Krejci469aab82012-07-22 18:42:20 +02002081 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
2082
2083 exit(APR_SUCCESS);
2084}
2085
Radek Krejcif23850c2012-07-23 16:14:17 +02002086static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002087{
Radek Krejcif23850c2012-07-23 16:14:17 +02002088 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
2089
2090 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2091 apr_pool_create(&config->pool, pool);
2092 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002093 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002094
2095 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002096}
2097
2098static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2099 apr_pool_t * plog, server_rec * s)
2100{
Radek Krejcif23850c2012-07-23 16:14:17 +02002101 mod_netconf_cfg *config;
2102 apr_status_t res;
2103
Radek Krejci469aab82012-07-22 18:42:20 +02002104 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002105 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002106 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002107
2108 /*
2109 * The following checks if this routine has been called before.
2110 * This is necessary because the parent process gets initialized
2111 * a couple of times as the server starts up.
2112 */
2113 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2114 if (!data) {
2115 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2116 return (OK);
2117 }
2118
Radek Krejcif23850c2012-07-23 16:14:17 +02002119 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
2120 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002121
Radek Krejcif23850c2012-07-23 16:14:17 +02002122 if (config && config->forkproc == NULL) {
2123 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2124 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002125 switch (res) {
2126 case APR_INCHILD:
2127 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002128 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002129 apr_signal(SIGTERM, signal_handler);
2130
2131 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02002132 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002133
2134 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002135 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002136
Radek Krejcif23850c2012-07-23 16:14:17 +02002137 /* I never should be here, wtf?!? */
2138 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002139 exit(APR_EGENERAL);
2140 break;
2141 case APR_INPARENT:
2142 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002143 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002144 break;
2145 default:
2146 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
2147 break;
2148 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002149 } else {
2150 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002151 }
2152
2153 return OK;
2154}
2155
Radek Krejci469aab82012-07-22 18:42:20 +02002156/**
2157 * Register module hooks
2158 */
2159static void mod_netconf_register_hooks(apr_pool_t * p)
2160{
Radek Krejcif23850c2012-07-23 16:14:17 +02002161 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02002162}
2163
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002164static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2165{
2166 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2167 return NULL;
2168}
2169
2170static const command_rec netconf_cmds[] = {
2171 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2172 {NULL}
2173};
2174
Radek Krejci469aab82012-07-22 18:42:20 +02002175/* Dispatch list for API hooks */
2176module AP_MODULE_DECLARE_DATA netconf_module = {
2177 STANDARD20_MODULE_STUFF,
2178 NULL, /* create per-dir config structures */
2179 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002180 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002181 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002182 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002183 mod_netconf_register_hooks /* register hooks */
2184};
Radek Krejcia332b692012-11-12 16:15:54 +01002185