blob: 42fb5bf2dfc1ea4174755a720e190e40a6d3c5bb [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{
211 json_object *json_obj;
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 Cejka45ab59f2013-05-15 00:10:49 +0200216 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200217 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
218 //json_object_object_del(s->hello_message, NULL);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200219
220 //old_sid = json_object_object_get(s->hello_message, "sid");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200221 }
222 s->hello_message = json_object_new_object();
223 if (session != NULL) {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200224 /** \todo reload hello - save old sid */
225 //if (old_sid != NULL) {
226 // /* use previous sid */
227 // json_object_object_add(s->hello_message, "sid", old_sid);
228 //} else {
229 /* we don't have old sid */
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200230 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200231 //}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200232 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
233 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
234 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
235 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
236 cpblts = nc_session_get_cpblts (session);
237 if (cpblts != NULL) {
238 json_obj = json_object_new_array();
239 nc_cpblts_iter_start (cpblts);
240 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
241 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
242 }
243 json_object_object_add(s->hello_message, "capabilities", json_obj);
244 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200245 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 +0200246 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200247 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200248 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
249 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
250 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200251 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200252
253}
254
255
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200256/**
257 * \defgroup netconf_operations NETCONF operations
258 * The list of NETCONF operations that mod_netconf supports.
259 * @{
260 */
261
262/**
263 * \brief Connect to NETCONF server
264 *
265 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
266 */
Kupka David00b9c5c2012-09-05 09:45:50 +0200267static 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 +0200268{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200269 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200270 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200271 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200272
273 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200274 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200275 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
276 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200277 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200278 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200279
Radek Krejci469aab82012-07-22 18:42:20 +0200280 /* if connected successful, add session to the list */
281 if (session != NULL) {
282 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200283 session_key = gen_ncsession_hash(
284 (host==NULL) ? "localhost" : host,
285 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200286 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200287
Tomas Cejkaba21b382013-04-13 02:37:32 +0200288 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200289 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
290 nc_session_free(session);
291 free (locked_session);
292 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
293 return NULL;
294 }
295 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200296 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200297 locked_session->hello_message = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200298 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100299 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200300 /* get exclusive access to sessions_list (conns) */
301 if (pthread_rwlock_wrlock (&session_lock) != 0) {
302 nc_session_free(session);
303 free (locked_session);
304 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
305 return NULL;
306 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200307 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200308 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100309 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200310 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100311 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200312
313 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200314 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200315
David Kupka8e60a372012-09-04 09:15:20 +0200316 /* end of critical section */
317 if (pthread_rwlock_unlock (&session_lock) != 0) {
318 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
319 return NULL;
320 }
Radek Krejci469aab82012-07-22 18:42:20 +0200321 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200322 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200323 } else {
324 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200325 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200326 }
327
Radek Krejci469aab82012-07-22 18:42:20 +0200328}
329
Radek Krejci80c10d92012-07-30 08:38:50 +0200330static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200331{
332 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200333 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200334
Radek Krejcif23850c2012-07-23 16:14:17 +0200335 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200336 /* get exclusive (write) access to sessions_list (conns) */
337 if (pthread_rwlock_wrlock (&session_lock) != 0) {
338 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
339 return EXIT_FAILURE;
340 }
341 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
342 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200343 /** \todo free all notifications from queue */
344 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200345 pthread_mutex_destroy(&locked_session->lock);
346 ns = locked_session->session;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200347 if (locked_session->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200348 //json_object_object_del(locked_session->hello_message, NULL);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200349 }
David Kupka8e60a372012-09-04 09:15:20 +0200350 free (locked_session);
351 }
Radek Krejci469aab82012-07-22 18:42:20 +0200352 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100353 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200354 nc_session_free (ns);
355 ns = NULL;
356
357 /* remove session from the active sessions list */
358 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200359 /* end of critical section */
360 if (pthread_rwlock_unlock (&session_lock) != 0) {
361 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
362 return EXIT_FAILURE;
363 }
Radek Krejci469aab82012-07-22 18:42:20 +0200364 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200365
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200366 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200367 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200368 if (pthread_rwlock_unlock (&session_lock) != 0) {
369 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
370 return EXIT_FAILURE;
371 }
Radek Krejci469aab82012-07-22 18:42:20 +0200372 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200373 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200374 }
375}
376
Radek Krejci80c10d92012-07-30 08:38:50 +0200377static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200378{
379 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200380 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200381 nc_reply* reply;
382 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100383 NC_MSG_TYPE msgt;
384 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200385
Radek Krejci8e4632a2012-07-26 13:40:34 +0200386 /* check requests */
387 if (rpc == NULL) {
388 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
389 return (EXIT_FAILURE);
390 }
391
David Kupka8e60a372012-09-04 09:15:20 +0200392 /* get non-exclusive (read) access to sessions_list (conns) */
393 if (pthread_rwlock_rdlock (&session_lock) != 0) {
394 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
395 return EXIT_FAILURE;
396 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200397 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200398 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
399 if (locked_session != NULL) {
400 session = locked_session->session;
401 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200402 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200403 /* get exclusive access to session */
404 if (pthread_mutex_lock(&locked_session->lock) != 0) {
405 /* unlock before returning error */
406 if (pthread_rwlock_unlock (&session_lock) != 0) {
407 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
408 return EXIT_FAILURE;
409 }
410 return EXIT_FAILURE;
411 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200412 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200413 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100414 msgt = nc_session_send_recv(session, rpc, &reply);
415
David Kupka8e60a372012-09-04 09:15:20 +0200416 /* first release exclusive lock for this session */
417 pthread_mutex_unlock(&locked_session->lock);
418 /* end of critical section */
419 if (pthread_rwlock_unlock (&session_lock) != 0) {
420 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
421 return EXIT_FAILURE;
422 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200423
Radek Krejcia332b692012-11-12 16:15:54 +0100424 /* process the result of the operation */
425 switch (msgt) {
426 case NC_MSG_UNKNOWN:
427 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
428 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
429 netconf_close(server, conns, session_key);
430 return (EXIT_FAILURE);
431 }
432 /* no break */
433 case NC_MSG_NONE:
434 /* there is error handled by callback */
435 return (EXIT_FAILURE);
436 break;
437 case NC_MSG_REPLY:
438 switch (replyt = nc_reply_get_type(reply)) {
439 case NC_REPLY_OK:
440 retval = EXIT_SUCCESS;
441 break;
442 default:
443 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
444 retval = EXIT_FAILURE;
445 break;
446 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200447 break;
448 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200450 retval = EXIT_FAILURE;
451 break;
452 }
453 nc_reply_free(reply);
454 return (retval);
455 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100456 /* release lock on failure */
457 if (pthread_rwlock_unlock (&session_lock) != 0) {
458 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
459 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200460 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
461 return (EXIT_FAILURE);
462 }
463}
Radek Krejci80c10d92012-07-30 08:38:50 +0200464
465static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200466{
467 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200468 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100469 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200470 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100471 NC_MSG_TYPE msgt;
472 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200473
474 /* check requests */
475 if (rpc == NULL) {
476 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
477 return (NULL);
478 }
479
David Kupka8e60a372012-09-04 09:15:20 +0200480 /* get non-exclusive (read) access to sessions_list (conns) */
481 if (pthread_rwlock_rdlock (&session_lock) != 0) {
482 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
483 return NULL;
484 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200485 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200486 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
487 if (locked_session != NULL) {
488 session = locked_session->session;
489 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200491 /* get exclusive access to session */
492 if (pthread_mutex_lock(&locked_session->lock) != 0) {
493 /* unlock before returning error */
494 if (pthread_rwlock_unlock (&session_lock) != 0) {
495 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
496 return NULL;
497 }
498 return NULL;
499 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200500 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200501 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100502 msgt = nc_session_send_recv(session, rpc, &reply);
503
David Kupka8e60a372012-09-04 09:15:20 +0200504 /* first release exclusive lock for this session */
505 pthread_mutex_unlock(&locked_session->lock);
506 /* end of critical section */
507 if (pthread_rwlock_unlock (&session_lock) != 0) {
508 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 +0100509 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200510 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200511
Radek Krejcia332b692012-11-12 16:15:54 +0100512 /* process the result of the operation */
513 switch (msgt) {
514 case NC_MSG_UNKNOWN:
515 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
516 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
517 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200518 return (NULL);
519 }
Radek Krejcia332b692012-11-12 16:15:54 +0100520 /* no break */
521 case NC_MSG_NONE:
522 /* there is error handled by callback */
523 return (NULL);
524 break;
525 case NC_MSG_REPLY:
526 switch (replyt = nc_reply_get_type(reply)) {
527 case NC_REPLY_DATA:
528 if ((data = nc_reply_get_data (reply)) == NULL) {
529 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
530 data = NULL;
531 }
532 break;
533 default:
534 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
535 data = NULL;
536 break;
537 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200538 break;
539 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100540 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
541 data = NULL;
542 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200543 }
544 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200545 return (data);
546 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100547 /* release lock on failure */
548 if (pthread_rwlock_unlock (&session_lock) != 0) {
549 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
550 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200551 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
552 return (NULL);
553 }
554}
555
Radek Krejci80c10d92012-07-30 08:38:50 +0200556static 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 +0200557{
558 nc_rpc* rpc;
559 struct nc_filter *f = NULL;
560 char* data = NULL;
561
562 /* create filter if set */
563 if (filter != NULL) {
564 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
565 }
566
567 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100568 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200569 nc_filter_free(f);
570 if (rpc == NULL) {
571 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
572 return (NULL);
573 }
574
575 data = netconf_opdata(server, conns, session_key, rpc);
576 nc_rpc_free (rpc);
577 return (data);
578}
579
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100580static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
581{
582 nc_rpc* rpc;
583 char* data = NULL;
584
585 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100586 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100587 if (rpc == NULL) {
588 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
589 return (NULL);
590 }
591
592 data = netconf_opdata(server, conns, session_key, rpc);
593 nc_rpc_free (rpc);
594 return (data);
595}
596
Radek Krejci80c10d92012-07-30 08:38:50 +0200597static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200598{
599 nc_rpc* rpc;
600 struct nc_filter *f = NULL;
601 char* data = NULL;
602
603 /* create filter if set */
604 if (filter != NULL) {
605 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
606 }
607
608 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100609 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200610 nc_filter_free(f);
611 if (rpc == NULL) {
612 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
613 return (NULL);
614 }
615
616 data = netconf_opdata(server, conns, session_key, rpc);
617 nc_rpc_free (rpc);
618 return (data);
619}
620
Tomas Cejka5064c232013-01-17 09:30:58 +0100621static 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 +0200622{
623 nc_rpc* rpc;
624 int retval = EXIT_SUCCESS;
625
626 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100627 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
628 if (target == NC_DATASTORE_URL) {
629 rpc = nc_rpc_copyconfig(source, target, config, url);
630 } else {
631 rpc = nc_rpc_copyconfig(source, target, config);
632 }
633 } else {
634 if (target == NC_DATASTORE_URL) {
635 rpc = nc_rpc_copyconfig(source, target, url);
636 } else {
637 rpc = nc_rpc_copyconfig(source, target);
638 }
639 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200640 if (rpc == NULL) {
641 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
642 return (EXIT_FAILURE);
643 }
644
645 retval = netconf_op(server, conns, session_key, rpc);
646 nc_rpc_free (rpc);
647 return (retval);
648}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200649
Tomas Cejka5064c232013-01-17 09:30:58 +0100650static 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 +0200651{
652 nc_rpc* rpc;
653 int retval = EXIT_SUCCESS;
654
655 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100656 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
657 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200658 if (rpc == NULL) {
659 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
660 return (EXIT_FAILURE);
661 }
662
663 retval = netconf_op(server, conns, session_key, rpc);
664 nc_rpc_free (rpc);
665 return (retval);
666}
667
Radek Krejci80c10d92012-07-30 08:38:50 +0200668static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200669{
670 nc_rpc* rpc;
671 int retval = EXIT_SUCCESS;
672
673 /* create requests */
674 rpc = nc_rpc_killsession(sid);
675 if (rpc == NULL) {
676 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
677 return (EXIT_FAILURE);
678 }
679
680 retval = netconf_op(server, conns, session_key, rpc);
681 nc_rpc_free (rpc);
682 return (retval);
683}
684
Radek Krejci80c10d92012-07-30 08:38:50 +0200685static 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 +0200686{
687 nc_rpc* rpc;
688 int retval = EXIT_SUCCESS;
689
690 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200691 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200692 if (rpc == NULL) {
693 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
694 return (EXIT_FAILURE);
695 }
696
697 retval = netconf_op(server, conns, session_key, rpc);
698 nc_rpc_free (rpc);
699 return (retval);
700}
701
Radek Krejci80c10d92012-07-30 08:38:50 +0200702static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200703{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200704 nc_rpc *rpc = NULL;
705 if (target != NC_DATASTORE_URL) {
706 rpc = nc_rpc_deleteconfig(target);
707 } else {
708 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
709 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
710 return (EXIT_FAILURE);
711 }
712
713 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200714}
715
Radek Krejci80c10d92012-07-30 08:38:50 +0200716static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200717{
718 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
719}
720
Radek Krejci80c10d92012-07-30 08:38:50 +0200721static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200722{
723 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
724}
725
Radek Krejci80c10d92012-07-30 08:38:50 +0200726/**
727 * @return REPLY_OK: 0, *data == NULL
728 * REPLY_DATA: 0, *data != NULL
729 * REPLY_ERROR: 1, *data == NULL
730 */
731static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
732{
733 struct nc_session *session = NULL;
734 nc_reply* reply;
735 nc_rpc* rpc;
736 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100737 NC_MSG_TYPE msgt;
738 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200739
740 /* create requests */
741 rpc = nc_rpc_generic(content);
742 if (rpc == NULL) {
743 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
744 return (EXIT_FAILURE);
745 }
746
Radek Krejcia332b692012-11-12 16:15:54 +0100747 if (data != NULL) {
748 *data = NULL;
749 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200750
751 /* get session where send the RPC */
752 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
753 if (session != NULL) {
754 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100755 msgt = nc_session_send_recv(session, rpc, &reply);
756 nc_rpc_free (rpc);
757
758 /* process the result of the operation */
759 switch (msgt) {
760 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200761 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
762 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
763 netconf_close(server, conns, session_key);
764 return (EXIT_FAILURE);
765 }
Radek Krejcia332b692012-11-12 16:15:54 +0100766 /* no break */
767 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200768 /* there is error handled by callback */
769 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200770 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100771 case NC_MSG_REPLY:
772 switch (replyt = nc_reply_get_type(reply)) {
773 case NC_REPLY_DATA:
774 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
775 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
776 nc_reply_free(reply);
777 return (EXIT_FAILURE);
778 }
779 retval = EXIT_SUCCESS;
780 break;
781 case NC_REPLY_OK:
782 retval = EXIT_SUCCESS;
783 break;
784 default:
785 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
786 retval = EXIT_FAILURE;
787 break;
788 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200789 break;
790 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100791 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200792 retval = EXIT_FAILURE;
793 break;
794 }
795 nc_reply_free(reply);
796
797 return (retval);
798 } else {
799 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
800 return (EXIT_FAILURE);
801 }
802}
803
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200804/**
805 * @}
806 *//* netconf_operations */
807
Radek Krejci469aab82012-07-22 18:42:20 +0200808server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200809void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200810{
Radek Krejci7338bde2012-08-10 12:57:30 +0200811 switch (level) {
812 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200813 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200814 break;
815 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200816 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200817 break;
818 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200819 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200820 break;
821 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200822 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200823 break;
824 }
Radek Krejci469aab82012-07-22 18:42:20 +0200825}
826
David Kupka8e60a372012-09-04 09:15:20 +0200827void * thread_routine (void * arg)
828{
829 void * retval = NULL;
830
831 ssize_t buffer_len;
832 struct pollfd fds;
833 int status, buffer_size, ret;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200834 json_object *request, *reply, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200835 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200836 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200837 char* session_key, *data;
838 const char *host, *port, *user, *pass;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200839 const char *msgtext;
David Kupka8e60a372012-09-04 09:15:20 +0200840 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100841 const char *identifier, *version, *format;
Kupka David00b9c5c2012-09-05 09:45:50 +0200842 struct nc_cpblts* cpblts = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200843 struct session_with_mutex * locked_session;
David Kupka8e60a372012-09-04 09:15:20 +0200844 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100845 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200846 NC_EDIT_ERROPT_TYPE erropt_type = 0;
847
848 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
849 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
850 server_rec * server = ((struct pass_to_thread*)arg)->server;
851 int client = ((struct pass_to_thread*)arg)->client;
852
853 char * buffer, chunk_len_str[12], *chunked_msg;
854 char c;
855
856 while (!isterminated) {
857 fds.fd = client;
858 fds.events = POLLIN;
859 fds.revents = 0;
860
861 status = poll(&fds, 1, 1000);
862
863 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
864 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
865 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
866 continue;
867 } else if (status < 0) {
868 /* 0: poll time outed
869 * close socket and ignore this request from the client, it can try it again
870 * -1: poll failed
871 * something wrong happend, close this socket and wait for another request
872 */
873 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
874 close(client);
875 break;
876 }
877 /* status > 0 */
878
879 /* check the status of the socket */
880
881 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
882 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
883 /* close client's socket (it's probably already closed by client */
884 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
885 close(client);
886 break;
887 }
888
889 /* read json in chunked framing */
890 buffer_size = 0;
891 buffer_len = 0;
892 buffer = NULL;
893 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200894 /* read chunk length */
895 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
896 free (buffer);
897 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200898 break;
899 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200900 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
901 free (buffer);
902 buffer = NULL;
903 break;
904 }
905 i=0;
906 memset (chunk_len_str, 0, 12);
907 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
908 if (i==0 && c == '#') {
909 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
910 /* end but invalid */
911 free (buffer);
912 buffer = NULL;
913 }
914 /* end of message, double-loop break */
915 goto msg_complete;
916 }
917 chunk_len_str[i++] = c;
918 }
919 if (c != '\n') {
920 free (buffer);
921 buffer = NULL;
922 break;
923 }
924 if ((chunk_len = atoi (chunk_len_str)) == 0) {
925 free (buffer);
926 buffer = NULL;
927 break;
928 }
929 buffer_size += chunk_len+1;
930 buffer = realloc (buffer, sizeof(char)*buffer_size);
931 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
932 free (buffer);
933 buffer = NULL;
934 break;
935 }
936 buffer_len += ret;
937 }
938msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200939
940 if (buffer != NULL) {
941 request = json_tokener_parse(buffer);
942 operation = json_object_get_int(json_object_object_get(request, "type"));
943
944 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
945 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
946 if (operation != MSG_CONNECT && session_key == NULL) {
947 reply = json_object_new_object();
948 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
949 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
950 msgtext = json_object_to_json_string(reply);
951 send(client, msgtext, strlen(msgtext) + 1, 0);
952 json_object_put(reply);
953 /* there is some stupid client, so close the connection to give a chance to some other client */
954 close(client);
955 break;
956 }
957
958 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100959 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200960 ds_type_t = -1;
961 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
962 if (strcmp(target, "running") == 0) {
963 ds_type_t = NC_DATASTORE_RUNNING;
964 } else if (strcmp(target, "startup") == 0) {
965 ds_type_t = NC_DATASTORE_STARTUP;
966 } else if (strcmp(target, "candidate") == 0) {
967 ds_type_t = NC_DATASTORE_CANDIDATE;
968 }
969 }
970 ds_type_s = -1;
971 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
972 if (strcmp(source, "running") == 0) {
973 ds_type_s = NC_DATASTORE_RUNNING;
974 } else if (strcmp(source, "startup") == 0) {
975 ds_type_s = NC_DATASTORE_STARTUP;
976 } else if (strcmp(source, "candidate") == 0) {
977 ds_type_s = NC_DATASTORE_CANDIDATE;
978 }
979 }
980
981 /* null global JSON error-reply */
982 err_reply = NULL;
983
984 /* prepare reply envelope */
985 reply = json_object_new_object();
986
987 /* process required operation */
988 switch (operation) {
989 case MSG_CONNECT:
990 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
991
992 host = json_object_get_string(json_object_object_get(request, "host"));
993 port = json_object_get_string(json_object_object_get(request, "port"));
994 user = json_object_get_string(json_object_object_get(request, "user"));
995 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200996 capabilities = json_object_object_get(request, "capabilities");
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200997 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
998 cpblts = nc_cpblts_new (NULL);
999 for (i=0; i<len; i++) {
1000 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1001 }
1002 } else {
1003 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1004 }
David Kupka8e60a372012-09-04 09:15:20 +02001005 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1006 if ((host == NULL) || (user == NULL)) {
1007 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1008 session_key = NULL;
1009 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +02001010 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
1011 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +02001012 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
1013 }
1014
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001015 /** \todo check if this is neccessary... probably leads to memory leaks */
David Kupka8e60a372012-09-04 09:15:20 +02001016 reply = json_object_new_object();
1017 if (session_key == NULL) {
1018 /* negative reply */
1019 if (err_reply == NULL) {
1020 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1021 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +01001022 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001023 } else {
1024 /* use filled err_reply from libnetconf's callback */
1025 json_object_put(reply);
1026 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +01001027 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +02001028 }
1029 } else {
1030 /* positive reply */
1031 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1032 json_object_object_add(reply, "session", json_object_new_string(session_key));
1033
1034 free(session_key);
1035 }
1036
1037 break;
1038 case MSG_GET:
1039 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1040
1041 filter = json_object_get_string(json_object_object_get(request, "filter"));
1042
1043 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
1044
Tomas Cejkacdc274e2012-09-05 18:15:33 +02001045 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001046 if (err_reply == NULL) {
1047 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1048 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
1049 } else {
1050 /* use filled err_reply from libnetconf's callback */
1051 json_object_put(reply);
1052 reply = err_reply;
1053 }
1054 } else {
1055 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1056 json_object_object_add(reply, "data", json_object_new_string(data));
1057 }
1058 break;
1059 case MSG_GETCONFIG:
1060 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1061
1062 filter = json_object_get_string(json_object_object_get(request, "filter"));
1063
1064 if (ds_type_s == -1) {
1065 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1066 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1067 break;
1068 }
1069
1070 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1071 if (err_reply == NULL) {
1072 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1073 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1074 } else {
1075 /* use filled err_reply from libnetconf's callback */
1076 json_object_put(reply);
1077 reply = err_reply;
1078 }
1079 } else {
1080 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1081 json_object_object_add(reply, "data", json_object_new_string(data));
1082 }
1083 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001084 case MSG_GETSCHEMA:
1085 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1086 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1087 if (identifier == NULL) {
1088 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1089 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1090 break;
1091 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001092 version = json_object_get_string(json_object_object_get(request, "version"));
1093 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001094
Tomas Cejka94da2c52013-01-08 18:20:30 +01001095 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001096 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001097 if (err_reply == NULL) {
1098 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1099 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1100 } else {
1101 /* use filled err_reply from libnetconf's callback */
1102 json_object_put(reply);
1103 reply = err_reply;
1104 }
1105 } else {
1106 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1107 json_object_object_add(reply, "data", json_object_new_string(data));
1108 }
1109 break;
David Kupka8e60a372012-09-04 09:15:20 +02001110 case MSG_EDITCONFIG:
1111 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1112
1113 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1114 if (defop != NULL) {
1115 if (strcmp(defop, "merge") == 0) {
1116 defop_type = NC_EDIT_DEFOP_MERGE;
1117 } else if (strcmp(defop, "replace") == 0) {
1118 defop_type = NC_EDIT_DEFOP_REPLACE;
1119 } else if (strcmp(defop, "none") == 0) {
1120 defop_type = NC_EDIT_DEFOP_NONE;
1121 } else {
1122 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1123 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1124 break;
1125 }
1126 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001127 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001128 }
1129
1130 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1131 if (erropt != NULL) {
1132 if (strcmp(erropt, "continue-on-error") == 0) {
1133 erropt_type = NC_EDIT_ERROPT_CONT;
1134 } else if (strcmp(erropt, "stop-on-error") == 0) {
1135 erropt_type = NC_EDIT_ERROPT_STOP;
1136 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1137 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1138 } else {
1139 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1140 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1141 break;
1142 }
1143 } else {
1144 erropt_type = 0;
1145 }
1146
1147 if (ds_type_t == -1) {
1148 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1149 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1150 break;
1151 }
1152
1153 config = json_object_get_string(json_object_object_get(request, "config"));
1154 if (config == NULL) {
1155 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1156 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1157 break;
1158 }
1159
Tomas Cejka5064c232013-01-17 09:30:58 +01001160 /* TODO url capability see netconf_editconfig */
1161 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1162 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 +02001163 if (err_reply == NULL) {
1164 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1165 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1166 } else {
1167 /* use filled err_reply from libnetconf's callback */
1168 json_object_put(reply);
1169 reply = err_reply;
1170 }
1171 } else {
1172 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1173 }
1174 break;
1175 case MSG_COPYCONFIG:
1176 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1177 config = NULL;
1178
1179 if (source == NULL) {
1180 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001181 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001182 config = json_object_get_string(json_object_object_get(request, "config"));
1183 } else if (ds_type_s == -1) {
1184 /* source datastore specified, but it is invalid */
1185 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1186 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1187 break;
1188 }
1189
1190 if (ds_type_t == -1) {
1191 /* invalid target datastore specified */
1192 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1193 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1194 break;
1195 }
1196
1197 if (source == NULL && config == NULL) {
1198 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1199 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1200 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001201 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 +02001202 if (err_reply == 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("copy-config failed."));
1205 } else {
1206 /* use filled err_reply from libnetconf's callback */
1207 json_object_put(reply);
1208 reply = err_reply;
1209 }
1210 } else {
1211 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1212 }
1213 }
1214 break;
1215 case MSG_DELETECONFIG:
1216 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1217 /* no break - unifying code */
1218 case MSG_LOCK:
1219 if (operation == MSG_LOCK) {
1220 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1221 }
1222 /* no break - unifying code */
1223 case MSG_UNLOCK:
1224 if (operation == MSG_UNLOCK) {
1225 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1226 }
1227
1228 if (ds_type_t == -1) {
1229 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1230 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1231 break;
1232 }
1233
1234 switch(operation) {
1235 case MSG_DELETECONFIG:
1236 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1237 break;
1238 case MSG_LOCK:
1239 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1240 break;
1241 case MSG_UNLOCK:
1242 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1243 break;
1244 default:
1245 status = -1;
1246 break;
1247 }
1248
1249 if (status != EXIT_SUCCESS) {
1250 if (err_reply == NULL) {
1251 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1252 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1253 } else {
1254 /* use filled err_reply from libnetconf's callback */
1255 json_object_put(reply);
1256 reply = err_reply;
1257 }
1258 } else {
1259 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1260 }
1261 break;
1262 case MSG_KILL:
1263 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1264
1265 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1266
1267 if (sid == NULL) {
1268 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1269 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1270 break;
1271 }
1272
1273 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1274 if (err_reply == NULL) {
1275 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1276 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1277 } else {
1278 /* use filled err_reply from libnetconf's callback */
1279 json_object_put(reply);
1280 reply = err_reply;
1281 }
1282 } else {
1283 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1284 }
1285 break;
1286 case MSG_DISCONNECT:
1287 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1288
1289 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1290 if (err_reply == NULL) {
1291 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1292 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1293 } else {
1294 /* use filled err_reply from libnetconf's callback */
1295 json_object_put(reply);
1296 reply = err_reply;
1297 }
1298 } else {
1299 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1300 }
1301 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001302 case MSG_RELOADHELLO:
David Kupka8e60a372012-09-04 09:15:20 +02001303 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1304
Kupka Davidda134a12012-09-06 14:12:16 +02001305 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 +02001306 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
1307 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +02001308 if (temp_session != NULL) {
1309 prepare_status_message(server, locked_session, temp_session);
1310 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1311 } else {
1312 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1313 }
David Kupka8e60a372012-09-04 09:15:20 +02001314 } else {
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("Invalid session identifier."));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001317 break;
David Kupka8e60a372012-09-04 09:15:20 +02001318 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001319 /* do NOT insert "break" here, we want to give new info back */;
1320 case MSG_INFO:
1321 if (operation != MSG_INFO) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001322 //json_object_object_del(reply, NULL);
1323 if (locked_session->hello_message != NULL) {
1324 reply = locked_session->hello_message;
1325 } else {
1326 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1327 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1328 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001329 } else {
1330 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001331
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001332 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 +02001333 if (locked_session != NULL) {
1334 if (locked_session->hello_message != NULL) {
1335 reply = locked_session->hello_message;
1336 } else {
1337 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1338 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1339 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001340 } else {
1341 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1342 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1343 }
1344 }
David Kupka8e60a372012-09-04 09:15:20 +02001345 break;
1346 case MSG_GENERIC:
1347 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1348
1349 config = json_object_get_string(json_object_object_get(request, "content"));
1350
1351 if (config == NULL) {
1352 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1353 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1354 break;
1355 }
1356
1357 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1358 if (err_reply == NULL) {
1359 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1360 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1361 } else {
1362 /* use filled err_reply from libnetconf's callback */
1363 json_object_put(reply);
1364 reply = err_reply;
1365 }
1366 } else {
1367 if (data == NULL) {
1368 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1369 } else {
1370 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1371 json_object_object_add(reply, "data", json_object_new_string(data));
1372 }
1373 }
1374 break;
1375 default:
1376 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1377 reply = json_object_new_object();
1378 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1379 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1380 break;
1381 }
1382 json_object_put(request);
1383
David Kupka1e3e4c82012-09-04 09:32:15 +02001384 /* send reply to caller */
1385 if (reply != NULL) {
1386 msgtext = json_object_to_json_string(reply);
1387 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1388 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001389 break;
1390 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001391 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1392 json_object_put(reply);
1393 free (chunked_msg);
1394 free (buffer);
1395 } else {
1396 break;
David Kupka8e60a372012-09-04 09:15:20 +02001397 }
1398 }
1399 }
David Kupka8e60a372012-09-04 09:15:20 +02001400 free (arg);
1401
1402 return retval;
1403}
1404
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001405/**
1406 * \brief Close all open NETCONF sessions.
1407 *
1408 * During termination of mod_netconf, it is useful to close all remaining
1409 * sessions. This function iterates over the list of sessions and close them
1410 * all.
1411 *
1412 * \param[in] server pointer to server_rec for logging
1413 * \param[in] p apr pool needed for hash table iterating
1414 * \param[in] ht hash table of session_with_mutex structs
1415 */
1416static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1417{
1418 apr_hash_index_t *hi;
1419 void *val = NULL;
1420 struct session_with_mutex *swm = NULL;
1421 struct nc_session *ns = NULL;
1422 const char *hashed_key = NULL;
1423 apr_ssize_t hashed_key_length;
1424
1425 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1426 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1427 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1428 swm = (struct session_with_mutex *) val;
1429 if (swm != NULL) {
1430 pthread_mutex_lock(&swm->lock);
1431 if (swm->session != NULL) {
1432 ns = swm->session;
1433 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1434 nc_session_free(ns);
1435 swm->session = NULL;
1436 }
1437 pthread_mutex_unlock(&swm->lock);
1438 }
1439 }
1440}
1441
1442static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1443{
1444 apr_hash_index_t *hi;
1445 void *val = NULL;
1446 struct nc_session *ns = NULL;
1447 struct session_with_mutex *swm = NULL;
1448 const char *hashed_key = NULL;
1449 apr_ssize_t hashed_key_length;
1450 apr_time_t current_time = apr_time_now();
1451
1452 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1453 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1454 swm = (struct session_with_mutex *) val;
1455 if (swm == NULL) {
1456 continue;
1457 }
1458 ns = swm->session;
1459 if (ns == NULL) {
1460 continue;
1461 }
1462 pthread_mutex_lock(&swm->lock);
1463 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1464 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1465 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1466 nc_session_free (ns);
1467 ns = NULL;
1468 /* remove session from the active sessions list */
1469 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1470 }
1471 pthread_mutex_unlock(&swm->lock);
1472 }
1473}
1474
1475
1476/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001477 * This is actually implementation of NETCONF client
1478 * - requests are received from UNIX socket in the predefined format
1479 * - results are replied through the same way
1480 * - the daemon run as a separate process, but it is started and stopped
1481 * automatically by Apache.
1482 *
1483 */
Radek Krejci469aab82012-07-22 18:42:20 +02001484static void forked_proc(apr_pool_t * pool, server_rec * server)
1485{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001486 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001487 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001488 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001489 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001490 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001491 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001492 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001493 struct pass_to_thread * arg;
1494 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1495 struct timespec maxtime;
1496 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001497 #ifdef WITH_NOTIFICATIONS
1498 char use_notifications = 0;
1499 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001500
Tomas Cejka404d37e2013-04-13 02:31:35 +02001501 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001502 maxtime.tv_sec = 5;
1503 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001504
Radek Krejcif23850c2012-07-23 16:14:17 +02001505 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001506 unixd_setup_child();
1507
Radek Krejciae021c12012-07-25 18:03:52 +02001508 cfg = ap_get_module_config(server->module_config, &netconf_module);
1509 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001510 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1511 return;
1512 }
Radek Krejci469aab82012-07-22 18:42:20 +02001513
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001514 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001515 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1516 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1517 return;
1518 }
1519
1520 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001521 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001522 unlink(local.sun_path);
1523 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1524
1525 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1526 if (errno == EADDRINUSE) {
1527 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1528 close(lsock);
1529 exit(0);
1530 }
1531 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1532 close(lsock);
1533 return;
1534 }
1535
1536 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1537 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1538 close(lsock);
1539 return;
1540 }
1541
Tomas Cejkaba21b382013-04-13 02:37:32 +02001542 /* prepare internal lists */
1543 netconf_sessions_list = apr_hash_make(pool);
1544
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001545 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001546 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001547 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1548 use_notifications = 0;
1549 } else {
1550 use_notifications = 1;
1551 }
1552 #endif
1553
Radek Krejci469aab82012-07-22 18:42:20 +02001554 /* setup libnetconf's callbacks */
1555 nc_verbosity(NC_VERB_DEBUG);
1556 clb_print_server = server;
1557 nc_callback_print(clb_print);
1558 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1559 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1560 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001561 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001562
1563 /* disable publickey authentication */
1564 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1565
Tomas Cejka44e71db2013-04-21 15:50:47 +02001566 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1567
David Kupka8e60a372012-09-04 09:15:20 +02001568 /* create mutex protecting session list */
1569 pthread_rwlockattr_init(&lock_attrs);
1570 /* rwlock is shared only with threads in this process */
1571 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1572 /* create rw lock */
1573 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1574 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1575 close (lsock);
1576 return;
1577 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001578
1579 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001580 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001581 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001582 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001583 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001584 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001585 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1586 }
1587 if (use_notifications == 1) {
1588 notification_handle();
1589 }
1590 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001591 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1592 check_timeout_and_close(server, pool, netconf_sessions_list);
1593 }
Radek Krejci469aab82012-07-22 18:42:20 +02001594
1595 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001596 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001597 if (((unsigned int)tv.tv_sec - olds) > 60) {
1598 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1599 olds = tv.tv_sec;
1600 }
David Kupka8e60a372012-09-04 09:15:20 +02001601 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001602 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1603 apr_sleep(SLEEP_TIME);
1604 continue;
1605 } else if (client == -1 && (errno == EINTR)) {
1606 continue;
1607 } else if (client == -1) {
1608 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1609 continue;
1610 }
Radek Krejci469aab82012-07-22 18:42:20 +02001611
1612 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001613 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001614
David Kupka8e60a372012-09-04 09:15:20 +02001615 arg = malloc (sizeof(struct pass_to_thread));
1616 arg->client = client;
1617 arg->pool = pool;
1618 arg->server = server;
1619 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001620
David Kupka8e60a372012-09-04 09:15:20 +02001621 /* start new thread. It will serve this particular request and then terminate */
1622 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1623 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1624 } else {
1625 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1626 pthread_count++;
1627 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1628 ptids[pthread_count] = 0;
1629 }
Radek Krejci469aab82012-07-22 18:42:20 +02001630
David Kupka8e60a372012-09-04 09:15:20 +02001631 /* check if some thread already terminated, free some resources by joining it */
1632 for (i=0; i<pthread_count; i++) {
1633 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1634 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1635 pthread_count--;
1636 if (pthread_count > 0) {
1637 /* place last Thread ID on the place of joined one */
1638 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001639 }
Radek Krejci469aab82012-07-22 18:42:20 +02001640 }
1641 }
David Kupka8e60a372012-09-04 09:15:20 +02001642 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001643 }
1644
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001645 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001646 /* join all threads */
1647 for (i=0; i<pthread_count; i++) {
1648 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1649 }
1650 free (ptids);
1651
Radek Krejci469aab82012-07-22 18:42:20 +02001652 close(lsock);
1653
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001654 #ifdef WITH_NOTIFICATIONS
1655 notification_close();
1656 #endif
1657
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001658 /* close all NETCONF sessions */
1659 close_all_nc_sessions(server, pool, netconf_sessions_list);
1660
David Kupka8e60a372012-09-04 09:15:20 +02001661 /* destroy rwlock */
1662 pthread_rwlock_destroy(&session_lock);
1663 pthread_rwlockattr_destroy(&lock_attrs);
1664
Radek Krejci469aab82012-07-22 18:42:20 +02001665 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1666
1667 exit(APR_SUCCESS);
1668}
1669
Radek Krejcif23850c2012-07-23 16:14:17 +02001670static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001671{
Radek Krejcif23850c2012-07-23 16:14:17 +02001672 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1673
1674 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1675 apr_pool_create(&config->pool, pool);
1676 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001677 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001678
1679 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001680}
1681
1682static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1683 apr_pool_t * plog, server_rec * s)
1684{
Radek Krejcif23850c2012-07-23 16:14:17 +02001685 mod_netconf_cfg *config;
1686 apr_status_t res;
1687
Radek Krejci469aab82012-07-22 18:42:20 +02001688 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001689 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001690 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001691
1692 /*
1693 * The following checks if this routine has been called before.
1694 * This is necessary because the parent process gets initialized
1695 * a couple of times as the server starts up.
1696 */
1697 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1698 if (!data) {
1699 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1700 return (OK);
1701 }
1702
Radek Krejcif23850c2012-07-23 16:14:17 +02001703 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1704 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001705
Radek Krejcif23850c2012-07-23 16:14:17 +02001706 if (config && config->forkproc == NULL) {
1707 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1708 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001709 switch (res) {
1710 case APR_INCHILD:
1711 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001712 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001713 apr_signal(SIGTERM, signal_handler);
1714
1715 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001716 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001717
1718 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001719 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001720
Radek Krejcif23850c2012-07-23 16:14:17 +02001721 /* I never should be here, wtf?!? */
1722 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001723 exit(APR_EGENERAL);
1724 break;
1725 case APR_INPARENT:
1726 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001727 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001728 break;
1729 default:
1730 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1731 break;
1732 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001733 } else {
1734 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001735 }
1736
1737 return OK;
1738}
1739
Radek Krejci469aab82012-07-22 18:42:20 +02001740/**
1741 * Register module hooks
1742 */
1743static void mod_netconf_register_hooks(apr_pool_t * p)
1744{
Radek Krejcif23850c2012-07-23 16:14:17 +02001745 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001746}
1747
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001748static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1749{
1750 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1751 return NULL;
1752}
1753
1754static const command_rec netconf_cmds[] = {
1755 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1756 {NULL}
1757};
1758
Radek Krejci469aab82012-07-22 18:42:20 +02001759/* Dispatch list for API hooks */
1760module AP_MODULE_DECLARE_DATA netconf_module = {
1761 STANDARD20_MODULE_STUFF,
1762 NULL, /* create per-dir config structures */
1763 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001764 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001765 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001766 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001767 mod_netconf_register_hooks /* register hooks */
1768};
Radek Krejcia332b692012-11-12 16:15:54 +01001769