blob: 7f5c2fbe72fbe198a7f98b769d5ae960656b60da [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
David Kupka8e60a372012-09-04 09:15:20 +0200918void * thread_routine (void * arg)
919{
920 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200921 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +0200922 json_object *request = NULL;
923 json_object *reply = NULL;
924 json_object *capabilities = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200925 int operation;
Tomas Cejka64b87482013-06-03 16:30:53 +0200926 int i, len, status= 0;
927 char *session_key_hash = NULL;
928 char *data;
David Kupka8e60a372012-09-04 09:15:20 +0200929 const char *host, *port, *user, *pass;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200930 const char *msgtext;
Tomas Cejka64b87482013-06-03 16:30:53 +0200931 const char *target, *source, *filter, *config, *defop, *erropt, *sid, *session_key;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100932 const char *identifier, *version, *format;
Kupka David00b9c5c2012-09-05 09:45:50 +0200933 struct nc_cpblts* cpblts = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200934 struct session_with_mutex * locked_session;
David Kupka8e60a372012-09-04 09:15:20 +0200935 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100936 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200937 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Tomas Cejka64b87482013-06-03 16:30:53 +0200938 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200939 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
940 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
941 server_rec * server = ((struct pass_to_thread*)arg)->server;
942 int client = ((struct pass_to_thread*)arg)->client;
943
Tomas Cejka00635972013-06-03 15:10:52 +0200944 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200945
946 while (!isterminated) {
947 fds.fd = client;
948 fds.events = POLLIN;
949 fds.revents = 0;
950
951 status = poll(&fds, 1, 1000);
952
953 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
954 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
955 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
956 continue;
957 } else if (status < 0) {
958 /* 0: poll time outed
959 * close socket and ignore this request from the client, it can try it again
960 * -1: poll failed
961 * something wrong happend, close this socket and wait for another request
962 */
963 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
964 close(client);
965 break;
966 }
967 /* status > 0 */
968
969 /* check the status of the socket */
970
971 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
972 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
973 /* close client's socket (it's probably already closed by client */
974 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
975 close(client);
976 break;
977 }
978
Tomas Cejka64b87482013-06-03 16:30:53 +0200979
980 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +0200981
982 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +0200983 enum json_tokener_error jerr;
984 request = json_tokener_parse_verbose(buffer, &jerr);
985 if (jerr != json_tokener_success) {
986 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
987 continue;
988 }
David Kupka8e60a372012-09-04 09:15:20 +0200989 operation = json_object_get_int(json_object_object_get(request, "type"));
990
Tomas Cejka64b87482013-06-03 16:30:53 +0200991 session_key = json_object_get_string(json_object_object_get(request, "session"));
David Kupka8e60a372012-09-04 09:15:20 +0200992 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
993 if (operation != MSG_CONNECT && session_key == NULL) {
994 reply = json_object_new_object();
995 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
996 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
997 msgtext = json_object_to_json_string(reply);
998 send(client, msgtext, strlen(msgtext) + 1, 0);
999 json_object_put(reply);
1000 /* there is some stupid client, so close the connection to give a chance to some other client */
1001 close(client);
1002 break;
1003 }
1004
1005 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +01001006 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +02001007 ds_type_t = -1;
1008 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1009 if (strcmp(target, "running") == 0) {
1010 ds_type_t = NC_DATASTORE_RUNNING;
1011 } else if (strcmp(target, "startup") == 0) {
1012 ds_type_t = NC_DATASTORE_STARTUP;
1013 } else if (strcmp(target, "candidate") == 0) {
1014 ds_type_t = NC_DATASTORE_CANDIDATE;
1015 }
1016 }
1017 ds_type_s = -1;
1018 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1019 if (strcmp(source, "running") == 0) {
1020 ds_type_s = NC_DATASTORE_RUNNING;
1021 } else if (strcmp(source, "startup") == 0) {
1022 ds_type_s = NC_DATASTORE_STARTUP;
1023 } else if (strcmp(source, "candidate") == 0) {
1024 ds_type_s = NC_DATASTORE_CANDIDATE;
1025 }
1026 }
1027
1028 /* null global JSON error-reply */
1029 err_reply = NULL;
1030
1031 /* prepare reply envelope */
1032 reply = json_object_new_object();
1033
1034 /* process required operation */
1035 switch (operation) {
1036 case MSG_CONNECT:
1037 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
1038
1039 host = json_object_get_string(json_object_object_get(request, "host"));
1040 port = json_object_get_string(json_object_object_get(request, "port"));
1041 user = json_object_get_string(json_object_object_get(request, "user"));
1042 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +02001043 capabilities = json_object_object_get(request, "capabilities");
Tomas Cejkaae9efe52013-04-23 13:37:36 +02001044 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1045 cpblts = nc_cpblts_new (NULL);
1046 for (i=0; i<len; i++) {
1047 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1048 }
1049 } else {
1050 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1051 }
David Kupka8e60a372012-09-04 09:15:20 +02001052 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1053 if ((host == NULL) || (user == NULL)) {
1054 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001055 session_key_hash = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001056 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001057 session_key_hash = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
1058 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1059 }
1060 if (cpblts != NULL) {
1061 nc_cpblts_free(cpblts);
David Kupka8e60a372012-09-04 09:15:20 +02001062 }
1063
Tomas Cejka64b87482013-06-03 16:30:53 +02001064 if (session_key_hash == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001065 /* negative reply */
1066 if (err_reply == NULL) {
1067 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1068 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +01001069 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001070 } else {
1071 /* use filled err_reply from libnetconf's callback */
1072 json_object_put(reply);
1073 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +01001074 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +02001075 }
1076 } else {
1077 /* positive reply */
1078 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
Tomas Cejka64b87482013-06-03 16:30:53 +02001079 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
David Kupka8e60a372012-09-04 09:15:20 +02001080
Tomas Cejka64b87482013-06-03 16:30:53 +02001081 free(session_key_hash);
David Kupka8e60a372012-09-04 09:15:20 +02001082 }
1083
1084 break;
1085 case MSG_GET:
1086 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1087
1088 filter = json_object_get_string(json_object_object_get(request, "filter"));
1089
1090 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
1091
Tomas Cejkacdc274e2012-09-05 18:15:33 +02001092 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001093 if (err_reply == NULL) {
1094 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1095 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
1096 } else {
1097 /* use filled err_reply from libnetconf's callback */
1098 json_object_put(reply);
1099 reply = err_reply;
1100 }
1101 } else {
1102 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1103 json_object_object_add(reply, "data", json_object_new_string(data));
1104 }
1105 break;
1106 case MSG_GETCONFIG:
1107 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1108
1109 filter = json_object_get_string(json_object_object_get(request, "filter"));
1110
1111 if (ds_type_s == -1) {
1112 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1113 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1114 break;
1115 }
1116
1117 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1118 if (err_reply == NULL) {
1119 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1120 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1121 } else {
1122 /* use filled err_reply from libnetconf's callback */
1123 json_object_put(reply);
1124 reply = err_reply;
1125 }
1126 } else {
1127 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1128 json_object_object_add(reply, "data", json_object_new_string(data));
1129 }
1130 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001131 case MSG_GETSCHEMA:
1132 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1133 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1134 if (identifier == NULL) {
1135 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1136 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1137 break;
1138 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001139 version = json_object_get_string(json_object_object_get(request, "version"));
1140 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001141
Tomas Cejka94da2c52013-01-08 18:20:30 +01001142 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001143 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001144 if (err_reply == NULL) {
1145 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1146 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1147 } else {
1148 /* use filled err_reply from libnetconf's callback */
1149 json_object_put(reply);
1150 reply = err_reply;
1151 }
1152 } else {
1153 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1154 json_object_object_add(reply, "data", json_object_new_string(data));
1155 }
1156 break;
David Kupka8e60a372012-09-04 09:15:20 +02001157 case MSG_EDITCONFIG:
1158 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1159
1160 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1161 if (defop != NULL) {
1162 if (strcmp(defop, "merge") == 0) {
1163 defop_type = NC_EDIT_DEFOP_MERGE;
1164 } else if (strcmp(defop, "replace") == 0) {
1165 defop_type = NC_EDIT_DEFOP_REPLACE;
1166 } else if (strcmp(defop, "none") == 0) {
1167 defop_type = NC_EDIT_DEFOP_NONE;
1168 } else {
1169 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1170 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1171 break;
1172 }
1173 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001174 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001175 }
1176
1177 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1178 if (erropt != NULL) {
1179 if (strcmp(erropt, "continue-on-error") == 0) {
1180 erropt_type = NC_EDIT_ERROPT_CONT;
1181 } else if (strcmp(erropt, "stop-on-error") == 0) {
1182 erropt_type = NC_EDIT_ERROPT_STOP;
1183 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1184 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1185 } else {
1186 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1187 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1188 break;
1189 }
1190 } else {
1191 erropt_type = 0;
1192 }
1193
1194 if (ds_type_t == -1) {
1195 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1196 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1197 break;
1198 }
1199
1200 config = json_object_get_string(json_object_object_get(request, "config"));
1201 if (config == NULL) {
1202 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1203 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1204 break;
1205 }
1206
Tomas Cejka5064c232013-01-17 09:30:58 +01001207 /* TODO url capability see netconf_editconfig */
1208 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1209 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
David Kupka8e60a372012-09-04 09:15:20 +02001210 if (err_reply == NULL) {
1211 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1212 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1213 } else {
1214 /* use filled err_reply from libnetconf's callback */
1215 json_object_put(reply);
1216 reply = err_reply;
1217 }
1218 } else {
1219 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1220 }
1221 break;
1222 case MSG_COPYCONFIG:
1223 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1224 config = NULL;
1225
1226 if (source == NULL) {
1227 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001228 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001229 config = json_object_get_string(json_object_object_get(request, "config"));
1230 } else if (ds_type_s == -1) {
1231 /* source datastore specified, but it is invalid */
1232 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1233 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1234 break;
1235 }
1236
1237 if (ds_type_t == -1) {
1238 /* invalid target datastore specified */
1239 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1240 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1241 break;
1242 }
1243
1244 if (source == NULL && config == NULL) {
1245 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1246 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1247 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001248 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
David Kupka8e60a372012-09-04 09:15:20 +02001249 if (err_reply == NULL) {
1250 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1251 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1252 } else {
1253 /* use filled err_reply from libnetconf's callback */
1254 json_object_put(reply);
1255 reply = err_reply;
1256 }
1257 } else {
1258 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1259 }
1260 }
1261 break;
1262 case MSG_DELETECONFIG:
1263 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1264 /* no break - unifying code */
1265 case MSG_LOCK:
1266 if (operation == MSG_LOCK) {
1267 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1268 }
1269 /* no break - unifying code */
1270 case MSG_UNLOCK:
1271 if (operation == MSG_UNLOCK) {
1272 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1273 }
1274
1275 if (ds_type_t == -1) {
1276 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1277 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1278 break;
1279 }
1280
1281 switch(operation) {
1282 case MSG_DELETECONFIG:
1283 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1284 break;
1285 case MSG_LOCK:
1286 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1287 break;
1288 case MSG_UNLOCK:
1289 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1290 break;
1291 default:
1292 status = -1;
1293 break;
1294 }
1295
1296 if (status != EXIT_SUCCESS) {
1297 if (err_reply == NULL) {
1298 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1299 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1300 } else {
1301 /* use filled err_reply from libnetconf's callback */
1302 json_object_put(reply);
1303 reply = err_reply;
1304 }
1305 } else {
1306 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1307 }
1308 break;
1309 case MSG_KILL:
1310 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1311
1312 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1313
1314 if (sid == NULL) {
1315 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1316 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1317 break;
1318 }
1319
1320 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1321 if (err_reply == NULL) {
1322 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1323 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1324 } else {
1325 /* use filled err_reply from libnetconf's callback */
1326 json_object_put(reply);
1327 reply = err_reply;
1328 }
1329 } else {
1330 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1331 }
1332 break;
1333 case MSG_DISCONNECT:
1334 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1335
1336 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1337 if (err_reply == NULL) {
1338 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1339 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1340 } else {
1341 /* use filled err_reply from libnetconf's callback */
1342 json_object_put(reply);
1343 reply = err_reply;
1344 }
1345 } else {
1346 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1347 }
1348 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001349 case MSG_RELOADHELLO:
David Kupka8e60a372012-09-04 09:15:20 +02001350 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1351
Kupka Davidda134a12012-09-06 14:12:16 +02001352 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001353 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001354 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001355 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +02001356 if (temp_session != NULL) {
1357 prepare_status_message(server, locked_session, temp_session);
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001358 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaf38a54c2013-05-27 21:57:35 +02001359 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1360 } else {
1361 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1362 }
David Kupka8e60a372012-09-04 09:15:20 +02001363 } else {
1364 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1365 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001366 break;
David Kupka8e60a372012-09-04 09:15:20 +02001367 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001368 /* do NOT insert "break" here, we want to give new info back */;
1369 case MSG_INFO:
1370 if (operation != MSG_INFO) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001371 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001372 json_object_put(reply);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001373 reply = locked_session->hello_message;
1374 } else {
1375 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1376 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1377 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001378 } else {
1379 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001380
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001381 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001382 if (locked_session != NULL) {
1383 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001384 json_object_put(reply);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001385 reply = locked_session->hello_message;
1386 } else {
1387 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1388 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1389 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001390 } else {
1391 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1392 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1393 }
1394 }
David Kupka8e60a372012-09-04 09:15:20 +02001395 break;
1396 case MSG_GENERIC:
1397 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1398
1399 config = json_object_get_string(json_object_object_get(request, "content"));
1400
1401 if (config == NULL) {
1402 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1403 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1404 break;
1405 }
1406
1407 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1408 if (err_reply == NULL) {
1409 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1410 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1411 } else {
1412 /* use filled err_reply from libnetconf's callback */
1413 json_object_put(reply);
1414 reply = err_reply;
1415 }
1416 } else {
1417 if (data == NULL) {
1418 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1419 } else {
1420 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1421 json_object_object_add(reply, "data", json_object_new_string(data));
1422 }
1423 }
1424 break;
1425 default:
1426 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
David Kupka8e60a372012-09-04 09:15:20 +02001427 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1428 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1429 break;
1430 }
1431 json_object_put(request);
1432
David Kupka1e3e4c82012-09-04 09:32:15 +02001433 /* send reply to caller */
1434 if (reply != NULL) {
1435 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001436 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001437 if (buffer != NULL) {
1438 free(buffer);
1439 buffer = NULL;
1440 }
David Kupka8e60a372012-09-04 09:15:20 +02001441 break;
1442 }
Tomas Cejka64b87482013-06-03 16:30:53 +02001443 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
David Kupka1e3e4c82012-09-04 09:32:15 +02001444 json_object_put(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001445 free(chunked_out_msg);
1446 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001447 if (buffer != NULL) {
1448 free(buffer);
1449 buffer = NULL;
1450 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001451 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001452 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001453 break;
David Kupka8e60a372012-09-04 09:15:20 +02001454 }
1455 }
1456 }
David Kupka8e60a372012-09-04 09:15:20 +02001457 free (arg);
1458
1459 return retval;
1460}
1461
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001462/**
1463 * \brief Close all open NETCONF sessions.
1464 *
1465 * During termination of mod_netconf, it is useful to close all remaining
1466 * sessions. This function iterates over the list of sessions and close them
1467 * all.
1468 *
1469 * \param[in] server pointer to server_rec for logging
1470 * \param[in] p apr pool needed for hash table iterating
1471 * \param[in] ht hash table of session_with_mutex structs
1472 */
1473static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1474{
1475 apr_hash_index_t *hi;
1476 void *val = NULL;
1477 struct session_with_mutex *swm = NULL;
1478 struct nc_session *ns = NULL;
1479 const char *hashed_key = NULL;
1480 apr_ssize_t hashed_key_length;
1481
1482 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1483 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1484 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1485 swm = (struct session_with_mutex *) val;
1486 if (swm != NULL) {
1487 pthread_mutex_lock(&swm->lock);
1488 if (swm->session != NULL) {
1489 ns = swm->session;
1490 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1491 nc_session_free(ns);
1492 swm->session = NULL;
1493 }
1494 pthread_mutex_unlock(&swm->lock);
1495 }
1496 }
1497}
1498
1499static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1500{
1501 apr_hash_index_t *hi;
1502 void *val = NULL;
1503 struct nc_session *ns = NULL;
1504 struct session_with_mutex *swm = NULL;
1505 const char *hashed_key = NULL;
1506 apr_ssize_t hashed_key_length;
1507 apr_time_t current_time = apr_time_now();
1508
1509 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1510 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1511 swm = (struct session_with_mutex *) val;
1512 if (swm == NULL) {
1513 continue;
1514 }
1515 ns = swm->session;
1516 if (ns == NULL) {
1517 continue;
1518 }
1519 pthread_mutex_lock(&swm->lock);
1520 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1521 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1522 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1523 nc_session_free (ns);
1524 ns = NULL;
1525 /* remove session from the active sessions list */
1526 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1527 }
1528 pthread_mutex_unlock(&swm->lock);
1529 }
1530}
1531
1532
1533/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001534 * This is actually implementation of NETCONF client
1535 * - requests are received from UNIX socket in the predefined format
1536 * - results are replied through the same way
1537 * - the daemon run as a separate process, but it is started and stopped
1538 * automatically by Apache.
1539 *
1540 */
Radek Krejci469aab82012-07-22 18:42:20 +02001541static void forked_proc(apr_pool_t * pool, server_rec * server)
1542{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001543 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001544 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001545 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001546 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001547 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001548 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001549 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001550 struct pass_to_thread * arg;
1551 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1552 struct timespec maxtime;
1553 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001554 #ifdef WITH_NOTIFICATIONS
1555 char use_notifications = 0;
1556 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001557
Tomas Cejka404d37e2013-04-13 02:31:35 +02001558 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001559 maxtime.tv_sec = 5;
1560 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001561
Radek Krejcif23850c2012-07-23 16:14:17 +02001562 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001563 unixd_setup_child();
1564
Radek Krejciae021c12012-07-25 18:03:52 +02001565 cfg = ap_get_module_config(server->module_config, &netconf_module);
1566 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001567 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1568 return;
1569 }
Radek Krejci469aab82012-07-22 18:42:20 +02001570
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001571 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001572 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1573 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1574 return;
1575 }
1576
1577 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001578 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001579 unlink(local.sun_path);
1580 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1581
1582 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1583 if (errno == EADDRINUSE) {
1584 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1585 close(lsock);
1586 exit(0);
1587 }
1588 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1589 close(lsock);
1590 return;
1591 }
1592
1593 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1594 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1595 close(lsock);
1596 return;
1597 }
1598
Tomas Cejkaba21b382013-04-13 02:37:32 +02001599 /* prepare internal lists */
1600 netconf_sessions_list = apr_hash_make(pool);
1601
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001602 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001603 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001604 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1605 use_notifications = 0;
1606 } else {
1607 use_notifications = 1;
1608 }
1609 #endif
1610
Radek Krejci469aab82012-07-22 18:42:20 +02001611 /* setup libnetconf's callbacks */
1612 nc_verbosity(NC_VERB_DEBUG);
1613 clb_print_server = server;
1614 nc_callback_print(clb_print);
1615 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1616 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1617 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001618 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001619
1620 /* disable publickey authentication */
1621 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1622
Tomas Cejka44e71db2013-04-21 15:50:47 +02001623 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1624
David Kupka8e60a372012-09-04 09:15:20 +02001625 /* create mutex protecting session list */
1626 pthread_rwlockattr_init(&lock_attrs);
1627 /* rwlock is shared only with threads in this process */
1628 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1629 /* create rw lock */
1630 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1631 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1632 close (lsock);
1633 return;
1634 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001635
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001636 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001637 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001638 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001639 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001640 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001641 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001642 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1643 }
1644 if (use_notifications == 1) {
1645 notification_handle();
1646 }
1647 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001648 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1649 check_timeout_and_close(server, pool, netconf_sessions_list);
1650 }
Radek Krejci469aab82012-07-22 18:42:20 +02001651
1652 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001653 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001654 if (((unsigned int)tv.tv_sec - olds) > 60) {
1655 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1656 olds = tv.tv_sec;
1657 }
David Kupka8e60a372012-09-04 09:15:20 +02001658 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001659 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1660 apr_sleep(SLEEP_TIME);
1661 continue;
1662 } else if (client == -1 && (errno == EINTR)) {
1663 continue;
1664 } else if (client == -1) {
1665 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1666 continue;
1667 }
Radek Krejci469aab82012-07-22 18:42:20 +02001668
1669 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001670 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001671
David Kupka8e60a372012-09-04 09:15:20 +02001672 arg = malloc (sizeof(struct pass_to_thread));
1673 arg->client = client;
1674 arg->pool = pool;
1675 arg->server = server;
1676 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001677
David Kupka8e60a372012-09-04 09:15:20 +02001678 /* start new thread. It will serve this particular request and then terminate */
1679 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1680 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1681 } else {
1682 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1683 pthread_count++;
1684 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1685 ptids[pthread_count] = 0;
1686 }
Radek Krejci469aab82012-07-22 18:42:20 +02001687
David Kupka8e60a372012-09-04 09:15:20 +02001688 /* check if some thread already terminated, free some resources by joining it */
1689 for (i=0; i<pthread_count; i++) {
1690 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1691 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1692 pthread_count--;
1693 if (pthread_count > 0) {
1694 /* place last Thread ID on the place of joined one */
1695 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001696 }
Radek Krejci469aab82012-07-22 18:42:20 +02001697 }
1698 }
David Kupka8e60a372012-09-04 09:15:20 +02001699 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001700 }
1701
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001702 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001703 /* join all threads */
1704 for (i=0; i<pthread_count; i++) {
1705 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1706 }
1707 free (ptids);
1708
Radek Krejci469aab82012-07-22 18:42:20 +02001709 close(lsock);
1710
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001711 #ifdef WITH_NOTIFICATIONS
1712 notification_close();
1713 #endif
1714
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001715 /* close all NETCONF sessions */
1716 close_all_nc_sessions(server, pool, netconf_sessions_list);
1717
David Kupka8e60a372012-09-04 09:15:20 +02001718 /* destroy rwlock */
1719 pthread_rwlock_destroy(&session_lock);
1720 pthread_rwlockattr_destroy(&lock_attrs);
1721
Radek Krejci469aab82012-07-22 18:42:20 +02001722 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1723
1724 exit(APR_SUCCESS);
1725}
1726
Radek Krejcif23850c2012-07-23 16:14:17 +02001727static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001728{
Radek Krejcif23850c2012-07-23 16:14:17 +02001729 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1730
1731 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1732 apr_pool_create(&config->pool, pool);
1733 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001734 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001735
1736 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001737}
1738
1739static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1740 apr_pool_t * plog, server_rec * s)
1741{
Radek Krejcif23850c2012-07-23 16:14:17 +02001742 mod_netconf_cfg *config;
1743 apr_status_t res;
1744
Radek Krejci469aab82012-07-22 18:42:20 +02001745 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001746 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001747 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001748
1749 /*
1750 * The following checks if this routine has been called before.
1751 * This is necessary because the parent process gets initialized
1752 * a couple of times as the server starts up.
1753 */
1754 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1755 if (!data) {
1756 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1757 return (OK);
1758 }
1759
Radek Krejcif23850c2012-07-23 16:14:17 +02001760 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1761 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001762
Radek Krejcif23850c2012-07-23 16:14:17 +02001763 if (config && config->forkproc == NULL) {
1764 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1765 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001766 switch (res) {
1767 case APR_INCHILD:
1768 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001769 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001770 apr_signal(SIGTERM, signal_handler);
1771
1772 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001773 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001774
1775 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001776 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001777
Radek Krejcif23850c2012-07-23 16:14:17 +02001778 /* I never should be here, wtf?!? */
1779 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001780 exit(APR_EGENERAL);
1781 break;
1782 case APR_INPARENT:
1783 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001784 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001785 break;
1786 default:
1787 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1788 break;
1789 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001790 } else {
1791 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001792 }
1793
1794 return OK;
1795}
1796
Radek Krejci469aab82012-07-22 18:42:20 +02001797/**
1798 * Register module hooks
1799 */
1800static void mod_netconf_register_hooks(apr_pool_t * p)
1801{
Radek Krejcif23850c2012-07-23 16:14:17 +02001802 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001803}
1804
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001805static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1806{
1807 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1808 return NULL;
1809}
1810
1811static const command_rec netconf_cmds[] = {
1812 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1813 {NULL}
1814};
1815
Radek Krejci469aab82012-07-22 18:42:20 +02001816/* Dispatch list for API hooks */
1817module AP_MODULE_DECLARE_DATA netconf_module = {
1818 STANDARD20_MODULE_STUFF,
1819 NULL, /* create per-dir config structures */
1820 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001821 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001822 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001823 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001824 mod_netconf_register_hooks /* register hooks */
1825};
Radek Krejcia332b692012-11-12 16:15:54 +01001826