blob: 318cdce57a8006d52f13c20bd9f1efe39d27dc58 [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01008 * \date 2013
Radek Krejci469aab82012-07-22 18:42:20 +02009 */
10/*
Tomas Cejkad340dbf2013-03-24 20:36:57 +010011 * Copyright (C) 2011-2013 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020012 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
Tomas Cejka689a1042013-01-16 15:08:25 +010046static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $";
Radek Krejci469aab82012-07-22 18:42:20 +020047
Radek Krejci7b4ddd02012-07-30 08:09:58 +020048#include <unistd.h>
49#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020050#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010053#include <sys/fcntl.h>
David Kupka8e60a372012-09-04 09:15:20 +020054#include <pthread.h>
55#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020056
57#include <unixd.h>
58#include <httpd.h>
59#include <http_log.h>
60#include <http_config.h>
61
62#include <apr_sha1.h>
63#include <apr_hash.h>
64#include <apr_signal.h>
65#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020066
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020067#include <json/json.h>
68
Radek Krejci469aab82012-07-22 18:42:20 +020069#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020070#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020071
Tomas Cejkad340dbf2013-03-24 20:36:57 +010072#ifdef WITH_NOTIFICATIONS
73#include "notification_module.h"
74#endif
75
Tomas Cejka94da2c52013-01-08 18:20:30 +010076#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020077#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020078
79#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020080#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020081#define MAX_SOCKET_CL 10
82#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020083#define NOTIFICATION_QUEUE_SIZE 10
84#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020085#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020086
87/* sleep in master process for non-blocking socket reading */
88#define SLEEP_TIME 200
89
90#ifndef offsetof
91#define offsetof(type, member) ((size_t) ((type *) 0)->member)
92#endif
93
Tomas Cejka027f3bc2012-11-10 20:28:36 +010094/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020095struct timeval timeout = { 1, 0 };
96
Tomas Cejka5064c232013-01-17 09:30:58 +010097#define NCWITHDEFAULTS NCWD_MODE_NOTSET
98
99
Radek Krejci469aab82012-07-22 18:42:20 +0200100#define MSG_OK 0
101#define MSG_OPEN 1
102#define MSG_DATA 2
103#define MSG_CLOSE 3
104#define MSG_ERROR 4
105#define MSG_UNKNOWN 5
106
Radek Krejci469aab82012-07-22 18:42:20 +0200107module AP_MODULE_DECLARE_DATA netconf_module;
108
Tomas Cejka47387fd2013-06-10 20:37:46 +0200109pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
Tomas Cejka6b886e02013-07-05 09:53:17 +0200110pthread_mutex_t ntf_history_lock; /**< mutex protecting notification history list */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200111apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200112
Tomas Cejkad016f9c2013-07-10 09:16:16 +0200113static pthread_key_t notif_history_key;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200114server_rec *http_server = NULL;
115
Radek Krejci469aab82012-07-22 18:42:20 +0200116volatile int isterminated = 0;
117
118static char* password;
119
Radek Krejci469aab82012-07-22 18:42:20 +0200120static void signal_handler(int sign)
121{
122 switch (sign) {
123 case SIGTERM:
124 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200125 break;
126 }
127}
128
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200129static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200130{
Radek Krejcif23850c2012-07-23 16:14:17 +0200131 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
132 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200133 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200134
Radek Krejci469aab82012-07-22 18:42:20 +0200135 apr_sha1_ctx_t sha1_ctx;
136 apr_sha1_init(&sha1_ctx);
137 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
138 apr_sha1_update(&sha1_ctx, port, strlen(port));
139 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200140 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200141
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200144 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200146 }
147 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200148
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200150}
151
152int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
153{
154 /* always approve */
155 return (EXIT_SUCCESS);
156}
157
158char* netconf_callback_sshauth_password (const char* username, const char* hostname)
159{
160 char* buf;
161
162 buf = malloc ((strlen(password) + 1) * sizeof(char));
163 apr_cpystrn(buf, password, strlen(password) + 1);
164
165 return (buf);
166}
167
168void netconf_callback_sshauth_interactive (const char* name,
169 int name_len,
170 const char* instruction,
171 int instruction_len,
172 int num_prompts,
173 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
174 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
175 void** abstract)
176{
177 int i;
178
179 for (i=0; i<num_prompts; i++) {
180 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
181 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
182 responses[i].length = strlen(responses[i].text) + 1;
183 }
184
185 return;
186}
187
Radek Krejcic11fd862012-07-26 12:41:21 +0200188static json_object *err_reply = NULL;
189void netconf_callback_error_process(const char* tag,
190 const char* type,
191 const char* severity,
192 const char* apptag,
193 const char* path,
194 const char* message,
195 const char* attribute,
196 const char* element,
197 const char* ns,
198 const char* sid)
199{
200 err_reply = json_object_new_object();
201 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
202 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
203 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
204 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
205 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
206 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
207 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
208 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
209 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
210 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
211 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
212}
213
Tomas Cejka47387fd2013-06-10 20:37:46 +0200214/**
215 * should be used in locked area
216 */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200217void prepare_status_message(server_rec* server, struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200218{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200219 json_object *json_obj = NULL;
Tomas Cejka73286932013-05-27 22:54:35 +0200220 //json_object *old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200221 const char *cpbltstr;
222 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200223
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200224 if (s == NULL) {
225 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "No session given.");
226 return;
227 }
228
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200229 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200230 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200231 //json_object_put(s->hello_message);
232 s->hello_message = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200233
234 //old_sid = json_object_object_get(s->hello_message, "sid");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200235 }
236 s->hello_message = json_object_new_object();
237 if (session != NULL) {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200238 /** \todo reload hello - save old sid */
239 //if (old_sid != NULL) {
240 // /* use previous sid */
241 // json_object_object_add(s->hello_message, "sid", old_sid);
242 //} else {
243 /* we don't have old sid */
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200244 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200245 //}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200246 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
247 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
248 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
249 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
250 cpblts = nc_session_get_cpblts (session);
251 if (cpblts != NULL) {
252 json_obj = json_object_new_array();
253 nc_cpblts_iter_start (cpblts);
254 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
255 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
256 }
257 json_object_object_add(s->hello_message, "capabilities", json_obj);
258 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200259 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "%s", json_object_to_json_string(s->hello_message));
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200260 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200261 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200262 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
263 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
264 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200265 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200266
267}
268
269
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200270/**
271 * \defgroup netconf_operations NETCONF operations
272 * The list of NETCONF operations that mod_netconf supports.
273 * @{
274 */
275
276/**
277 * \brief Connect to NETCONF server
278 *
279 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
280 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200281static char* netconf_connect(server_rec* server, apr_pool_t* pool, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200282{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200283 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200284 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200285 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200286
287 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200288 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200289 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
290 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200291 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200292 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200293
Radek Krejci469aab82012-07-22 18:42:20 +0200294 /* if connected successful, add session to the list */
295 if (session != NULL) {
296 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200297 session_key = gen_ncsession_hash(
298 (host==NULL) ? "localhost" : host,
299 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200300 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200301
Tomas Cejkaba21b382013-04-13 02:37:32 +0200302 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200303 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
304 nc_session_free(session);
305 free (locked_session);
306 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
307 return NULL;
308 }
309 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200310 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200311 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200312 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200313 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100314 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200315 /* get exclusive access to sessions_list (conns) */
316 if (pthread_rwlock_wrlock (&session_lock) != 0) {
317 nc_session_free(session);
318 free (locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200319 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200320 return NULL;
321 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200322 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200323 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100324 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200325 apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100326 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200327
Tomas Cejka47387fd2013-06-10 20:37:46 +0200328 /* lock session */
329 pthread_mutex_lock(&locked_session->lock);
330
331 /* unlock session list */
332 if (pthread_rwlock_unlock (&session_lock) != 0) {
333 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
334 }
335
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200336 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200337 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200338
Tomas Cejka47387fd2013-06-10 20:37:46 +0200339 pthread_mutex_unlock(&locked_session->lock);
340
Radek Krejci469aab82012-07-22 18:42:20 +0200341 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200342 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200343 } else {
344 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200345 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200346 }
347
Radek Krejci469aab82012-07-22 18:42:20 +0200348}
349
Tomas Cejka47387fd2013-06-10 20:37:46 +0200350static int close_and_free_session(server_rec *server, struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200351{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200352 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock private lock.");
353 if (pthread_mutex_lock(&locked_session->lock) != 0) {
354 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
355 }
356 locked_session->ntfc_subscribed = 0;
357 locked_session->closed = 1;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200358 nc_session_free(locked_session->session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200359 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "session closed.");
360 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock private lock.");
361 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
362 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
363 }
364
365 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
366 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "closed session, disabled notif(?), wait 2s");
367 usleep(500000); /* let notification thread stop */
368
369 /* session shouldn't be used by now */
370 /** \todo free all notifications from queue */
371 apr_array_clear(locked_session->notifications);
372 pthread_mutex_destroy(&locked_session->lock);
373 if (locked_session->hello_message != NULL) {
374 /** \todo free hello_message */
375 //json_object_put(locked_session->hello_message);
376 locked_session->hello_message = NULL;
377 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200378 locked_session->session = NULL;
Tomas Cejkaaecc5f72013-10-01 00:03:50 +0200379 free(locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200380 locked_session = NULL;
381 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed, everything cleared.");
382 return (EXIT_SUCCESS);
383}
384
385static int netconf_close(server_rec* server, const char* session_key)
386{
David Kupka8e60a372012-09-04 09:15:20 +0200387 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200388
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200389 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200390
David Kupka8e60a372012-09-04 09:15:20 +0200391 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200392 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200393 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200394 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
David Kupka8e60a372012-09-04 09:15:20 +0200395 return EXIT_FAILURE;
396 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200397 /* get session to close */
398 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
399 /* remove session from the active sessions list -> nobody new can now work with session */
400 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200401
Tomas Cejka47387fd2013-06-10 20:37:46 +0200402 if (pthread_rwlock_unlock (&session_lock) != 0) {
403 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock");
404 }
405
406 if ((locked_session != NULL) && (locked_session->session != NULL)) {
407 return close_and_free_session(server, locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200408 } else {
409 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200410 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200411 }
412}
413
Tomas Cejkac7929632013-10-24 19:25:15 +0200414/**
415 * Test reply message type and return error message.
416 *
417 * \param[in] server httpd server for logging
418 * \param[in] session nc_session internal struct
419 * \param[in] session_key session key, NULL to disable disconnect on error
420 * \param[in] msgt RPC-REPLY message type
421 * \param[out] data
422 * \return NULL on success
423 */
424json_object *netconf_test_reply(server_rec *server, struct nc_session *session, const char *session_key, NC_MSG_TYPE msgt, nc_reply *reply, char **data)
425{
426 NC_REPLY_TYPE replyt;
427
428 /* process the result of the operation */
429 switch (msgt) {
430 case NC_MSG_UNKNOWN:
431 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
432 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
433 if (session_key != NULL) {
434 netconf_close(server, session_key);
435 }
436 return create_error("Internal: Receiving RPC-REPLY failed.");
437 }
438 case NC_MSG_NONE:
439 /* there is error handled by callback */
440 (*data) = NULL;
441 return NULL;
442 case NC_MSG_REPLY:
443 switch (replyt = nc_reply_get_type(reply)) {
444 case NC_REPLY_OK:
445 (*data) = NULL;
446 return NULL;
447 case NC_REPLY_DATA:
448 if (((*data) = nc_reply_get_data (reply)) == NULL) {
449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
450 return create_error("Internal: No data from reply received.");
451 } else {
452 return NULL;
453 }
454 break;
455 case NC_REPLY_ERROR:
456 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
457 (*data) = NULL;
458 return create_error(nc_reply_get_errormsg(reply));
459 default:
460 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
461 (*data) = NULL;
462 return create_error(nc_reply_get_errormsg(reply));
463 }
464 break;
465 default:
466 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
467 (*data) = NULL;
468 return create_error("Internal: Unexpected RPC-REPLY message type.");
469 }
470}
471
472json_object *netconf_unlocked_op(server_rec* server, struct nc_session *session, nc_rpc* rpc)
Tomas Cejka6b886e02013-07-05 09:53:17 +0200473{
474 nc_reply* reply = NULL;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200475 NC_MSG_TYPE msgt;
Tomas Cejka6b886e02013-07-05 09:53:17 +0200476
477 /* check requests */
478 if (rpc == NULL) {
479 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200480 return create_error("Internal error: RPC is not created");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200481 }
482
483 if (session != NULL) {
484 /* send the request and get the reply */
485 msgt = nc_session_send_recv(session, rpc, &reply);
486 /* process the result of the operation */
Tomas Cejkac7929632013-10-24 19:25:15 +0200487 return netconf_test_reply(server, session, NULL, msgt, reply, NULL);
Tomas Cejka6b886e02013-07-05 09:53:17 +0200488 } else {
489 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200490 return create_error("Internal error: Unknown session to process.");
Tomas Cejka6b886e02013-07-05 09:53:17 +0200491 }
492}
493
Tomas Cejkac7929632013-10-24 19:25:15 +0200494/**
495 * Perform RPC method that returns data.
496 *
497 * \param[in] server httpd server
498 * \param[in] session_key session identifier
499 * \param[in] rpc RPC message to perform
500 * \param[out] received_data received data string, can be NULL when no data expected, value can be set to NULL if no data received
501 * \return NULL on success, json object with error otherwise
502 */
503static json_object *netconf_op(server_rec* server, const char* session_key, nc_rpc* rpc, char **received_data)
Radek Krejci469aab82012-07-22 18:42:20 +0200504{
505 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200506 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200507 nc_reply* reply = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200508 json_object *res = NULL;
509 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100510 NC_MSG_TYPE msgt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200511
Radek Krejci8e4632a2012-07-26 13:40:34 +0200512 /* check requests */
513 if (rpc == NULL) {
514 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
Tomas Cejkac7929632013-10-24 19:25:15 +0200515 res = create_error("Internal: RPC could not be created.");
516 data = NULL;
517 goto finished;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200518 }
519
David Kupka8e60a372012-09-04 09:15:20 +0200520 /* get non-exclusive (read) access to sessions_list (conns) */
521 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200522 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200523 res = create_error("Internal: Lock failed.");
524 data = NULL;
525 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200526 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200527 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200528 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 +0200529 if (locked_session != NULL) {
530 session = locked_session->session;
531 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200532 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200533 /* get exclusive access to session */
534 if (pthread_mutex_lock(&locked_session->lock) != 0) {
535 /* unlock before returning error */
536 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200537
Tomas Cejka47387fd2013-06-10 20:37:46 +0200538 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200539 res = create_error("Internal: Could not unlock.");
540 goto finished;
David Kupka8e60a372012-09-04 09:15:20 +0200541 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200542 res = create_error("Internal: Could not unlock.");
David Kupka8e60a372012-09-04 09:15:20 +0200543 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200544 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200545
Tomas Cejka47387fd2013-06-10 20:37:46 +0200546 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkac7929632013-10-24 19:25:15 +0200547 res = create_error("Internal: Could not unlock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200548 }
549
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200550 locked_session->last_activity = apr_time_now();
Tomas Cejkac7929632013-10-24 19:25:15 +0200551
Radek Krejci035bf4e2012-07-25 10:59:09 +0200552 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100553 msgt = nc_session_send_recv(session, rpc, &reply);
554
David Kupka8e60a372012-09-04 09:15:20 +0200555 /* first release exclusive lock for this session */
556 pthread_mutex_unlock(&locked_session->lock);
557 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200558
Tomas Cejkac7929632013-10-24 19:25:15 +0200559 res = netconf_test_reply(server, session, session_key, msgt, reply, &data);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200560 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100561 /* release lock on failure */
562 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200563 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 +0100564 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejkac7929632013-10-24 19:25:15 +0200566 res = create_error("Unknown session to process.");
567 data = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200568 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200569finished:
570 nc_reply_free(reply);
571 if (received_data != NULL) {
572 (*received_data) = data;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200573 }
Tomas Cejkac7929632013-10-24 19:25:15 +0200574 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200575}
576
Tomas Cejka47387fd2013-06-10 20:37:46 +0200577static char* netconf_getconfig(server_rec* server, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200578{
579 nc_rpc* rpc;
580 struct nc_filter *f = NULL;
581 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200582 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200583
584 /* create filter if set */
585 if (filter != NULL) {
586 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
587 }
588
589 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100590 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200591 nc_filter_free(f);
592 if (rpc == NULL) {
593 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
594 return (NULL);
595 }
596
Tomas Cejka94674662013-09-13 15:55:24 +0200597 /* tell server to show all elements even if they have default values */
598 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
599 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
600 }
601
Tomas Cejkac7929632013-10-24 19:25:15 +0200602 res = netconf_op(server, session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200603 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200604 if (res == NULL) {
605 /* TODO return error message somehow */
606 }
607
Radek Krejci8e4632a2012-07-26 13:40:34 +0200608 return (data);
609}
610
Tomas Cejka47387fd2013-06-10 20:37:46 +0200611static 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 +0100612{
613 nc_rpc* rpc;
614 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200615 json_object *res = NULL;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100616
617 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100618 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100619 if (rpc == NULL) {
620 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
621 return (NULL);
622 }
623
Tomas Cejkac7929632013-10-24 19:25:15 +0200624 res = netconf_op(server, session_key, rpc, &data);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100625 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200626 if (res == NULL) {
627 /* TODO return error message somehow */
628 }
629
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100630 return (data);
631}
632
Tomas Cejka47387fd2013-06-10 20:37:46 +0200633static char* netconf_get(server_rec* server, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200634{
635 nc_rpc* rpc;
636 struct nc_filter *f = NULL;
637 char* data = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200638 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200639
640 /* create filter if set */
641 if (filter != NULL) {
642 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
643 }
644
645 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100646 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200647 nc_filter_free(f);
648 if (rpc == NULL) {
649 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
650 return (NULL);
651 }
652
Tomas Cejka94674662013-09-13 15:55:24 +0200653 /* tell server to show all elements even if they have default values */
654 if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
655 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: setting withdefaults failed");
656 }
657
Tomas Cejkac7929632013-10-24 19:25:15 +0200658 res = netconf_op(server, session_key, rpc, &data);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200659 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200660 if (res == NULL) {
661 /* TODO return error message somehow */
662 }
663
Radek Krejci8e4632a2012-07-26 13:40:34 +0200664 return (data);
665}
666
Tomas Cejkac7929632013-10-24 19:25:15 +0200667static json_object *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 +0200668{
669 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200670 json_object *res = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200671
672 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100673 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
674 if (target == NC_DATASTORE_URL) {
675 rpc = nc_rpc_copyconfig(source, target, config, url);
676 } else {
677 rpc = nc_rpc_copyconfig(source, target, config);
678 }
679 } else {
680 if (target == NC_DATASTORE_URL) {
681 rpc = nc_rpc_copyconfig(source, target, url);
682 } else {
683 rpc = nc_rpc_copyconfig(source, target);
684 }
685 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200686 if (rpc == NULL) {
687 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200688 return create_error("Internal: Creating rpc request failed");
Radek Krejci8e4632a2012-07-26 13:40:34 +0200689 }
690
Tomas Cejkac7929632013-10-24 19:25:15 +0200691 res = netconf_op(server, session_key, rpc, NULL);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200692 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200693
694 return res;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200695}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200696
Tomas Cejkac7929632013-10-24 19:25:15 +0200697static json_object *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 +0200698{
699 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200700 json_object *res = NULL;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200701
702 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100703 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
704 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200705 if (rpc == NULL) {
706 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200707 return create_error("Internal: Creating rpc request failed");
Radek Krejci62ab34b2012-07-26 13:42:05 +0200708 }
709
Tomas Cejkac7929632013-10-24 19:25:15 +0200710 res = netconf_op(server, session_key, rpc, NULL);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200711 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200712
713 return res;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200714}
715
Tomas Cejkac7929632013-10-24 19:25:15 +0200716static json_object *netconf_killsession(server_rec* server, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200717{
718 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200719 json_object *res = NULL;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200720
721 /* create requests */
722 rpc = nc_rpc_killsession(sid);
723 if (rpc == NULL) {
724 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200725 return create_error("Internal: Creating rpc request failed");
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200726 }
727
Tomas Cejkac7929632013-10-24 19:25:15 +0200728 res = netconf_op(server, session_key, rpc, NULL);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200729 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200730 return res;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200731}
732
Tomas Cejkac7929632013-10-24 19:25:15 +0200733static json_object *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 +0200734{
735 nc_rpc* rpc;
Tomas Cejkac7929632013-10-24 19:25:15 +0200736 json_object *res = NULL;
Radek Krejci2f318372012-07-26 14:22:35 +0200737
738 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200739 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200740 if (rpc == NULL) {
741 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200742 return create_error("Internal: Creating rpc request failed");
Radek Krejci2f318372012-07-26 14:22:35 +0200743 }
744
Tomas Cejkac7929632013-10-24 19:25:15 +0200745 res = netconf_op(server, session_key, rpc, NULL);
Radek Krejci2f318372012-07-26 14:22:35 +0200746 nc_rpc_free (rpc);
Tomas Cejkac7929632013-10-24 19:25:15 +0200747 return res;
Radek Krejci2f318372012-07-26 14:22:35 +0200748}
749
Tomas Cejkac7929632013-10-24 19:25:15 +0200750static json_object *netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target, const char *url)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200751{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200752 nc_rpc *rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200753 json_object *res = NULL;
Tomas Cejka404d37e2013-04-13 02:31:35 +0200754 if (target != NC_DATASTORE_URL) {
755 rpc = nc_rpc_deleteconfig(target);
756 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +0200757 rpc = nc_rpc_deleteconfig(target, url);
758 }
759 if (rpc == NULL) {
Tomas Cejka404d37e2013-04-13 02:31:35 +0200760 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200761 return create_error("Internal: Creating rpc request failed");
Tomas Cejka404d37e2013-04-13 02:31:35 +0200762 }
763
Tomas Cejkac7929632013-10-24 19:25:15 +0200764 res = netconf_op(server, session_key, rpc, NULL);
765 nc_rpc_free (rpc);
766 return res;
Radek Krejci5cd7d422012-07-26 14:50:29 +0200767}
768
Tomas Cejkac7929632013-10-24 19:25:15 +0200769static json_object *netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200770{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200771 return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200772}
773
Tomas Cejkac7929632013-10-24 19:25:15 +0200774static json_object *netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200775{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200776 return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200777}
778
Tomas Cejkac7929632013-10-24 19:25:15 +0200779static json_object *netconf_generic(server_rec* server, const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200780{
Tomas Cejka00635972013-06-03 15:10:52 +0200781 nc_rpc* rpc = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +0200782 json_object *res = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200783
784 /* create requests */
785 rpc = nc_rpc_generic(content);
786 if (rpc == NULL) {
787 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
Tomas Cejkac7929632013-10-24 19:25:15 +0200788 return create_error("Internal: Creating rpc request failed");
Radek Krejci80c10d92012-07-30 08:38:50 +0200789 }
790
Radek Krejcia332b692012-11-12 16:15:54 +0100791 if (data != NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +0200792 // TODO ?free(*data);
793 (*data) = NULL;
Radek Krejcia332b692012-11-12 16:15:54 +0100794 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200795
796 /* get session where send the RPC */
Tomas Cejkac7929632013-10-24 19:25:15 +0200797 res = netconf_op(server, session_key, rpc, data);
798 nc_rpc_free (rpc);
799 return res;
Radek Krejci80c10d92012-07-30 08:38:50 +0200800}
801
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200802/**
803 * @}
804 *//* netconf_operations */
805
Radek Krejci469aab82012-07-22 18:42:20 +0200806server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200807void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200808{
Radek Krejci7338bde2012-08-10 12:57:30 +0200809 switch (level) {
810 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200811 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200812 break;
813 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200814 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200815 break;
816 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200817 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200818 break;
819 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200820 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200821 break;
822 }
Radek Krejci469aab82012-07-22 18:42:20 +0200823}
824
Tomas Cejka64b87482013-06-03 16:30:53 +0200825/**
Tomas Cejka6e8f4262013-07-10 09:20:19 +0200826 * Receive message from client over UNIX socket and return pointer to it.
Tomas Cejka64b87482013-06-03 16:30:53 +0200827 * Caller should free message memory.
828 * \param[in] client socket descriptor of client
829 * \param[in] server httpd server for logging
830 * \return pointer to message
831 */
832char *get_framed_message(server_rec *server, int client)
833{
834 /* read json in chunked framing */
835 unsigned int buffer_size = 0;
836 ssize_t buffer_len = 0;
837 char *buffer = NULL;
838 char c;
839 ssize_t ret;
840 int i, chunk_len;
841 char chunk_len_str[12];
842
843 while (1) {
844 /* read chunk length */
845 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
846 if (buffer != NULL) {
847 free (buffer);
848 buffer = NULL;
849 }
850 break;
851 }
852 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
853 if (buffer != NULL) {
854 free (buffer);
855 buffer = NULL;
856 }
857 break;
858 }
859 i=0;
860 memset (chunk_len_str, 0, 12);
861 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
862 if (i==0 && c == '#') {
863 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
864 /* end but invalid */
865 if (buffer != NULL) {
866 free (buffer);
867 buffer = NULL;
868 }
869 }
870 /* end of message, double-loop break */
871 goto msg_complete;
872 }
873 chunk_len_str[i++] = c;
874 if (i==11) {
875 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
876 break;
877 }
878 }
879 if (c != '\n') {
880 if (buffer != NULL) {
881 free (buffer);
882 buffer = NULL;
883 }
884 break;
885 }
886 chunk_len_str[i] = 0;
887 if ((chunk_len = atoi (chunk_len_str)) == 0) {
888 if (buffer != NULL) {
889 free (buffer);
890 buffer = NULL;
891 }
892 break;
893 }
894 buffer_size += chunk_len+1;
895 buffer = realloc (buffer, sizeof(char)*buffer_size);
896 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
897 if (buffer != NULL) {
898 free (buffer);
899 buffer = NULL;
900 }
901 break;
902 }
903 buffer_len += ret;
904 }
905msg_complete:
906 return buffer;
907}
908
Tomas Cejkad5b53772013-06-08 23:01:07 +0200909NC_DATASTORE parse_datastore(const char *ds)
910{
911 if (strcmp(ds, "running") == 0) {
912 return NC_DATASTORE_RUNNING;
913 } else if (strcmp(ds, "startup") == 0) {
914 return NC_DATASTORE_STARTUP;
915 } else if (strcmp(ds, "candidate") == 0) {
916 return NC_DATASTORE_CANDIDATE;
Tomas Cejka4003a702013-10-01 00:02:45 +0200917 } else if (strcmp(ds, "url") == 0) {
918 return NC_DATASTORE_URL;
Tomas Cejkad5b53772013-06-08 23:01:07 +0200919 }
920 return -1;
921}
922
923json_object *create_error(const char *errmess)
924{
925 json_object *reply = json_object_new_object();
926 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
927 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
928 return reply;
929
930}
931
932json_object *create_data(const char *data)
933{
934 json_object *reply = json_object_new_object();
935 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
936 json_object_object_add(reply, "data", json_object_new_string(data));
937 return reply;
938}
939
Tomas Cejka47387fd2013-06-10 20:37:46 +0200940json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +0200941{
942 const char *host = NULL;
943 const char *port = NULL;
944 const char *user = NULL;
945 const char *pass = NULL;
946 json_object *capabilities = NULL;
947 json_object *reply = NULL;
948 char *session_key_hash = NULL;
949 struct nc_cpblts* cpblts = NULL;
950 unsigned int len, i;
951
952 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
953 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
954 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
955 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
956 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
957
958 capabilities = json_object_object_get((json_object *) request, "capabilities");
959 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
960 cpblts = nc_cpblts_new(NULL);
961 for (i=0; i<len; i++) {
962 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
963 }
964 } else {
965 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
966 }
967
968 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
969 if ((host == NULL) || (user == NULL)) {
970 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
971 session_key_hash = NULL;
972 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200973 session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts);
Tomas Cejkad5b53772013-06-08 23:01:07 +0200974 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
975 }
976 if (cpblts != NULL) {
977 nc_cpblts_free(cpblts);
978 }
979
980 if (session_key_hash == NULL) {
981 /* negative reply */
982 if (err_reply == NULL) {
983 reply = json_object_new_object();
984 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
985 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
986 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
987 } else {
988 /* use filled err_reply from libnetconf's callback */
989 reply = err_reply;
990 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
991 }
992 } else {
993 /* positive reply */
994 reply = json_object_new_object();
995 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
996 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
997
998 free(session_key_hash);
999 }
1000 return reply;
1001}
1002
Tomas Cejka47387fd2013-06-10 20:37:46 +02001003json_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 +02001004{
1005 const char *filter = NULL;
1006 const char *data = NULL;
1007 json_object *reply = NULL;
1008
1009 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1010
1011 filter = json_object_get_string(json_object_object_get(request, "filter"));
1012
Tomas Cejka47387fd2013-06-10 20:37:46 +02001013 if ((data = netconf_get(server, session_key, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001014 if (err_reply == NULL) {
1015 reply = create_error("Get information from device failed.");
1016 } else {
1017 /* use filled err_reply from libnetconf's callback */
1018 reply = err_reply;
1019 }
1020 } else {
1021 return create_data(data);
1022 }
1023 return reply;
1024}
1025
Tomas Cejka47387fd2013-06-10 20:37:46 +02001026json_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 +02001027{
1028 NC_DATASTORE ds_type_s = -1;
1029 NC_DATASTORE ds_type_t = -1;
1030 const char *filter = NULL;
1031 const char *data = NULL;
1032 const char *source = NULL;
1033 const char *target = NULL;
1034 json_object *reply = NULL;
1035
1036 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1037
1038 filter = json_object_get_string(json_object_object_get(request, "filter"));
1039
1040 /* get parameters */
1041 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1042 ds_type_t = parse_datastore(target);
1043 }
1044 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1045 ds_type_s = parse_datastore(source);
1046 }
1047 if (ds_type_s == -1) {
1048 return create_error("Invalid source repository type requested.");
1049 }
1050
Tomas Cejka47387fd2013-06-10 20:37:46 +02001051 if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001052 if (err_reply == NULL) {
1053 reply = create_error("Get configuration information from device failed.");
1054 } else {
1055 /* use filled err_reply from libnetconf's callback */
1056 reply = err_reply;
1057 }
1058 } else {
1059 return create_data(data);
1060 }
1061 return reply;
1062}
1063
Tomas Cejka47387fd2013-06-10 20:37:46 +02001064json_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 +02001065{
1066 const char *data = NULL;
1067 const char *identifier = NULL;
1068 const char *version = NULL;
1069 const char *format = NULL;
1070 json_object *reply = NULL;
1071
1072 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1073 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1074 if (identifier == NULL) {
1075 return create_error("No identifier for get-schema supplied.");
1076 }
1077 version = json_object_get_string(json_object_object_get(request, "version"));
1078 format = json_object_get_string(json_object_object_get(request, "format"));
1079
1080 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001081 if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001082 if (err_reply == NULL) {
1083 reply = create_error("Get schema failed.");
1084 } else {
1085 /* use filled err_reply from libnetconf's callback */
1086 reply = err_reply;
1087 }
1088 } else {
1089 return create_data(data);
1090 }
1091 return reply;
1092}
1093
Tomas Cejka47387fd2013-06-10 20:37:46 +02001094json_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 +02001095{
1096 NC_DATASTORE ds_type_s = -1;
1097 NC_DATASTORE ds_type_t = -1;
1098 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1099 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1100 const char *defop = NULL;
1101 const char *erropt = NULL;
1102 const char *config = NULL;
1103 const char *source = NULL;
1104 const char *target = NULL;
1105 json_object *reply = NULL;
1106
1107 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1108
1109 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1110 if (defop != NULL) {
1111 if (strcmp(defop, "merge") == 0) {
1112 defop_type = NC_EDIT_DEFOP_MERGE;
1113 } else if (strcmp(defop, "replace") == 0) {
1114 defop_type = NC_EDIT_DEFOP_REPLACE;
1115 } else if (strcmp(defop, "none") == 0) {
1116 defop_type = NC_EDIT_DEFOP_NONE;
1117 } else {
1118 return create_error("Invalid default-operation parameter.");
1119 }
1120 } else {
1121 defop_type = NC_EDIT_DEFOP_NOTSET;
1122 }
1123
1124 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1125 if (erropt != NULL) {
1126 if (strcmp(erropt, "continue-on-error") == 0) {
1127 erropt_type = NC_EDIT_ERROPT_CONT;
1128 } else if (strcmp(erropt, "stop-on-error") == 0) {
1129 erropt_type = NC_EDIT_ERROPT_STOP;
1130 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1131 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1132 } else {
1133 return create_error("Invalid error-option parameter.");
1134 }
1135 } else {
1136 erropt_type = 0;
1137 }
1138
1139 /* get parameters */
1140 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1141 ds_type_t = parse_datastore(target);
1142 }
1143 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1144 ds_type_s = parse_datastore(source);
1145 }
1146 if (ds_type_t == -1) {
1147 return create_error("Invalid target repository type requested.");
1148 }
1149
1150 config = json_object_get_string(json_object_object_get(request, "config"));
1151 if (config == NULL) {
1152 return create_error("Invalid config data parameter.");
1153 }
1154
Tomas Cejka114b55d2013-10-01 00:04:37 +02001155 if (netconf_editconfig(server, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_TESTSET, config) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001156 if (err_reply == NULL) {
1157 reply = create_error("Edit-config failed.");
1158 } else {
1159 /* use filled err_reply from libnetconf's callback */
1160 reply = err_reply;
1161 }
1162 } else {
1163 reply = json_object_new_object();
1164 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1165 }
1166 return reply;
1167}
1168
Tomas Cejka47387fd2013-06-10 20:37:46 +02001169json_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 +02001170{
1171 NC_DATASTORE ds_type_s = -1;
1172 NC_DATASTORE ds_type_t = -1;
1173 const char *config = NULL;
1174 const char *target = NULL;
1175 const char *source = NULL;
1176 json_object *reply = NULL;
1177
1178 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1179
1180 /* get parameters */
1181 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1182 ds_type_t = parse_datastore(target);
1183 }
1184 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1185 ds_type_s = parse_datastore(source);
1186 }
1187 if (source == NULL) {
1188 /* no explicit source specified -> use config data */
1189 ds_type_s = NC_DATASTORE_CONFIG;
1190 config = json_object_get_string(json_object_object_get(request, "config"));
1191 } else if (ds_type_s == -1) {
1192 /* source datastore specified, but it is invalid */
1193 return create_error("Invalid source repository type requested.");
1194 }
1195
1196 if (ds_type_t == -1) {
1197 /* invalid target datastore specified */
1198 return create_error("Invalid target repository type requested.");
1199 }
1200
1201 if (source == NULL && config == NULL) {
1202 reply = create_error("invalid input parameters - one of source and config is required.");
1203 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001204 if (netconf_copyconfig(server, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001205 if (err_reply == NULL) {
1206 reply = create_error("Copying of configuration failed.");
1207 } else {
1208 /* use filled err_reply from libnetconf's callback */
1209 reply = err_reply;
1210 }
1211 } else {
1212 reply = json_object_new_object();
1213 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1214 }
1215 }
1216 return reply;
1217}
1218
Tomas Cejka47387fd2013-06-10 20:37:46 +02001219json_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 +02001220{
1221 json_object *reply = NULL;
1222 const char *config = NULL;
1223 char *data = NULL;
1224
1225 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1226
1227 config = json_object_get_string(json_object_object_get(request, "content"));
1228
1229 if (config == NULL) {
1230 return create_error("Missing content parameter.");
1231 }
1232
Tomas Cejka47387fd2013-06-10 20:37:46 +02001233 if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001234 if (err_reply == NULL) {
1235 reply = create_error("Killing of session failed.");
1236 } else {
1237 /* use filled err_reply from libnetconf's callback */
1238 reply = err_reply;
1239 }
1240 } else {
1241 if (data == NULL) {
1242 reply = json_object_new_object();
1243 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1244 } else {
1245 return create_data(data);
1246 }
1247 }
1248 return reply;
1249}
1250
Tomas Cejka47387fd2013-06-10 20:37:46 +02001251json_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 +02001252{
1253 json_object *reply = NULL;
1254 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1255
Tomas Cejka47387fd2013-06-10 20:37:46 +02001256 if (netconf_close(server, session_key) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001257 if (err_reply == NULL) {
1258 reply = create_error("Invalid session identifier.");
1259 } else {
1260 /* use filled err_reply from libnetconf's callback */
1261 reply = err_reply;
1262 }
1263 } else {
1264 reply = json_object_new_object();
1265 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1266 }
1267 return reply;
1268}
1269
Tomas Cejka47387fd2013-06-10 20:37:46 +02001270json_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 +02001271{
1272 json_object *reply = NULL;
1273 const char *sid = NULL;
1274
1275 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1276
1277 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1278
1279 if (sid == NULL) {
1280 return create_error("Missing session-id parameter.");
1281 }
1282
Tomas Cejka47387fd2013-06-10 20:37:46 +02001283 if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001284 if (err_reply == NULL) {
1285 reply = create_error("Killing of session failed.");
1286 } else {
1287 /* use filled err_reply from libnetconf's callback */
1288 reply = err_reply;
1289 }
1290 } else {
1291 reply = json_object_new_object();
1292 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1293 }
1294 return reply;
1295}
1296
Tomas Cejka47387fd2013-06-10 20:37:46 +02001297json_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 +02001298{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001299 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001300 struct session_with_mutex * locked_session = NULL;
1301 json_object *reply = NULL;
1302 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1303
Tomas Cejka47387fd2013-06-10 20:37:46 +02001304 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1305 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1306 return NULL;
1307 }
1308
Tomas Cejkad5b53772013-06-08 23:01:07 +02001309 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1310 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001311 pthread_mutex_lock(&locked_session->lock);
1312 if (pthread_rwlock_unlock(&session_lock) != 0) {
1313 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1314 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001315 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001316 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001317 if (temp_session != NULL) {
1318 prepare_status_message(server, locked_session, temp_session);
1319 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001320 nc_session_free(temp_session);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001321 } else {
1322 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1323 reply = create_error("Reload was unsuccessful, connection failed.");
1324 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001325 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001326 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001327 if (pthread_rwlock_unlock(&session_lock) != 0) {
1328 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1329 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001330 reply = create_error("Invalid session identifier.");
1331 }
1332
1333 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1334 reply = locked_session->hello_message;
1335 }
1336 return reply;
1337}
1338
Tomas Cejka47387fd2013-06-10 20:37:46 +02001339json_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 +02001340{
1341 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001342 struct session_with_mutex * locked_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001343 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1344
Tomas Cejka47387fd2013-06-10 20:37:46 +02001345 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1346 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1347 }
1348
Tomas Cejkad5b53772013-06-08 23:01:07 +02001349 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1350 if (locked_session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001351 pthread_mutex_lock(&locked_session->lock);
1352 if (pthread_rwlock_unlock(&session_lock) != 0) {
1353 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1354 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001355 if (locked_session->hello_message != NULL) {
1356 reply = locked_session->hello_message;
1357 } else {
1358 reply = create_error("Invalid session identifier.");
1359 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001360 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001361 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001362 if (pthread_rwlock_unlock(&session_lock) != 0) {
1363 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1364 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001365 reply = create_error("Invalid session identifier.");
1366 }
1367
Tomas Cejka47387fd2013-06-10 20:37:46 +02001368
Tomas Cejkad5b53772013-06-08 23:01:07 +02001369 return reply;
1370}
1371
Tomas Cejka6b886e02013-07-05 09:53:17 +02001372void notification_history(time_t eventtime, const char *content)
1373{
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001374 json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
1375 if (notif_history_array == NULL) {
1376 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "No list of notification history found.");
1377 return;
1378 }
1379 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 +02001380 json_object *notif = json_object_new_object();
1381 if (notif == NULL) {
1382 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Could not allocate memory for notification (json).");
1383 return;
1384 }
1385 json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
1386 json_object_object_add(notif, "content", json_object_new_string(content));
1387 json_object_array_add(notif_history_array, notif);
1388}
1389
1390json_object *handle_op_ntfgethistory(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1391{
1392 json_object *reply = NULL;
1393 const char *sid = NULL;
1394 struct session_with_mutex *locked_session = NULL;
1395 struct nc_session *temp_session = NULL;
1396 nc_rpc *rpc = NULL;
1397 time_t start = 0;
1398 time_t stop = 0;
1399 int64_t from, to;
1400
1401 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get notification history, session %s", session_key);
1402
1403 sid = json_object_get_string(json_object_object_get(request, "session"));
1404 from = json_object_get_int64(json_object_object_get(request, "from"));
1405 to = json_object_get_int64(json_object_object_get(request, "to"));
1406
1407 start = time(NULL) + from;
1408 stop = time(NULL) + to;
1409
1410 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "notification history interval %li %li", (long int) from, (long int) to);
1411
1412 if (sid == NULL) {
1413 return create_error("Missing session parameter.");
1414 }
1415
1416 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1417 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1418 return NULL;
1419 }
1420
1421 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1422 if (locked_session != NULL) {
1423 pthread_mutex_lock(&locked_session->lock);
1424 if (pthread_rwlock_unlock(&session_lock) != 0) {
1425 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1426 }
1427 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1428 temp_session = nc_session_connect_channel(locked_session->session, NULL);
1429 if (temp_session != NULL) {
1430 rpc = nc_rpc_subscribe(NULL /* stream */, NULL /* filter */, &start, &stop);
1431 if (rpc == NULL) {
Tomas Cejkac7929632013-10-24 19:25:15 +02001432 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001433 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "notifications: creating an rpc request failed.");
1434 return create_error("notifications: creating an rpc request failed.");
1435 }
1436
1437 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Send NC subscribe.");
1438 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkac7929632013-10-24 19:25:15 +02001439 json_object *res = netconf_unlocked_op(server, temp_session, rpc);
1440 if (res != NULL) {
1441 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001442 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Subscription RPC failed.");
Tomas Cejkac7929632013-10-24 19:25:15 +02001443 return res;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001444 }
1445 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
1446
1447 pthread_mutex_unlock(&locked_session->lock);
1448 pthread_mutex_lock(&ntf_history_lock);
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001449 json_object *notif_history_array = json_object_new_array();
1450 if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
1451 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notif_history: cannot set thread-specific hash value.");
1452 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001453
1454 ncntf_dispatch_receive(temp_session, notification_history);
1455
1456 reply = json_object_new_object();
1457 json_object_object_add(reply, "notifications", notif_history_array);
1458 //json_object_put(notif_history_array);
1459
1460 pthread_mutex_unlock(&ntf_history_lock);
1461 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaaecc5f72013-10-01 00:03:50 +02001462 nc_session_free(temp_session);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001463 } else {
Tomas Cejkac7929632013-10-24 19:25:15 +02001464 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejka6b886e02013-07-05 09:53:17 +02001465 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get history of notification failed due to channel establishment");
1466 reply = create_error("Get history of notification was unsuccessful, connection failed.");
1467 }
Tomas Cejka6b886e02013-07-05 09:53:17 +02001468 } else {
1469 if (pthread_rwlock_unlock(&session_lock) != 0) {
1470 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1471 }
1472 reply = create_error("Invalid session identifier.");
1473 }
1474
Tomas Cejka4003a702013-10-01 00:02:45 +02001475 return reply;
1476}
1477
1478json_object *handle_op_validate(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
1479{
1480 json_object *reply = NULL;
1481 const char *sid = NULL;
1482 const char *target = NULL;
1483 const char *url = NULL;
1484 struct session_with_mutex *locked_session = NULL;
1485 nc_rpc *rpc = NULL;
1486 NC_DATASTORE target_ds;
1487
1488 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: validate datastore, session %s", session_key);
1489
1490 sid = json_object_get_string(json_object_object_get(request, "session"));
1491 target = json_object_get_string(json_object_object_get(request, "target"));
1492 url = json_object_get_string(json_object_object_get(request, "url"));
1493
1494
1495 if ((sid == NULL) || (target == NULL)) {
1496 return create_error("Missing session parameter.");
Tomas Cejka6b886e02013-07-05 09:53:17 +02001497 }
Tomas Cejka4003a702013-10-01 00:02:45 +02001498
1499 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1500 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1501 return NULL;
1502 }
1503
1504 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1505 if (locked_session != NULL) {
1506 pthread_mutex_lock(&locked_session->lock);
1507 if (pthread_rwlock_unlock(&session_lock) != 0) {
1508 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1509 }
1510 /* validation */
1511 target_ds = parse_datastore(target);
1512 if (target_ds == NC_DATASTORE_URL) {
1513 if (url != NULL) {
1514 rpc = nc_rpc_validate(target_ds, url);
1515 }
1516 } else if ((target_ds == NC_DATASTORE_RUNNING) || (target_ds == NC_DATASTORE_STARTUP)
1517 || (target_ds == NC_DATASTORE_CANDIDATE)) {
1518 rpc = nc_rpc_validate(target_ds);
1519 }
1520 if (rpc == NULL) {
1521 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
1522 reply = create_error("Creation of RPC request failed.");
1523 pthread_mutex_unlock(&locked_session->lock);
1524 return reply;
1525 }
1526
Tomas Cejkac7929632013-10-24 19:25:15 +02001527 if ((reply = netconf_op(server, session_key, rpc, NULL)) == NULL) {
Tomas Cejka4003a702013-10-01 00:02:45 +02001528 reply = json_object_new_object();
1529 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka4003a702013-10-01 00:02:45 +02001530 }
1531 nc_rpc_free (rpc);
1532
1533 pthread_mutex_unlock(&locked_session->lock);
1534 } else {
1535 if (pthread_rwlock_unlock(&session_lock) != 0) {
1536 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1537 }
1538 reply = create_error("Invalid session identifier.");
1539 }
1540
Tomas Cejka6b886e02013-07-05 09:53:17 +02001541 return reply;
1542}
1543
David Kupka8e60a372012-09-04 09:15:20 +02001544void * thread_routine (void * arg)
1545{
1546 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001547 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001548 json_object *request = NULL;
1549 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001550 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001551 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001552 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001553 const char *session_key;
1554 const char *target = NULL;
1555 const char *source = NULL;
Tomas Cejkac7929632013-10-24 19:25:15 +02001556 const char *url = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001557 NC_DATASTORE ds_type_t = -1;
1558 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001559 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001560 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
David Kupka8e60a372012-09-04 09:15:20 +02001561 server_rec * server = ((struct pass_to_thread*)arg)->server;
1562 int client = ((struct pass_to_thread*)arg)->client;
1563
Tomas Cejka00635972013-06-03 15:10:52 +02001564 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001565
1566 while (!isterminated) {
1567 fds.fd = client;
1568 fds.events = POLLIN;
1569 fds.revents = 0;
1570
1571 status = poll(&fds, 1, 1000);
1572
1573 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1574 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1575 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1576 continue;
1577 } else if (status < 0) {
1578 /* 0: poll time outed
1579 * close socket and ignore this request from the client, it can try it again
1580 * -1: poll failed
1581 * something wrong happend, close this socket and wait for another request
1582 */
1583 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1584 close(client);
1585 break;
1586 }
1587 /* status > 0 */
1588
1589 /* check the status of the socket */
1590
1591 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1592 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1593 /* close client's socket (it's probably already closed by client */
1594 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1595 close(client);
1596 break;
1597 }
1598
Tomas Cejka47387fd2013-06-10 20:37:46 +02001599 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message...");
Tomas Cejka64b87482013-06-03 16:30:53 +02001600 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001601
Tomas Cejka47387fd2013-06-10 20:37:46 +02001602 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001603 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001604 enum json_tokener_error jerr;
1605 request = json_tokener_parse_verbose(buffer, &jerr);
1606 if (jerr != json_tokener_success) {
1607 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1608 continue;
1609 }
David Kupka8e60a372012-09-04 09:15:20 +02001610 operation = json_object_get_int(json_object_object_get(request, "type"));
1611
Tomas Cejka64b87482013-06-03 16:30:53 +02001612 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001613 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001614 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1615 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001616 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001617 msgtext = json_object_to_json_string(reply);
1618 send(client, msgtext, strlen(msgtext) + 1, 0);
1619 json_object_put(reply);
1620 /* there is some stupid client, so close the connection to give a chance to some other client */
1621 close(client);
1622 break;
1623 }
1624
David Kupka8e60a372012-09-04 09:15:20 +02001625 /* null global JSON error-reply */
1626 err_reply = NULL;
1627
1628 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001629 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001630
1631 /* process required operation */
1632 switch (operation) {
1633 case MSG_CONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001634 reply = handle_op_connect(server, pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001635 break;
1636 case MSG_GET:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001637 reply = handle_op_get(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001638 break;
1639 case MSG_GETCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001640 reply = handle_op_getconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001641 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001642 case MSG_GETSCHEMA:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001643 reply = handle_op_getschema(server, pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001644 break;
David Kupka8e60a372012-09-04 09:15:20 +02001645 case MSG_EDITCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001646 reply = handle_op_editconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001647 break;
1648 case MSG_COPYCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001649 reply = handle_op_copyconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001650 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001651
David Kupka8e60a372012-09-04 09:15:20 +02001652 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001653 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001654 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001655 /* get parameters */
1656 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1657 ds_type_t = parse_datastore(target);
1658 }
1659 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1660 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001661 }
1662
1663 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001664 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001665 break;
1666 }
David Kupka8e60a372012-09-04 09:15:20 +02001667 switch(operation) {
1668 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001669 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001670 url = json_object_get_string(json_object_object_get(request, "url"));
1671 reply = netconf_deleteconfig(server, session_key, ds_type_t, url);
David Kupka8e60a372012-09-04 09:15:20 +02001672 break;
1673 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001674 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001675 reply = netconf_lock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001676 break;
1677 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001678 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
Tomas Cejkac7929632013-10-24 19:25:15 +02001679 reply = netconf_unlock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001680 break;
1681 default:
Tomas Cejkac7929632013-10-24 19:25:15 +02001682 reply = create_error("Internal: Unknown request type.");
David Kupka8e60a372012-09-04 09:15:20 +02001683 break;
1684 }
1685
Tomas Cejkac7929632013-10-24 19:25:15 +02001686 if (reply == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001687 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001688 /** \todo more clever error message wanted */
Tomas Cejkac7929632013-10-24 19:25:15 +02001689 reply = json_object_new_object();
1690 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
David Kupka8e60a372012-09-04 09:15:20 +02001691 } else {
1692 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001693 reply = err_reply;
1694 }
David Kupka8e60a372012-09-04 09:15:20 +02001695 }
1696 break;
1697 case MSG_KILL:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001698 reply = handle_op_kill(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001699 break;
1700 case MSG_DISCONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001701 reply = handle_op_disconnect(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001702 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001703 case MSG_RELOADHELLO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001704 reply = handle_op_reloadhello(server, pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001705 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001706 case MSG_INFO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001707 reply = handle_op_info(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001708 break;
1709 case MSG_GENERIC:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001710 reply = handle_op_generic(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001711 break;
Tomas Cejka6b886e02013-07-05 09:53:17 +02001712 case MSG_NTF_GETHISTORY:
1713 reply = handle_op_ntfgethistory(server, pool, request, session_key);
1714 break;
Tomas Cejka4003a702013-10-01 00:02:45 +02001715 case MSG_VALIDATE:
1716 reply = handle_op_validate(server, pool, request, session_key);
1717 break;
David Kupka8e60a372012-09-04 09:15:20 +02001718 default:
1719 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001720 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001721 break;
1722 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001723 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001724 json_object_put(request);
1725
Tomas Cejkad5b53772013-06-08 23:01:07 +02001726 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001727 /* send reply to caller */
1728 if (reply != NULL) {
1729 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001730 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001731 if (buffer != NULL) {
1732 free(buffer);
1733 buffer = NULL;
1734 }
David Kupka8e60a372012-09-04 09:15:20 +02001735 break;
1736 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001737 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001738 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001739 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001740 json_object_put(reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001741 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001742 free(chunked_out_msg);
1743 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001744 if (buffer != NULL) {
1745 free(buffer);
1746 buffer = NULL;
1747 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001748 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001749 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001750 break;
David Kupka8e60a372012-09-04 09:15:20 +02001751 }
1752 }
1753 }
David Kupka8e60a372012-09-04 09:15:20 +02001754 free (arg);
1755
1756 return retval;
1757}
1758
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001759/**
1760 * \brief Close all open NETCONF sessions.
1761 *
1762 * During termination of mod_netconf, it is useful to close all remaining
1763 * sessions. This function iterates over the list of sessions and close them
1764 * all.
1765 *
1766 * \param[in] server pointer to server_rec for logging
1767 * \param[in] p apr pool needed for hash table iterating
1768 * \param[in] ht hash table of session_with_mutex structs
1769 */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001770static void close_all_nc_sessions(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001771{
1772 apr_hash_index_t *hi;
1773 void *val = NULL;
1774 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001775 const char *hashed_key = NULL;
1776 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001777 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001778
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001779 /* get exclusive access to sessions_list (conns) */
1780 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1781 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1782 return;
1783 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001784 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001785 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1786 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1787 swm = (struct session_with_mutex *) val;
1788 if (swm != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001789 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001790 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001791
1792 /* close_and_free_session handles locking on its own */
1793 close_and_free_session(server, swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001794 }
1795 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001796 /* get exclusive access to sessions_list (conns) */
1797 if (pthread_rwlock_unlock (&session_lock) != 0) {
1798 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1799 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001800}
1801
Tomas Cejka47387fd2013-06-10 20:37:46 +02001802static void check_timeout_and_close(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001803{
1804 apr_hash_index_t *hi;
1805 void *val = NULL;
1806 struct nc_session *ns = NULL;
1807 struct session_with_mutex *swm = NULL;
1808 const char *hashed_key = NULL;
1809 apr_ssize_t hashed_key_length;
1810 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001811 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001812
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001813 /* get exclusive access to sessions_list (conns) */
1814 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1815 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1816 return;
1817 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001818 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001819 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1820 swm = (struct session_with_mutex *) val;
1821 if (swm == NULL) {
1822 continue;
1823 }
1824 ns = swm->session;
1825 if (ns == NULL) {
1826 continue;
1827 }
1828 pthread_mutex_lock(&swm->lock);
1829 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1830 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001831 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001832 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
1833 pthread_mutex_unlock(&swm->lock);
1834
1835 /* close_and_free_session handles locking on its own */
1836 close_and_free_session(server, swm);
1837 } else {
1838 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001839 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001840 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001841 /* get exclusive access to sessions_list (conns) */
1842 if (pthread_rwlock_unlock (&session_lock) != 0) {
1843 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1844 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001845}
1846
1847
1848/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001849 * This is actually implementation of NETCONF client
1850 * - requests are received from UNIX socket in the predefined format
1851 * - results are replied through the same way
1852 * - the daemon run as a separate process, but it is started and stopped
1853 * automatically by Apache.
1854 *
1855 */
Radek Krejci469aab82012-07-22 18:42:20 +02001856static void forked_proc(apr_pool_t * pool, server_rec * server)
1857{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001858 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001859 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001860 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001861 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001862 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001863 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001864 struct pass_to_thread * arg;
1865 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1866 struct timespec maxtime;
1867 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001868 #ifdef WITH_NOTIFICATIONS
1869 char use_notifications = 0;
1870 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001871
Tomas Cejka6b886e02013-07-05 09:53:17 +02001872 http_server = server;
1873
Tomas Cejka404d37e2013-04-13 02:31:35 +02001874 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001875 maxtime.tv_sec = 5;
1876 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001877
Radek Krejcif23850c2012-07-23 16:14:17 +02001878 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001879 unixd_setup_child();
1880
Radek Krejciae021c12012-07-25 18:03:52 +02001881 cfg = ap_get_module_config(server->module_config, &netconf_module);
1882 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001883 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1884 return;
1885 }
Radek Krejci469aab82012-07-22 18:42:20 +02001886
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001887 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001888 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1889 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1890 return;
1891 }
1892
1893 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001894 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001895 unlink(local.sun_path);
1896 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1897
1898 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1899 if (errno == EADDRINUSE) {
1900 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1901 close(lsock);
1902 exit(0);
1903 }
1904 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1905 close(lsock);
1906 return;
1907 }
1908
1909 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1910 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1911 close(lsock);
1912 return;
1913 }
1914
Tomas Cejkaba21b382013-04-13 02:37:32 +02001915 /* prepare internal lists */
1916 netconf_sessions_list = apr_hash_make(pool);
1917
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001918 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02001919 if (notification_init(pool, server) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001920 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1921 use_notifications = 0;
1922 } else {
1923 use_notifications = 1;
1924 }
1925 #endif
1926
Radek Krejci469aab82012-07-22 18:42:20 +02001927 /* setup libnetconf's callbacks */
1928 nc_verbosity(NC_VERB_DEBUG);
1929 clb_print_server = server;
1930 nc_callback_print(clb_print);
1931 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1932 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1933 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001934 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001935
1936 /* disable publickey authentication */
1937 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1938
David Kupka8e60a372012-09-04 09:15:20 +02001939 /* create mutex protecting session list */
1940 pthread_rwlockattr_init(&lock_attrs);
1941 /* rwlock is shared only with threads in this process */
1942 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1943 /* create rw lock */
1944 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1945 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1946 close (lsock);
1947 return;
1948 }
Tomas Cejkad016f9c2013-07-10 09:16:16 +02001949 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "init of notif_history_key.");
1950 if (pthread_key_create(&notif_history_key, NULL) != 0) {
1951 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "init of notif_history_key failed");
1952 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001953
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001954 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001955 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001956 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001957 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001958 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001959 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001960 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1961 }
1962 if (use_notifications == 1) {
1963 notification_handle();
1964 }
1965 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001966 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001967 check_timeout_and_close(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001968 }
Radek Krejci469aab82012-07-22 18:42:20 +02001969
1970 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001971 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001972 if (((unsigned int)tv.tv_sec - olds) > 60) {
1973 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1974 olds = tv.tv_sec;
1975 }
David Kupka8e60a372012-09-04 09:15:20 +02001976 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001977 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1978 apr_sleep(SLEEP_TIME);
1979 continue;
1980 } else if (client == -1 && (errno == EINTR)) {
1981 continue;
1982 } else if (client == -1) {
1983 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1984 continue;
1985 }
Radek Krejci469aab82012-07-22 18:42:20 +02001986
1987 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001988 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001989
David Kupka8e60a372012-09-04 09:15:20 +02001990 arg = malloc (sizeof(struct pass_to_thread));
1991 arg->client = client;
1992 arg->pool = pool;
1993 arg->server = server;
1994 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001995
David Kupka8e60a372012-09-04 09:15:20 +02001996 /* start new thread. It will serve this particular request and then terminate */
1997 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1998 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1999 } else {
2000 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
2001 pthread_count++;
2002 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
2003 ptids[pthread_count] = 0;
2004 }
Radek Krejci469aab82012-07-22 18:42:20 +02002005
David Kupka8e60a372012-09-04 09:15:20 +02002006 /* check if some thread already terminated, free some resources by joining it */
2007 for (i=0; i<pthread_count; i++) {
2008 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
2009 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
2010 pthread_count--;
2011 if (pthread_count > 0) {
2012 /* place last Thread ID on the place of joined one */
2013 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002014 }
Radek Krejci469aab82012-07-22 18:42:20 +02002015 }
2016 }
David Kupka8e60a372012-09-04 09:15:20 +02002017 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02002018 }
2019
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002020 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02002021 /* join all threads */
2022 for (i=0; i<pthread_count; i++) {
2023 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
2024 }
2025 free (ptids);
2026
Radek Krejci469aab82012-07-22 18:42:20 +02002027 close(lsock);
2028
Tomas Cejkad340dbf2013-03-24 20:36:57 +01002029 #ifdef WITH_NOTIFICATIONS
2030 notification_close();
2031 #endif
2032
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002033 /* close all NETCONF sessions */
Tomas Cejka47387fd2013-06-10 20:37:46 +02002034 close_all_nc_sessions(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02002035
David Kupka8e60a372012-09-04 09:15:20 +02002036 /* destroy rwlock */
2037 pthread_rwlock_destroy(&session_lock);
2038 pthread_rwlockattr_destroy(&lock_attrs);
2039
Radek Krejci469aab82012-07-22 18:42:20 +02002040 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
2041
2042 exit(APR_SUCCESS);
2043}
2044
Radek Krejcif23850c2012-07-23 16:14:17 +02002045static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02002046{
Radek Krejcif23850c2012-07-23 16:14:17 +02002047 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
2048
2049 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
2050 apr_pool_create(&config->pool, pool);
2051 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002052 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02002053
2054 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02002055}
2056
2057static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
2058 apr_pool_t * plog, server_rec * s)
2059{
Radek Krejcif23850c2012-07-23 16:14:17 +02002060 mod_netconf_cfg *config;
2061 apr_status_t res;
2062
Radek Krejci469aab82012-07-22 18:42:20 +02002063 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01002064 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02002065 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02002066
2067 /*
2068 * The following checks if this routine has been called before.
2069 * This is necessary because the parent process gets initialized
2070 * a couple of times as the server starts up.
2071 */
2072 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
2073 if (!data) {
2074 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
2075 return (OK);
2076 }
2077
Radek Krejcif23850c2012-07-23 16:14:17 +02002078 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
2079 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02002080
Radek Krejcif23850c2012-07-23 16:14:17 +02002081 if (config && config->forkproc == NULL) {
2082 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
2083 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002084 switch (res) {
2085 case APR_INCHILD:
2086 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02002087 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02002088 apr_signal(SIGTERM, signal_handler);
2089
2090 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02002091 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02002092
2093 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02002094 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02002095
Radek Krejcif23850c2012-07-23 16:14:17 +02002096 /* I never should be here, wtf?!? */
2097 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02002098 exit(APR_EGENERAL);
2099 break;
2100 case APR_INPARENT:
2101 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02002102 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02002103 break;
2104 default:
2105 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
2106 break;
2107 }
Radek Krejcif23850c2012-07-23 16:14:17 +02002108 } else {
2109 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02002110 }
2111
2112 return OK;
2113}
2114
Radek Krejci469aab82012-07-22 18:42:20 +02002115/**
2116 * Register module hooks
2117 */
2118static void mod_netconf_register_hooks(apr_pool_t * p)
2119{
Radek Krejcif23850c2012-07-23 16:14:17 +02002120 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02002121}
2122
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002123static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2124{
2125 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2126 return NULL;
2127}
2128
2129static const command_rec netconf_cmds[] = {
2130 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2131 {NULL}
2132};
2133
Radek Krejci469aab82012-07-22 18:42:20 +02002134/* Dispatch list for API hooks */
2135module AP_MODULE_DECLARE_DATA netconf_module = {
2136 STANDARD20_MODULE_STUFF,
2137 NULL, /* create per-dir config structures */
2138 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002139 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002140 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002141 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002142 mod_netconf_register_hooks /* register hooks */
2143};
Radek Krejcia332b692012-11-12 16:15:54 +01002144