blob: 805a6a116b1d566e2e64f4fd450c0fa4b313e175 [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;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200388 nc_reply* reply;
389 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;
741 nc_reply* reply;
742 nc_rpc* rpc;
743 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
David Kupka8e60a372012-09-04 09:15:20 +0200834void * thread_routine (void * arg)
835{
836 void * retval = NULL;
837
838 ssize_t buffer_len;
839 struct pollfd fds;
840 int status, buffer_size, ret;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200841 json_object *request, *reply, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200842 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200843 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200844 char* session_key, *data;
845 const char *host, *port, *user, *pass;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200846 const char *msgtext;
David Kupka8e60a372012-09-04 09:15:20 +0200847 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100848 const char *identifier, *version, *format;
Kupka David00b9c5c2012-09-05 09:45:50 +0200849 struct nc_cpblts* cpblts = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200850 struct session_with_mutex * locked_session;
David Kupka8e60a372012-09-04 09:15:20 +0200851 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100852 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200853 NC_EDIT_ERROPT_TYPE erropt_type = 0;
854
855 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
856 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
857 server_rec * server = ((struct pass_to_thread*)arg)->server;
858 int client = ((struct pass_to_thread*)arg)->client;
859
860 char * buffer, chunk_len_str[12], *chunked_msg;
861 char c;
862
863 while (!isterminated) {
864 fds.fd = client;
865 fds.events = POLLIN;
866 fds.revents = 0;
867
868 status = poll(&fds, 1, 1000);
869
870 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
871 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
872 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
873 continue;
874 } else if (status < 0) {
875 /* 0: poll time outed
876 * close socket and ignore this request from the client, it can try it again
877 * -1: poll failed
878 * something wrong happend, close this socket and wait for another request
879 */
880 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
881 close(client);
882 break;
883 }
884 /* status > 0 */
885
886 /* check the status of the socket */
887
888 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
889 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
890 /* close client's socket (it's probably already closed by client */
891 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
892 close(client);
893 break;
894 }
895
896 /* read json in chunked framing */
897 buffer_size = 0;
898 buffer_len = 0;
899 buffer = NULL;
900 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200901 /* read chunk length */
902 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
903 free (buffer);
904 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200905 break;
906 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200907 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
908 free (buffer);
909 buffer = NULL;
910 break;
911 }
912 i=0;
913 memset (chunk_len_str, 0, 12);
914 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
915 if (i==0 && c == '#') {
916 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
917 /* end but invalid */
918 free (buffer);
919 buffer = NULL;
920 }
921 /* end of message, double-loop break */
922 goto msg_complete;
923 }
924 chunk_len_str[i++] = c;
925 }
926 if (c != '\n') {
927 free (buffer);
928 buffer = NULL;
929 break;
930 }
931 if ((chunk_len = atoi (chunk_len_str)) == 0) {
932 free (buffer);
933 buffer = NULL;
934 break;
935 }
936 buffer_size += chunk_len+1;
937 buffer = realloc (buffer, sizeof(char)*buffer_size);
938 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
939 free (buffer);
940 buffer = NULL;
941 break;
942 }
943 buffer_len += ret;
944 }
945msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200946
947 if (buffer != NULL) {
948 request = json_tokener_parse(buffer);
949 operation = json_object_get_int(json_object_object_get(request, "type"));
950
951 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
952 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
953 if (operation != MSG_CONNECT && session_key == NULL) {
954 reply = json_object_new_object();
955 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
956 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
957 msgtext = json_object_to_json_string(reply);
958 send(client, msgtext, strlen(msgtext) + 1, 0);
959 json_object_put(reply);
960 /* there is some stupid client, so close the connection to give a chance to some other client */
961 close(client);
962 break;
963 }
964
965 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100966 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200967 ds_type_t = -1;
968 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
969 if (strcmp(target, "running") == 0) {
970 ds_type_t = NC_DATASTORE_RUNNING;
971 } else if (strcmp(target, "startup") == 0) {
972 ds_type_t = NC_DATASTORE_STARTUP;
973 } else if (strcmp(target, "candidate") == 0) {
974 ds_type_t = NC_DATASTORE_CANDIDATE;
975 }
976 }
977 ds_type_s = -1;
978 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
979 if (strcmp(source, "running") == 0) {
980 ds_type_s = NC_DATASTORE_RUNNING;
981 } else if (strcmp(source, "startup") == 0) {
982 ds_type_s = NC_DATASTORE_STARTUP;
983 } else if (strcmp(source, "candidate") == 0) {
984 ds_type_s = NC_DATASTORE_CANDIDATE;
985 }
986 }
987
988 /* null global JSON error-reply */
989 err_reply = NULL;
990
991 /* prepare reply envelope */
992 reply = json_object_new_object();
993
994 /* process required operation */
995 switch (operation) {
996 case MSG_CONNECT:
997 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
998
999 host = json_object_get_string(json_object_object_get(request, "host"));
1000 port = json_object_get_string(json_object_object_get(request, "port"));
1001 user = json_object_get_string(json_object_object_get(request, "user"));
1002 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +02001003 capabilities = json_object_object_get(request, "capabilities");
Tomas Cejkaae9efe52013-04-23 13:37:36 +02001004 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1005 cpblts = nc_cpblts_new (NULL);
1006 for (i=0; i<len; i++) {
1007 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1008 }
1009 } else {
1010 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1011 }
David Kupka8e60a372012-09-04 09:15:20 +02001012 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1013 if ((host == NULL) || (user == NULL)) {
1014 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1015 session_key = NULL;
1016 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +02001017 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
1018 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +02001019 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
1020 }
1021
David Kupka8e60a372012-09-04 09:15:20 +02001022 if (session_key == NULL) {
1023 /* negative reply */
1024 if (err_reply == NULL) {
1025 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1026 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +01001027 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001028 } else {
1029 /* use filled err_reply from libnetconf's callback */
1030 json_object_put(reply);
1031 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +01001032 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +02001033 }
1034 } else {
1035 /* positive reply */
1036 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1037 json_object_object_add(reply, "session", json_object_new_string(session_key));
1038
1039 free(session_key);
1040 }
1041
1042 break;
1043 case MSG_GET:
1044 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1045
1046 filter = json_object_get_string(json_object_object_get(request, "filter"));
1047
1048 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
1049
Tomas Cejkacdc274e2012-09-05 18:15:33 +02001050 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001051 if (err_reply == NULL) {
1052 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1053 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
1054 } else {
1055 /* use filled err_reply from libnetconf's callback */
1056 json_object_put(reply);
1057 reply = err_reply;
1058 }
1059 } else {
1060 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1061 json_object_object_add(reply, "data", json_object_new_string(data));
1062 }
1063 break;
1064 case MSG_GETCONFIG:
1065 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1066
1067 filter = json_object_get_string(json_object_object_get(request, "filter"));
1068
1069 if (ds_type_s == -1) {
1070 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1071 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1072 break;
1073 }
1074
1075 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1076 if (err_reply == NULL) {
1077 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1078 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1079 } else {
1080 /* use filled err_reply from libnetconf's callback */
1081 json_object_put(reply);
1082 reply = err_reply;
1083 }
1084 } else {
1085 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1086 json_object_object_add(reply, "data", json_object_new_string(data));
1087 }
1088 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001089 case MSG_GETSCHEMA:
1090 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1091 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1092 if (identifier == NULL) {
1093 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1094 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1095 break;
1096 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001097 version = json_object_get_string(json_object_object_get(request, "version"));
1098 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001099
Tomas Cejka94da2c52013-01-08 18:20:30 +01001100 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001101 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001102 if (err_reply == NULL) {
1103 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1104 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1105 } else {
1106 /* use filled err_reply from libnetconf's callback */
1107 json_object_put(reply);
1108 reply = err_reply;
1109 }
1110 } else {
1111 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1112 json_object_object_add(reply, "data", json_object_new_string(data));
1113 }
1114 break;
David Kupka8e60a372012-09-04 09:15:20 +02001115 case MSG_EDITCONFIG:
1116 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1117
1118 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1119 if (defop != NULL) {
1120 if (strcmp(defop, "merge") == 0) {
1121 defop_type = NC_EDIT_DEFOP_MERGE;
1122 } else if (strcmp(defop, "replace") == 0) {
1123 defop_type = NC_EDIT_DEFOP_REPLACE;
1124 } else if (strcmp(defop, "none") == 0) {
1125 defop_type = NC_EDIT_DEFOP_NONE;
1126 } else {
1127 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1128 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1129 break;
1130 }
1131 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001132 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001133 }
1134
1135 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1136 if (erropt != NULL) {
1137 if (strcmp(erropt, "continue-on-error") == 0) {
1138 erropt_type = NC_EDIT_ERROPT_CONT;
1139 } else if (strcmp(erropt, "stop-on-error") == 0) {
1140 erropt_type = NC_EDIT_ERROPT_STOP;
1141 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1142 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1143 } else {
1144 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1145 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1146 break;
1147 }
1148 } else {
1149 erropt_type = 0;
1150 }
1151
1152 if (ds_type_t == -1) {
1153 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1154 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1155 break;
1156 }
1157
1158 config = json_object_get_string(json_object_object_get(request, "config"));
1159 if (config == NULL) {
1160 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1161 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1162 break;
1163 }
1164
Tomas Cejka5064c232013-01-17 09:30:58 +01001165 /* TODO url capability see netconf_editconfig */
1166 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1167 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 +02001168 if (err_reply == NULL) {
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("edit-config failed."));
1171 } else {
1172 /* use filled err_reply from libnetconf's callback */
1173 json_object_put(reply);
1174 reply = err_reply;
1175 }
1176 } else {
1177 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1178 }
1179 break;
1180 case MSG_COPYCONFIG:
1181 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1182 config = NULL;
1183
1184 if (source == NULL) {
1185 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001186 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001187 config = json_object_get_string(json_object_object_get(request, "config"));
1188 } else if (ds_type_s == -1) {
1189 /* source datastore specified, but it is invalid */
1190 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1191 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1192 break;
1193 }
1194
1195 if (ds_type_t == -1) {
1196 /* invalid target datastore specified */
1197 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1198 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1199 break;
1200 }
1201
1202 if (source == NULL && config == NULL) {
1203 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1204 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1205 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001206 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 +02001207 if (err_reply == NULL) {
1208 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1209 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1210 } else {
1211 /* use filled err_reply from libnetconf's callback */
1212 json_object_put(reply);
1213 reply = err_reply;
1214 }
1215 } else {
1216 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1217 }
1218 }
1219 break;
1220 case MSG_DELETECONFIG:
1221 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1222 /* no break - unifying code */
1223 case MSG_LOCK:
1224 if (operation == MSG_LOCK) {
1225 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1226 }
1227 /* no break - unifying code */
1228 case MSG_UNLOCK:
1229 if (operation == MSG_UNLOCK) {
1230 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1231 }
1232
1233 if (ds_type_t == -1) {
1234 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1235 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1236 break;
1237 }
1238
1239 switch(operation) {
1240 case MSG_DELETECONFIG:
1241 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1242 break;
1243 case MSG_LOCK:
1244 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1245 break;
1246 case MSG_UNLOCK:
1247 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1248 break;
1249 default:
1250 status = -1;
1251 break;
1252 }
1253
1254 if (status != EXIT_SUCCESS) {
1255 if (err_reply == NULL) {
1256 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1257 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1258 } else {
1259 /* use filled err_reply from libnetconf's callback */
1260 json_object_put(reply);
1261 reply = err_reply;
1262 }
1263 } else {
1264 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1265 }
1266 break;
1267 case MSG_KILL:
1268 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1269
1270 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1271
1272 if (sid == NULL) {
1273 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1274 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1275 break;
1276 }
1277
1278 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1279 if (err_reply == NULL) {
1280 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1281 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1282 } else {
1283 /* use filled err_reply from libnetconf's callback */
1284 json_object_put(reply);
1285 reply = err_reply;
1286 }
1287 } else {
1288 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1289 }
1290 break;
1291 case MSG_DISCONNECT:
1292 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1293
1294 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1295 if (err_reply == NULL) {
1296 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1297 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1298 } else {
1299 /* use filled err_reply from libnetconf's callback */
1300 json_object_put(reply);
1301 reply = err_reply;
1302 }
1303 } else {
1304 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1305 }
1306 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001307 case MSG_RELOADHELLO:
David Kupka8e60a372012-09-04 09:15:20 +02001308 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1309
Kupka Davidda134a12012-09-06 14:12:16 +02001310 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 +02001311 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001312 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001313 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +02001314 if (temp_session != NULL) {
1315 prepare_status_message(server, locked_session, temp_session);
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001316 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
Tomas Cejkaf38a54c2013-05-27 21:57:35 +02001317 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 }
David Kupka8e60a372012-09-04 09:15:20 +02001321 } else {
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("Invalid session identifier."));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001324 break;
David Kupka8e60a372012-09-04 09:15:20 +02001325 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001326 /* do NOT insert "break" here, we want to give new info back */;
1327 case MSG_INFO:
1328 if (operation != MSG_INFO) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001329 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001330 json_object_put(reply);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001331 reply = locked_session->hello_message;
1332 } else {
1333 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1334 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1335 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001336 } else {
1337 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001338
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001339 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 +02001340 if (locked_session != NULL) {
1341 if (locked_session->hello_message != NULL) {
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001342 json_object_put(reply);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001343 reply = locked_session->hello_message;
1344 } else {
1345 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1346 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1347 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001348 } else {
1349 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1350 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1351 }
1352 }
David Kupka8e60a372012-09-04 09:15:20 +02001353 break;
1354 case MSG_GENERIC:
1355 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1356
1357 config = json_object_get_string(json_object_object_get(request, "content"));
1358
1359 if (config == NULL) {
1360 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1361 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1362 break;
1363 }
1364
1365 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1366 if (err_reply == NULL) {
1367 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1368 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1369 } else {
1370 /* use filled err_reply from libnetconf's callback */
1371 json_object_put(reply);
1372 reply = err_reply;
1373 }
1374 } else {
1375 if (data == NULL) {
1376 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1377 } else {
1378 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1379 json_object_object_add(reply, "data", json_object_new_string(data));
1380 }
1381 }
1382 break;
1383 default:
1384 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
David Kupka8e60a372012-09-04 09:15:20 +02001385 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1386 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1387 break;
1388 }
1389 json_object_put(request);
1390
David Kupka1e3e4c82012-09-04 09:32:15 +02001391 /* send reply to caller */
1392 if (reply != NULL) {
1393 msgtext = json_object_to_json_string(reply);
1394 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1395 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001396 break;
1397 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001398 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1399 json_object_put(reply);
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001400 reply = NULL;
David Kupka1e3e4c82012-09-04 09:32:15 +02001401 free (chunked_msg);
1402 free (buffer);
1403 } else {
1404 break;
David Kupka8e60a372012-09-04 09:15:20 +02001405 }
1406 }
1407 }
David Kupka8e60a372012-09-04 09:15:20 +02001408 free (arg);
1409
1410 return retval;
1411}
1412
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001413/**
1414 * \brief Close all open NETCONF sessions.
1415 *
1416 * During termination of mod_netconf, it is useful to close all remaining
1417 * sessions. This function iterates over the list of sessions and close them
1418 * all.
1419 *
1420 * \param[in] server pointer to server_rec for logging
1421 * \param[in] p apr pool needed for hash table iterating
1422 * \param[in] ht hash table of session_with_mutex structs
1423 */
1424static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1425{
1426 apr_hash_index_t *hi;
1427 void *val = NULL;
1428 struct session_with_mutex *swm = NULL;
1429 struct nc_session *ns = NULL;
1430 const char *hashed_key = NULL;
1431 apr_ssize_t hashed_key_length;
1432
1433 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1434 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1435 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1436 swm = (struct session_with_mutex *) val;
1437 if (swm != NULL) {
1438 pthread_mutex_lock(&swm->lock);
1439 if (swm->session != NULL) {
1440 ns = swm->session;
1441 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1442 nc_session_free(ns);
1443 swm->session = NULL;
1444 }
1445 pthread_mutex_unlock(&swm->lock);
1446 }
1447 }
1448}
1449
1450static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1451{
1452 apr_hash_index_t *hi;
1453 void *val = NULL;
1454 struct nc_session *ns = NULL;
1455 struct session_with_mutex *swm = NULL;
1456 const char *hashed_key = NULL;
1457 apr_ssize_t hashed_key_length;
1458 apr_time_t current_time = apr_time_now();
1459
1460 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1461 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1462 swm = (struct session_with_mutex *) val;
1463 if (swm == NULL) {
1464 continue;
1465 }
1466 ns = swm->session;
1467 if (ns == NULL) {
1468 continue;
1469 }
1470 pthread_mutex_lock(&swm->lock);
1471 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1472 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1473 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1474 nc_session_free (ns);
1475 ns = NULL;
1476 /* remove session from the active sessions list */
1477 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1478 }
1479 pthread_mutex_unlock(&swm->lock);
1480 }
1481}
1482
1483
1484/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001485 * This is actually implementation of NETCONF client
1486 * - requests are received from UNIX socket in the predefined format
1487 * - results are replied through the same way
1488 * - the daemon run as a separate process, but it is started and stopped
1489 * automatically by Apache.
1490 *
1491 */
Radek Krejci469aab82012-07-22 18:42:20 +02001492static void forked_proc(apr_pool_t * pool, server_rec * server)
1493{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001494 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001495 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001496 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001497 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001498 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001499 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001500 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001501 struct pass_to_thread * arg;
1502 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1503 struct timespec maxtime;
1504 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001505 #ifdef WITH_NOTIFICATIONS
1506 char use_notifications = 0;
1507 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001508
Tomas Cejka404d37e2013-04-13 02:31:35 +02001509 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001510 maxtime.tv_sec = 5;
1511 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001512
Radek Krejcif23850c2012-07-23 16:14:17 +02001513 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001514 unixd_setup_child();
1515
Radek Krejciae021c12012-07-25 18:03:52 +02001516 cfg = ap_get_module_config(server->module_config, &netconf_module);
1517 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001518 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1519 return;
1520 }
Radek Krejci469aab82012-07-22 18:42:20 +02001521
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001522 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001523 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1524 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1525 return;
1526 }
1527
1528 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001529 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001530 unlink(local.sun_path);
1531 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1532
1533 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1534 if (errno == EADDRINUSE) {
1535 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1536 close(lsock);
1537 exit(0);
1538 }
1539 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1540 close(lsock);
1541 return;
1542 }
1543
1544 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1545 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1546 close(lsock);
1547 return;
1548 }
1549
Tomas Cejkaba21b382013-04-13 02:37:32 +02001550 /* prepare internal lists */
1551 netconf_sessions_list = apr_hash_make(pool);
1552
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001553 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001554 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001555 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1556 use_notifications = 0;
1557 } else {
1558 use_notifications = 1;
1559 }
1560 #endif
1561
Radek Krejci469aab82012-07-22 18:42:20 +02001562 /* setup libnetconf's callbacks */
1563 nc_verbosity(NC_VERB_DEBUG);
1564 clb_print_server = server;
1565 nc_callback_print(clb_print);
1566 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1567 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1568 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001569 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001570
1571 /* disable publickey authentication */
1572 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1573
Tomas Cejka44e71db2013-04-21 15:50:47 +02001574 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1575
David Kupka8e60a372012-09-04 09:15:20 +02001576 /* create mutex protecting session list */
1577 pthread_rwlockattr_init(&lock_attrs);
1578 /* rwlock is shared only with threads in this process */
1579 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1580 /* create rw lock */
1581 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1582 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1583 close (lsock);
1584 return;
1585 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001586
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001587 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001588 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001589 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001590 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001591 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001592 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001593 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1594 }
1595 if (use_notifications == 1) {
1596 notification_handle();
1597 }
1598 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001599 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1600 check_timeout_and_close(server, pool, netconf_sessions_list);
1601 }
Radek Krejci469aab82012-07-22 18:42:20 +02001602
1603 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001604 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001605 if (((unsigned int)tv.tv_sec - olds) > 60) {
1606 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1607 olds = tv.tv_sec;
1608 }
David Kupka8e60a372012-09-04 09:15:20 +02001609 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001610 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1611 apr_sleep(SLEEP_TIME);
1612 continue;
1613 } else if (client == -1 && (errno == EINTR)) {
1614 continue;
1615 } else if (client == -1) {
1616 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1617 continue;
1618 }
Radek Krejci469aab82012-07-22 18:42:20 +02001619
1620 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001621 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001622
David Kupka8e60a372012-09-04 09:15:20 +02001623 arg = malloc (sizeof(struct pass_to_thread));
1624 arg->client = client;
1625 arg->pool = pool;
1626 arg->server = server;
1627 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001628
David Kupka8e60a372012-09-04 09:15:20 +02001629 /* start new thread. It will serve this particular request and then terminate */
1630 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1631 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1632 } else {
1633 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1634 pthread_count++;
1635 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1636 ptids[pthread_count] = 0;
1637 }
Radek Krejci469aab82012-07-22 18:42:20 +02001638
David Kupka8e60a372012-09-04 09:15:20 +02001639 /* check if some thread already terminated, free some resources by joining it */
1640 for (i=0; i<pthread_count; i++) {
1641 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1642 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1643 pthread_count--;
1644 if (pthread_count > 0) {
1645 /* place last Thread ID on the place of joined one */
1646 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001647 }
Radek Krejci469aab82012-07-22 18:42:20 +02001648 }
1649 }
David Kupka8e60a372012-09-04 09:15:20 +02001650 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001651 }
1652
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001654 /* join all threads */
1655 for (i=0; i<pthread_count; i++) {
1656 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1657 }
1658 free (ptids);
1659
Radek Krejci469aab82012-07-22 18:42:20 +02001660 close(lsock);
1661
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001662 #ifdef WITH_NOTIFICATIONS
1663 notification_close();
1664 #endif
1665
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001666 /* close all NETCONF sessions */
1667 close_all_nc_sessions(server, pool, netconf_sessions_list);
1668
David Kupka8e60a372012-09-04 09:15:20 +02001669 /* destroy rwlock */
1670 pthread_rwlock_destroy(&session_lock);
1671 pthread_rwlockattr_destroy(&lock_attrs);
1672
Radek Krejci469aab82012-07-22 18:42:20 +02001673 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1674
1675 exit(APR_SUCCESS);
1676}
1677
Radek Krejcif23850c2012-07-23 16:14:17 +02001678static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001679{
Radek Krejcif23850c2012-07-23 16:14:17 +02001680 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1681
1682 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1683 apr_pool_create(&config->pool, pool);
1684 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001685 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001686
1687 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001688}
1689
1690static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1691 apr_pool_t * plog, server_rec * s)
1692{
Radek Krejcif23850c2012-07-23 16:14:17 +02001693 mod_netconf_cfg *config;
1694 apr_status_t res;
1695
Radek Krejci469aab82012-07-22 18:42:20 +02001696 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001697 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001698 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001699
1700 /*
1701 * The following checks if this routine has been called before.
1702 * This is necessary because the parent process gets initialized
1703 * a couple of times as the server starts up.
1704 */
1705 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1706 if (!data) {
1707 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1708 return (OK);
1709 }
1710
Radek Krejcif23850c2012-07-23 16:14:17 +02001711 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1712 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001713
Radek Krejcif23850c2012-07-23 16:14:17 +02001714 if (config && config->forkproc == NULL) {
1715 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1716 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001717 switch (res) {
1718 case APR_INCHILD:
1719 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001720 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001721 apr_signal(SIGTERM, signal_handler);
1722
1723 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001724 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001725
1726 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001727 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001728
Radek Krejcif23850c2012-07-23 16:14:17 +02001729 /* I never should be here, wtf?!? */
1730 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001731 exit(APR_EGENERAL);
1732 break;
1733 case APR_INPARENT:
1734 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001735 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001736 break;
1737 default:
1738 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1739 break;
1740 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001741 } else {
1742 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001743 }
1744
1745 return OK;
1746}
1747
Radek Krejci469aab82012-07-22 18:42:20 +02001748/**
1749 * Register module hooks
1750 */
1751static void mod_netconf_register_hooks(apr_pool_t * p)
1752{
Radek Krejcif23850c2012-07-23 16:14:17 +02001753 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001754}
1755
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001756static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1757{
1758 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1759 return NULL;
1760}
1761
1762static const command_rec netconf_cmds[] = {
1763 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1764 {NULL}
1765};
1766
Radek Krejci469aab82012-07-22 18:42:20 +02001767/* Dispatch list for API hooks */
1768module AP_MODULE_DECLARE_DATA netconf_module = {
1769 STANDARD20_MODULE_STUFF,
1770 NULL, /* create per-dir config structures */
1771 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001772 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001773 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001774 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001775 mod_netconf_register_hooks /* register hooks */
1776};
Radek Krejcia332b692012-11-12 16:15:54 +01001777