blob: 0aec5040eedebd7f683da49b39f6854d929936d7 [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;
212 const char *cpbltstr;
213 struct nc_cpblts* cpblts = NULL;
214 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200215 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
216 //json_object_object_del(s->hello_message, NULL);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200217 }
218 s->hello_message = json_object_new_object();
219 if (session != NULL) {
220 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
221 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
222 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
223 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
224 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
225 cpblts = nc_session_get_cpblts (session);
226 if (cpblts != NULL) {
227 json_obj = json_object_new_array();
228 nc_cpblts_iter_start (cpblts);
229 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
230 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
231 }
232 json_object_object_add(s->hello_message, "capabilities", json_obj);
233 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200234 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 +0200235 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200236 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200237 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
238 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
239 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200240 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200241
242}
243
244
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200245/**
246 * \defgroup netconf_operations NETCONF operations
247 * The list of NETCONF operations that mod_netconf supports.
248 * @{
249 */
250
251/**
252 * \brief Connect to NETCONF server
253 *
254 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
255 */
Kupka David00b9c5c2012-09-05 09:45:50 +0200256static 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 +0200257{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200258 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200259 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200260 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200261
262 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200263 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200264 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
265 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200266 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkab3cc64f2013-05-03 19:44:54 +0200267 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done (%p)", session);
David Kupka8e60a372012-09-04 09:15:20 +0200268
Radek Krejci469aab82012-07-22 18:42:20 +0200269 /* if connected successful, add session to the list */
270 if (session != NULL) {
271 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200272 session_key = gen_ncsession_hash(
273 (host==NULL) ? "localhost" : host,
274 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200275 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200276
Tomas Cejkaba21b382013-04-13 02:37:32 +0200277 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200278 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
279 nc_session_free(session);
280 free (locked_session);
281 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
282 return NULL;
283 }
284 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200285 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200286 locked_session->hello_message = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200287 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100288 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200289 /* get exclusive access to sessions_list (conns) */
290 if (pthread_rwlock_wrlock (&session_lock) != 0) {
291 nc_session_free(session);
292 free (locked_session);
293 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
294 return NULL;
295 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200296 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200297 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100298 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200299 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100300 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200301
302 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200303 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200304
David Kupka8e60a372012-09-04 09:15:20 +0200305 /* end of critical section */
306 if (pthread_rwlock_unlock (&session_lock) != 0) {
307 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
308 return NULL;
309 }
Radek Krejci469aab82012-07-22 18:42:20 +0200310 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200311 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200312 } else {
313 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200314 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200315 }
316
Radek Krejci469aab82012-07-22 18:42:20 +0200317}
318
Radek Krejci80c10d92012-07-30 08:38:50 +0200319static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200320{
321 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200322 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200323
Radek Krejcif23850c2012-07-23 16:14:17 +0200324 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200325 /* get exclusive (write) access to sessions_list (conns) */
326 if (pthread_rwlock_wrlock (&session_lock) != 0) {
327 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
328 return EXIT_FAILURE;
329 }
330 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
331 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200332 /** \todo free all notifications from queue */
333 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200334 pthread_mutex_destroy(&locked_session->lock);
335 ns = locked_session->session;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200336 if (locked_session->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200337 //json_object_object_del(locked_session->hello_message, NULL);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200338 }
David Kupka8e60a372012-09-04 09:15:20 +0200339 free (locked_session);
340 }
Radek Krejci469aab82012-07-22 18:42:20 +0200341 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100342 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200343 nc_session_free (ns);
344 ns = NULL;
345
346 /* remove session from the active sessions list */
347 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200348 /* end of critical section */
349 if (pthread_rwlock_unlock (&session_lock) != 0) {
350 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
351 return EXIT_FAILURE;
352 }
Radek Krejci469aab82012-07-22 18:42:20 +0200353 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200354
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200355 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200356 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200357 if (pthread_rwlock_unlock (&session_lock) != 0) {
358 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
359 return EXIT_FAILURE;
360 }
Radek Krejci469aab82012-07-22 18:42:20 +0200361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200362 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200363 }
364}
365
Radek Krejci80c10d92012-07-30 08:38:50 +0200366static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200367{
368 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200369 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200370 nc_reply* reply;
371 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100372 NC_MSG_TYPE msgt;
373 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200374
Radek Krejci8e4632a2012-07-26 13:40:34 +0200375 /* check requests */
376 if (rpc == NULL) {
377 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
378 return (EXIT_FAILURE);
379 }
380
David Kupka8e60a372012-09-04 09:15:20 +0200381 /* get non-exclusive (read) access to sessions_list (conns) */
382 if (pthread_rwlock_rdlock (&session_lock) != 0) {
383 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
384 return EXIT_FAILURE;
385 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200386 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200387 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
388 if (locked_session != NULL) {
389 session = locked_session->session;
390 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200391 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200392 /* get exclusive access to session */
393 if (pthread_mutex_lock(&locked_session->lock) != 0) {
394 /* unlock before returning error */
395 if (pthread_rwlock_unlock (&session_lock) != 0) {
396 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
397 return EXIT_FAILURE;
398 }
399 return EXIT_FAILURE;
400 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200401 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200402 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100403 msgt = nc_session_send_recv(session, rpc, &reply);
404
David Kupka8e60a372012-09-04 09:15:20 +0200405 /* first release exclusive lock for this session */
406 pthread_mutex_unlock(&locked_session->lock);
407 /* end of critical section */
408 if (pthread_rwlock_unlock (&session_lock) != 0) {
409 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
410 return EXIT_FAILURE;
411 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200412
Radek Krejcia332b692012-11-12 16:15:54 +0100413 /* process the result of the operation */
414 switch (msgt) {
415 case NC_MSG_UNKNOWN:
416 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
417 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
418 netconf_close(server, conns, session_key);
419 return (EXIT_FAILURE);
420 }
421 /* no break */
422 case NC_MSG_NONE:
423 /* there is error handled by callback */
424 return (EXIT_FAILURE);
425 break;
426 case NC_MSG_REPLY:
427 switch (replyt = nc_reply_get_type(reply)) {
428 case NC_REPLY_OK:
429 retval = EXIT_SUCCESS;
430 break;
431 default:
432 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
433 retval = EXIT_FAILURE;
434 break;
435 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200436 break;
437 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100438 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200439 retval = EXIT_FAILURE;
440 break;
441 }
442 nc_reply_free(reply);
443 return (retval);
444 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100445 /* release lock on failure */
446 if (pthread_rwlock_unlock (&session_lock) != 0) {
447 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
448 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
450 return (EXIT_FAILURE);
451 }
452}
Radek Krejci80c10d92012-07-30 08:38:50 +0200453
454static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200455{
456 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200457 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100458 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200459 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100460 NC_MSG_TYPE msgt;
461 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200462
463 /* check requests */
464 if (rpc == NULL) {
465 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
466 return (NULL);
467 }
468
David Kupka8e60a372012-09-04 09:15:20 +0200469 /* get non-exclusive (read) access to sessions_list (conns) */
470 if (pthread_rwlock_rdlock (&session_lock) != 0) {
471 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
472 return NULL;
473 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200474 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200475 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
476 if (locked_session != NULL) {
477 session = locked_session->session;
478 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200479 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200480 /* get exclusive access to session */
481 if (pthread_mutex_lock(&locked_session->lock) != 0) {
482 /* unlock before returning error */
483 if (pthread_rwlock_unlock (&session_lock) != 0) {
484 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
485 return NULL;
486 }
487 return NULL;
488 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200489 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100491 msgt = nc_session_send_recv(session, rpc, &reply);
492
David Kupka8e60a372012-09-04 09:15:20 +0200493 /* first release exclusive lock for this session */
494 pthread_mutex_unlock(&locked_session->lock);
495 /* end of critical section */
496 if (pthread_rwlock_unlock (&session_lock) != 0) {
497 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 +0100498 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200499 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200500
Radek Krejcia332b692012-11-12 16:15:54 +0100501 /* process the result of the operation */
502 switch (msgt) {
503 case NC_MSG_UNKNOWN:
504 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
505 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
506 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200507 return (NULL);
508 }
Radek Krejcia332b692012-11-12 16:15:54 +0100509 /* no break */
510 case NC_MSG_NONE:
511 /* there is error handled by callback */
512 return (NULL);
513 break;
514 case NC_MSG_REPLY:
515 switch (replyt = nc_reply_get_type(reply)) {
516 case NC_REPLY_DATA:
517 if ((data = nc_reply_get_data (reply)) == NULL) {
518 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
519 data = NULL;
520 }
521 break;
522 default:
523 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
524 data = NULL;
525 break;
526 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200527 break;
528 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100529 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
530 data = NULL;
531 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200532 }
533 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200534 return (data);
535 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100536 /* release lock on failure */
537 if (pthread_rwlock_unlock (&session_lock) != 0) {
538 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
539 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200540 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
541 return (NULL);
542 }
543}
544
Radek Krejci80c10d92012-07-30 08:38:50 +0200545static 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 +0200546{
547 nc_rpc* rpc;
548 struct nc_filter *f = NULL;
549 char* data = NULL;
550
551 /* create filter if set */
552 if (filter != NULL) {
553 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
554 }
555
556 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100557 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200558 nc_filter_free(f);
559 if (rpc == NULL) {
560 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
561 return (NULL);
562 }
563
564 data = netconf_opdata(server, conns, session_key, rpc);
565 nc_rpc_free (rpc);
566 return (data);
567}
568
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100569static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
570{
571 nc_rpc* rpc;
572 char* data = NULL;
573
574 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100575 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100576 if (rpc == NULL) {
577 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
578 return (NULL);
579 }
580
581 data = netconf_opdata(server, conns, session_key, rpc);
582 nc_rpc_free (rpc);
583 return (data);
584}
585
Radek Krejci80c10d92012-07-30 08:38:50 +0200586static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200587{
588 nc_rpc* rpc;
589 struct nc_filter *f = NULL;
590 char* data = NULL;
591
592 /* create filter if set */
593 if (filter != NULL) {
594 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
595 }
596
597 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100598 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200599 nc_filter_free(f);
600 if (rpc == NULL) {
601 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
602 return (NULL);
603 }
604
605 data = netconf_opdata(server, conns, session_key, rpc);
606 nc_rpc_free (rpc);
607 return (data);
608}
609
Tomas Cejka5064c232013-01-17 09:30:58 +0100610static 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 +0200611{
612 nc_rpc* rpc;
613 int retval = EXIT_SUCCESS;
614
615 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100616 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
617 if (target == NC_DATASTORE_URL) {
618 rpc = nc_rpc_copyconfig(source, target, config, url);
619 } else {
620 rpc = nc_rpc_copyconfig(source, target, config);
621 }
622 } else {
623 if (target == NC_DATASTORE_URL) {
624 rpc = nc_rpc_copyconfig(source, target, url);
625 } else {
626 rpc = nc_rpc_copyconfig(source, target);
627 }
628 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200629 if (rpc == NULL) {
630 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
631 return (EXIT_FAILURE);
632 }
633
634 retval = netconf_op(server, conns, session_key, rpc);
635 nc_rpc_free (rpc);
636 return (retval);
637}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200638
Tomas Cejka5064c232013-01-17 09:30:58 +0100639static 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 +0200640{
641 nc_rpc* rpc;
642 int retval = EXIT_SUCCESS;
643
644 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100645 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
646 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +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}
656
Radek Krejci80c10d92012-07-30 08:38:50 +0200657static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200658{
659 nc_rpc* rpc;
660 int retval = EXIT_SUCCESS;
661
662 /* create requests */
663 rpc = nc_rpc_killsession(sid);
664 if (rpc == NULL) {
665 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
666 return (EXIT_FAILURE);
667 }
668
669 retval = netconf_op(server, conns, session_key, rpc);
670 nc_rpc_free (rpc);
671 return (retval);
672}
673
Radek Krejci80c10d92012-07-30 08:38:50 +0200674static 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 +0200675{
676 nc_rpc* rpc;
677 int retval = EXIT_SUCCESS;
678
679 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200680 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200681 if (rpc == NULL) {
682 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
683 return (EXIT_FAILURE);
684 }
685
686 retval = netconf_op(server, conns, session_key, rpc);
687 nc_rpc_free (rpc);
688 return (retval);
689}
690
Radek Krejci80c10d92012-07-30 08:38:50 +0200691static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200692{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200693 nc_rpc *rpc = NULL;
694 if (target != NC_DATASTORE_URL) {
695 rpc = nc_rpc_deleteconfig(target);
696 } else {
697 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
698 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
699 return (EXIT_FAILURE);
700 }
701
702 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200703}
704
Radek Krejci80c10d92012-07-30 08:38:50 +0200705static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200706{
707 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
708}
709
Radek Krejci80c10d92012-07-30 08:38:50 +0200710static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200711{
712 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
713}
714
Radek Krejci80c10d92012-07-30 08:38:50 +0200715/**
716 * @return REPLY_OK: 0, *data == NULL
717 * REPLY_DATA: 0, *data != NULL
718 * REPLY_ERROR: 1, *data == NULL
719 */
720static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
721{
722 struct nc_session *session = NULL;
723 nc_reply* reply;
724 nc_rpc* rpc;
725 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100726 NC_MSG_TYPE msgt;
727 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200728
729 /* create requests */
730 rpc = nc_rpc_generic(content);
731 if (rpc == NULL) {
732 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
733 return (EXIT_FAILURE);
734 }
735
Radek Krejcia332b692012-11-12 16:15:54 +0100736 if (data != NULL) {
737 *data = NULL;
738 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200739
740 /* get session where send the RPC */
741 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
742 if (session != NULL) {
743 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100744 msgt = nc_session_send_recv(session, rpc, &reply);
745 nc_rpc_free (rpc);
746
747 /* process the result of the operation */
748 switch (msgt) {
749 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200750 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
751 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
752 netconf_close(server, conns, session_key);
753 return (EXIT_FAILURE);
754 }
Radek Krejcia332b692012-11-12 16:15:54 +0100755 /* no break */
756 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200757 /* there is error handled by callback */
758 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200759 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100760 case NC_MSG_REPLY:
761 switch (replyt = nc_reply_get_type(reply)) {
762 case NC_REPLY_DATA:
763 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
764 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
765 nc_reply_free(reply);
766 return (EXIT_FAILURE);
767 }
768 retval = EXIT_SUCCESS;
769 break;
770 case NC_REPLY_OK:
771 retval = EXIT_SUCCESS;
772 break;
773 default:
774 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
775 retval = EXIT_FAILURE;
776 break;
777 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200778 break;
779 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100780 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200781 retval = EXIT_FAILURE;
782 break;
783 }
784 nc_reply_free(reply);
785
786 return (retval);
787 } else {
788 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
789 return (EXIT_FAILURE);
790 }
791}
792
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200793/**
794 * @}
795 *//* netconf_operations */
796
Radek Krejci469aab82012-07-22 18:42:20 +0200797server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200798void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200799{
Radek Krejci7338bde2012-08-10 12:57:30 +0200800 switch (level) {
801 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200802 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200803 break;
804 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200805 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200806 break;
807 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200808 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200809 break;
810 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200811 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200812 break;
813 }
Radek Krejci469aab82012-07-22 18:42:20 +0200814}
815
David Kupka8e60a372012-09-04 09:15:20 +0200816void * thread_routine (void * arg)
817{
818 void * retval = NULL;
819
820 ssize_t buffer_len;
821 struct pollfd fds;
822 int status, buffer_size, ret;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200823 json_object *request, *reply, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200824 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200825 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200826 char* session_key, *data;
827 const char *host, *port, *user, *pass;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200828 const char *msgtext;
David Kupka8e60a372012-09-04 09:15:20 +0200829 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100830 const char *identifier, *version, *format;
Kupka David00b9c5c2012-09-05 09:45:50 +0200831 struct nc_cpblts* cpblts = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200832 struct session_with_mutex * locked_session;
David Kupka8e60a372012-09-04 09:15:20 +0200833 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100834 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200835 NC_EDIT_ERROPT_TYPE erropt_type = 0;
836
837 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
838 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
839 server_rec * server = ((struct pass_to_thread*)arg)->server;
840 int client = ((struct pass_to_thread*)arg)->client;
841
842 char * buffer, chunk_len_str[12], *chunked_msg;
843 char c;
844
845 while (!isterminated) {
846 fds.fd = client;
847 fds.events = POLLIN;
848 fds.revents = 0;
849
850 status = poll(&fds, 1, 1000);
851
852 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
853 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
854 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
855 continue;
856 } else if (status < 0) {
857 /* 0: poll time outed
858 * close socket and ignore this request from the client, it can try it again
859 * -1: poll failed
860 * something wrong happend, close this socket and wait for another request
861 */
862 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
863 close(client);
864 break;
865 }
866 /* status > 0 */
867
868 /* check the status of the socket */
869
870 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
871 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
872 /* close client's socket (it's probably already closed by client */
873 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
874 close(client);
875 break;
876 }
877
878 /* read json in chunked framing */
879 buffer_size = 0;
880 buffer_len = 0;
881 buffer = NULL;
882 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200883 /* read chunk length */
884 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
885 free (buffer);
886 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200887 break;
888 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200889 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
890 free (buffer);
891 buffer = NULL;
892 break;
893 }
894 i=0;
895 memset (chunk_len_str, 0, 12);
896 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
897 if (i==0 && c == '#') {
898 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
899 /* end but invalid */
900 free (buffer);
901 buffer = NULL;
902 }
903 /* end of message, double-loop break */
904 goto msg_complete;
905 }
906 chunk_len_str[i++] = c;
907 }
908 if (c != '\n') {
909 free (buffer);
910 buffer = NULL;
911 break;
912 }
913 if ((chunk_len = atoi (chunk_len_str)) == 0) {
914 free (buffer);
915 buffer = NULL;
916 break;
917 }
918 buffer_size += chunk_len+1;
919 buffer = realloc (buffer, sizeof(char)*buffer_size);
920 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
921 free (buffer);
922 buffer = NULL;
923 break;
924 }
925 buffer_len += ret;
926 }
927msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200928
929 if (buffer != NULL) {
930 request = json_tokener_parse(buffer);
931 operation = json_object_get_int(json_object_object_get(request, "type"));
932
933 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
934 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
935 if (operation != MSG_CONNECT && session_key == NULL) {
936 reply = json_object_new_object();
937 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
938 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
939 msgtext = json_object_to_json_string(reply);
940 send(client, msgtext, strlen(msgtext) + 1, 0);
941 json_object_put(reply);
942 /* there is some stupid client, so close the connection to give a chance to some other client */
943 close(client);
944 break;
945 }
946
947 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100948 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200949 ds_type_t = -1;
950 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
951 if (strcmp(target, "running") == 0) {
952 ds_type_t = NC_DATASTORE_RUNNING;
953 } else if (strcmp(target, "startup") == 0) {
954 ds_type_t = NC_DATASTORE_STARTUP;
955 } else if (strcmp(target, "candidate") == 0) {
956 ds_type_t = NC_DATASTORE_CANDIDATE;
957 }
958 }
959 ds_type_s = -1;
960 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
961 if (strcmp(source, "running") == 0) {
962 ds_type_s = NC_DATASTORE_RUNNING;
963 } else if (strcmp(source, "startup") == 0) {
964 ds_type_s = NC_DATASTORE_STARTUP;
965 } else if (strcmp(source, "candidate") == 0) {
966 ds_type_s = NC_DATASTORE_CANDIDATE;
967 }
968 }
969
970 /* null global JSON error-reply */
971 err_reply = NULL;
972
973 /* prepare reply envelope */
974 reply = json_object_new_object();
975
976 /* process required operation */
977 switch (operation) {
978 case MSG_CONNECT:
979 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
980
981 host = json_object_get_string(json_object_object_get(request, "host"));
982 port = json_object_get_string(json_object_object_get(request, "port"));
983 user = json_object_get_string(json_object_object_get(request, "user"));
984 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200985 capabilities = json_object_object_get(request, "capabilities");
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200986 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
987 cpblts = nc_cpblts_new (NULL);
988 for (i=0; i<len; i++) {
989 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
990 }
991 } else {
992 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
993 }
David Kupka8e60a372012-09-04 09:15:20 +0200994 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
995 if ((host == NULL) || (user == NULL)) {
996 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
997 session_key = NULL;
998 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200999 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
1000 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +02001001 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
1002 }
1003
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001004 /** \todo check if this is neccessary... probably leads to memory leaks */
David Kupka8e60a372012-09-04 09:15:20 +02001005 reply = json_object_new_object();
1006 if (session_key == NULL) {
1007 /* negative reply */
1008 if (err_reply == NULL) {
1009 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1010 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +01001011 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001012 } else {
1013 /* use filled err_reply from libnetconf's callback */
1014 json_object_put(reply);
1015 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +01001016 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +02001017 }
1018 } else {
1019 /* positive reply */
1020 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1021 json_object_object_add(reply, "session", json_object_new_string(session_key));
1022
1023 free(session_key);
1024 }
1025
1026 break;
1027 case MSG_GET:
1028 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1029
1030 filter = json_object_get_string(json_object_object_get(request, "filter"));
1031
1032 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
1033
Tomas Cejkacdc274e2012-09-05 18:15:33 +02001034 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +02001035 if (err_reply == NULL) {
1036 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1037 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
1038 } else {
1039 /* use filled err_reply from libnetconf's callback */
1040 json_object_put(reply);
1041 reply = err_reply;
1042 }
1043 } else {
1044 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1045 json_object_object_add(reply, "data", json_object_new_string(data));
1046 }
1047 break;
1048 case MSG_GETCONFIG:
1049 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1050
1051 filter = json_object_get_string(json_object_object_get(request, "filter"));
1052
1053 if (ds_type_s == -1) {
1054 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1055 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1056 break;
1057 }
1058
1059 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1060 if (err_reply == NULL) {
1061 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1062 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1063 } else {
1064 /* use filled err_reply from libnetconf's callback */
1065 json_object_put(reply);
1066 reply = err_reply;
1067 }
1068 } else {
1069 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1070 json_object_object_add(reply, "data", json_object_new_string(data));
1071 }
1072 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001073 case MSG_GETSCHEMA:
1074 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1075 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1076 if (identifier == 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("No identifier for get-schema supplied."));
1079 break;
1080 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001081 version = json_object_get_string(json_object_object_get(request, "version"));
1082 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001083
Tomas Cejka94da2c52013-01-08 18:20:30 +01001084 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001085 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001086 if (err_reply == NULL) {
1087 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1088 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1089 } else {
1090 /* use filled err_reply from libnetconf's callback */
1091 json_object_put(reply);
1092 reply = err_reply;
1093 }
1094 } else {
1095 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1096 json_object_object_add(reply, "data", json_object_new_string(data));
1097 }
1098 break;
David Kupka8e60a372012-09-04 09:15:20 +02001099 case MSG_EDITCONFIG:
1100 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1101
1102 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1103 if (defop != NULL) {
1104 if (strcmp(defop, "merge") == 0) {
1105 defop_type = NC_EDIT_DEFOP_MERGE;
1106 } else if (strcmp(defop, "replace") == 0) {
1107 defop_type = NC_EDIT_DEFOP_REPLACE;
1108 } else if (strcmp(defop, "none") == 0) {
1109 defop_type = NC_EDIT_DEFOP_NONE;
1110 } else {
1111 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1112 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1113 break;
1114 }
1115 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001116 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001117 }
1118
1119 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1120 if (erropt != NULL) {
1121 if (strcmp(erropt, "continue-on-error") == 0) {
1122 erropt_type = NC_EDIT_ERROPT_CONT;
1123 } else if (strcmp(erropt, "stop-on-error") == 0) {
1124 erropt_type = NC_EDIT_ERROPT_STOP;
1125 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1126 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1127 } else {
1128 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1129 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1130 break;
1131 }
1132 } else {
1133 erropt_type = 0;
1134 }
1135
1136 if (ds_type_t == -1) {
1137 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1138 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1139 break;
1140 }
1141
1142 config = json_object_get_string(json_object_object_get(request, "config"));
1143 if (config == NULL) {
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 config data parameter."));
1146 break;
1147 }
1148
Tomas Cejka5064c232013-01-17 09:30:58 +01001149 /* TODO url capability see netconf_editconfig */
1150 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1151 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 +02001152 if (err_reply == NULL) {
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("edit-config failed."));
1155 } else {
1156 /* use filled err_reply from libnetconf's callback */
1157 json_object_put(reply);
1158 reply = err_reply;
1159 }
1160 } else {
1161 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1162 }
1163 break;
1164 case MSG_COPYCONFIG:
1165 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1166 config = NULL;
1167
1168 if (source == NULL) {
1169 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001170 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001171 config = json_object_get_string(json_object_object_get(request, "config"));
1172 } else if (ds_type_s == -1) {
1173 /* source datastore specified, but it is invalid */
1174 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1175 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1176 break;
1177 }
1178
1179 if (ds_type_t == -1) {
1180 /* invalid target datastore specified */
1181 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1182 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1183 break;
1184 }
1185
1186 if (source == NULL && config == NULL) {
1187 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1188 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1189 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001190 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 +02001191 if (err_reply == NULL) {
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("copy-config failed."));
1194 } else {
1195 /* use filled err_reply from libnetconf's callback */
1196 json_object_put(reply);
1197 reply = err_reply;
1198 }
1199 } else {
1200 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1201 }
1202 }
1203 break;
1204 case MSG_DELETECONFIG:
1205 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1206 /* no break - unifying code */
1207 case MSG_LOCK:
1208 if (operation == MSG_LOCK) {
1209 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1210 }
1211 /* no break - unifying code */
1212 case MSG_UNLOCK:
1213 if (operation == MSG_UNLOCK) {
1214 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1215 }
1216
1217 if (ds_type_t == -1) {
1218 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1219 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1220 break;
1221 }
1222
1223 switch(operation) {
1224 case MSG_DELETECONFIG:
1225 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1226 break;
1227 case MSG_LOCK:
1228 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1229 break;
1230 case MSG_UNLOCK:
1231 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1232 break;
1233 default:
1234 status = -1;
1235 break;
1236 }
1237
1238 if (status != EXIT_SUCCESS) {
1239 if (err_reply == NULL) {
1240 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1241 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1242 } else {
1243 /* use filled err_reply from libnetconf's callback */
1244 json_object_put(reply);
1245 reply = err_reply;
1246 }
1247 } else {
1248 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1249 }
1250 break;
1251 case MSG_KILL:
1252 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1253
1254 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1255
1256 if (sid == NULL) {
1257 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1258 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1259 break;
1260 }
1261
1262 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1263 if (err_reply == NULL) {
1264 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1265 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1266 } else {
1267 /* use filled err_reply from libnetconf's callback */
1268 json_object_put(reply);
1269 reply = err_reply;
1270 }
1271 } else {
1272 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1273 }
1274 break;
1275 case MSG_DISCONNECT:
1276 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1277
1278 if (netconf_close(server, netconf_sessions_list, session_key) != 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("Invalid session identifier."));
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;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001291 case MSG_RELOADHELLO:
David Kupka8e60a372012-09-04 09:15:20 +02001292 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1293
Kupka Davidda134a12012-09-06 14:12:16 +02001294 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 +02001295 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
1296 struct nc_session *temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001297 prepare_status_message(server, locked_session, temp_session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001298 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
David Kupka8e60a372012-09-04 09:15:20 +02001299 } else {
1300 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1301 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001302 break;
David Kupka8e60a372012-09-04 09:15:20 +02001303 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001304 /* do NOT insert "break" here, we want to give new info back */;
1305 case MSG_INFO:
1306 if (operation != MSG_INFO) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +02001307 //json_object_object_del(reply, NULL);
1308 if (locked_session->hello_message != NULL) {
1309 reply = locked_session->hello_message;
1310 } else {
1311 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1312 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1313 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001314 } else {
1315 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001316
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001317 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 +02001318 if (locked_session != NULL) {
1319 if (locked_session->hello_message != NULL) {
1320 reply = locked_session->hello_message;
1321 } 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."));
1324 }
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001325 } 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 }
1329 }
David Kupka8e60a372012-09-04 09:15:20 +02001330 break;
1331 case MSG_GENERIC:
1332 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1333
1334 config = json_object_get_string(json_object_object_get(request, "content"));
1335
1336 if (config == NULL) {
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("Missing content parameter."));
1339 break;
1340 }
1341
1342 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1343 if (err_reply == NULL) {
1344 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1345 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1346 } else {
1347 /* use filled err_reply from libnetconf's callback */
1348 json_object_put(reply);
1349 reply = err_reply;
1350 }
1351 } else {
1352 if (data == NULL) {
1353 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1354 } else {
1355 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1356 json_object_object_add(reply, "data", json_object_new_string(data));
1357 }
1358 }
1359 break;
1360 default:
1361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1362 reply = json_object_new_object();
1363 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1364 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1365 break;
1366 }
1367 json_object_put(request);
1368
David Kupka1e3e4c82012-09-04 09:32:15 +02001369 /* send reply to caller */
1370 if (reply != NULL) {
1371 msgtext = json_object_to_json_string(reply);
1372 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1373 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001374 break;
1375 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001376 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1377 json_object_put(reply);
1378 free (chunked_msg);
1379 free (buffer);
1380 } else {
1381 break;
David Kupka8e60a372012-09-04 09:15:20 +02001382 }
1383 }
1384 }
David Kupka8e60a372012-09-04 09:15:20 +02001385 free (arg);
1386
1387 return retval;
1388}
1389
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001390/**
1391 * \brief Close all open NETCONF sessions.
1392 *
1393 * During termination of mod_netconf, it is useful to close all remaining
1394 * sessions. This function iterates over the list of sessions and close them
1395 * all.
1396 *
1397 * \param[in] server pointer to server_rec for logging
1398 * \param[in] p apr pool needed for hash table iterating
1399 * \param[in] ht hash table of session_with_mutex structs
1400 */
1401static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1402{
1403 apr_hash_index_t *hi;
1404 void *val = NULL;
1405 struct session_with_mutex *swm = NULL;
1406 struct nc_session *ns = NULL;
1407 const char *hashed_key = NULL;
1408 apr_ssize_t hashed_key_length;
1409
1410 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1411 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1412 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1413 swm = (struct session_with_mutex *) val;
1414 if (swm != NULL) {
1415 pthread_mutex_lock(&swm->lock);
1416 if (swm->session != NULL) {
1417 ns = swm->session;
1418 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1419 nc_session_free(ns);
1420 swm->session = NULL;
1421 }
1422 pthread_mutex_unlock(&swm->lock);
1423 }
1424 }
1425}
1426
1427static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1428{
1429 apr_hash_index_t *hi;
1430 void *val = NULL;
1431 struct nc_session *ns = NULL;
1432 struct session_with_mutex *swm = NULL;
1433 const char *hashed_key = NULL;
1434 apr_ssize_t hashed_key_length;
1435 apr_time_t current_time = apr_time_now();
1436
1437 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1438 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1439 swm = (struct session_with_mutex *) val;
1440 if (swm == NULL) {
1441 continue;
1442 }
1443 ns = swm->session;
1444 if (ns == NULL) {
1445 continue;
1446 }
1447 pthread_mutex_lock(&swm->lock);
1448 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1449 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1450 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1451 nc_session_free (ns);
1452 ns = NULL;
1453 /* remove session from the active sessions list */
1454 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1455 }
1456 pthread_mutex_unlock(&swm->lock);
1457 }
1458}
1459
1460
1461/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001462 * This is actually implementation of NETCONF client
1463 * - requests are received from UNIX socket in the predefined format
1464 * - results are replied through the same way
1465 * - the daemon run as a separate process, but it is started and stopped
1466 * automatically by Apache.
1467 *
1468 */
Radek Krejci469aab82012-07-22 18:42:20 +02001469static void forked_proc(apr_pool_t * pool, server_rec * server)
1470{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001471 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001472 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001473 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001474 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001475 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001476 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001477 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001478 struct pass_to_thread * arg;
1479 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1480 struct timespec maxtime;
1481 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001482 #ifdef WITH_NOTIFICATIONS
1483 char use_notifications = 0;
1484 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001485
Tomas Cejka404d37e2013-04-13 02:31:35 +02001486 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001487 maxtime.tv_sec = 5;
1488 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001489
Radek Krejcif23850c2012-07-23 16:14:17 +02001490 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001491 unixd_setup_child();
1492
Radek Krejciae021c12012-07-25 18:03:52 +02001493 cfg = ap_get_module_config(server->module_config, &netconf_module);
1494 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001495 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1496 return;
1497 }
Radek Krejci469aab82012-07-22 18:42:20 +02001498
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001499 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001500 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1501 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1502 return;
1503 }
1504
1505 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001506 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001507 unlink(local.sun_path);
1508 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1509
1510 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1511 if (errno == EADDRINUSE) {
1512 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1513 close(lsock);
1514 exit(0);
1515 }
1516 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1517 close(lsock);
1518 return;
1519 }
1520
1521 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1522 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1523 close(lsock);
1524 return;
1525 }
1526
Tomas Cejkaba21b382013-04-13 02:37:32 +02001527 /* prepare internal lists */
1528 netconf_sessions_list = apr_hash_make(pool);
1529
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001530 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001531 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1533 use_notifications = 0;
1534 } else {
1535 use_notifications = 1;
1536 }
1537 #endif
1538
Radek Krejci469aab82012-07-22 18:42:20 +02001539 /* setup libnetconf's callbacks */
1540 nc_verbosity(NC_VERB_DEBUG);
1541 clb_print_server = server;
1542 nc_callback_print(clb_print);
1543 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1544 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1545 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001546 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001547
1548 /* disable publickey authentication */
1549 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1550
Tomas Cejka44e71db2013-04-21 15:50:47 +02001551 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1552
David Kupka8e60a372012-09-04 09:15:20 +02001553 /* create mutex protecting session list */
1554 pthread_rwlockattr_init(&lock_attrs);
1555 /* rwlock is shared only with threads in this process */
1556 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1557 /* create rw lock */
1558 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1559 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1560 close (lsock);
1561 return;
1562 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001563
1564 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001565 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001566 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001567 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001568 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001569 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001570 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1571 }
1572 if (use_notifications == 1) {
1573 notification_handle();
1574 }
1575 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001576 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1577 check_timeout_and_close(server, pool, netconf_sessions_list);
1578 }
Radek Krejci469aab82012-07-22 18:42:20 +02001579
1580 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001581 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001582 if (((unsigned int)tv.tv_sec - olds) > 60) {
1583 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1584 olds = tv.tv_sec;
1585 }
David Kupka8e60a372012-09-04 09:15:20 +02001586 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001587 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1588 apr_sleep(SLEEP_TIME);
1589 continue;
1590 } else if (client == -1 && (errno == EINTR)) {
1591 continue;
1592 } else if (client == -1) {
1593 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1594 continue;
1595 }
Radek Krejci469aab82012-07-22 18:42:20 +02001596
1597 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001598 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001599
David Kupka8e60a372012-09-04 09:15:20 +02001600 arg = malloc (sizeof(struct pass_to_thread));
1601 arg->client = client;
1602 arg->pool = pool;
1603 arg->server = server;
1604 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001605
David Kupka8e60a372012-09-04 09:15:20 +02001606 /* start new thread. It will serve this particular request and then terminate */
1607 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1608 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1609 } else {
1610 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1611 pthread_count++;
1612 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1613 ptids[pthread_count] = 0;
1614 }
Radek Krejci469aab82012-07-22 18:42:20 +02001615
David Kupka8e60a372012-09-04 09:15:20 +02001616 /* check if some thread already terminated, free some resources by joining it */
1617 for (i=0; i<pthread_count; i++) {
1618 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1619 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1620 pthread_count--;
1621 if (pthread_count > 0) {
1622 /* place last Thread ID on the place of joined one */
1623 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001624 }
Radek Krejci469aab82012-07-22 18:42:20 +02001625 }
1626 }
David Kupka8e60a372012-09-04 09:15:20 +02001627 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001628 }
1629
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001630 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001631 /* join all threads */
1632 for (i=0; i<pthread_count; i++) {
1633 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1634 }
1635 free (ptids);
1636
Radek Krejci469aab82012-07-22 18:42:20 +02001637 close(lsock);
1638
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001639 #ifdef WITH_NOTIFICATIONS
1640 notification_close();
1641 #endif
1642
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001643 /* close all NETCONF sessions */
1644 close_all_nc_sessions(server, pool, netconf_sessions_list);
1645
David Kupka8e60a372012-09-04 09:15:20 +02001646 /* destroy rwlock */
1647 pthread_rwlock_destroy(&session_lock);
1648 pthread_rwlockattr_destroy(&lock_attrs);
1649
Radek Krejci469aab82012-07-22 18:42:20 +02001650 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1651
1652 exit(APR_SUCCESS);
1653}
1654
Radek Krejcif23850c2012-07-23 16:14:17 +02001655static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001656{
Radek Krejcif23850c2012-07-23 16:14:17 +02001657 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1658
1659 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1660 apr_pool_create(&config->pool, pool);
1661 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001662 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001663
1664 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001665}
1666
1667static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1668 apr_pool_t * plog, server_rec * s)
1669{
Radek Krejcif23850c2012-07-23 16:14:17 +02001670 mod_netconf_cfg *config;
1671 apr_status_t res;
1672
Radek Krejci469aab82012-07-22 18:42:20 +02001673 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001674 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001675 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001676
1677 /*
1678 * The following checks if this routine has been called before.
1679 * This is necessary because the parent process gets initialized
1680 * a couple of times as the server starts up.
1681 */
1682 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1683 if (!data) {
1684 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1685 return (OK);
1686 }
1687
Radek Krejcif23850c2012-07-23 16:14:17 +02001688 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1689 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001690
Radek Krejcif23850c2012-07-23 16:14:17 +02001691 if (config && config->forkproc == NULL) {
1692 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1693 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001694 switch (res) {
1695 case APR_INCHILD:
1696 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001697 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001698 apr_signal(SIGTERM, signal_handler);
1699
1700 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001701 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001702
1703 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001704 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001705
Radek Krejcif23850c2012-07-23 16:14:17 +02001706 /* I never should be here, wtf?!? */
1707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001708 exit(APR_EGENERAL);
1709 break;
1710 case APR_INPARENT:
1711 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001712 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001713 break;
1714 default:
1715 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1716 break;
1717 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001718 } else {
1719 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001720 }
1721
1722 return OK;
1723}
1724
Radek Krejci469aab82012-07-22 18:42:20 +02001725/**
1726 * Register module hooks
1727 */
1728static void mod_netconf_register_hooks(apr_pool_t * p)
1729{
Radek Krejcif23850c2012-07-23 16:14:17 +02001730 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001731}
1732
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001733static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1734{
1735 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1736 return NULL;
1737}
1738
1739static const command_rec netconf_cmds[] = {
1740 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1741 {NULL}
1742};
1743
Radek Krejci469aab82012-07-22 18:42:20 +02001744/* Dispatch list for API hooks */
1745module AP_MODULE_DECLARE_DATA netconf_module = {
1746 STANDARD20_MODULE_STUFF,
1747 NULL, /* create per-dir config structures */
1748 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001749 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001750 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001751 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001752 mod_netconf_register_hooks /* register hooks */
1753};
Radek Krejcia332b692012-11-12 16:15:54 +01001754