blob: d5ae61b4897616d565a217c38712e53e5af236a5 [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;
David Kupka8e60a372012-09-04 09:15:20 +0200304 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100305 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200306 /* get exclusive access to sessions_list (conns) */
307 if (pthread_rwlock_wrlock (&session_lock) != 0) {
308 nc_session_free(session);
309 free (locked_session);
310 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
311 return NULL;
312 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200313 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200314 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100315 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200316 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100317 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200318
319 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200320 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200321
David Kupka8e60a372012-09-04 09:15:20 +0200322 /* end of critical section */
323 if (pthread_rwlock_unlock (&session_lock) != 0) {
324 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
325 return NULL;
326 }
Radek Krejci469aab82012-07-22 18:42:20 +0200327 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200328 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200329 } else {
330 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200331 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200332 }
333
Radek Krejci469aab82012-07-22 18:42:20 +0200334}
335
Radek Krejci80c10d92012-07-30 08:38:50 +0200336static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200337{
338 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200339 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200340
Radek Krejcif23850c2012-07-23 16:14:17 +0200341 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200342 /* get exclusive (write) access to sessions_list (conns) */
343 if (pthread_rwlock_wrlock (&session_lock) != 0) {
344 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
345 return EXIT_FAILURE;
346 }
347 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
348 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200349 /** \todo free all notifications from queue */
350 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200351 pthread_mutex_destroy(&locked_session->lock);
352 ns = locked_session->session;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200353 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200354 json_object_put(locked_session->hello_message);
355 locked_session->hello_message = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200356 }
David Kupka8e60a372012-09-04 09:15:20 +0200357 free (locked_session);
358 }
Radek Krejci469aab82012-07-22 18:42:20 +0200359 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100360 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200361 nc_session_free (ns);
362 ns = NULL;
363
364 /* remove session from the active sessions list */
365 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200366 /* end of critical section */
367 if (pthread_rwlock_unlock (&session_lock) != 0) {
368 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
369 return EXIT_FAILURE;
370 }
Radek Krejci469aab82012-07-22 18:42:20 +0200371 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200372
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200373 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200374 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200375 if (pthread_rwlock_unlock (&session_lock) != 0) {
376 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
377 return EXIT_FAILURE;
378 }
Radek Krejci469aab82012-07-22 18:42:20 +0200379 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200380 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200381 }
382}
383
Radek Krejci80c10d92012-07-30 08:38:50 +0200384static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200385{
386 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200387 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200388 nc_reply* reply = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200389 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100390 NC_MSG_TYPE msgt;
391 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200392
Radek Krejci8e4632a2012-07-26 13:40:34 +0200393 /* check requests */
394 if (rpc == NULL) {
395 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
396 return (EXIT_FAILURE);
397 }
398
David Kupka8e60a372012-09-04 09:15:20 +0200399 /* get non-exclusive (read) access to sessions_list (conns) */
400 if (pthread_rwlock_rdlock (&session_lock) != 0) {
401 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
402 return EXIT_FAILURE;
403 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200404 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200405 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
406 if (locked_session != NULL) {
407 session = locked_session->session;
408 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200409 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200410 /* get exclusive access to session */
411 if (pthread_mutex_lock(&locked_session->lock) != 0) {
412 /* unlock before returning error */
413 if (pthread_rwlock_unlock (&session_lock) != 0) {
414 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
415 return EXIT_FAILURE;
416 }
417 return EXIT_FAILURE;
418 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200419 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200420 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100421 msgt = nc_session_send_recv(session, rpc, &reply);
422
David Kupka8e60a372012-09-04 09:15:20 +0200423 /* first release exclusive lock for this session */
424 pthread_mutex_unlock(&locked_session->lock);
425 /* end of critical section */
426 if (pthread_rwlock_unlock (&session_lock) != 0) {
427 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
428 return EXIT_FAILURE;
429 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200430
Radek Krejcia332b692012-11-12 16:15:54 +0100431 /* process the result of the operation */
432 switch (msgt) {
433 case NC_MSG_UNKNOWN:
434 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
435 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
436 netconf_close(server, conns, session_key);
437 return (EXIT_FAILURE);
438 }
439 /* no break */
440 case NC_MSG_NONE:
441 /* there is error handled by callback */
442 return (EXIT_FAILURE);
443 break;
444 case NC_MSG_REPLY:
445 switch (replyt = nc_reply_get_type(reply)) {
446 case NC_REPLY_OK:
447 retval = EXIT_SUCCESS;
448 break;
449 default:
450 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
451 retval = EXIT_FAILURE;
452 break;
453 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200454 break;
455 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100456 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200457 retval = EXIT_FAILURE;
458 break;
459 }
460 nc_reply_free(reply);
461 return (retval);
462 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100463 /* release lock on failure */
464 if (pthread_rwlock_unlock (&session_lock) != 0) {
465 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
466 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200467 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
468 return (EXIT_FAILURE);
469 }
470}
Radek Krejci80c10d92012-07-30 08:38:50 +0200471
472static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200473{
474 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200475 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100476 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200477 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100478 NC_MSG_TYPE msgt;
479 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200480
481 /* check requests */
482 if (rpc == NULL) {
483 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
484 return (NULL);
485 }
486
David Kupka8e60a372012-09-04 09:15:20 +0200487 /* get non-exclusive (read) access to sessions_list (conns) */
488 if (pthread_rwlock_rdlock (&session_lock) != 0) {
489 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
490 return NULL;
491 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200492 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200493 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
494 if (locked_session != NULL) {
495 session = locked_session->session;
496 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200497 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200498 /* get exclusive access to session */
499 if (pthread_mutex_lock(&locked_session->lock) != 0) {
500 /* unlock before returning error */
501 if (pthread_rwlock_unlock (&session_lock) != 0) {
502 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
503 return NULL;
504 }
505 return NULL;
506 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200507 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200508 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100509 msgt = nc_session_send_recv(session, rpc, &reply);
510
David Kupka8e60a372012-09-04 09:15:20 +0200511 /* first release exclusive lock for this session */
512 pthread_mutex_unlock(&locked_session->lock);
513 /* end of critical section */
514 if (pthread_rwlock_unlock (&session_lock) != 0) {
515 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 +0100516 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200517 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200518
Radek Krejcia332b692012-11-12 16:15:54 +0100519 /* process the result of the operation */
520 switch (msgt) {
521 case NC_MSG_UNKNOWN:
522 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
523 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
524 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200525 return (NULL);
526 }
Radek Krejcia332b692012-11-12 16:15:54 +0100527 /* no break */
528 case NC_MSG_NONE:
529 /* there is error handled by callback */
530 return (NULL);
531 break;
532 case NC_MSG_REPLY:
533 switch (replyt = nc_reply_get_type(reply)) {
534 case NC_REPLY_DATA:
535 if ((data = nc_reply_get_data (reply)) == NULL) {
536 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
537 data = NULL;
538 }
539 break;
540 default:
541 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
542 data = NULL;
543 break;
544 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200545 break;
546 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100547 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
548 data = NULL;
549 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200550 }
551 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200552 return (data);
553 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100554 /* release lock on failure */
555 if (pthread_rwlock_unlock (&session_lock) != 0) {
556 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
557 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200558 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
559 return (NULL);
560 }
561}
562
Radek Krejci80c10d92012-07-30 08:38:50 +0200563static 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 +0200564{
565 nc_rpc* rpc;
566 struct nc_filter *f = NULL;
567 char* data = NULL;
568
569 /* create filter if set */
570 if (filter != NULL) {
571 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
572 }
573
574 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100575 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200576 nc_filter_free(f);
577 if (rpc == NULL) {
578 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
579 return (NULL);
580 }
581
582 data = netconf_opdata(server, conns, session_key, rpc);
583 nc_rpc_free (rpc);
584 return (data);
585}
586
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100587static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
588{
589 nc_rpc* rpc;
590 char* data = NULL;
591
592 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100593 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100594 if (rpc == NULL) {
595 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
596 return (NULL);
597 }
598
599 data = netconf_opdata(server, conns, session_key, rpc);
600 nc_rpc_free (rpc);
601 return (data);
602}
603
Radek Krejci80c10d92012-07-30 08:38:50 +0200604static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200605{
606 nc_rpc* rpc;
607 struct nc_filter *f = NULL;
608 char* data = NULL;
609
610 /* create filter if set */
611 if (filter != NULL) {
612 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
613 }
614
615 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100616 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200617 nc_filter_free(f);
618 if (rpc == NULL) {
619 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
620 return (NULL);
621 }
622
623 data = netconf_opdata(server, conns, session_key, rpc);
624 nc_rpc_free (rpc);
625 return (data);
626}
627
Tomas Cejka5064c232013-01-17 09:30:58 +0100628static 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 +0200629{
630 nc_rpc* rpc;
631 int retval = EXIT_SUCCESS;
632
633 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100634 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
635 if (target == NC_DATASTORE_URL) {
636 rpc = nc_rpc_copyconfig(source, target, config, url);
637 } else {
638 rpc = nc_rpc_copyconfig(source, target, config);
639 }
640 } else {
641 if (target == NC_DATASTORE_URL) {
642 rpc = nc_rpc_copyconfig(source, target, url);
643 } else {
644 rpc = nc_rpc_copyconfig(source, target);
645 }
646 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200647 if (rpc == NULL) {
648 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
649 return (EXIT_FAILURE);
650 }
651
652 retval = netconf_op(server, conns, session_key, rpc);
653 nc_rpc_free (rpc);
654 return (retval);
655}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200656
Tomas Cejka5064c232013-01-17 09:30:58 +0100657static 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 +0200658{
659 nc_rpc* rpc;
660 int retval = EXIT_SUCCESS;
661
662 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100663 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
664 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200665 if (rpc == NULL) {
666 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
667 return (EXIT_FAILURE);
668 }
669
670 retval = netconf_op(server, conns, session_key, rpc);
671 nc_rpc_free (rpc);
672 return (retval);
673}
674
Radek Krejci80c10d92012-07-30 08:38:50 +0200675static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200676{
677 nc_rpc* rpc;
678 int retval = EXIT_SUCCESS;
679
680 /* create requests */
681 rpc = nc_rpc_killsession(sid);
682 if (rpc == NULL) {
683 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
684 return (EXIT_FAILURE);
685 }
686
687 retval = netconf_op(server, conns, session_key, rpc);
688 nc_rpc_free (rpc);
689 return (retval);
690}
691
Radek Krejci80c10d92012-07-30 08:38:50 +0200692static 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 +0200693{
694 nc_rpc* rpc;
695 int retval = EXIT_SUCCESS;
696
697 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200698 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200699 if (rpc == NULL) {
700 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
701 return (EXIT_FAILURE);
702 }
703
704 retval = netconf_op(server, conns, session_key, rpc);
705 nc_rpc_free (rpc);
706 return (retval);
707}
708
Radek Krejci80c10d92012-07-30 08:38:50 +0200709static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200710{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200711 nc_rpc *rpc = NULL;
712 if (target != NC_DATASTORE_URL) {
713 rpc = nc_rpc_deleteconfig(target);
714 } else {
715 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
716 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
717 return (EXIT_FAILURE);
718 }
719
720 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200721}
722
Radek Krejci80c10d92012-07-30 08:38:50 +0200723static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200724{
725 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
726}
727
Radek Krejci80c10d92012-07-30 08:38:50 +0200728static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200729{
730 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
731}
732
Radek Krejci80c10d92012-07-30 08:38:50 +0200733/**
734 * @return REPLY_OK: 0, *data == NULL
735 * REPLY_DATA: 0, *data != NULL
736 * REPLY_ERROR: 1, *data == NULL
737 */
738static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
739{
740 struct nc_session *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200741 nc_reply* reply = NULL;
742 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200743 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100744 NC_MSG_TYPE msgt;
745 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200746
747 /* create requests */
748 rpc = nc_rpc_generic(content);
749 if (rpc == NULL) {
750 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
751 return (EXIT_FAILURE);
752 }
753
Radek Krejcia332b692012-11-12 16:15:54 +0100754 if (data != NULL) {
755 *data = NULL;
756 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200757
758 /* get session where send the RPC */
759 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
760 if (session != NULL) {
761 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100762 msgt = nc_session_send_recv(session, rpc, &reply);
763 nc_rpc_free (rpc);
764
765 /* process the result of the operation */
766 switch (msgt) {
767 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200768 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
769 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
770 netconf_close(server, conns, session_key);
771 return (EXIT_FAILURE);
772 }
Radek Krejcia332b692012-11-12 16:15:54 +0100773 /* no break */
774 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200775 /* there is error handled by callback */
776 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200777 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100778 case NC_MSG_REPLY:
779 switch (replyt = nc_reply_get_type(reply)) {
780 case NC_REPLY_DATA:
781 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
782 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
783 nc_reply_free(reply);
784 return (EXIT_FAILURE);
785 }
786 retval = EXIT_SUCCESS;
787 break;
788 case NC_REPLY_OK:
789 retval = EXIT_SUCCESS;
790 break;
791 default:
792 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
793 retval = EXIT_FAILURE;
794 break;
795 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200796 break;
797 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100798 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200799 retval = EXIT_FAILURE;
800 break;
801 }
802 nc_reply_free(reply);
803
804 return (retval);
805 } else {
806 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
807 return (EXIT_FAILURE);
808 }
809}
810
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200811/**
812 * @}
813 *//* netconf_operations */
814
Radek Krejci469aab82012-07-22 18:42:20 +0200815server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200816void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200817{
Radek Krejci7338bde2012-08-10 12:57:30 +0200818 switch (level) {
819 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200820 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200821 break;
822 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200823 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200824 break;
825 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200826 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200827 break;
828 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200829 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200830 break;
831 }
Radek Krejci469aab82012-07-22 18:42:20 +0200832}
833
Tomas Cejka64b87482013-06-03 16:30:53 +0200834/**
835 * Receive message from client and return pointer to it.
836 * Caller should free message memory.
837 * \param[in] client socket descriptor of client
838 * \param[in] server httpd server for logging
839 * \return pointer to message
840 */
841char *get_framed_message(server_rec *server, int client)
842{
843 /* read json in chunked framing */
844 unsigned int buffer_size = 0;
845 ssize_t buffer_len = 0;
846 char *buffer = NULL;
847 char c;
848 ssize_t ret;
849 int i, chunk_len;
850 char chunk_len_str[12];
851
852 while (1) {
853 /* read chunk length */
854 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
855 if (buffer != NULL) {
856 free (buffer);
857 buffer = NULL;
858 }
859 break;
860 }
861 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
862 if (buffer != NULL) {
863 free (buffer);
864 buffer = NULL;
865 }
866 break;
867 }
868 i=0;
869 memset (chunk_len_str, 0, 12);
870 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
871 if (i==0 && c == '#') {
872 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
873 /* end but invalid */
874 if (buffer != NULL) {
875 free (buffer);
876 buffer = NULL;
877 }
878 }
879 /* end of message, double-loop break */
880 goto msg_complete;
881 }
882 chunk_len_str[i++] = c;
883 if (i==11) {
884 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
885 break;
886 }
887 }
888 if (c != '\n') {
889 if (buffer != NULL) {
890 free (buffer);
891 buffer = NULL;
892 }
893 break;
894 }
895 chunk_len_str[i] = 0;
896 if ((chunk_len = atoi (chunk_len_str)) == 0) {
897 if (buffer != NULL) {
898 free (buffer);
899 buffer = NULL;
900 }
901 break;
902 }
903 buffer_size += chunk_len+1;
904 buffer = realloc (buffer, sizeof(char)*buffer_size);
905 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
906 if (buffer != NULL) {
907 free (buffer);
908 buffer = NULL;
909 }
910 break;
911 }
912 buffer_len += ret;
913 }
914msg_complete:
915 return buffer;
916}
917
Tomas Cejkad5b53772013-06-08 23:01:07 +0200918NC_DATASTORE parse_datastore(const char *ds)
919{
920 if (strcmp(ds, "running") == 0) {
921 return NC_DATASTORE_RUNNING;
922 } else if (strcmp(ds, "startup") == 0) {
923 return NC_DATASTORE_STARTUP;
924 } else if (strcmp(ds, "candidate") == 0) {
925 return NC_DATASTORE_CANDIDATE;
926 }
927 return -1;
928}
929
930json_object *create_error(const char *errmess)
931{
932 json_object *reply = json_object_new_object();
933 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
934 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
935 return reply;
936
937}
938
939json_object *create_data(const char *data)
940{
941 json_object *reply = json_object_new_object();
942 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
943 json_object_object_add(reply, "data", json_object_new_string(data));
944 return reply;
945}
946
947json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list)
948{
949 const char *host = NULL;
950 const char *port = NULL;
951 const char *user = NULL;
952 const char *pass = NULL;
953 json_object *capabilities = NULL;
954 json_object *reply = NULL;
955 char *session_key_hash = NULL;
956 struct nc_cpblts* cpblts = NULL;
957 unsigned int len, i;
958
959 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
960 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
961 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
962 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
963 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
964
965 capabilities = json_object_object_get((json_object *) request, "capabilities");
966 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
967 cpblts = nc_cpblts_new(NULL);
968 for (i=0; i<len; i++) {
969 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
970 }
971 } else {
972 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
973 }
974
975 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
976 if ((host == NULL) || (user == NULL)) {
977 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
978 session_key_hash = NULL;
979 } else {
980 session_key_hash = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
981 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
982 }
983 if (cpblts != NULL) {
984 nc_cpblts_free(cpblts);
985 }
986
987 if (session_key_hash == NULL) {
988 /* negative reply */
989 if (err_reply == NULL) {
990 reply = json_object_new_object();
991 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
992 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
993 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
994 } else {
995 /* use filled err_reply from libnetconf's callback */
996 reply = err_reply;
997 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
998 }
999 } else {
1000 /* positive reply */
1001 reply = json_object_new_object();
1002 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1003 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1004
1005 free(session_key_hash);
1006 }
1007 return reply;
1008}
1009
1010json_object *handle_op_get(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1011{
1012 const char *filter = NULL;
1013 const char *data = NULL;
1014 json_object *reply = NULL;
1015
1016 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1017
1018 filter = json_object_get_string(json_object_object_get(request, "filter"));
1019
1020 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
1021 if (err_reply == NULL) {
1022 reply = create_error("Get information from device failed.");
1023 } else {
1024 /* use filled err_reply from libnetconf's callback */
1025 reply = err_reply;
1026 }
1027 } else {
1028 return create_data(data);
1029 }
1030 return reply;
1031}
1032
1033json_object *handle_op_getconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1034{
1035 NC_DATASTORE ds_type_s = -1;
1036 NC_DATASTORE ds_type_t = -1;
1037 const char *filter = NULL;
1038 const char *data = NULL;
1039 const char *source = NULL;
1040 const char *target = NULL;
1041 json_object *reply = NULL;
1042
1043 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1044
1045 filter = json_object_get_string(json_object_object_get(request, "filter"));
1046
1047 /* get parameters */
1048 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1049 ds_type_t = parse_datastore(target);
1050 }
1051 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1052 ds_type_s = parse_datastore(source);
1053 }
1054 if (ds_type_s == -1) {
1055 return create_error("Invalid source repository type requested.");
1056 }
1057
1058 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1059 if (err_reply == NULL) {
1060 reply = create_error("Get configuration information from device failed.");
1061 } else {
1062 /* use filled err_reply from libnetconf's callback */
1063 reply = err_reply;
1064 }
1065 } else {
1066 return create_data(data);
1067 }
1068 return reply;
1069}
1070
1071json_object *handle_op_getschema(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1072{
1073 const char *data = NULL;
1074 const char *identifier = NULL;
1075 const char *version = NULL;
1076 const char *format = NULL;
1077 json_object *reply = NULL;
1078
1079 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1080 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1081 if (identifier == NULL) {
1082 return create_error("No identifier for get-schema supplied.");
1083 }
1084 version = json_object_get_string(json_object_object_get(request, "version"));
1085 format = json_object_get_string(json_object_object_get(request, "format"));
1086
1087 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
1088 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
1089 if (err_reply == NULL) {
1090 reply = create_error("Get schema failed.");
1091 } else {
1092 /* use filled err_reply from libnetconf's callback */
1093 reply = err_reply;
1094 }
1095 } else {
1096 return create_data(data);
1097 }
1098 return reply;
1099}
1100
1101json_object *handle_op_editconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1102{
1103 NC_DATASTORE ds_type_s = -1;
1104 NC_DATASTORE ds_type_t = -1;
1105 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1106 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1107 const char *defop = NULL;
1108 const char *erropt = NULL;
1109 const char *config = NULL;
1110 const char *source = NULL;
1111 const char *target = NULL;
1112 json_object *reply = NULL;
1113
1114 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1115
1116 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1117 if (defop != NULL) {
1118 if (strcmp(defop, "merge") == 0) {
1119 defop_type = NC_EDIT_DEFOP_MERGE;
1120 } else if (strcmp(defop, "replace") == 0) {
1121 defop_type = NC_EDIT_DEFOP_REPLACE;
1122 } else if (strcmp(defop, "none") == 0) {
1123 defop_type = NC_EDIT_DEFOP_NONE;
1124 } else {
1125 return create_error("Invalid default-operation parameter.");
1126 }
1127 } else {
1128 defop_type = NC_EDIT_DEFOP_NOTSET;
1129 }
1130
1131 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1132 if (erropt != NULL) {
1133 if (strcmp(erropt, "continue-on-error") == 0) {
1134 erropt_type = NC_EDIT_ERROPT_CONT;
1135 } else if (strcmp(erropt, "stop-on-error") == 0) {
1136 erropt_type = NC_EDIT_ERROPT_STOP;
1137 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1138 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1139 } else {
1140 return create_error("Invalid error-option parameter.");
1141 }
1142 } else {
1143 erropt_type = 0;
1144 }
1145
1146 /* get parameters */
1147 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1148 ds_type_t = parse_datastore(target);
1149 }
1150 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1151 ds_type_s = parse_datastore(source);
1152 }
1153 if (ds_type_t == -1) {
1154 return create_error("Invalid target repository type requested.");
1155 }
1156
1157 config = json_object_get_string(json_object_object_get(request, "config"));
1158 if (config == NULL) {
1159 return create_error("Invalid config data parameter.");
1160 }
1161
1162 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
1163 if (err_reply == NULL) {
1164 reply = create_error("Edit-config failed.");
1165 } else {
1166 /* use filled err_reply from libnetconf's callback */
1167 reply = err_reply;
1168 }
1169 } else {
1170 reply = json_object_new_object();
1171 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1172 }
1173 return reply;
1174}
1175
1176json_object *handle_op_copyconfig(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1177{
1178 NC_DATASTORE ds_type_s = -1;
1179 NC_DATASTORE ds_type_t = -1;
1180 const char *config = NULL;
1181 const char *target = NULL;
1182 const char *source = NULL;
1183 json_object *reply = NULL;
1184
1185 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1186
1187 /* get parameters */
1188 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1189 ds_type_t = parse_datastore(target);
1190 }
1191 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1192 ds_type_s = parse_datastore(source);
1193 }
1194 if (source == NULL) {
1195 /* no explicit source specified -> use config data */
1196 ds_type_s = NC_DATASTORE_CONFIG;
1197 config = json_object_get_string(json_object_object_get(request, "config"));
1198 } else if (ds_type_s == -1) {
1199 /* source datastore specified, but it is invalid */
1200 return create_error("Invalid source repository type requested.");
1201 }
1202
1203 if (ds_type_t == -1) {
1204 /* invalid target datastore specified */
1205 return create_error("Invalid target repository type requested.");
1206 }
1207
1208 if (source == NULL && config == NULL) {
1209 reply = create_error("invalid input parameters - one of source and config is required.");
1210 } else {
1211 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
1212 if (err_reply == NULL) {
1213 reply = create_error("Copying of configuration failed.");
1214 } else {
1215 /* use filled err_reply from libnetconf's callback */
1216 reply = err_reply;
1217 }
1218 } else {
1219 reply = json_object_new_object();
1220 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1221 }
1222 }
1223 return reply;
1224}
1225
1226json_object *handle_op_generic(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1227{
1228 json_object *reply = NULL;
1229 const char *config = NULL;
1230 char *data = NULL;
1231
1232 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1233
1234 config = json_object_get_string(json_object_object_get(request, "content"));
1235
1236 if (config == NULL) {
1237 return create_error("Missing content parameter.");
1238 }
1239
1240 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1241 if (err_reply == NULL) {
1242 reply = create_error("Killing of session failed.");
1243 } else {
1244 /* use filled err_reply from libnetconf's callback */
1245 reply = err_reply;
1246 }
1247 } else {
1248 if (data == NULL) {
1249 reply = json_object_new_object();
1250 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1251 } else {
1252 return create_data(data);
1253 }
1254 }
1255 return reply;
1256}
1257
1258json_object *handle_op_disconnect(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1259{
1260 json_object *reply = NULL;
1261 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1262
1263 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1264 if (err_reply == NULL) {
1265 reply = create_error("Invalid session identifier.");
1266 } else {
1267 /* use filled err_reply from libnetconf's callback */
1268 reply = err_reply;
1269 }
1270 } else {
1271 reply = json_object_new_object();
1272 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1273 }
1274 return reply;
1275}
1276
1277json_object *handle_op_kill(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1278{
1279 json_object *reply = NULL;
1280 const char *sid = NULL;
1281
1282 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1283
1284 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1285
1286 if (sid == NULL) {
1287 return create_error("Missing session-id parameter.");
1288 }
1289
1290 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1291 if (err_reply == NULL) {
1292 reply = create_error("Killing of session failed.");
1293 } else {
1294 /* use filled err_reply from libnetconf's callback */
1295 reply = err_reply;
1296 }
1297 } else {
1298 reply = json_object_new_object();
1299 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1300 }
1301 return reply;
1302}
1303
1304json_object *handle_op_reloadhello(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1305{
1306 struct session_with_mutex * locked_session = NULL;
1307 json_object *reply = NULL;
1308 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1309
1310 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1311 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
1312 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
1313 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
1314 if (temp_session != NULL) {
1315 prepare_status_message(server, locked_session, temp_session);
1316 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1317 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1318 } else {
1319 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1320 reply = create_error("Reload was unsuccessful, connection failed.");
1321 }
1322 } else {
1323 reply = create_error("Invalid session identifier.");
1324 }
1325
1326 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1327 reply = locked_session->hello_message;
1328 }
1329 return reply;
1330}
1331
1332json_object *handle_op_info(server_rec *server, apr_pool_t *pool, json_object *request, apr_hash_t *netconf_sessions_list, const char *session_key)
1333{
1334 json_object *reply = NULL;
1335 struct session_with_mutex * locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1336 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1337
1338 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1339 if (locked_session != NULL) {
1340 if (locked_session->hello_message != NULL) {
1341 reply = locked_session->hello_message;
1342 } else {
1343 reply = create_error("Invalid session identifier.");
1344 }
1345 } else {
1346 reply = create_error("Invalid session identifier.");
1347 }
1348
1349 return reply;
1350}
1351
David Kupka8e60a372012-09-04 09:15:20 +02001352void * thread_routine (void * arg)
1353{
1354 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001355 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001356 json_object *request = NULL;
1357 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001358 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001359 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001360 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001361 const char *session_key;
1362 const char *target = NULL;
1363 const char *source = NULL;
1364 NC_DATASTORE ds_type_t = -1;
1365 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001366 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001367 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
1368 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
1369 server_rec * server = ((struct pass_to_thread*)arg)->server;
1370 int client = ((struct pass_to_thread*)arg)->client;
1371
Tomas Cejka00635972013-06-03 15:10:52 +02001372 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001373
1374 while (!isterminated) {
1375 fds.fd = client;
1376 fds.events = POLLIN;
1377 fds.revents = 0;
1378
1379 status = poll(&fds, 1, 1000);
1380
1381 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1382 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1383 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1384 continue;
1385 } else if (status < 0) {
1386 /* 0: poll time outed
1387 * close socket and ignore this request from the client, it can try it again
1388 * -1: poll failed
1389 * something wrong happend, close this socket and wait for another request
1390 */
1391 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1392 close(client);
1393 break;
1394 }
1395 /* status > 0 */
1396
1397 /* check the status of the socket */
1398
1399 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1400 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1401 /* close client's socket (it's probably already closed by client */
1402 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1403 close(client);
1404 break;
1405 }
1406
Tomas Cejka64b87482013-06-03 16:30:53 +02001407
1408 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001409
1410 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001411 enum json_tokener_error jerr;
1412 request = json_tokener_parse_verbose(buffer, &jerr);
1413 if (jerr != json_tokener_success) {
1414 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1415 continue;
1416 }
David Kupka8e60a372012-09-04 09:15:20 +02001417 operation = json_object_get_int(json_object_object_get(request, "type"));
1418
Tomas Cejka64b87482013-06-03 16:30:53 +02001419 session_key = json_object_get_string(json_object_object_get(request, "session"));
David Kupka8e60a372012-09-04 09:15:20 +02001420 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1421 if (operation != MSG_CONNECT && session_key == NULL) {
1422 reply = json_object_new_object();
1423 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1424 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
1425 msgtext = json_object_to_json_string(reply);
1426 send(client, msgtext, strlen(msgtext) + 1, 0);
1427 json_object_put(reply);
1428 /* there is some stupid client, so close the connection to give a chance to some other client */
1429 close(client);
1430 break;
1431 }
1432
David Kupka8e60a372012-09-04 09:15:20 +02001433 /* null global JSON error-reply */
1434 err_reply = NULL;
1435
1436 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001437 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001438
1439 /* process required operation */
1440 switch (operation) {
1441 case MSG_CONNECT:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001442 reply = handle_op_connect(server, pool, request, netconf_sessions_list);
David Kupka8e60a372012-09-04 09:15:20 +02001443 break;
1444 case MSG_GET:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001445 reply = handle_op_get(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001446 break;
1447 case MSG_GETCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001448 reply = handle_op_getconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001449 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001450 case MSG_GETSCHEMA:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001451 reply = handle_op_getschema(server, pool, request, netconf_sessions_list, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001452 break;
David Kupka8e60a372012-09-04 09:15:20 +02001453 case MSG_EDITCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001454 reply = handle_op_editconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001455 break;
1456 case MSG_COPYCONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001457 reply = handle_op_copyconfig(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001458 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001459
David Kupka8e60a372012-09-04 09:15:20 +02001460 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001461 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001462 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001463 /* get parameters */
1464 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1465 ds_type_t = parse_datastore(target);
1466 }
1467 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1468 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001469 }
1470
1471 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001472 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001473 break;
1474 }
David Kupka8e60a372012-09-04 09:15:20 +02001475 switch(operation) {
1476 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001477 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001478 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1479 break;
1480 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001481 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001482 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1483 break;
1484 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001485 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001486 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1487 break;
1488 default:
1489 status = -1;
1490 break;
1491 }
1492
1493 if (status != EXIT_SUCCESS) {
1494 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001495 /** \todo more clever error message wanted */
1496 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001497 } else {
1498 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001499 reply = err_reply;
1500 }
1501 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001502 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001503 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1504 }
1505 break;
1506 case MSG_KILL:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001507 reply = handle_op_kill(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001508 break;
1509 case MSG_DISCONNECT:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001510 reply = handle_op_disconnect(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001511 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001512 case MSG_RELOADHELLO:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001513 reply = handle_op_reloadhello(server, pool, request, netconf_sessions_list, session_key);
1514 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001515 case MSG_INFO:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001516 reply = handle_op_info(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001517 break;
1518 case MSG_GENERIC:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001519 reply = handle_op_generic(server, pool, request, netconf_sessions_list, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001520 break;
1521 default:
1522 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001523 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001524 break;
1525 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001526 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001527 json_object_put(request);
1528
Tomas Cejkad5b53772013-06-08 23:01:07 +02001529 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001530 /* send reply to caller */
1531 if (reply != NULL) {
1532 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001533 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001534 if (buffer != NULL) {
1535 free(buffer);
1536 buffer = NULL;
1537 }
David Kupka8e60a372012-09-04 09:15:20 +02001538 break;
1539 }
Tomas Cejka64b87482013-06-03 16:30:53 +02001540 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
David Kupka1e3e4c82012-09-04 09:32:15 +02001541 json_object_put(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001542 free(chunked_out_msg);
1543 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001544 if (buffer != NULL) {
1545 free(buffer);
1546 buffer = NULL;
1547 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001548 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001549 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001550 break;
David Kupka8e60a372012-09-04 09:15:20 +02001551 }
1552 }
1553 }
David Kupka8e60a372012-09-04 09:15:20 +02001554 free (arg);
1555
1556 return retval;
1557}
1558
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001559/**
1560 * \brief Close all open NETCONF sessions.
1561 *
1562 * During termination of mod_netconf, it is useful to close all remaining
1563 * sessions. This function iterates over the list of sessions and close them
1564 * all.
1565 *
1566 * \param[in] server pointer to server_rec for logging
1567 * \param[in] p apr pool needed for hash table iterating
1568 * \param[in] ht hash table of session_with_mutex structs
1569 */
1570static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1571{
1572 apr_hash_index_t *hi;
1573 void *val = NULL;
1574 struct session_with_mutex *swm = NULL;
1575 struct nc_session *ns = NULL;
1576 const char *hashed_key = NULL;
1577 apr_ssize_t hashed_key_length;
1578
1579 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1580 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1581 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1582 swm = (struct session_with_mutex *) val;
1583 if (swm != NULL) {
1584 pthread_mutex_lock(&swm->lock);
1585 if (swm->session != NULL) {
1586 ns = swm->session;
1587 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1588 nc_session_free(ns);
1589 swm->session = NULL;
1590 }
1591 pthread_mutex_unlock(&swm->lock);
1592 }
1593 }
1594}
1595
1596static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1597{
1598 apr_hash_index_t *hi;
1599 void *val = NULL;
1600 struct nc_session *ns = NULL;
1601 struct session_with_mutex *swm = NULL;
1602 const char *hashed_key = NULL;
1603 apr_ssize_t hashed_key_length;
1604 apr_time_t current_time = apr_time_now();
1605
1606 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1607 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1608 swm = (struct session_with_mutex *) val;
1609 if (swm == NULL) {
1610 continue;
1611 }
1612 ns = swm->session;
1613 if (ns == NULL) {
1614 continue;
1615 }
1616 pthread_mutex_lock(&swm->lock);
1617 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1618 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1619 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1620 nc_session_free (ns);
1621 ns = NULL;
1622 /* remove session from the active sessions list */
1623 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1624 }
1625 pthread_mutex_unlock(&swm->lock);
1626 }
1627}
1628
1629
1630/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001631 * This is actually implementation of NETCONF client
1632 * - requests are received from UNIX socket in the predefined format
1633 * - results are replied through the same way
1634 * - the daemon run as a separate process, but it is started and stopped
1635 * automatically by Apache.
1636 *
1637 */
Radek Krejci469aab82012-07-22 18:42:20 +02001638static void forked_proc(apr_pool_t * pool, server_rec * server)
1639{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001640 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001641 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001642 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001643 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001644 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001645 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001646 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001647 struct pass_to_thread * arg;
1648 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1649 struct timespec maxtime;
1650 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001651 #ifdef WITH_NOTIFICATIONS
1652 char use_notifications = 0;
1653 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001654
Tomas Cejka404d37e2013-04-13 02:31:35 +02001655 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001656 maxtime.tv_sec = 5;
1657 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001658
Radek Krejcif23850c2012-07-23 16:14:17 +02001659 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001660 unixd_setup_child();
1661
Radek Krejciae021c12012-07-25 18:03:52 +02001662 cfg = ap_get_module_config(server->module_config, &netconf_module);
1663 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001664 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1665 return;
1666 }
Radek Krejci469aab82012-07-22 18:42:20 +02001667
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001668 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001669 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1670 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1671 return;
1672 }
1673
1674 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001675 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001676 unlink(local.sun_path);
1677 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1678
1679 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1680 if (errno == EADDRINUSE) {
1681 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1682 close(lsock);
1683 exit(0);
1684 }
1685 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1686 close(lsock);
1687 return;
1688 }
1689
1690 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1691 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1692 close(lsock);
1693 return;
1694 }
1695
Tomas Cejkaba21b382013-04-13 02:37:32 +02001696 /* prepare internal lists */
1697 netconf_sessions_list = apr_hash_make(pool);
1698
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001699 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001700 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001701 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1702 use_notifications = 0;
1703 } else {
1704 use_notifications = 1;
1705 }
1706 #endif
1707
Radek Krejci469aab82012-07-22 18:42:20 +02001708 /* setup libnetconf's callbacks */
1709 nc_verbosity(NC_VERB_DEBUG);
1710 clb_print_server = server;
1711 nc_callback_print(clb_print);
1712 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1713 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1714 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001715 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001716
1717 /* disable publickey authentication */
1718 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1719
Tomas Cejka44e71db2013-04-21 15:50:47 +02001720 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1721
David Kupka8e60a372012-09-04 09:15:20 +02001722 /* create mutex protecting session list */
1723 pthread_rwlockattr_init(&lock_attrs);
1724 /* rwlock is shared only with threads in this process */
1725 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1726 /* create rw lock */
1727 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1728 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1729 close (lsock);
1730 return;
1731 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001732
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001733 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001734 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001735 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001736 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001737 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001738 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001739 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1740 }
1741 if (use_notifications == 1) {
1742 notification_handle();
1743 }
1744 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001745 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1746 check_timeout_and_close(server, pool, netconf_sessions_list);
1747 }
Radek Krejci469aab82012-07-22 18:42:20 +02001748
1749 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001750 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001751 if (((unsigned int)tv.tv_sec - olds) > 60) {
1752 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1753 olds = tv.tv_sec;
1754 }
David Kupka8e60a372012-09-04 09:15:20 +02001755 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001756 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1757 apr_sleep(SLEEP_TIME);
1758 continue;
1759 } else if (client == -1 && (errno == EINTR)) {
1760 continue;
1761 } else if (client == -1) {
1762 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1763 continue;
1764 }
Radek Krejci469aab82012-07-22 18:42:20 +02001765
1766 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001767 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001768
David Kupka8e60a372012-09-04 09:15:20 +02001769 arg = malloc (sizeof(struct pass_to_thread));
1770 arg->client = client;
1771 arg->pool = pool;
1772 arg->server = server;
1773 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001774
David Kupka8e60a372012-09-04 09:15:20 +02001775 /* start new thread. It will serve this particular request and then terminate */
1776 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1777 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1778 } else {
1779 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1780 pthread_count++;
1781 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1782 ptids[pthread_count] = 0;
1783 }
Radek Krejci469aab82012-07-22 18:42:20 +02001784
David Kupka8e60a372012-09-04 09:15:20 +02001785 /* check if some thread already terminated, free some resources by joining it */
1786 for (i=0; i<pthread_count; i++) {
1787 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1788 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1789 pthread_count--;
1790 if (pthread_count > 0) {
1791 /* place last Thread ID on the place of joined one */
1792 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001793 }
Radek Krejci469aab82012-07-22 18:42:20 +02001794 }
1795 }
David Kupka8e60a372012-09-04 09:15:20 +02001796 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001797 }
1798
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001799 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001800 /* join all threads */
1801 for (i=0; i<pthread_count; i++) {
1802 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1803 }
1804 free (ptids);
1805
Radek Krejci469aab82012-07-22 18:42:20 +02001806 close(lsock);
1807
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001808 #ifdef WITH_NOTIFICATIONS
1809 notification_close();
1810 #endif
1811
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001812 /* close all NETCONF sessions */
1813 close_all_nc_sessions(server, pool, netconf_sessions_list);
1814
David Kupka8e60a372012-09-04 09:15:20 +02001815 /* destroy rwlock */
1816 pthread_rwlock_destroy(&session_lock);
1817 pthread_rwlockattr_destroy(&lock_attrs);
1818
Radek Krejci469aab82012-07-22 18:42:20 +02001819 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1820
1821 exit(APR_SUCCESS);
1822}
1823
Radek Krejcif23850c2012-07-23 16:14:17 +02001824static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001825{
Radek Krejcif23850c2012-07-23 16:14:17 +02001826 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1827
1828 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1829 apr_pool_create(&config->pool, pool);
1830 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001831 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001832
1833 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001834}
1835
1836static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1837 apr_pool_t * plog, server_rec * s)
1838{
Radek Krejcif23850c2012-07-23 16:14:17 +02001839 mod_netconf_cfg *config;
1840 apr_status_t res;
1841
Radek Krejci469aab82012-07-22 18:42:20 +02001842 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001843 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001844 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001845
1846 /*
1847 * The following checks if this routine has been called before.
1848 * This is necessary because the parent process gets initialized
1849 * a couple of times as the server starts up.
1850 */
1851 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1852 if (!data) {
1853 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1854 return (OK);
1855 }
1856
Radek Krejcif23850c2012-07-23 16:14:17 +02001857 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1858 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001859
Radek Krejcif23850c2012-07-23 16:14:17 +02001860 if (config && config->forkproc == NULL) {
1861 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1862 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001863 switch (res) {
1864 case APR_INCHILD:
1865 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001866 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001867 apr_signal(SIGTERM, signal_handler);
1868
1869 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001870 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001871
1872 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001873 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001874
Radek Krejcif23850c2012-07-23 16:14:17 +02001875 /* I never should be here, wtf?!? */
1876 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001877 exit(APR_EGENERAL);
1878 break;
1879 case APR_INPARENT:
1880 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001881 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001882 break;
1883 default:
1884 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1885 break;
1886 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001887 } else {
1888 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001889 }
1890
1891 return OK;
1892}
1893
Radek Krejci469aab82012-07-22 18:42:20 +02001894/**
1895 * Register module hooks
1896 */
1897static void mod_netconf_register_hooks(apr_pool_t * p)
1898{
Radek Krejcif23850c2012-07-23 16:14:17 +02001899 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001900}
1901
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001902static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1903{
1904 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1905 return NULL;
1906}
1907
1908static const command_rec netconf_cmds[] = {
1909 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1910 {NULL}
1911};
1912
Radek Krejci469aab82012-07-22 18:42:20 +02001913/* Dispatch list for API hooks */
1914module AP_MODULE_DECLARE_DATA netconf_module = {
1915 STANDARD20_MODULE_STUFF,
1916 NULL, /* create per-dir config structures */
1917 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001918 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001919 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001920 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001921 mod_netconf_register_hooks /* register hooks */
1922};
Radek Krejcia332b692012-11-12 16:15:54 +01001923