blob: 1bc0390684bf47c6929e79065711a5898cc71637 [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
David Kupka8e60a372012-09-04 09:15:20 +0200109pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
110
Radek Krejci469aab82012-07-22 18:42:20 +0200111volatile int isterminated = 0;
112
113static char* password;
114
Radek Krejci469aab82012-07-22 18:42:20 +0200115static void signal_handler(int sign)
116{
117 switch (sign) {
118 case SIGTERM:
119 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200120 break;
121 }
122}
123
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200124static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200125{
Radek Krejcif23850c2012-07-23 16:14:17 +0200126 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
127 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200128 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200129
Radek Krejci469aab82012-07-22 18:42:20 +0200130 apr_sha1_ctx_t sha1_ctx;
131 apr_sha1_init(&sha1_ctx);
132 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
133 apr_sha1_update(&sha1_ctx, port, strlen(port));
134 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200135 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200136
Radek Krejcif23850c2012-07-23 16:14:17 +0200137 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200138 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200139 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200140 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200141 }
142 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200143
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200144 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200145}
146
147int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
148{
149 /* always approve */
150 return (EXIT_SUCCESS);
151}
152
153char* netconf_callback_sshauth_password (const char* username, const char* hostname)
154{
155 char* buf;
156
157 buf = malloc ((strlen(password) + 1) * sizeof(char));
158 apr_cpystrn(buf, password, strlen(password) + 1);
159
160 return (buf);
161}
162
163void netconf_callback_sshauth_interactive (const char* name,
164 int name_len,
165 const char* instruction,
166 int instruction_len,
167 int num_prompts,
168 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
169 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
170 void** abstract)
171{
172 int i;
173
174 for (i=0; i<num_prompts; i++) {
175 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
176 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
177 responses[i].length = strlen(responses[i].text) + 1;
178 }
179
180 return;
181}
182
Radek Krejcic11fd862012-07-26 12:41:21 +0200183static json_object *err_reply = NULL;
184void netconf_callback_error_process(const char* tag,
185 const char* type,
186 const char* severity,
187 const char* apptag,
188 const char* path,
189 const char* message,
190 const char* attribute,
191 const char* element,
192 const char* ns,
193 const char* sid)
194{
195 err_reply = json_object_new_object();
196 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
197 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
198 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
199 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
200 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
201 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
202 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
203 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
204 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
205 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
206 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
207}
208
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200209void prepare_status_message(server_rec* server, struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200210{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200211 json_object *json_obj = NULL;
Tomas Cejka73286932013-05-27 22:54:35 +0200212 //json_object *old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200213 const char *cpbltstr;
214 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200215
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200216 if (s == NULL) {
217 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "No session given.");
218 return;
219 }
220
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200221 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200222 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200223 //json_object_put(s->hello_message);
224 s->hello_message = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200225
226 //old_sid = json_object_object_get(s->hello_message, "sid");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200227 }
228 s->hello_message = json_object_new_object();
229 if (session != NULL) {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200230 /** \todo reload hello - save old sid */
231 //if (old_sid != NULL) {
232 // /* use previous sid */
233 // json_object_object_add(s->hello_message, "sid", old_sid);
234 //} else {
235 /* we don't have old sid */
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200236 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200237 //}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200238 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
239 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
240 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
241 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
242 cpblts = nc_session_get_cpblts (session);
243 if (cpblts != NULL) {
244 json_obj = json_object_new_array();
245 nc_cpblts_iter_start (cpblts);
246 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
247 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
248 }
249 json_object_object_add(s->hello_message, "capabilities", json_obj);
250 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200251 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 +0200252 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200253 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200254 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
255 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
256 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200257 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200258
259}
260
261
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200262/**
263 * \defgroup netconf_operations NETCONF operations
264 * The list of NETCONF operations that mod_netconf supports.
265 * @{
266 */
267
268/**
269 * \brief Connect to NETCONF server
270 *
271 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
272 */
Kupka David00b9c5c2012-09-05 09:45:50 +0200273static char* netconf_connect(server_rec* server, apr_pool_t* pool, apr_hash_t* conns, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200274{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200275 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200276 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200277 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200278
279 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200280 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200281 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
282 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200283 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200284 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200285
Radek Krejci469aab82012-07-22 18:42:20 +0200286 /* if connected successful, add session to the list */
287 if (session != NULL) {
288 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200289 session_key = gen_ncsession_hash(
290 (host==NULL) ? "localhost" : host,
291 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200292 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200293
Tomas Cejkaba21b382013-04-13 02:37:32 +0200294 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200295 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
296 nc_session_free(session);
297 free (locked_session);
298 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
299 return NULL;
300 }
301 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200302 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200303 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200304 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200305 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100306 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200307 /* get exclusive access to sessions_list (conns) */
308 if (pthread_rwlock_wrlock (&session_lock) != 0) {
309 nc_session_free(session);
310 free (locked_session);
311 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
312 return NULL;
313 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200314 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200315 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100316 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200317 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100318 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200319
320 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200321 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200322
David Kupka8e60a372012-09-04 09:15:20 +0200323 /* end of critical section */
324 if (pthread_rwlock_unlock (&session_lock) != 0) {
325 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
326 return NULL;
327 }
Radek Krejci469aab82012-07-22 18:42:20 +0200328 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200329 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200330 } else {
331 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200332 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200333 }
334
Radek Krejci469aab82012-07-22 18:42:20 +0200335}
336
Radek Krejci80c10d92012-07-30 08:38:50 +0200337static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200338{
339 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200340 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200341
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200342 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to close: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200343 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200344 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200345 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200346 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 +0200347 return EXIT_FAILURE;
348 }
349 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
350 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200351 /** \todo free all notifications from queue */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200352 ns = locked_session->session;
353 }
354 if (ns != NULL) {
355 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock private lock.");
356 if (pthread_mutex_lock(&locked_session->lock) != 0) {
357 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
358 return EXIT_FAILURE;
359 }
360 locked_session->ntfc_subscribed = 0;
361 locked_session->closed = 1;
362 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
363 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "session closed.");
364 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock private lock.");
365 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
366 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
367 return EXIT_FAILURE;
368 }
369 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
370 if (pthread_rwlock_unlock (&session_lock) != 0) {
371 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
372 return EXIT_FAILURE;
373 }
374 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "closed session, disabled notif(?), wait 2s");
375 sleep(2); /* let notification thread stop */
376 /* remove session from the active sessions list */
377 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
378 /* end of critical section */
379
380 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
381 if (pthread_rwlock_wrlock (&session_lock) != 0) {
382 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
383 return EXIT_FAILURE;
384 }
385 /** \todo free all notifications from queue */
Tomas Cejkaba21b382013-04-13 02:37:32 +0200386 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200387 pthread_mutex_destroy(&locked_session->lock);
388 ns = locked_session->session;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200389 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200390 json_object_put(locked_session->hello_message);
391 locked_session->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200392 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200393 nc_session_free(ns);
Radek Krejci469aab82012-07-22 18:42:20 +0200394 ns = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200395 free (locked_session);
396 locked_session = NULL;
397 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200398 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200399 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200400 return EXIT_FAILURE;
401 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200402 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed, everything cleared.");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200403 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200404 } else {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200405 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200406 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200407 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200408 return EXIT_FAILURE;
409 }
Radek Krejci469aab82012-07-22 18:42:20 +0200410 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200411 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200412 }
413}
414
Radek Krejci80c10d92012-07-30 08:38:50 +0200415static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200416{
417 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200418 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200419 nc_reply* reply = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200420 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100421 NC_MSG_TYPE msgt;
422 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200423
Radek Krejci8e4632a2012-07-26 13:40:34 +0200424 /* check requests */
425 if (rpc == NULL) {
426 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
427 return (EXIT_FAILURE);
428 }
429
David Kupka8e60a372012-09-04 09:15:20 +0200430 /* get non-exclusive (read) access to sessions_list (conns) */
431 if (pthread_rwlock_rdlock (&session_lock) != 0) {
432 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
433 return EXIT_FAILURE;
434 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200435 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200436 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
437 if (locked_session != NULL) {
438 session = locked_session->session;
439 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200440 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200441 /* get exclusive access to session */
442 if (pthread_mutex_lock(&locked_session->lock) != 0) {
443 /* unlock before returning error */
444 if (pthread_rwlock_unlock (&session_lock) != 0) {
445 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
446 return EXIT_FAILURE;
447 }
448 return EXIT_FAILURE;
449 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200450 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200451 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100452 msgt = nc_session_send_recv(session, rpc, &reply);
453
David Kupka8e60a372012-09-04 09:15:20 +0200454 /* first release exclusive lock for this session */
455 pthread_mutex_unlock(&locked_session->lock);
456 /* end of critical section */
457 if (pthread_rwlock_unlock (&session_lock) != 0) {
458 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
459 return EXIT_FAILURE;
460 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200461
Radek Krejcia332b692012-11-12 16:15:54 +0100462 /* process the result of the operation */
463 switch (msgt) {
464 case NC_MSG_UNKNOWN:
465 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
466 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
467 netconf_close(server, conns, session_key);
468 return (EXIT_FAILURE);
469 }
470 /* no break */
471 case NC_MSG_NONE:
472 /* there is error handled by callback */
473 return (EXIT_FAILURE);
474 break;
475 case NC_MSG_REPLY:
476 switch (replyt = nc_reply_get_type(reply)) {
477 case NC_REPLY_OK:
478 retval = EXIT_SUCCESS;
479 break;
480 default:
481 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
482 retval = EXIT_FAILURE;
483 break;
484 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200485 break;
486 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100487 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200488 retval = EXIT_FAILURE;
489 break;
490 }
491 nc_reply_free(reply);
492 return (retval);
493 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100494 /* release lock on failure */
495 if (pthread_rwlock_unlock (&session_lock) != 0) {
496 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
497 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200498 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
499 return (EXIT_FAILURE);
500 }
501}
Radek Krejci80c10d92012-07-30 08:38:50 +0200502
503static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200504{
505 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200506 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100507 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200508 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100509 NC_MSG_TYPE msgt;
510 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200511
512 /* check requests */
513 if (rpc == NULL) {
514 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
515 return (NULL);
516 }
517
David Kupka8e60a372012-09-04 09:15:20 +0200518 /* get non-exclusive (read) access to sessions_list (conns) */
519 if (pthread_rwlock_rdlock (&session_lock) != 0) {
520 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
521 return NULL;
522 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200523 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200524 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
525 if (locked_session != NULL) {
526 session = locked_session->session;
527 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200528 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200529 /* get exclusive access to session */
530 if (pthread_mutex_lock(&locked_session->lock) != 0) {
531 /* unlock before returning error */
532 if (pthread_rwlock_unlock (&session_lock) != 0) {
533 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
534 return NULL;
535 }
536 return NULL;
537 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200538 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200539 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100540 msgt = nc_session_send_recv(session, rpc, &reply);
541
David Kupka8e60a372012-09-04 09:15:20 +0200542 /* first release exclusive lock for this session */
543 pthread_mutex_unlock(&locked_session->lock);
544 /* end of critical section */
545 if (pthread_rwlock_unlock (&session_lock) != 0) {
546 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Radek Krejcia332b692012-11-12 16:15:54 +0100547 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200548 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200549
Radek Krejcia332b692012-11-12 16:15:54 +0100550 /* process the result of the operation */
551 switch (msgt) {
552 case NC_MSG_UNKNOWN:
553 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
554 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
555 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200556 return (NULL);
557 }
Radek Krejcia332b692012-11-12 16:15:54 +0100558 /* no break */
559 case NC_MSG_NONE:
560 /* there is error handled by callback */
561 return (NULL);
562 break;
563 case NC_MSG_REPLY:
564 switch (replyt = nc_reply_get_type(reply)) {
565 case NC_REPLY_DATA:
566 if ((data = nc_reply_get_data (reply)) == NULL) {
567 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
568 data = NULL;
569 }
570 break;
571 default:
572 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
573 data = NULL;
574 break;
575 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200576 break;
577 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100578 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
579 data = NULL;
580 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200581 }
582 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200583 return (data);
584 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100585 /* release lock on failure */
586 if (pthread_rwlock_unlock (&session_lock) != 0) {
587 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
588 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200589 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
590 return (NULL);
591 }
592}
593
Radek Krejci80c10d92012-07-30 08:38:50 +0200594static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200595{
596 nc_rpc* rpc;
597 struct nc_filter *f = NULL;
598 char* data = NULL;
599
600 /* create filter if set */
601 if (filter != NULL) {
602 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
603 }
604
605 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100606 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200607 nc_filter_free(f);
608 if (rpc == NULL) {
609 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
610 return (NULL);
611 }
612
613 data = netconf_opdata(server, conns, session_key, rpc);
614 nc_rpc_free (rpc);
615 return (data);
616}
617
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100618static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
619{
620 nc_rpc* rpc;
621 char* data = NULL;
622
623 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100624 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100625 if (rpc == NULL) {
626 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
627 return (NULL);
628 }
629
630 data = netconf_opdata(server, conns, session_key, rpc);
631 nc_rpc_free (rpc);
632 return (data);
633}
634
Radek Krejci80c10d92012-07-30 08:38:50 +0200635static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200636{
637 nc_rpc* rpc;
638 struct nc_filter *f = NULL;
639 char* data = NULL;
640
641 /* create filter if set */
642 if (filter != NULL) {
643 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
644 }
645
646 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100647 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200648 nc_filter_free(f);
649 if (rpc == NULL) {
650 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
651 return (NULL);
652 }
653
654 data = netconf_opdata(server, conns, session_key, rpc);
655 nc_rpc_free (rpc);
656 return (data);
657}
658
Tomas Cejka5064c232013-01-17 09:30:58 +0100659static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *url)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200660{
661 nc_rpc* rpc;
662 int retval = EXIT_SUCCESS;
663
664 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100665 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
666 if (target == NC_DATASTORE_URL) {
667 rpc = nc_rpc_copyconfig(source, target, config, url);
668 } else {
669 rpc = nc_rpc_copyconfig(source, target, config);
670 }
671 } else {
672 if (target == NC_DATASTORE_URL) {
673 rpc = nc_rpc_copyconfig(source, target, url);
674 } else {
675 rpc = nc_rpc_copyconfig(source, target);
676 }
677 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200678 if (rpc == NULL) {
679 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
680 return (EXIT_FAILURE);
681 }
682
683 retval = netconf_op(server, conns, session_key, rpc);
684 nc_rpc_free (rpc);
685 return (retval);
686}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200687
Tomas Cejka5064c232013-01-17 09:30:58 +0100688static int netconf_editconfig(server_rec* server, apr_hash_t* conns, 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 +0200689{
690 nc_rpc* rpc;
691 int retval = EXIT_SUCCESS;
692
693 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100694 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
695 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200696 if (rpc == NULL) {
697 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
698 return (EXIT_FAILURE);
699 }
700
701 retval = netconf_op(server, conns, session_key, rpc);
702 nc_rpc_free (rpc);
703 return (retval);
704}
705
Radek Krejci80c10d92012-07-30 08:38:50 +0200706static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200707{
708 nc_rpc* rpc;
709 int retval = EXIT_SUCCESS;
710
711 /* create requests */
712 rpc = nc_rpc_killsession(sid);
713 if (rpc == NULL) {
714 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
715 return (EXIT_FAILURE);
716 }
717
718 retval = netconf_op(server, conns, session_key, rpc);
719 nc_rpc_free (rpc);
720 return (retval);
721}
722
Radek Krejci80c10d92012-07-30 08:38:50 +0200723static int netconf_onlytargetop(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200724{
725 nc_rpc* rpc;
726 int retval = EXIT_SUCCESS;
727
728 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200729 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200730 if (rpc == NULL) {
731 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
732 return (EXIT_FAILURE);
733 }
734
735 retval = netconf_op(server, conns, session_key, rpc);
736 nc_rpc_free (rpc);
737 return (retval);
738}
739
Radek Krejci80c10d92012-07-30 08:38:50 +0200740static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200741{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200742 nc_rpc *rpc = NULL;
743 if (target != NC_DATASTORE_URL) {
744 rpc = nc_rpc_deleteconfig(target);
745 } else {
746 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
747 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
748 return (EXIT_FAILURE);
749 }
750
751 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200752}
753
Radek Krejci80c10d92012-07-30 08:38:50 +0200754static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200755{
756 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
757}
758
Radek Krejci80c10d92012-07-30 08:38:50 +0200759static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200760{
761 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
762}
763
Radek Krejci80c10d92012-07-30 08:38:50 +0200764/**
765 * @return REPLY_OK: 0, *data == NULL
766 * REPLY_DATA: 0, *data != NULL
767 * REPLY_ERROR: 1, *data == NULL
768 */
769static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
770{
771 struct nc_session *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200772 nc_reply* reply = NULL;
773 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200774 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100775 NC_MSG_TYPE msgt;
776 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200777
778 /* create requests */
779 rpc = nc_rpc_generic(content);
780 if (rpc == NULL) {
781 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
782 return (EXIT_FAILURE);
783 }
784
Radek Krejcia332b692012-11-12 16:15:54 +0100785 if (data != NULL) {
786 *data = NULL;
787 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200788
789 /* get session where send the RPC */
790 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
791 if (session != NULL) {
792 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100793 msgt = nc_session_send_recv(session, rpc, &reply);
794 nc_rpc_free (rpc);
795
796 /* process the result of the operation */
797 switch (msgt) {
798 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200799 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
800 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
801 netconf_close(server, conns, session_key);
802 return (EXIT_FAILURE);
803 }
Radek Krejcia332b692012-11-12 16:15:54 +0100804 /* no break */
805 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200806 /* there is error handled by callback */
807 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200808 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100809 case NC_MSG_REPLY:
810 switch (replyt = nc_reply_get_type(reply)) {
811 case NC_REPLY_DATA:
812 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
813 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
814 nc_reply_free(reply);
815 return (EXIT_FAILURE);
816 }
817 retval = EXIT_SUCCESS;
818 break;
819 case NC_REPLY_OK:
820 retval = EXIT_SUCCESS;
821 break;
822 default:
823 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
824 retval = EXIT_FAILURE;
825 break;
826 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200827 break;
828 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100829 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200830 retval = EXIT_FAILURE;
831 break;
832 }
833 nc_reply_free(reply);
834
835 return (retval);
836 } else {
837 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
838 return (EXIT_FAILURE);
839 }
840}
841
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200842/**
843 * @}
844 *//* netconf_operations */
845
Radek Krejci469aab82012-07-22 18:42:20 +0200846server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200847void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200848{
Radek Krejci7338bde2012-08-10 12:57:30 +0200849 switch (level) {
850 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200851 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200852 break;
853 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200854 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200855 break;
856 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200857 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200858 break;
859 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200860 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200861 break;
862 }
Radek Krejci469aab82012-07-22 18:42:20 +0200863}
864
Tomas Cejka64b87482013-06-03 16:30:53 +0200865/**
866 * Receive message from client and return pointer to it.
867 * Caller should free message memory.
868 * \param[in] client socket descriptor of client
869 * \param[in] server httpd server for logging
870 * \return pointer to message
871 */
872char *get_framed_message(server_rec *server, int client)
873{
874 /* read json in chunked framing */
875 unsigned int buffer_size = 0;
876 ssize_t buffer_len = 0;
877 char *buffer = NULL;
878 char c;
879 ssize_t ret;
880 int i, chunk_len;
881 char chunk_len_str[12];
882
883 while (1) {
884 /* read chunk length */
885 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
886 if (buffer != NULL) {
887 free (buffer);
888 buffer = NULL;
889 }
890 break;
891 }
892 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
893 if (buffer != NULL) {
894 free (buffer);
895 buffer = NULL;
896 }
897 break;
898 }
899 i=0;
900 memset (chunk_len_str, 0, 12);
901 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
902 if (i==0 && c == '#') {
903 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
904 /* end but invalid */
905 if (buffer != NULL) {
906 free (buffer);
907 buffer = NULL;
908 }
909 }
910 /* end of message, double-loop break */
911 goto msg_complete;
912 }
913 chunk_len_str[i++] = c;
914 if (i==11) {
915 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
916 break;
917 }
918 }
919 if (c != '\n') {
920 if (buffer != NULL) {
921 free (buffer);
922 buffer = NULL;
923 }
924 break;
925 }
926 chunk_len_str[i] = 0;
927 if ((chunk_len = atoi (chunk_len_str)) == 0) {
928 if (buffer != NULL) {
929 free (buffer);
930 buffer = NULL;
931 }
932 break;
933 }
934 buffer_size += chunk_len+1;
935 buffer = realloc (buffer, sizeof(char)*buffer_size);
936 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
937 if (buffer != NULL) {
938 free (buffer);
939 buffer = NULL;
940 }
941 break;
942 }
943 buffer_len += ret;
944 }
945msg_complete:
946 return buffer;
947}
948
Tomas Cejkad5b53772013-06-08 23:01:07 +0200949NC_DATASTORE parse_datastore(const char *ds)
950{
951 if (strcmp(ds, "running") == 0) {
952 return NC_DATASTORE_RUNNING;
953 } else if (strcmp(ds, "startup") == 0) {
954 return NC_DATASTORE_STARTUP;
955 } else if (strcmp(ds, "candidate") == 0) {
956 return NC_DATASTORE_CANDIDATE;
957 }
958 return -1;
959}
960
961json_object *create_error(const char *errmess)
962{
963 json_object *reply = json_object_new_object();
964 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
965 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
966 return reply;
967
968}
969
970json_object *create_data(const char *data)
971{
972 json_object *reply = json_object_new_object();
973 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
974 json_object_object_add(reply, "data", json_object_new_string(data));
975 return reply;
976}
977
978json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list)
979{
980 const char *host = NULL;
981 const char *port = NULL;
982 const char *user = NULL;
983 const char *pass = NULL;
984 json_object *capabilities = NULL;
985 json_object *reply = NULL;
986 char *session_key_hash = NULL;
987 struct nc_cpblts* cpblts = NULL;
988 unsigned int len, i;
989
990 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
991 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
992 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
993 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
994 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
995
996 capabilities = json_object_object_get((json_object *) request, "capabilities");
997 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
998 cpblts = nc_cpblts_new(NULL);
999 for (i=0; i<len; i++) {
1000 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1001 }
1002 } else {
1003 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1004 }
1005
1006 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1007 if ((host == NULL) || (user == NULL)) {
1008 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1009 session_key_hash = NULL;
1010 } else {
1011 session_key_hash = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
1012 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1013 }
1014 if (cpblts != NULL) {
1015 nc_cpblts_free(cpblts);
1016 }
1017
1018 if (session_key_hash == NULL) {
1019 /* negative reply */
1020 if (err_reply == NULL) {
1021 reply = json_object_new_object();
1022 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1023 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
1024 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
1025 } else {
1026 /* use filled err_reply from libnetconf's callback */
1027 reply = err_reply;
1028 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
1029 }
1030 } else {
1031 /* positive reply */
1032 reply = json_object_new_object();
1033 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1034 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1035
1036 free(session_key_hash);
1037 }
1038 return reply;
1039}
1040
1041json_object *handle_op_get(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1042{
1043 const char *filter = NULL;
1044 const char *data = NULL;
1045 json_object *reply = NULL;
1046
1047 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1048
1049 filter = json_object_get_string(json_object_object_get(request, "filter"));
1050
1051 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
1052 if (err_reply == NULL) {
1053 reply = create_error("Get 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
1064json_object *handle_op_getconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1065{
1066 NC_DATASTORE ds_type_s = -1;
1067 NC_DATASTORE ds_type_t = -1;
1068 const char *filter = NULL;
1069 const char *data = NULL;
1070 const char *source = NULL;
1071 const char *target = NULL;
1072 json_object *reply = NULL;
1073
1074 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1075
1076 filter = json_object_get_string(json_object_object_get(request, "filter"));
1077
1078 /* get parameters */
1079 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1080 ds_type_t = parse_datastore(target);
1081 }
1082 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1083 ds_type_s = parse_datastore(source);
1084 }
1085 if (ds_type_s == -1) {
1086 return create_error("Invalid source repository type requested.");
1087 }
1088
1089 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1090 if (err_reply == NULL) {
1091 reply = create_error("Get configuration information from device failed.");
1092 } else {
1093 /* use filled err_reply from libnetconf's callback */
1094 reply = err_reply;
1095 }
1096 } else {
1097 return create_data(data);
1098 }
1099 return reply;
1100}
1101
1102json_object *handle_op_getschema(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1103{
1104 const char *data = NULL;
1105 const char *identifier = NULL;
1106 const char *version = NULL;
1107 const char *format = NULL;
1108 json_object *reply = NULL;
1109
1110 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1111 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1112 if (identifier == NULL) {
1113 return create_error("No identifier for get-schema supplied.");
1114 }
1115 version = json_object_get_string(json_object_object_get(request, "version"));
1116 format = json_object_get_string(json_object_object_get(request, "format"));
1117
1118 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
1119 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
1120 if (err_reply == NULL) {
1121 reply = create_error("Get schema failed.");
1122 } else {
1123 /* use filled err_reply from libnetconf's callback */
1124 reply = err_reply;
1125 }
1126 } else {
1127 return create_data(data);
1128 }
1129 return reply;
1130}
1131
1132json_object *handle_op_editconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1133{
1134 NC_DATASTORE ds_type_s = -1;
1135 NC_DATASTORE ds_type_t = -1;
1136 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1137 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1138 const char *defop = NULL;
1139 const char *erropt = NULL;
1140 const char *config = NULL;
1141 const char *source = NULL;
1142 const char *target = NULL;
1143 json_object *reply = NULL;
1144
1145 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1146
1147 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1148 if (defop != NULL) {
1149 if (strcmp(defop, "merge") == 0) {
1150 defop_type = NC_EDIT_DEFOP_MERGE;
1151 } else if (strcmp(defop, "replace") == 0) {
1152 defop_type = NC_EDIT_DEFOP_REPLACE;
1153 } else if (strcmp(defop, "none") == 0) {
1154 defop_type = NC_EDIT_DEFOP_NONE;
1155 } else {
1156 return create_error("Invalid default-operation parameter.");
1157 }
1158 } else {
1159 defop_type = NC_EDIT_DEFOP_NOTSET;
1160 }
1161
1162 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1163 if (erropt != NULL) {
1164 if (strcmp(erropt, "continue-on-error") == 0) {
1165 erropt_type = NC_EDIT_ERROPT_CONT;
1166 } else if (strcmp(erropt, "stop-on-error") == 0) {
1167 erropt_type = NC_EDIT_ERROPT_STOP;
1168 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1169 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1170 } else {
1171 return create_error("Invalid error-option parameter.");
1172 }
1173 } else {
1174 erropt_type = 0;
1175 }
1176
1177 /* get parameters */
1178 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1179 ds_type_t = parse_datastore(target);
1180 }
1181 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1182 ds_type_s = parse_datastore(source);
1183 }
1184 if (ds_type_t == -1) {
1185 return create_error("Invalid target repository type requested.");
1186 }
1187
1188 config = json_object_get_string(json_object_object_get(request, "config"));
1189 if (config == NULL) {
1190 return create_error("Invalid config data parameter.");
1191 }
1192
1193 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
1194 if (err_reply == NULL) {
1195 reply = create_error("Edit-config failed.");
1196 } else {
1197 /* use filled err_reply from libnetconf's callback */
1198 reply = err_reply;
1199 }
1200 } else {
1201 reply = json_object_new_object();
1202 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1203 }
1204 return reply;
1205}
1206
1207json_object *handle_op_copyconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1208{
1209 NC_DATASTORE ds_type_s = -1;
1210 NC_DATASTORE ds_type_t = -1;
1211 const char *config = NULL;
1212 const char *target = NULL;
1213 const char *source = NULL;
1214 json_object *reply = NULL;
1215
1216 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1217
1218 /* get parameters */
1219 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1220 ds_type_t = parse_datastore(target);
1221 }
1222 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1223 ds_type_s = parse_datastore(source);
1224 }
1225 if (source == NULL) {
1226 /* no explicit source specified -> use config data */
1227 ds_type_s = NC_DATASTORE_CONFIG;
1228 config = json_object_get_string(json_object_object_get(request, "config"));
1229 } else if (ds_type_s == -1) {
1230 /* source datastore specified, but it is invalid */
1231 return create_error("Invalid source repository type requested.");
1232 }
1233
1234 if (ds_type_t == -1) {
1235 /* invalid target datastore specified */
1236 return create_error("Invalid target repository type requested.");
1237 }
1238
1239 if (source == NULL && config == NULL) {
1240 reply = create_error("invalid input parameters - one of source and config is required.");
1241 } else {
1242 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
1243 if (err_reply == NULL) {
1244 reply = create_error("Copying of configuration failed.");
1245 } else {
1246 /* use filled err_reply from libnetconf's callback */
1247 reply = err_reply;
1248 }
1249 } else {
1250 reply = json_object_new_object();
1251 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1252 }
1253 }
1254 return reply;
1255}
1256
1257json_object *handle_op_generic(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1258{
1259 json_object *reply = NULL;
1260 const char *config = NULL;
1261 char *data = NULL;
1262
1263 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1264
1265 config = json_object_get_string(json_object_object_get(request, "content"));
1266
1267 if (config == NULL) {
1268 return create_error("Missing content parameter.");
1269 }
1270
1271 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1272 if (err_reply == NULL) {
1273 reply = create_error("Killing of session failed.");
1274 } else {
1275 /* use filled err_reply from libnetconf's callback */
1276 reply = err_reply;
1277 }
1278 } else {
1279 if (data == NULL) {
1280 reply = json_object_new_object();
1281 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1282 } else {
1283 return create_data(data);
1284 }
1285 }
1286 return reply;
1287}
1288
1289json_object *handle_op_disconnect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1290{
1291 json_object *reply = NULL;
1292 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1293
1294 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1295 if (err_reply == NULL) {
1296 reply = create_error("Invalid session identifier.");
1297 } else {
1298 /* use filled err_reply from libnetconf's callback */
1299 reply = err_reply;
1300 }
1301 } else {
1302 reply = json_object_new_object();
1303 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1304 }
1305 return reply;
1306}
1307
1308json_object *handle_op_kill(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1309{
1310 json_object *reply = NULL;
1311 const char *sid = NULL;
1312
1313 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1314
1315 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1316
1317 if (sid == NULL) {
1318 return create_error("Missing session-id parameter.");
1319 }
1320
1321 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1322 if (err_reply == NULL) {
1323 reply = create_error("Killing of session failed.");
1324 } else {
1325 /* use filled err_reply from libnetconf's callback */
1326 reply = err_reply;
1327 }
1328 } else {
1329 reply = json_object_new_object();
1330 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1331 }
1332 return reply;
1333}
1334
1335json_object *handle_op_reloadhello(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1336{
1337 struct session_with_mutex * locked_session = NULL;
1338 json_object *reply = NULL;
1339 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1340
1341 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1342 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
1343 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1344 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
1345 if (temp_session != NULL) {
1346 prepare_status_message(server, locked_session, temp_session);
1347 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1348 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1349 } else {
1350 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1351 reply = create_error("Reload was unsuccessful, connection failed.");
1352 }
1353 } else {
1354 reply = create_error("Invalid session identifier.");
1355 }
1356
1357 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1358 reply = locked_session->hello_message;
1359 }
1360 return reply;
1361}
1362
1363json_object *handle_op_info(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1364{
1365 json_object *reply = NULL;
1366 struct session_with_mutex * locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1367 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1368
1369 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1370 if (locked_session != NULL) {
1371 if (locked_session->hello_message != NULL) {
1372 reply = locked_session->hello_message;
1373 } else {
1374 reply = create_error("Invalid session identifier.");
1375 }
1376 } else {
1377 reply = create_error("Invalid session identifier.");
1378 }
1379
1380 return reply;
1381}
1382
David Kupka8e60a372012-09-04 09:15:20 +02001383void * thread_routine (void * arg)
1384{
1385 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001386 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001387 json_object *request = NULL;
1388 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001389 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001390 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001391 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001392 const char *session_key;
1393 const char *target = NULL;
1394 const char *source = NULL;
1395 NC_DATASTORE ds_type_t = -1;
1396 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001397 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001398 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
1399 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
1400 server_rec * server = ((struct pass_to_thread*)arg)->server;
1401 int client = ((struct pass_to_thread*)arg)->client;
1402
Tomas Cejka00635972013-06-03 15:10:52 +02001403 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001404
1405 while (!isterminated) {
1406 fds.fd = client;
1407 fds.events = POLLIN;
1408 fds.revents = 0;
1409
1410 status = poll(&fds, 1, 1000);
1411
1412 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1413 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1414 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1415 continue;
1416 } else if (status < 0) {
1417 /* 0: poll time outed
1418 * close socket and ignore this request from the client, it can try it again
1419 * -1: poll failed
1420 * something wrong happend, close this socket and wait for another request
1421 */
1422 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1423 close(client);
1424 break;
1425 }
1426 /* status > 0 */
1427
1428 /* check the status of the socket */
1429
1430 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1431 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1432 /* close client's socket (it's probably already closed by client */
1433 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1434 close(client);
1435 break;
1436 }
1437
Tomas Cejka64b87482013-06-03 16:30:53 +02001438
1439 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001440
1441 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001442 enum json_tokener_error jerr;
1443 request = json_tokener_parse_verbose(buffer, &jerr);
1444 if (jerr != json_tokener_success) {
1445 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1446 continue;
1447 }
David Kupka8e60a372012-09-04 09:15:20 +02001448 operation = json_object_get_int(json_object_object_get(request, "type"));
1449
Tomas Cejka64b87482013-06-03 16:30:53 +02001450 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001451 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001452 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1453 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001454 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001455 msgtext = json_object_to_json_string(reply);
1456 send(client, msgtext, strlen(msgtext) + 1, 0);
1457 json_object_put(reply);
1458 /* there is some stupid client, so close the connection to give a chance to some other client */
1459 close(client);
1460 break;
1461 }
1462
David Kupka8e60a372012-09-04 09:15:20 +02001463 /* null global JSON error-reply */
1464 err_reply = NULL;
1465
1466 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001467 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001468
1469 /* process required operation */
1470 switch (operation) {
1471 case MSG_CONNECT:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472 reply = handle_op_connect(server, pool, request, netconf_sessions_list);
David Kupka8e60a372012-09-04 09:15:20 +02001473 break;
1474 case MSG_GET:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001475 reply = handle_op_get(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001476 break;
1477 case MSG_GETCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001478 reply = handle_op_getconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001479 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001480 case MSG_GETSCHEMA:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001481 reply = handle_op_getschema(server, pool, request, netconf_sessions_list, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001482 break;
David Kupka8e60a372012-09-04 09:15:20 +02001483 case MSG_EDITCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001484 reply = handle_op_editconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001485 break;
1486 case MSG_COPYCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001487 reply = handle_op_copyconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001488 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001489
David Kupka8e60a372012-09-04 09:15:20 +02001490 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001491 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001492 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001493 /* get parameters */
1494 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1495 ds_type_t = parse_datastore(target);
1496 }
1497 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1498 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001499 }
1500
1501 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001502 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001503 break;
1504 }
David Kupka8e60a372012-09-04 09:15:20 +02001505 switch(operation) {
1506 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001507 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001508 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1509 break;
1510 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001511 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001512 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1513 break;
1514 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001515 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001516 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1517 break;
1518 default:
1519 status = -1;
1520 break;
1521 }
1522
1523 if (status != EXIT_SUCCESS) {
1524 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001525 /** \todo more clever error message wanted */
1526 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001527 } else {
1528 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001529 reply = err_reply;
1530 }
1531 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001532 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001533 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1534 }
1535 break;
1536 case MSG_KILL:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001537 reply = handle_op_kill(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001538 break;
1539 case MSG_DISCONNECT:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001540 reply = handle_op_disconnect(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001541 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001542 case MSG_RELOADHELLO:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001543 reply = handle_op_reloadhello(server, pool, request, netconf_sessions_list, session_key);
1544 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001545 case MSG_INFO:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001546 reply = handle_op_info(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001547 break;
1548 case MSG_GENERIC:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001549 reply = handle_op_generic(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001550 break;
1551 default:
1552 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001553 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001554 break;
1555 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001556 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001557 json_object_put(request);
1558
Tomas Cejkad5b53772013-06-08 23:01:07 +02001559 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001560 /* send reply to caller */
1561 if (reply != NULL) {
1562 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001563 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001564 if (buffer != NULL) {
1565 free(buffer);
1566 buffer = NULL;
1567 }
David Kupka8e60a372012-09-04 09:15:20 +02001568 break;
1569 }
Tomas Cejka64b87482013-06-03 16:30:53 +02001570 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
David Kupka1e3e4c82012-09-04 09:32:15 +02001571 json_object_put(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001572 free(chunked_out_msg);
1573 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001574 if (buffer != NULL) {
1575 free(buffer);
1576 buffer = NULL;
1577 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001578 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001579 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001580 break;
David Kupka8e60a372012-09-04 09:15:20 +02001581 }
1582 }
1583 }
David Kupka8e60a372012-09-04 09:15:20 +02001584 free (arg);
1585
1586 return retval;
1587}
1588
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001589/**
1590 * \brief Close all open NETCONF sessions.
1591 *
1592 * During termination of mod_netconf, it is useful to close all remaining
1593 * sessions. This function iterates over the list of sessions and close them
1594 * all.
1595 *
1596 * \param[in] server pointer to server_rec for logging
1597 * \param[in] p apr pool needed for hash table iterating
1598 * \param[in] ht hash table of session_with_mutex structs
1599 */
1600static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1601{
1602 apr_hash_index_t *hi;
1603 void *val = NULL;
1604 struct session_with_mutex *swm = NULL;
1605 struct nc_session *ns = NULL;
1606 const char *hashed_key = NULL;
1607 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001608 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001609
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001610 /* get exclusive access to sessions_list (conns) */
1611 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1612 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1613 return;
1614 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001615 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1616 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1617 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1618 swm = (struct session_with_mutex *) val;
1619 if (swm != NULL) {
1620 pthread_mutex_lock(&swm->lock);
1621 if (swm->session != NULL) {
1622 ns = swm->session;
1623 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1624 nc_session_free(ns);
1625 swm->session = NULL;
1626 }
1627 pthread_mutex_unlock(&swm->lock);
1628 }
1629 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001630 /* get exclusive access to sessions_list (conns) */
1631 if (pthread_rwlock_unlock (&session_lock) != 0) {
1632 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1633 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001634}
1635
1636static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1637{
1638 apr_hash_index_t *hi;
1639 void *val = NULL;
1640 struct nc_session *ns = NULL;
1641 struct session_with_mutex *swm = NULL;
1642 const char *hashed_key = NULL;
1643 apr_ssize_t hashed_key_length;
1644 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001645 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001646
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001647 /* get exclusive access to sessions_list (conns) */
1648 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1649 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1650 return;
1651 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001652 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1653 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1654 swm = (struct session_with_mutex *) val;
1655 if (swm == NULL) {
1656 continue;
1657 }
1658 ns = swm->session;
1659 if (ns == NULL) {
1660 continue;
1661 }
1662 pthread_mutex_lock(&swm->lock);
1663 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1664 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1665 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1666 nc_session_free (ns);
1667 ns = NULL;
1668 /* remove session from the active sessions list */
1669 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1670 }
1671 pthread_mutex_unlock(&swm->lock);
1672 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001673 /* get exclusive access to sessions_list (conns) */
1674 if (pthread_rwlock_unlock (&session_lock) != 0) {
1675 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1676 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001677}
1678
1679
1680/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001681 * This is actually implementation of NETCONF client
1682 * - requests are received from UNIX socket in the predefined format
1683 * - results are replied through the same way
1684 * - the daemon run as a separate process, but it is started and stopped
1685 * automatically by Apache.
1686 *
1687 */
Radek Krejci469aab82012-07-22 18:42:20 +02001688static void forked_proc(apr_pool_t * pool, server_rec * server)
1689{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001690 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001691 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001692 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001693 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001694 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001695 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001696 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001697 struct pass_to_thread * arg;
1698 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1699 struct timespec maxtime;
1700 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001701 #ifdef WITH_NOTIFICATIONS
1702 char use_notifications = 0;
1703 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001704
Tomas Cejka404d37e2013-04-13 02:31:35 +02001705 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001706 maxtime.tv_sec = 5;
1707 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001708
Radek Krejcif23850c2012-07-23 16:14:17 +02001709 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001710 unixd_setup_child();
1711
Radek Krejciae021c12012-07-25 18:03:52 +02001712 cfg = ap_get_module_config(server->module_config, &netconf_module);
1713 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001714 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1715 return;
1716 }
Radek Krejci469aab82012-07-22 18:42:20 +02001717
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001718 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001719 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1720 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1721 return;
1722 }
1723
1724 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001725 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001726 unlink(local.sun_path);
1727 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1728
1729 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1730 if (errno == EADDRINUSE) {
1731 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1732 close(lsock);
1733 exit(0);
1734 }
1735 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1736 close(lsock);
1737 return;
1738 }
1739
1740 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1741 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1742 close(lsock);
1743 return;
1744 }
1745
Tomas Cejkaba21b382013-04-13 02:37:32 +02001746 /* prepare internal lists */
1747 netconf_sessions_list = apr_hash_make(pool);
1748
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001749 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001750 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001751 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1752 use_notifications = 0;
1753 } else {
1754 use_notifications = 1;
1755 }
1756 #endif
1757
Radek Krejci469aab82012-07-22 18:42:20 +02001758 /* setup libnetconf's callbacks */
1759 nc_verbosity(NC_VERB_DEBUG);
1760 clb_print_server = server;
1761 nc_callback_print(clb_print);
1762 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1763 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1764 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001765 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001766
1767 /* disable publickey authentication */
1768 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1769
Tomas Cejka44e71db2013-04-21 15:50:47 +02001770 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1771
David Kupka8e60a372012-09-04 09:15:20 +02001772 /* create mutex protecting session list */
1773 pthread_rwlockattr_init(&lock_attrs);
1774 /* rwlock is shared only with threads in this process */
1775 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1776 /* create rw lock */
1777 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1778 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1779 close (lsock);
1780 return;
1781 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001782
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001783 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001784 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001785 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001786 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001787 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001788 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001789 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1790 }
1791 if (use_notifications == 1) {
1792 notification_handle();
1793 }
1794 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001795 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1796 check_timeout_and_close(server, pool, netconf_sessions_list);
1797 }
Radek Krejci469aab82012-07-22 18:42:20 +02001798
1799 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001800 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001801 if (((unsigned int)tv.tv_sec - olds) > 60) {
1802 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1803 olds = tv.tv_sec;
1804 }
David Kupka8e60a372012-09-04 09:15:20 +02001805 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001806 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1807 apr_sleep(SLEEP_TIME);
1808 continue;
1809 } else if (client == -1 && (errno == EINTR)) {
1810 continue;
1811 } else if (client == -1) {
1812 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1813 continue;
1814 }
Radek Krejci469aab82012-07-22 18:42:20 +02001815
1816 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001817 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001818
David Kupka8e60a372012-09-04 09:15:20 +02001819 arg = malloc (sizeof(struct pass_to_thread));
1820 arg->client = client;
1821 arg->pool = pool;
1822 arg->server = server;
1823 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001824
David Kupka8e60a372012-09-04 09:15:20 +02001825 /* start new thread. It will serve this particular request and then terminate */
1826 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1827 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1828 } else {
1829 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1830 pthread_count++;
1831 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1832 ptids[pthread_count] = 0;
1833 }
Radek Krejci469aab82012-07-22 18:42:20 +02001834
David Kupka8e60a372012-09-04 09:15:20 +02001835 /* check if some thread already terminated, free some resources by joining it */
1836 for (i=0; i<pthread_count; i++) {
1837 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1838 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1839 pthread_count--;
1840 if (pthread_count > 0) {
1841 /* place last Thread ID on the place of joined one */
1842 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001843 }
Radek Krejci469aab82012-07-22 18:42:20 +02001844 }
1845 }
David Kupka8e60a372012-09-04 09:15:20 +02001846 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001847 }
1848
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001849 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001850 /* join all threads */
1851 for (i=0; i<pthread_count; i++) {
1852 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1853 }
1854 free (ptids);
1855
Radek Krejci469aab82012-07-22 18:42:20 +02001856 close(lsock);
1857
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001858 #ifdef WITH_NOTIFICATIONS
1859 notification_close();
1860 #endif
1861
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001862 /* close all NETCONF sessions */
1863 close_all_nc_sessions(server, pool, netconf_sessions_list);
1864
David Kupka8e60a372012-09-04 09:15:20 +02001865 /* destroy rwlock */
1866 pthread_rwlock_destroy(&session_lock);
1867 pthread_rwlockattr_destroy(&lock_attrs);
1868
Radek Krejci469aab82012-07-22 18:42:20 +02001869 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1870
1871 exit(APR_SUCCESS);
1872}
1873
Radek Krejcif23850c2012-07-23 16:14:17 +02001874static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001875{
Radek Krejcif23850c2012-07-23 16:14:17 +02001876 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1877
1878 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1879 apr_pool_create(&config->pool, pool);
1880 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001881 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001882
1883 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001884}
1885
1886static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1887 apr_pool_t * plog, server_rec * s)
1888{
Radek Krejcif23850c2012-07-23 16:14:17 +02001889 mod_netconf_cfg *config;
1890 apr_status_t res;
1891
Radek Krejci469aab82012-07-22 18:42:20 +02001892 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001893 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001894 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001895
1896 /*
1897 * The following checks if this routine has been called before.
1898 * This is necessary because the parent process gets initialized
1899 * a couple of times as the server starts up.
1900 */
1901 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1902 if (!data) {
1903 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1904 return (OK);
1905 }
1906
Radek Krejcif23850c2012-07-23 16:14:17 +02001907 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1908 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001909
Radek Krejcif23850c2012-07-23 16:14:17 +02001910 if (config && config->forkproc == NULL) {
1911 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1912 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001913 switch (res) {
1914 case APR_INCHILD:
1915 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001916 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001917 apr_signal(SIGTERM, signal_handler);
1918
1919 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001920 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001921
1922 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001923 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001924
Radek Krejcif23850c2012-07-23 16:14:17 +02001925 /* I never should be here, wtf?!? */
1926 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001927 exit(APR_EGENERAL);
1928 break;
1929 case APR_INPARENT:
1930 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001931 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001932 break;
1933 default:
1934 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1935 break;
1936 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001937 } else {
1938 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001939 }
1940
1941 return OK;
1942}
1943
Radek Krejci469aab82012-07-22 18:42:20 +02001944/**
1945 * Register module hooks
1946 */
1947static void mod_netconf_register_hooks(apr_pool_t * p)
1948{
Radek Krejcif23850c2012-07-23 16:14:17 +02001949 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001950}
1951
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001952static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1953{
1954 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1955 return NULL;
1956}
1957
1958static const command_rec netconf_cmds[] = {
1959 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1960 {NULL}
1961};
1962
Radek Krejci469aab82012-07-22 18:42:20 +02001963/* Dispatch list for API hooks */
1964module AP_MODULE_DECLARE_DATA netconf_module = {
1965 STANDARD20_MODULE_STUFF,
1966 NULL, /* create per-dir config structures */
1967 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001968 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001969 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001970 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001971 mod_netconf_register_hooks /* register hooks */
1972};
Radek Krejcia332b692012-11-12 16:15:54 +01001973