blob: b1c8555229a34b42cbde3013859be5204d2beabe [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
Tomas Cejka47387fd2013-06-10 20:37:46 +0200109pthread_rwlock_t session_lock; /**< mutex protecting netconf_sessions_list from multiple access errors */
110apr_hash_t *netconf_sessions_list = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200111
Radek Krejci469aab82012-07-22 18:42:20 +0200112volatile int isterminated = 0;
113
114static char* password;
115
Radek Krejci469aab82012-07-22 18:42:20 +0200116static void signal_handler(int sign)
117{
118 switch (sign) {
119 case SIGTERM:
120 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200121 break;
122 }
123}
124
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200125static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200126{
Radek Krejcif23850c2012-07-23 16:14:17 +0200127 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
128 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200129 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200130
Radek Krejci469aab82012-07-22 18:42:20 +0200131 apr_sha1_ctx_t sha1_ctx;
132 apr_sha1_init(&sha1_ctx);
133 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
134 apr_sha1_update(&sha1_ctx, port, strlen(port));
135 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200136 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200137
Radek Krejcif23850c2012-07-23 16:14:17 +0200138 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200139 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200140 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200141 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 }
143 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200144
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200146}
147
148int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
149{
150 /* always approve */
151 return (EXIT_SUCCESS);
152}
153
154char* netconf_callback_sshauth_password (const char* username, const char* hostname)
155{
156 char* buf;
157
158 buf = malloc ((strlen(password) + 1) * sizeof(char));
159 apr_cpystrn(buf, password, strlen(password) + 1);
160
161 return (buf);
162}
163
164void netconf_callback_sshauth_interactive (const char* name,
165 int name_len,
166 const char* instruction,
167 int instruction_len,
168 int num_prompts,
169 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
170 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
171 void** abstract)
172{
173 int i;
174
175 for (i=0; i<num_prompts; i++) {
176 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
177 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
178 responses[i].length = strlen(responses[i].text) + 1;
179 }
180
181 return;
182}
183
Radek Krejcic11fd862012-07-26 12:41:21 +0200184static json_object *err_reply = NULL;
185void netconf_callback_error_process(const char* tag,
186 const char* type,
187 const char* severity,
188 const char* apptag,
189 const char* path,
190 const char* message,
191 const char* attribute,
192 const char* element,
193 const char* ns,
194 const char* sid)
195{
196 err_reply = json_object_new_object();
197 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
198 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
199 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
200 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
201 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
202 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
203 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
204 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
205 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
206 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
207 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
208}
209
Tomas Cejka47387fd2013-06-10 20:37:46 +0200210/**
211 * should be used in locked area
212 */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200213void prepare_status_message(server_rec* server, struct session_with_mutex *s, struct nc_session *session)
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200214{
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200215 json_object *json_obj = NULL;
Tomas Cejka73286932013-05-27 22:54:35 +0200216 //json_object *old_sid = NULL;
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200217 const char *cpbltstr;
218 struct nc_cpblts* cpblts = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200219
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200220 if (s == NULL) {
221 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "No session given.");
222 return;
223 }
224
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200225 if (s->hello_message != NULL) {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200226 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "clean previous hello message");
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200227 //json_object_put(s->hello_message);
228 s->hello_message = NULL;
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200229
230 //old_sid = json_object_object_get(s->hello_message, "sid");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200231 }
232 s->hello_message = json_object_new_object();
233 if (session != NULL) {
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200234 /** \todo reload hello - save old sid */
235 //if (old_sid != NULL) {
236 // /* use previous sid */
237 // json_object_object_add(s->hello_message, "sid", old_sid);
238 //} else {
239 /* we don't have old sid */
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200240 json_object_object_add(s->hello_message, "sid", json_object_new_string(nc_session_get_id(session)));
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200241 //}
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200242 json_object_object_add(s->hello_message, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
243 json_object_object_add(s->hello_message, "host", json_object_new_string(nc_session_get_host(session)));
244 json_object_object_add(s->hello_message, "port", json_object_new_string(nc_session_get_port(session)));
245 json_object_object_add(s->hello_message, "user", json_object_new_string(nc_session_get_user(session)));
246 cpblts = nc_session_get_cpblts (session);
247 if (cpblts != NULL) {
248 json_obj = json_object_new_array();
249 nc_cpblts_iter_start (cpblts);
250 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
251 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
252 }
253 json_object_object_add(s->hello_message, "capabilities", json_obj);
254 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200255 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 +0200256 } else {
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200257 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Session was not given.");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200258 json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
259 json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
260 }
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200261 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Status info from hello message prepared");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200262
263}
264
265
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200266/**
267 * \defgroup netconf_operations NETCONF operations
268 * The list of NETCONF operations that mod_netconf supports.
269 * @{
270 */
271
272/**
273 * \brief Connect to NETCONF server
274 *
275 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
276 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200277static char* netconf_connect(server_rec* server, apr_pool_t* pool, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200278{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200279 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200280 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200281 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200282
283 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200284 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200285 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
286 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200287 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkaf38a54c2013-05-27 21:57:35 +0200288 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done");
David Kupka8e60a372012-09-04 09:15:20 +0200289
Radek Krejci469aab82012-07-22 18:42:20 +0200290 /* if connected successful, add session to the list */
291 if (session != NULL) {
292 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200293 session_key = gen_ncsession_hash(
294 (host==NULL) ? "localhost" : host,
295 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200296 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200297
Tomas Cejkaba21b382013-04-13 02:37:32 +0200298 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200299 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
300 nc_session_free(session);
301 free (locked_session);
302 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
303 return NULL;
304 }
305 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200306 locked_session->last_activity = apr_time_now();
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200307 locked_session->hello_message = NULL;
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200308 locked_session->closed = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200309 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100310 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200311 /* get exclusive access to sessions_list (conns) */
312 if (pthread_rwlock_wrlock (&session_lock) != 0) {
313 nc_session_free(session);
314 free (locked_session);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200315 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200316 return NULL;
317 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200318 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200319 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100320 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200321 apr_hash_set(netconf_sessions_list, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100322 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200323
Tomas Cejka47387fd2013-06-10 20:37:46 +0200324 /* lock session */
325 pthread_mutex_lock(&locked_session->lock);
326
327 /* unlock session list */
328 if (pthread_rwlock_unlock (&session_lock) != 0) {
329 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
330 }
331
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200332 /* store information about session from hello message for future usage */
Tomas Cejkaa72dfdb2013-05-15 13:26:21 +0200333 prepare_status_message(server, locked_session, session);
Tomas Cejka45ab59f2013-05-15 00:10:49 +0200334
Tomas Cejka47387fd2013-06-10 20:37:46 +0200335 pthread_mutex_unlock(&locked_session->lock);
336
Radek Krejci469aab82012-07-22 18:42:20 +0200337 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200338 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200339 } else {
340 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200341 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200342 }
343
Radek Krejci469aab82012-07-22 18:42:20 +0200344}
345
Tomas Cejka47387fd2013-06-10 20:37:46 +0200346static int close_and_free_session(server_rec *server, struct session_with_mutex *locked_session)
Radek Krejci469aab82012-07-22 18:42:20 +0200347{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200348 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock private lock.");
349 if (pthread_mutex_lock(&locked_session->lock) != 0) {
350 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
351 }
352 locked_session->ntfc_subscribed = 0;
353 locked_session->closed = 1;
354 nc_session_close(locked_session->session, NC_SESSION_TERM_CLOSED);
355 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "session closed.");
356 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock private lock.");
357 if (pthread_mutex_unlock(&locked_session->lock) != 0) {
358 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
359 }
360
361 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "unlock session lock.");
362 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "closed session, disabled notif(?), wait 2s");
363 usleep(500000); /* let notification thread stop */
364
365 /* session shouldn't be used by now */
366 /** \todo free all notifications from queue */
367 apr_array_clear(locked_session->notifications);
368 pthread_mutex_destroy(&locked_session->lock);
369 if (locked_session->hello_message != NULL) {
370 /** \todo free hello_message */
371 //json_object_put(locked_session->hello_message);
372 locked_session->hello_message = NULL;
373 }
374 nc_session_free(locked_session->session);
375 locked_session->session = NULL;
376 free (locked_session);
377 locked_session = NULL;
378 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed, everything cleared.");
379 return (EXIT_SUCCESS);
380}
381
382static int netconf_close(server_rec* server, const char* session_key)
383{
David Kupka8e60a372012-09-04 09:15:20 +0200384 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200385
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200386 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to close: %s", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200387
David Kupka8e60a372012-09-04 09:15:20 +0200388 /* get exclusive (write) access to sessions_list (conns) */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200389 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "lock session lock.");
David Kupka8e60a372012-09-04 09:15:20 +0200390 if (pthread_rwlock_wrlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200391 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock");
David Kupka8e60a372012-09-04 09:15:20 +0200392 return EXIT_FAILURE;
393 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200394 /* get session to close */
395 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
396 /* remove session from the active sessions list -> nobody new can now work with session */
397 apr_hash_set(netconf_sessions_list, session_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200398
Tomas Cejka47387fd2013-06-10 20:37:46 +0200399 if (pthread_rwlock_unlock (&session_lock) != 0) {
400 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock");
401 }
402
403 if ((locked_session != NULL) && (locked_session->session != NULL)) {
404 return close_and_free_session(server, locked_session);
Radek Krejci469aab82012-07-22 18:42:20 +0200405 } else {
406 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200407 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200408 }
409}
410
Tomas Cejka47387fd2013-06-10 20:37:46 +0200411int netconf_op(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200412{
413 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200414 struct session_with_mutex * locked_session;
Tomas Cejka00635972013-06-03 15:10:52 +0200415 nc_reply* reply = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200416 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100417 NC_MSG_TYPE msgt;
418 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200419
Radek Krejci8e4632a2012-07-26 13:40:34 +0200420 /* check requests */
421 if (rpc == NULL) {
422 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
423 return (EXIT_FAILURE);
424 }
425
David Kupka8e60a372012-09-04 09:15:20 +0200426 /* get non-exclusive (read) access to sessions_list (conns) */
427 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200428 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200429 return EXIT_FAILURE;
430 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200431 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200432 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
David Kupka8e60a372012-09-04 09:15:20 +0200433 if (locked_session != NULL) {
434 session = locked_session->session;
435 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200436 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200437 /* get exclusive access to session */
438 if (pthread_mutex_lock(&locked_session->lock) != 0) {
439 /* unlock before returning error */
440 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200441 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200442 return EXIT_FAILURE;
443 }
444 return EXIT_FAILURE;
445 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200446 if (pthread_rwlock_unlock(&session_lock) != 0) {
447 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
448 }
449
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200450 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200451 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100452 msgt = nc_session_send_recv(session, rpc, &reply);
453
David Kupka8e60a372012-09-04 09:15:20 +0200454 /* first release exclusive lock for this session */
455 pthread_mutex_unlock(&locked_session->lock);
456 /* end of critical section */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200457
Radek Krejcia332b692012-11-12 16:15:54 +0100458 /* process the result of the operation */
459 switch (msgt) {
460 case NC_MSG_UNKNOWN:
461 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
462 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200463 netconf_close(server, session_key);
Radek Krejcia332b692012-11-12 16:15:54 +0100464 return (EXIT_FAILURE);
465 }
466 /* no break */
467 case NC_MSG_NONE:
468 /* there is error handled by callback */
469 return (EXIT_FAILURE);
470 break;
471 case NC_MSG_REPLY:
472 switch (replyt = nc_reply_get_type(reply)) {
473 case NC_REPLY_OK:
474 retval = EXIT_SUCCESS;
475 break;
476 default:
477 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
478 retval = EXIT_FAILURE;
479 break;
480 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200481 break;
482 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100483 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200484 retval = EXIT_FAILURE;
485 break;
486 }
487 nc_reply_free(reply);
488 return (retval);
489 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100490 /* release lock on failure */
491 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200492 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100493 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200494 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
495 return (EXIT_FAILURE);
496 }
497}
Radek Krejci80c10d92012-07-30 08:38:50 +0200498
Tomas Cejka47387fd2013-06-10 20:37:46 +0200499static char* netconf_opdata(server_rec* server, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200500{
501 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200502 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100503 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200504 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100505 NC_MSG_TYPE msgt;
506 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200507
508 /* check requests */
509 if (rpc == NULL) {
510 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
511 return (NULL);
512 }
513
David Kupka8e60a372012-09-04 09:15:20 +0200514 /* get non-exclusive (read) access to sessions_list (conns) */
515 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200516 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200517 return NULL;
518 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200519 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200520 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
David Kupka8e60a372012-09-04 09:15:20 +0200521 if (locked_session != NULL) {
522 session = locked_session->session;
523 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200524 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200525 /* get exclusive access to session */
526 if (pthread_mutex_lock(&locked_session->lock) != 0) {
527 /* unlock before returning error */
528 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200529 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
David Kupka8e60a372012-09-04 09:15:20 +0200530 }
531 return NULL;
532 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200533 if (pthread_rwlock_unlock (&session_lock) != 0) {
534 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
535 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200536 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200537 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100538 msgt = nc_session_send_recv(session, rpc, &reply);
539
David Kupka8e60a372012-09-04 09:15:20 +0200540 /* first release exclusive lock for this session */
541 pthread_mutex_unlock(&locked_session->lock);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200542
Radek Krejcia332b692012-11-12 16:15:54 +0100543 /* process the result of the operation */
544 switch (msgt) {
545 case NC_MSG_UNKNOWN:
546 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
547 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200548 netconf_close(server, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200549 return (NULL);
550 }
Radek Krejcia332b692012-11-12 16:15:54 +0100551 /* no break */
552 case NC_MSG_NONE:
553 /* there is error handled by callback */
554 return (NULL);
555 break;
556 case NC_MSG_REPLY:
557 switch (replyt = nc_reply_get_type(reply)) {
558 case NC_REPLY_DATA:
559 if ((data = nc_reply_get_data (reply)) == NULL) {
560 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
561 data = NULL;
562 }
563 break;
564 default:
565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
566 data = NULL;
567 break;
568 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200569 break;
570 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100571 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
572 data = NULL;
573 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200574 }
575 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200576 return (data);
577 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100578 /* release lock on failure */
579 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200580 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100581 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200582 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
583 return (NULL);
584 }
585}
586
Tomas Cejka47387fd2013-06-10 20:37:46 +0200587static char* netconf_getconfig(server_rec* server, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200588{
589 nc_rpc* rpc;
590 struct nc_filter *f = NULL;
591 char* data = NULL;
592
593 /* create filter if set */
594 if (filter != NULL) {
595 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
596 }
597
598 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100599 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200600 nc_filter_free(f);
601 if (rpc == NULL) {
602 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
603 return (NULL);
604 }
605
Tomas Cejka47387fd2013-06-10 20:37:46 +0200606 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200607 nc_rpc_free (rpc);
608 return (data);
609}
610
Tomas Cejka47387fd2013-06-10 20:37:46 +0200611static char* netconf_getschema(server_rec* server, const char* session_key, const char* identifier, const char* version, const char* format)
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100612{
613 nc_rpc* rpc;
614 char* data = NULL;
615
616 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100617 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100618 if (rpc == NULL) {
619 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
620 return (NULL);
621 }
622
Tomas Cejka47387fd2013-06-10 20:37:46 +0200623 data = netconf_opdata(server, session_key, rpc);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100624 nc_rpc_free (rpc);
625 return (data);
626}
627
Tomas Cejka47387fd2013-06-10 20:37:46 +0200628static char* netconf_get(server_rec* server, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200629{
630 nc_rpc* rpc;
631 struct nc_filter *f = NULL;
632 char* data = NULL;
633
634 /* create filter if set */
635 if (filter != NULL) {
636 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
637 }
638
639 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100640 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200641 nc_filter_free(f);
642 if (rpc == NULL) {
643 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
644 return (NULL);
645 }
646
Tomas Cejka47387fd2013-06-10 20:37:46 +0200647 data = netconf_opdata(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200648 nc_rpc_free (rpc);
649 return (data);
650}
651
Tomas Cejka47387fd2013-06-10 20:37:46 +0200652static int netconf_copyconfig(server_rec* server, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *url)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200653{
654 nc_rpc* rpc;
655 int retval = EXIT_SUCCESS;
656
657 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100658 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
659 if (target == NC_DATASTORE_URL) {
660 rpc = nc_rpc_copyconfig(source, target, config, url);
661 } else {
662 rpc = nc_rpc_copyconfig(source, target, config);
663 }
664 } else {
665 if (target == NC_DATASTORE_URL) {
666 rpc = nc_rpc_copyconfig(source, target, url);
667 } else {
668 rpc = nc_rpc_copyconfig(source, target);
669 }
670 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200671 if (rpc == NULL) {
672 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
673 return (EXIT_FAILURE);
674 }
675
Tomas Cejka47387fd2013-06-10 20:37:46 +0200676 retval = netconf_op(server, session_key, rpc);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200677 nc_rpc_free (rpc);
678 return (retval);
679}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200680
Tomas Cejka47387fd2013-06-10 20:37:46 +0200681static int netconf_editconfig(server_rec* server, 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 +0200682{
683 nc_rpc* rpc;
684 int retval = EXIT_SUCCESS;
685
686 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100687 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
688 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200689 if (rpc == NULL) {
690 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
691 return (EXIT_FAILURE);
692 }
693
Tomas Cejka47387fd2013-06-10 20:37:46 +0200694 retval = netconf_op(server, session_key, rpc);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200695 nc_rpc_free (rpc);
696 return (retval);
697}
698
Tomas Cejka47387fd2013-06-10 20:37:46 +0200699static int netconf_killsession(server_rec* server, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200700{
701 nc_rpc* rpc;
702 int retval = EXIT_SUCCESS;
703
704 /* create requests */
705 rpc = nc_rpc_killsession(sid);
706 if (rpc == NULL) {
707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
708 return (EXIT_FAILURE);
709 }
710
Tomas Cejka47387fd2013-06-10 20:37:46 +0200711 retval = netconf_op(server, session_key, rpc);
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200712 nc_rpc_free (rpc);
713 return (retval);
714}
715
Tomas Cejka47387fd2013-06-10 20:37:46 +0200716static int netconf_onlytargetop(server_rec* server, const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200717{
718 nc_rpc* rpc;
719 int retval = EXIT_SUCCESS;
720
721 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200722 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200723 if (rpc == NULL) {
724 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
725 return (EXIT_FAILURE);
726 }
727
Tomas Cejka47387fd2013-06-10 20:37:46 +0200728 retval = netconf_op(server, session_key, rpc);
Radek Krejci2f318372012-07-26 14:22:35 +0200729 nc_rpc_free (rpc);
730 return (retval);
731}
732
Tomas Cejka47387fd2013-06-10 20:37:46 +0200733static int netconf_deleteconfig(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200734{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200735 nc_rpc *rpc = NULL;
736 if (target != NC_DATASTORE_URL) {
737 rpc = nc_rpc_deleteconfig(target);
738 } else {
739 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
740 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
741 return (EXIT_FAILURE);
742 }
743
Tomas Cejka47387fd2013-06-10 20:37:46 +0200744 return netconf_op(server, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200745}
746
Tomas Cejka47387fd2013-06-10 20:37:46 +0200747static int netconf_lock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200748{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200749 return (netconf_onlytargetop(server, session_key, target, nc_rpc_lock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200750}
751
Tomas Cejka47387fd2013-06-10 20:37:46 +0200752static int netconf_unlock(server_rec* server, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200753{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200754 return (netconf_onlytargetop(server, session_key, target, nc_rpc_unlock));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200755}
756
Radek Krejci80c10d92012-07-30 08:38:50 +0200757/**
758 * @return REPLY_OK: 0, *data == NULL
759 * REPLY_DATA: 0, *data != NULL
760 * REPLY_ERROR: 1, *data == NULL
761 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200762static int netconf_generic(server_rec* server, const char* session_key, const char* content, char** data)
Radek Krejci80c10d92012-07-30 08:38:50 +0200763{
Tomas Cejka47387fd2013-06-10 20:37:46 +0200764 struct session_with_mutex *session = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +0200765 nc_reply* reply = NULL;
766 nc_rpc* rpc = NULL;
Radek Krejci80c10d92012-07-30 08:38:50 +0200767 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100768 NC_MSG_TYPE msgt;
769 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200770
771 /* create requests */
772 rpc = nc_rpc_generic(content);
773 if (rpc == NULL) {
774 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
775 return (EXIT_FAILURE);
776 }
777
Radek Krejcia332b692012-11-12 16:15:54 +0100778 if (data != NULL) {
779 *data = NULL;
780 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200781
Tomas Cejka47387fd2013-06-10 20:37:46 +0200782 if (pthread_rwlock_rdlock(&session_lock) != 0) {
783 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
784 return EXIT_FAILURE;
785 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200786 /* get session where send the RPC */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200787 session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Radek Krejci80c10d92012-07-30 08:38:50 +0200788 if (session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200789 pthread_mutex_lock(&session->lock);
790 if (pthread_rwlock_unlock(&session_lock) != 0) {
791 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
792 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200793 /* send the request and get the reply */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200794 msgt = nc_session_send_recv(session->session, rpc, &reply);
795 nc_rpc_free(rpc);
796 pthread_mutex_unlock(&session->lock);
Radek Krejcia332b692012-11-12 16:15:54 +0100797
798 /* process the result of the operation */
799 switch (msgt) {
800 case NC_MSG_UNKNOWN:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200801 if (nc_session_get_status(session->session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci80c10d92012-07-30 08:38:50 +0200802 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200803 netconf_close(server, session_key);
Radek Krejci80c10d92012-07-30 08:38:50 +0200804 return (EXIT_FAILURE);
805 }
Radek Krejcia332b692012-11-12 16:15:54 +0100806 /* no break */
807 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200808 /* there is error handled by callback */
809 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200810 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100811 case NC_MSG_REPLY:
812 switch (replyt = nc_reply_get_type(reply)) {
813 case NC_REPLY_DATA:
814 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
815 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
816 nc_reply_free(reply);
817 return (EXIT_FAILURE);
818 }
819 retval = EXIT_SUCCESS;
820 break;
821 case NC_REPLY_OK:
822 retval = EXIT_SUCCESS;
823 break;
824 default:
825 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
826 retval = EXIT_FAILURE;
827 break;
828 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200829 break;
830 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100831 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200832 retval = EXIT_FAILURE;
833 break;
834 }
835 nc_reply_free(reply);
836
837 return (retval);
838 } else {
839 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200840 if (pthread_rwlock_unlock(&session_lock) != 0) {
841 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
842 return EXIT_FAILURE;
843 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200844 return (EXIT_FAILURE);
845 }
846}
847
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200848/**
849 * @}
850 *//* netconf_operations */
851
Radek Krejci469aab82012-07-22 18:42:20 +0200852server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200853void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200854{
Radek Krejci7338bde2012-08-10 12:57:30 +0200855 switch (level) {
856 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200857 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200858 break;
859 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200860 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200861 break;
862 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200863 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200864 break;
865 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200866 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200867 break;
868 }
Radek Krejci469aab82012-07-22 18:42:20 +0200869}
870
Tomas Cejka64b87482013-06-03 16:30:53 +0200871/**
872 * Receive message from client and return pointer to it.
873 * Caller should free message memory.
874 * \param[in] client socket descriptor of client
875 * \param[in] server httpd server for logging
876 * \return pointer to message
877 */
878char *get_framed_message(server_rec *server, int client)
879{
880 /* read json in chunked framing */
881 unsigned int buffer_size = 0;
882 ssize_t buffer_len = 0;
883 char *buffer = NULL;
884 char c;
885 ssize_t ret;
886 int i, chunk_len;
887 char chunk_len_str[12];
888
889 while (1) {
890 /* read chunk length */
891 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
892 if (buffer != NULL) {
893 free (buffer);
894 buffer = NULL;
895 }
896 break;
897 }
898 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
899 if (buffer != NULL) {
900 free (buffer);
901 buffer = NULL;
902 }
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 if (buffer != NULL) {
912 free (buffer);
913 buffer = NULL;
914 }
915 }
916 /* end of message, double-loop break */
917 goto msg_complete;
918 }
919 chunk_len_str[i++] = c;
920 if (i==11) {
921 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Message is too long, buffer for length is not big enought!!!!");
922 break;
923 }
924 }
925 if (c != '\n') {
926 if (buffer != NULL) {
927 free (buffer);
928 buffer = NULL;
929 }
930 break;
931 }
932 chunk_len_str[i] = 0;
933 if ((chunk_len = atoi (chunk_len_str)) == 0) {
934 if (buffer != NULL) {
935 free (buffer);
936 buffer = NULL;
937 }
938 break;
939 }
940 buffer_size += chunk_len+1;
941 buffer = realloc (buffer, sizeof(char)*buffer_size);
942 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
943 if (buffer != NULL) {
944 free (buffer);
945 buffer = NULL;
946 }
947 break;
948 }
949 buffer_len += ret;
950 }
951msg_complete:
952 return buffer;
953}
954
Tomas Cejkad5b53772013-06-08 23:01:07 +0200955NC_DATASTORE parse_datastore(const char *ds)
956{
957 if (strcmp(ds, "running") == 0) {
958 return NC_DATASTORE_RUNNING;
959 } else if (strcmp(ds, "startup") == 0) {
960 return NC_DATASTORE_STARTUP;
961 } else if (strcmp(ds, "candidate") == 0) {
962 return NC_DATASTORE_CANDIDATE;
963 }
964 return -1;
965}
966
967json_object *create_error(const char *errmess)
968{
969 json_object *reply = json_object_new_object();
970 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
971 json_object_object_add(reply, "error-message", json_object_new_string(errmess));
972 return reply;
973
974}
975
976json_object *create_data(const char *data)
977{
978 json_object *reply = json_object_new_object();
979 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
980 json_object_object_add(reply, "data", json_object_new_string(data));
981 return reply;
982}
983
Tomas Cejka47387fd2013-06-10 20:37:46 +0200984json_object *handle_op_connect(server_rec *server, apr_pool_t *pool, json_object *request)
Tomas Cejkad5b53772013-06-08 23:01:07 +0200985{
986 const char *host = NULL;
987 const char *port = NULL;
988 const char *user = NULL;
989 const char *pass = NULL;
990 json_object *capabilities = NULL;
991 json_object *reply = NULL;
992 char *session_key_hash = NULL;
993 struct nc_cpblts* cpblts = NULL;
994 unsigned int len, i;
995
996 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
997 host = json_object_get_string(json_object_object_get((json_object *) request, "host"));
998 port = json_object_get_string(json_object_object_get((json_object *) request, "port"));
999 user = json_object_get_string(json_object_object_get((json_object *) request, "user"));
1000 pass = json_object_get_string(json_object_object_get((json_object *) request, "pass"));
1001
1002 capabilities = json_object_object_get((json_object *) request, "capabilities");
1003 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
1004 cpblts = nc_cpblts_new(NULL);
1005 for (i=0; i<len; i++) {
1006 nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
1007 }
1008 } else {
1009 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
1010 }
1011
1012 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
1013 if ((host == NULL) || (user == NULL)) {
1014 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
1015 session_key_hash = NULL;
1016 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001017 session_key_hash = netconf_connect(server, pool, host, port, user, pass, cpblts);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001018 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key_hash);
1019 }
1020 if (cpblts != NULL) {
1021 nc_cpblts_free(cpblts);
1022 }
1023
1024 if (session_key_hash == NULL) {
1025 /* negative reply */
1026 if (err_reply == NULL) {
1027 reply = json_object_new_object();
1028 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1029 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
1030 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
1031 } else {
1032 /* use filled err_reply from libnetconf's callback */
1033 reply = err_reply;
1034 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
1035 }
1036 } else {
1037 /* positive reply */
1038 reply = json_object_new_object();
1039 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1040 json_object_object_add(reply, "session", json_object_new_string(session_key_hash));
1041
1042 free(session_key_hash);
1043 }
1044 return reply;
1045}
1046
Tomas Cejka47387fd2013-06-10 20:37:46 +02001047json_object *handle_op_get(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001048{
1049 const char *filter = NULL;
1050 const char *data = NULL;
1051 json_object *reply = NULL;
1052
1053 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
1054
1055 filter = json_object_get_string(json_object_object_get(request, "filter"));
1056
Tomas Cejka47387fd2013-06-10 20:37:46 +02001057 if ((data = netconf_get(server, session_key, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001058 if (err_reply == NULL) {
1059 reply = create_error("Get information from device failed.");
1060 } else {
1061 /* use filled err_reply from libnetconf's callback */
1062 reply = err_reply;
1063 }
1064 } else {
1065 return create_data(data);
1066 }
1067 return reply;
1068}
1069
Tomas Cejka47387fd2013-06-10 20:37:46 +02001070json_object *handle_op_getconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001071{
1072 NC_DATASTORE ds_type_s = -1;
1073 NC_DATASTORE ds_type_t = -1;
1074 const char *filter = NULL;
1075 const char *data = NULL;
1076 const char *source = NULL;
1077 const char *target = NULL;
1078 json_object *reply = NULL;
1079
1080 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1081
1082 filter = json_object_get_string(json_object_object_get(request, "filter"));
1083
1084 /* get parameters */
1085 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1086 ds_type_t = parse_datastore(target);
1087 }
1088 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1089 ds_type_s = parse_datastore(source);
1090 }
1091 if (ds_type_s == -1) {
1092 return create_error("Invalid source repository type requested.");
1093 }
1094
Tomas Cejka47387fd2013-06-10 20:37:46 +02001095 if ((data = netconf_getconfig(server, session_key, ds_type_s, filter)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001096 if (err_reply == NULL) {
1097 reply = create_error("Get configuration information from device failed.");
1098 } else {
1099 /* use filled err_reply from libnetconf's callback */
1100 reply = err_reply;
1101 }
1102 } else {
1103 return create_data(data);
1104 }
1105 return reply;
1106}
1107
Tomas Cejka47387fd2013-06-10 20:37:46 +02001108json_object *handle_op_getschema(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001109{
1110 const char *data = NULL;
1111 const char *identifier = NULL;
1112 const char *version = NULL;
1113 const char *format = NULL;
1114 json_object *reply = NULL;
1115
1116 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1117 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1118 if (identifier == NULL) {
1119 return create_error("No identifier for get-schema supplied.");
1120 }
1121 version = json_object_get_string(json_object_object_get(request, "version"));
1122 format = json_object_get_string(json_object_object_get(request, "format"));
1123
1124 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001125 if ((data = netconf_getschema(server, session_key, identifier, version, format)) == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001126 if (err_reply == NULL) {
1127 reply = create_error("Get schema failed.");
1128 } else {
1129 /* use filled err_reply from libnetconf's callback */
1130 reply = err_reply;
1131 }
1132 } else {
1133 return create_data(data);
1134 }
1135 return reply;
1136}
1137
Tomas Cejka47387fd2013-06-10 20:37:46 +02001138json_object *handle_op_editconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001139{
1140 NC_DATASTORE ds_type_s = -1;
1141 NC_DATASTORE ds_type_t = -1;
1142 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
1143 NC_EDIT_ERROPT_TYPE erropt_type = 0;
1144 const char *defop = NULL;
1145 const char *erropt = NULL;
1146 const char *config = NULL;
1147 const char *source = NULL;
1148 const char *target = NULL;
1149 json_object *reply = NULL;
1150
1151 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1152
1153 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1154 if (defop != NULL) {
1155 if (strcmp(defop, "merge") == 0) {
1156 defop_type = NC_EDIT_DEFOP_MERGE;
1157 } else if (strcmp(defop, "replace") == 0) {
1158 defop_type = NC_EDIT_DEFOP_REPLACE;
1159 } else if (strcmp(defop, "none") == 0) {
1160 defop_type = NC_EDIT_DEFOP_NONE;
1161 } else {
1162 return create_error("Invalid default-operation parameter.");
1163 }
1164 } else {
1165 defop_type = NC_EDIT_DEFOP_NOTSET;
1166 }
1167
1168 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1169 if (erropt != NULL) {
1170 if (strcmp(erropt, "continue-on-error") == 0) {
1171 erropt_type = NC_EDIT_ERROPT_CONT;
1172 } else if (strcmp(erropt, "stop-on-error") == 0) {
1173 erropt_type = NC_EDIT_ERROPT_STOP;
1174 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1175 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1176 } else {
1177 return create_error("Invalid error-option parameter.");
1178 }
1179 } else {
1180 erropt_type = 0;
1181 }
1182
1183 /* get parameters */
1184 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1185 ds_type_t = parse_datastore(target);
1186 }
1187 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1188 ds_type_s = parse_datastore(source);
1189 }
1190 if (ds_type_t == -1) {
1191 return create_error("Invalid target repository type requested.");
1192 }
1193
1194 config = json_object_get_string(json_object_object_get(request, "config"));
1195 if (config == NULL) {
1196 return create_error("Invalid config data parameter.");
1197 }
1198
Tomas Cejka47387fd2013-06-10 20:37:46 +02001199 if (netconf_editconfig(server, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001200 if (err_reply == NULL) {
1201 reply = create_error("Edit-config failed.");
1202 } else {
1203 /* use filled err_reply from libnetconf's callback */
1204 reply = err_reply;
1205 }
1206 } else {
1207 reply = json_object_new_object();
1208 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1209 }
1210 return reply;
1211}
1212
Tomas Cejka47387fd2013-06-10 20:37:46 +02001213json_object *handle_op_copyconfig(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001214{
1215 NC_DATASTORE ds_type_s = -1;
1216 NC_DATASTORE ds_type_t = -1;
1217 const char *config = NULL;
1218 const char *target = NULL;
1219 const char *source = NULL;
1220 json_object *reply = NULL;
1221
1222 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1223
1224 /* get parameters */
1225 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1226 ds_type_t = parse_datastore(target);
1227 }
1228 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1229 ds_type_s = parse_datastore(source);
1230 }
1231 if (source == NULL) {
1232 /* no explicit source specified -> use config data */
1233 ds_type_s = NC_DATASTORE_CONFIG;
1234 config = json_object_get_string(json_object_object_get(request, "config"));
1235 } else if (ds_type_s == -1) {
1236 /* source datastore specified, but it is invalid */
1237 return create_error("Invalid source repository type requested.");
1238 }
1239
1240 if (ds_type_t == -1) {
1241 /* invalid target datastore specified */
1242 return create_error("Invalid target repository type requested.");
1243 }
1244
1245 if (source == NULL && config == NULL) {
1246 reply = create_error("invalid input parameters - one of source and config is required.");
1247 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001248 if (netconf_copyconfig(server, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001249 if (err_reply == NULL) {
1250 reply = create_error("Copying of configuration failed.");
1251 } else {
1252 /* use filled err_reply from libnetconf's callback */
1253 reply = err_reply;
1254 }
1255 } else {
1256 reply = json_object_new_object();
1257 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1258 }
1259 }
1260 return reply;
1261}
1262
Tomas Cejka47387fd2013-06-10 20:37:46 +02001263json_object *handle_op_generic(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001264{
1265 json_object *reply = NULL;
1266 const char *config = NULL;
1267 char *data = NULL;
1268
1269 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1270
1271 config = json_object_get_string(json_object_object_get(request, "content"));
1272
1273 if (config == NULL) {
1274 return create_error("Missing content parameter.");
1275 }
1276
Tomas Cejka47387fd2013-06-10 20:37:46 +02001277 if (netconf_generic(server, session_key, config, &data) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001278 if (err_reply == NULL) {
1279 reply = create_error("Killing of session failed.");
1280 } else {
1281 /* use filled err_reply from libnetconf's callback */
1282 reply = err_reply;
1283 }
1284 } else {
1285 if (data == NULL) {
1286 reply = json_object_new_object();
1287 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1288 } else {
1289 return create_data(data);
1290 }
1291 }
1292 return reply;
1293}
1294
Tomas Cejka47387fd2013-06-10 20:37:46 +02001295json_object *handle_op_disconnect(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001296{
1297 json_object *reply = NULL;
1298 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1299
Tomas Cejka47387fd2013-06-10 20:37:46 +02001300 if (netconf_close(server, session_key) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001301 if (err_reply == NULL) {
1302 reply = create_error("Invalid session identifier.");
1303 } else {
1304 /* use filled err_reply from libnetconf's callback */
1305 reply = err_reply;
1306 }
1307 } else {
1308 reply = json_object_new_object();
1309 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1310 }
1311 return reply;
1312}
1313
Tomas Cejka47387fd2013-06-10 20:37:46 +02001314json_object *handle_op_kill(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001315{
1316 json_object *reply = NULL;
1317 const char *sid = NULL;
1318
1319 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1320
1321 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1322
1323 if (sid == NULL) {
1324 return create_error("Missing session-id parameter.");
1325 }
1326
Tomas Cejka47387fd2013-06-10 20:37:46 +02001327 if (netconf_killsession(server, session_key, sid) != EXIT_SUCCESS) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001328 if (err_reply == NULL) {
1329 reply = create_error("Killing of session failed.");
1330 } else {
1331 /* use filled err_reply from libnetconf's callback */
1332 reply = err_reply;
1333 }
1334 } else {
1335 reply = json_object_new_object();
1336 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1337 }
1338 return reply;
1339}
1340
Tomas Cejka47387fd2013-06-10 20:37:46 +02001341json_object *handle_op_reloadhello(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001342{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001343 struct nc_session *temp_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001344 struct session_with_mutex * locked_session = NULL;
1345 json_object *reply = NULL;
1346 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1347
Tomas Cejka47387fd2013-06-10 20:37:46 +02001348 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1349 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1350 return NULL;
1351 }
1352
Tomas Cejkad5b53772013-06-08 23:01:07 +02001353 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1354 if ((locked_session != NULL) && (locked_session->hello_message != NULL)) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001355 pthread_mutex_lock(&locked_session->lock);
1356 if (pthread_rwlock_unlock(&session_lock) != 0) {
1357 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1358 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001359 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "creating temporal NC session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +02001360 temp_session = nc_session_connect_channel(locked_session->session, NULL);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001361 if (temp_session != NULL) {
1362 prepare_status_message(server, locked_session, temp_session);
1363 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "closing temporal NC session.");
1364 nc_session_close(temp_session, NC_SESSION_TERM_CLOSED);
1365 } else {
1366 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Reload hello failed due to channel establishment");
1367 reply = create_error("Reload was unsuccessful, connection failed.");
1368 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001369 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001370 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001371 if (pthread_rwlock_unlock(&session_lock) != 0) {
1372 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1373 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001374 reply = create_error("Invalid session identifier.");
1375 }
1376
1377 if ((reply == NULL) && (locked_session->hello_message != NULL)) {
1378 reply = locked_session->hello_message;
1379 }
1380 return reply;
1381}
1382
Tomas Cejka47387fd2013-06-10 20:37:46 +02001383json_object *handle_op_info(server_rec *server, apr_pool_t *pool, json_object *request, const char *session_key)
Tomas Cejkad5b53772013-06-08 23:01:07 +02001384{
1385 json_object *reply = NULL;
Tomas Cejka47387fd2013-06-10 20:37:46 +02001386 struct session_with_mutex * locked_session = NULL;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001387 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1388
Tomas Cejka47387fd2013-06-10 20:37:46 +02001389 if (pthread_rwlock_rdlock(&session_lock) != 0) {
1390 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1391 }
1392
Tomas Cejkad5b53772013-06-08 23:01:07 +02001393 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1394 if (locked_session != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001395 pthread_mutex_lock(&locked_session->lock);
1396 if (pthread_rwlock_unlock(&session_lock) != 0) {
1397 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1398 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001399 if (locked_session->hello_message != NULL) {
1400 reply = locked_session->hello_message;
1401 } else {
1402 reply = create_error("Invalid session identifier.");
1403 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001404 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001405 } else {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001406 if (pthread_rwlock_unlock(&session_lock) != 0) {
1407 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1408 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001409 reply = create_error("Invalid session identifier.");
1410 }
1411
Tomas Cejka47387fd2013-06-10 20:37:46 +02001412
Tomas Cejkad5b53772013-06-08 23:01:07 +02001413 return reply;
1414}
1415
David Kupka8e60a372012-09-04 09:15:20 +02001416void * thread_routine (void * arg)
1417{
1418 void * retval = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001419 struct pollfd fds;
Tomas Cejka00635972013-06-03 15:10:52 +02001420 json_object *request = NULL;
1421 json_object *reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001422 int operation;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001423 int status = 0;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001424 const char *msgtext;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001425 const char *session_key;
1426 const char *target = NULL;
1427 const char *source = NULL;
1428 NC_DATASTORE ds_type_t = -1;
1429 NC_DATASTORE ds_type_s = -1;
Tomas Cejka64b87482013-06-03 16:30:53 +02001430 char *chunked_out_msg = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001431 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
David Kupka8e60a372012-09-04 09:15:20 +02001432 server_rec * server = ((struct pass_to_thread*)arg)->server;
1433 int client = ((struct pass_to_thread*)arg)->client;
1434
Tomas Cejka00635972013-06-03 15:10:52 +02001435 char *buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001436
1437 while (!isterminated) {
1438 fds.fd = client;
1439 fds.events = POLLIN;
1440 fds.revents = 0;
1441
1442 status = poll(&fds, 1, 1000);
1443
1444 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
1445 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
1446 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
1447 continue;
1448 } else if (status < 0) {
1449 /* 0: poll time outed
1450 * close socket and ignore this request from the client, it can try it again
1451 * -1: poll failed
1452 * something wrong happend, close this socket and wait for another request
1453 */
1454 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
1455 close(client);
1456 break;
1457 }
1458 /* status > 0 */
1459
1460 /* check the status of the socket */
1461
1462 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
1463 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
1464 /* close client's socket (it's probably already closed by client */
1465 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
1466 close(client);
1467 break;
1468 }
1469
Tomas Cejka47387fd2013-06-10 20:37:46 +02001470 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Get framed message...");
Tomas Cejka64b87482013-06-03 16:30:53 +02001471 buffer = get_framed_message(server, client);
David Kupka8e60a372012-09-04 09:15:20 +02001472
Tomas Cejka47387fd2013-06-10 20:37:46 +02001473 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Check read buffer.");
David Kupka8e60a372012-09-04 09:15:20 +02001474 if (buffer != NULL) {
Tomas Cejka00635972013-06-03 15:10:52 +02001475 enum json_tokener_error jerr;
1476 request = json_tokener_parse_verbose(buffer, &jerr);
1477 if (jerr != json_tokener_success) {
1478 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "JSON parsing error");
1479 continue;
1480 }
David Kupka8e60a372012-09-04 09:15:20 +02001481 operation = json_object_get_int(json_object_object_get(request, "type"));
1482
Tomas Cejka64b87482013-06-03 16:30:53 +02001483 session_key = json_object_get_string(json_object_object_get(request, "session"));
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001484 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "operation %d session_key %s.", operation, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001485 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
1486 if (operation != MSG_CONNECT && session_key == NULL) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001487 reply = create_error("Missing session specification.");
David Kupka8e60a372012-09-04 09:15:20 +02001488 msgtext = json_object_to_json_string(reply);
1489 send(client, msgtext, strlen(msgtext) + 1, 0);
1490 json_object_put(reply);
1491 /* there is some stupid client, so close the connection to give a chance to some other client */
1492 close(client);
1493 break;
1494 }
1495
David Kupka8e60a372012-09-04 09:15:20 +02001496 /* null global JSON error-reply */
1497 err_reply = NULL;
1498
1499 /* prepare reply envelope */
Tomas Cejkad5b53772013-06-08 23:01:07 +02001500 reply = NULL;
David Kupka8e60a372012-09-04 09:15:20 +02001501
1502 /* process required operation */
1503 switch (operation) {
1504 case MSG_CONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001505 reply = handle_op_connect(server, pool, request);
David Kupka8e60a372012-09-04 09:15:20 +02001506 break;
1507 case MSG_GET:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001508 reply = handle_op_get(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001509 break;
1510 case MSG_GETCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001511 reply = handle_op_getconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001512 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001513 case MSG_GETSCHEMA:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001514 reply = handle_op_getschema(server, pool, request, session_key);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001515 break;
David Kupka8e60a372012-09-04 09:15:20 +02001516 case MSG_EDITCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001517 reply = handle_op_editconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001518 break;
1519 case MSG_COPYCONFIG:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001520 reply = handle_op_copyconfig(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001521 break;
Tomas Cejkad5b53772013-06-08 23:01:07 +02001522
David Kupka8e60a372012-09-04 09:15:20 +02001523 case MSG_DELETECONFIG:
David Kupka8e60a372012-09-04 09:15:20 +02001524 case MSG_LOCK:
David Kupka8e60a372012-09-04 09:15:20 +02001525 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001526 /* get parameters */
1527 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
1528 ds_type_t = parse_datastore(target);
1529 }
1530 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
1531 ds_type_s = parse_datastore(source);
David Kupka8e60a372012-09-04 09:15:20 +02001532 }
1533
1534 if (ds_type_t == -1) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001535 reply = create_error("Invalid target repository type requested.");
David Kupka8e60a372012-09-04 09:15:20 +02001536 break;
1537 }
David Kupka8e60a372012-09-04 09:15:20 +02001538 switch(operation) {
1539 case MSG_DELETECONFIG:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001540 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001541 status = netconf_deleteconfig(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001542 break;
1543 case MSG_LOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001544 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001545 status = netconf_lock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001546 break;
1547 case MSG_UNLOCK:
Tomas Cejkad5b53772013-06-08 23:01:07 +02001548 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001549 status = netconf_unlock(server, session_key, ds_type_t);
David Kupka8e60a372012-09-04 09:15:20 +02001550 break;
1551 default:
1552 status = -1;
1553 break;
1554 }
1555
1556 if (status != EXIT_SUCCESS) {
1557 if (err_reply == NULL) {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001558 /** \todo more clever error message wanted */
1559 reply = create_error("operation failed.");
David Kupka8e60a372012-09-04 09:15:20 +02001560 } else {
1561 /* use filled err_reply from libnetconf's callback */
David Kupka8e60a372012-09-04 09:15:20 +02001562 reply = err_reply;
1563 }
1564 } else {
Tomas Cejkad5b53772013-06-08 23:01:07 +02001565 reply = json_object_new_object();
David Kupka8e60a372012-09-04 09:15:20 +02001566 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1567 }
1568 break;
1569 case MSG_KILL:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001570 reply = handle_op_kill(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001571 break;
1572 case MSG_DISCONNECT:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001573 reply = handle_op_disconnect(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001574 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001575 case MSG_RELOADHELLO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001576 reply = handle_op_reloadhello(server, pool, request, session_key);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001577 break;
Tomas Cejka45ab59f2013-05-15 00:10:49 +02001578 case MSG_INFO:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001579 reply = handle_op_info(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001580 break;
1581 case MSG_GENERIC:
Tomas Cejka47387fd2013-06-10 20:37:46 +02001582 reply = handle_op_generic(server, pool, request, session_key);
David Kupka8e60a372012-09-04 09:15:20 +02001583 break;
1584 default:
1585 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
Tomas Cejkad5b53772013-06-08 23:01:07 +02001586 reply = create_error("Operation not supported.");
David Kupka8e60a372012-09-04 09:15:20 +02001587 break;
1588 }
Tomas Cejkad5b53772013-06-08 23:01:07 +02001589 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean request json object.");
David Kupka8e60a372012-09-04 09:15:20 +02001590 json_object_put(request);
1591
Tomas Cejkad5b53772013-06-08 23:01:07 +02001592 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001593 /* send reply to caller */
1594 if (reply != NULL) {
1595 msgtext = json_object_to_json_string(reply);
Tomas Cejka64b87482013-06-03 16:30:53 +02001596 if (asprintf (&chunked_out_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
Tomas Cejka00635972013-06-03 15:10:52 +02001597 if (buffer != NULL) {
1598 free(buffer);
1599 buffer = NULL;
1600 }
David Kupka8e60a372012-09-04 09:15:20 +02001601 break;
1602 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001603 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Send framed reply json object.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001604 send(client, chunked_out_msg, strlen(chunked_out_msg) + 1, 0);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001605 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean reply json object.");
David Kupka1e3e4c82012-09-04 09:32:15 +02001606 json_object_put(reply);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001607 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Clean message buffer.");
Tomas Cejka64b87482013-06-03 16:30:53 +02001608 free(chunked_out_msg);
1609 chunked_out_msg = NULL;
Tomas Cejka00635972013-06-03 15:10:52 +02001610 if (buffer != NULL) {
1611 free(buffer);
1612 buffer = NULL;
1613 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001614 } else {
Tomas Cejka64b87482013-06-03 16:30:53 +02001615 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Reply is NULL, shouldn't be...");
David Kupka1e3e4c82012-09-04 09:32:15 +02001616 break;
David Kupka8e60a372012-09-04 09:15:20 +02001617 }
1618 }
1619 }
David Kupka8e60a372012-09-04 09:15:20 +02001620 free (arg);
1621
1622 return retval;
1623}
1624
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001625/**
1626 * \brief Close all open NETCONF sessions.
1627 *
1628 * During termination of mod_netconf, it is useful to close all remaining
1629 * sessions. This function iterates over the list of sessions and close them
1630 * all.
1631 *
1632 * \param[in] server pointer to server_rec for logging
1633 * \param[in] p apr pool needed for hash table iterating
1634 * \param[in] ht hash table of session_with_mutex structs
1635 */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001636static void close_all_nc_sessions(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001637{
1638 apr_hash_index_t *hi;
1639 void *val = NULL;
1640 struct session_with_mutex *swm = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001641 const char *hashed_key = NULL;
1642 apr_ssize_t hashed_key_length;
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001643 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001644
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001645 /* get exclusive access to sessions_list (conns) */
1646 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1647 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1648 return;
1649 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001650 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001651 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1652 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1653 swm = (struct session_with_mutex *) val;
1654 if (swm != NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001655 //pthread_mutex_lock(&swm->lock);
1656 //if (swm->session != NULL) {
1657 // swm->closed = 1;
1658 // ns = swm->session;
1659 // nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1660 // nc_session_free(ns);
1661 // swm->session = NULL;
1662 //}
1663 //pthread_mutex_unlock(&swm->lock);
1664
1665 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001666 pthread_mutex_unlock(&swm->lock);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001667
1668 /* close_and_free_session handles locking on its own */
1669 close_and_free_session(server, swm);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001670 }
1671 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001672 /* get exclusive access to sessions_list (conns) */
1673 if (pthread_rwlock_unlock (&session_lock) != 0) {
1674 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1675 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001676}
1677
Tomas Cejka47387fd2013-06-10 20:37:46 +02001678static void check_timeout_and_close(server_rec* server, apr_pool_t *p)
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001679{
Tomas Cejka47387fd2013-06-10 20:37:46 +02001680/** \todo fix locking */
1681return;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001682 apr_hash_index_t *hi;
1683 void *val = NULL;
1684 struct nc_session *ns = NULL;
1685 struct session_with_mutex *swm = NULL;
1686 const char *hashed_key = NULL;
1687 apr_ssize_t hashed_key_length;
1688 apr_time_t current_time = apr_time_now();
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001689 int ret;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001690
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001691 /* get exclusive access to sessions_list (conns) */
1692 if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
1693 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while locking rwlock: %d (%s)", ret, strerror(ret));
1694 return;
1695 }
Tomas Cejka47387fd2013-06-10 20:37:46 +02001696 for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001697 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1698 swm = (struct session_with_mutex *) val;
1699 if (swm == NULL) {
1700 continue;
1701 }
1702 ns = swm->session;
1703 if (ns == NULL) {
1704 continue;
1705 }
1706 pthread_mutex_lock(&swm->lock);
1707 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1708 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
Tomas Cejka47387fd2013-06-10 20:37:46 +02001709 //nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1710 //nc_session_free (ns);
1711 //ns = NULL;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001712 /* remove session from the active sessions list */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001713 apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
1714 pthread_mutex_unlock(&swm->lock);
1715
1716 /* close_and_free_session handles locking on its own */
1717 close_and_free_session(server, swm);
1718 } else {
1719 pthread_mutex_unlock(&swm->lock);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001720 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001721 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +02001722 /* get exclusive access to sessions_list (conns) */
1723 if (pthread_rwlock_unlock (&session_lock) != 0) {
1724 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
1725 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001726}
1727
1728
1729/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001730 * This is actually implementation of NETCONF client
1731 * - requests are received from UNIX socket in the predefined format
1732 * - results are replied through the same way
1733 * - the daemon run as a separate process, but it is started and stopped
1734 * automatically by Apache.
1735 *
1736 */
Radek Krejci469aab82012-07-22 18:42:20 +02001737static void forked_proc(apr_pool_t * pool, server_rec * server)
1738{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001739 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001740 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001741 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001742 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001743 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001744 mod_netconf_cfg *cfg;
David Kupka8e60a372012-09-04 09:15:20 +02001745 struct pass_to_thread * arg;
1746 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1747 struct timespec maxtime;
1748 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001749 #ifdef WITH_NOTIFICATIONS
1750 char use_notifications = 0;
1751 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001752
Tomas Cejka404d37e2013-04-13 02:31:35 +02001753 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001754 maxtime.tv_sec = 5;
1755 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001756
Radek Krejcif23850c2012-07-23 16:14:17 +02001757 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001758 unixd_setup_child();
1759
Radek Krejciae021c12012-07-25 18:03:52 +02001760 cfg = ap_get_module_config(server->module_config, &netconf_module);
1761 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001762 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1763 return;
1764 }
Radek Krejci469aab82012-07-22 18:42:20 +02001765
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001766 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001767 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1768 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1769 return;
1770 }
1771
1772 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001773 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001774 unlink(local.sun_path);
1775 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1776
1777 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1778 if (errno == EADDRINUSE) {
1779 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1780 close(lsock);
1781 exit(0);
1782 }
1783 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1784 close(lsock);
1785 return;
1786 }
1787
1788 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1789 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1790 close(lsock);
1791 return;
1792 }
1793
Tomas Cejkaba21b382013-04-13 02:37:32 +02001794 /* prepare internal lists */
1795 netconf_sessions_list = apr_hash_make(pool);
1796
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001797 #ifdef WITH_NOTIFICATIONS
Tomas Cejka47387fd2013-06-10 20:37:46 +02001798 if (notification_init(pool, server) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001799 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1800 use_notifications = 0;
1801 } else {
1802 use_notifications = 1;
1803 }
1804 #endif
1805
Radek Krejci469aab82012-07-22 18:42:20 +02001806 /* setup libnetconf's callbacks */
1807 nc_verbosity(NC_VERB_DEBUG);
1808 clb_print_server = server;
1809 nc_callback_print(clb_print);
1810 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1811 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1812 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001813 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001814
1815 /* disable publickey authentication */
1816 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1817
Tomas Cejka44e71db2013-04-21 15:50:47 +02001818 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1819
David Kupka8e60a372012-09-04 09:15:20 +02001820 /* create mutex protecting session list */
1821 pthread_rwlockattr_init(&lock_attrs);
1822 /* rwlock is shared only with threads in this process */
1823 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1824 /* create rw lock */
1825 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1826 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1827 close (lsock);
1828 return;
1829 }
Tomas Cejka8a82dab2013-05-30 23:37:23 +02001830
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001831 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001832 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001833 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001834 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001835 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001836 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001837 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1838 }
1839 if (use_notifications == 1) {
1840 notification_handle();
1841 }
1842 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001843 if (timediff > ACTIVITY_CHECK_INTERVAL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +02001844 check_timeout_and_close(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001845 }
Radek Krejci469aab82012-07-22 18:42:20 +02001846
1847 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001848 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001849 if (((unsigned int)tv.tv_sec - olds) > 60) {
1850 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1851 olds = tv.tv_sec;
1852 }
David Kupka8e60a372012-09-04 09:15:20 +02001853 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001854 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1855 apr_sleep(SLEEP_TIME);
1856 continue;
1857 } else if (client == -1 && (errno == EINTR)) {
1858 continue;
1859 } else if (client == -1) {
1860 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1861 continue;
1862 }
Radek Krejci469aab82012-07-22 18:42:20 +02001863
1864 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001865 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001866
David Kupka8e60a372012-09-04 09:15:20 +02001867 arg = malloc (sizeof(struct pass_to_thread));
1868 arg->client = client;
1869 arg->pool = pool;
1870 arg->server = server;
1871 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001872
David Kupka8e60a372012-09-04 09:15:20 +02001873 /* start new thread. It will serve this particular request and then terminate */
1874 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1875 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1876 } else {
1877 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1878 pthread_count++;
1879 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1880 ptids[pthread_count] = 0;
1881 }
Radek Krejci469aab82012-07-22 18:42:20 +02001882
David Kupka8e60a372012-09-04 09:15:20 +02001883 /* check if some thread already terminated, free some resources by joining it */
1884 for (i=0; i<pthread_count; i++) {
1885 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1886 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1887 pthread_count--;
1888 if (pthread_count > 0) {
1889 /* place last Thread ID on the place of joined one */
1890 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001891 }
Radek Krejci469aab82012-07-22 18:42:20 +02001892 }
1893 }
David Kupka8e60a372012-09-04 09:15:20 +02001894 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001895 }
1896
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001897 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001898 /* join all threads */
1899 for (i=0; i<pthread_count; i++) {
1900 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1901 }
1902 free (ptids);
1903
Radek Krejci469aab82012-07-22 18:42:20 +02001904 close(lsock);
1905
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001906 #ifdef WITH_NOTIFICATIONS
1907 notification_close();
1908 #endif
1909
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001910 /* close all NETCONF sessions */
Tomas Cejka47387fd2013-06-10 20:37:46 +02001911 close_all_nc_sessions(server, pool);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001912
David Kupka8e60a372012-09-04 09:15:20 +02001913 /* destroy rwlock */
1914 pthread_rwlock_destroy(&session_lock);
1915 pthread_rwlockattr_destroy(&lock_attrs);
1916
Radek Krejci469aab82012-07-22 18:42:20 +02001917 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1918
1919 exit(APR_SUCCESS);
1920}
1921
Radek Krejcif23850c2012-07-23 16:14:17 +02001922static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001923{
Radek Krejcif23850c2012-07-23 16:14:17 +02001924 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1925
1926 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1927 apr_pool_create(&config->pool, pool);
1928 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001929 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001930
1931 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001932}
1933
1934static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1935 apr_pool_t * plog, server_rec * s)
1936{
Radek Krejcif23850c2012-07-23 16:14:17 +02001937 mod_netconf_cfg *config;
1938 apr_status_t res;
1939
Radek Krejci469aab82012-07-22 18:42:20 +02001940 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001941 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001942 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001943
1944 /*
1945 * The following checks if this routine has been called before.
1946 * This is necessary because the parent process gets initialized
1947 * a couple of times as the server starts up.
1948 */
1949 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1950 if (!data) {
1951 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1952 return (OK);
1953 }
1954
Radek Krejcif23850c2012-07-23 16:14:17 +02001955 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1956 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001957
Radek Krejcif23850c2012-07-23 16:14:17 +02001958 if (config && config->forkproc == NULL) {
1959 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1960 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001961 switch (res) {
1962 case APR_INCHILD:
1963 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001964 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001965 apr_signal(SIGTERM, signal_handler);
1966
1967 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001968 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001969
1970 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001971 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001972
Radek Krejcif23850c2012-07-23 16:14:17 +02001973 /* I never should be here, wtf?!? */
1974 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001975 exit(APR_EGENERAL);
1976 break;
1977 case APR_INPARENT:
1978 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001979 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001980 break;
1981 default:
1982 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1983 break;
1984 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001985 } else {
1986 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001987 }
1988
1989 return OK;
1990}
1991
Radek Krejci469aab82012-07-22 18:42:20 +02001992/**
1993 * Register module hooks
1994 */
1995static void mod_netconf_register_hooks(apr_pool_t * p)
1996{
Radek Krejcif23850c2012-07-23 16:14:17 +02001997 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001998}
1999
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002000static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
2001{
2002 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
2003 return NULL;
2004}
2005
2006static const command_rec netconf_cmds[] = {
2007 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
2008 {NULL}
2009};
2010
Radek Krejci469aab82012-07-22 18:42:20 +02002011/* Dispatch list for API hooks */
2012module AP_MODULE_DECLARE_DATA netconf_module = {
2013 STANDARD20_MODULE_STUFF,
2014 NULL, /* create per-dir config structures */
2015 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02002016 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02002017 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02002018 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02002019 mod_netconf_register_hooks /* register hooks */
2020};
Radek Krejcia332b692012-11-12 16:15:54 +01002021