blob: f3a8631b40ec363eee8e48711ba26e9bed7ff21c [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01008 * \date 2013
Radek Krejci469aab82012-07-22 18:42:20 +02009 */
10/*
Tomas Cejkad340dbf2013-03-24 20:36:57 +010011 * Copyright (C) 2011-2013 CESNET
Radek Krejci469aab82012-07-22 18:42:20 +020012 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
Tomas Cejka689a1042013-01-16 15:08:25 +010046static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $";
Radek Krejci469aab82012-07-22 18:42:20 +020047
Radek Krejci7b4ddd02012-07-30 08:09:58 +020048#include <unistd.h>
49#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020050#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/un.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010053#include <sys/fcntl.h>
David Kupka8e60a372012-09-04 09:15:20 +020054#include <pthread.h>
55#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020056
57#include <unixd.h>
58#include <httpd.h>
59#include <http_log.h>
60#include <http_config.h>
61
62#include <apr_sha1.h>
63#include <apr_hash.h>
64#include <apr_signal.h>
65#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020066
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020067#include <json/json.h>
68
Radek Krejci469aab82012-07-22 18:42:20 +020069#include <libnetconf.h>
Tomas Cejkab3cc64f2013-05-03 19:44:54 +020070#include <libnetconf_ssh.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020071
Tomas Cejkad340dbf2013-03-24 20:36:57 +010072#ifdef WITH_NOTIFICATIONS
73#include "notification_module.h"
74#endif
75
Tomas Cejka94da2c52013-01-08 18:20:30 +010076#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020077#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020078
79#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020080#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020081#define MAX_SOCKET_CL 10
82#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020083#define NOTIFICATION_QUEUE_SIZE 10
84#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020085#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020086
87/* sleep in master process for non-blocking socket reading */
88#define SLEEP_TIME 200
89
90#ifndef offsetof
91#define offsetof(type, member) ((size_t) ((type *) 0)->member)
92#endif
93
Tomas Cejka027f3bc2012-11-10 20:28:36 +010094/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020095struct timeval timeout = { 1, 0 };
96
Tomas Cejka5064c232013-01-17 09:30:58 +010097#define NCWITHDEFAULTS NCWD_MODE_NOTSET
98
99
Radek Krejci469aab82012-07-22 18:42:20 +0200100#define MSG_OK 0
101#define MSG_OPEN 1
102#define MSG_DATA 2
103#define MSG_CLOSE 3
104#define MSG_ERROR 4
105#define MSG_UNKNOWN 5
106
Radek Krejci469aab82012-07-22 18:42:20 +0200107module AP_MODULE_DECLARE_DATA netconf_module;
108
David Kupka8e60a372012-09-04 09:15:20 +0200109pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
110
Radek Krejci469aab82012-07-22 18:42:20 +0200111volatile int isterminated = 0;
112
113static char* password;
114
Radek Krejci469aab82012-07-22 18:42:20 +0200115static void signal_handler(int sign)
116{
117 switch (sign) {
118 case SIGTERM:
119 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200120 break;
121 }
122}
123
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200124static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200125{
Radek Krejcif23850c2012-07-23 16:14:17 +0200126 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
127 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200128 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200129
Radek Krejci469aab82012-07-22 18:42:20 +0200130 apr_sha1_ctx_t sha1_ctx;
131 apr_sha1_init(&sha1_ctx);
132 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
133 apr_sha1_update(&sha1_ctx, port, strlen(port));
134 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200135 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200136
Radek Krejcif23850c2012-07-23 16:14:17 +0200137 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200138 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200139 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200140 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200141 }
142 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200143
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200144 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200145}
146
147int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
148{
149 /* always approve */
150 return (EXIT_SUCCESS);
151}
152
153char* netconf_callback_sshauth_password (const char* username, const char* hostname)
154{
155 char* buf;
156
157 buf = malloc ((strlen(password) + 1) * sizeof(char));
158 apr_cpystrn(buf, password, strlen(password) + 1);
159
160 return (buf);
161}
162
163void netconf_callback_sshauth_interactive (const char* name,
164 int name_len,
165 const char* instruction,
166 int instruction_len,
167 int num_prompts,
168 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
169 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
170 void** abstract)
171{
172 int i;
173
174 for (i=0; i<num_prompts; i++) {
175 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
176 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
177 responses[i].length = strlen(responses[i].text) + 1;
178 }
179
180 return;
181}
182
Radek Krejcic11fd862012-07-26 12:41:21 +0200183static json_object *err_reply = NULL;
184void netconf_callback_error_process(const char* tag,
185 const char* type,
186 const char* severity,
187 const char* apptag,
188 const char* path,
189 const char* message,
190 const char* attribute,
191 const char* element,
192 const char* ns,
193 const char* sid)
194{
195 err_reply = json_object_new_object();
196 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
197 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
198 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
199 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
200 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
201 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
202 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
203 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
204 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
205 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
206 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
207}
208
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200209/**
210 * \defgroup netconf_operations NETCONF operations
211 * The list of NETCONF operations that mod_netconf supports.
212 * @{
213 */
214
215/**
216 * \brief Connect to NETCONF server
217 *
218 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
219 */
Kupka David00b9c5c2012-09-05 09:45:50 +0200220static char* netconf_connect(server_rec* server, apr_pool_t* pool, apr_hash_t* conns, const char* host, const char* port, const char* user, const char* pass, struct nc_cpblts * cpblts)
Radek Krejci469aab82012-07-22 18:42:20 +0200221{
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200222 struct nc_session* session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200223 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200224 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200225
226 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200227 password = (char*)pass;
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200228 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "prepare to connect %s@%s:%s", user, host, port);
229 nc_verbosity(NC_VERB_DEBUG);
David Kupka8e60a372012-09-04 09:15:20 +0200230 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
Tomas Cejkab3cc64f2013-05-03 19:44:54 +0200231 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "nc_session_connect done (%p)", session);
David Kupka8e60a372012-09-04 09:15:20 +0200232
Radek Krejci469aab82012-07-22 18:42:20 +0200233 /* if connected successful, add session to the list */
234 if (session != NULL) {
235 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200236 session_key = gen_ncsession_hash(
237 (host==NULL) ? "localhost" : host,
238 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200239 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200240
Tomas Cejkaba21b382013-04-13 02:37:32 +0200241 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200242 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
243 nc_session_free(session);
244 free (locked_session);
245 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
246 return NULL;
247 }
248 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200249 locked_session->last_activity = apr_time_now();
David Kupka8e60a372012-09-04 09:15:20 +0200250 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100251 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200252 /* get exclusive access to sessions_list (conns) */
253 if (pthread_rwlock_wrlock (&session_lock) != 0) {
254 nc_session_free(session);
255 free (locked_session);
256 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
257 return NULL;
258 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200259 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejka654f84e2013-04-19 11:55:01 +0200260 locked_session->ntfc_subscribed = 0;
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100261 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200262 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100263 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200264 /* end of critical section */
265 if (pthread_rwlock_unlock (&session_lock) != 0) {
266 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
267 return NULL;
268 }
Radek Krejci469aab82012-07-22 18:42:20 +0200269 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200270 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200271 } else {
272 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200273 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200274 }
275
Radek Krejci469aab82012-07-22 18:42:20 +0200276}
277
Radek Krejci80c10d92012-07-30 08:38:50 +0200278static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200279{
280 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200281 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200282
Radek Krejcif23850c2012-07-23 16:14:17 +0200283 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200284 /* get exclusive (write) access to sessions_list (conns) */
285 if (pthread_rwlock_wrlock (&session_lock) != 0) {
286 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
287 return EXIT_FAILURE;
288 }
289 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
290 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200291 /** \todo free all notifications from queue */
292 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200293 pthread_mutex_destroy(&locked_session->lock);
294 ns = locked_session->session;
295 free (locked_session);
296 }
Radek Krejci469aab82012-07-22 18:42:20 +0200297 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100298 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200299 nc_session_free (ns);
300 ns = NULL;
301
302 /* remove session from the active sessions list */
303 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200304 /* end of critical section */
305 if (pthread_rwlock_unlock (&session_lock) != 0) {
306 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
307 return EXIT_FAILURE;
308 }
Radek Krejci469aab82012-07-22 18:42:20 +0200309 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200310
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200311 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200312 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200313 if (pthread_rwlock_unlock (&session_lock) != 0) {
314 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
315 return EXIT_FAILURE;
316 }
Radek Krejci469aab82012-07-22 18:42:20 +0200317 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200318 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200319 }
320}
321
Radek Krejci80c10d92012-07-30 08:38:50 +0200322static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200323{
324 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200325 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200326 nc_reply* reply;
327 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100328 NC_MSG_TYPE msgt;
329 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200330
Radek Krejci8e4632a2012-07-26 13:40:34 +0200331 /* check requests */
332 if (rpc == NULL) {
333 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
334 return (EXIT_FAILURE);
335 }
336
David Kupka8e60a372012-09-04 09:15:20 +0200337 /* get non-exclusive (read) access to sessions_list (conns) */
338 if (pthread_rwlock_rdlock (&session_lock) != 0) {
339 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
340 return EXIT_FAILURE;
341 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200342 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200343 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
344 if (locked_session != NULL) {
345 session = locked_session->session;
346 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200347 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200348 /* get exclusive access to session */
349 if (pthread_mutex_lock(&locked_session->lock) != 0) {
350 /* unlock before returning error */
351 if (pthread_rwlock_unlock (&session_lock) != 0) {
352 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
353 return EXIT_FAILURE;
354 }
355 return EXIT_FAILURE;
356 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200357 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200358 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100359 msgt = nc_session_send_recv(session, rpc, &reply);
360
David Kupka8e60a372012-09-04 09:15:20 +0200361 /* first release exclusive lock for this session */
362 pthread_mutex_unlock(&locked_session->lock);
363 /* end of critical section */
364 if (pthread_rwlock_unlock (&session_lock) != 0) {
365 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
366 return EXIT_FAILURE;
367 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200368
Radek Krejcia332b692012-11-12 16:15:54 +0100369 /* process the result of the operation */
370 switch (msgt) {
371 case NC_MSG_UNKNOWN:
372 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
373 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
374 netconf_close(server, conns, session_key);
375 return (EXIT_FAILURE);
376 }
377 /* no break */
378 case NC_MSG_NONE:
379 /* there is error handled by callback */
380 return (EXIT_FAILURE);
381 break;
382 case NC_MSG_REPLY:
383 switch (replyt = nc_reply_get_type(reply)) {
384 case NC_REPLY_OK:
385 retval = EXIT_SUCCESS;
386 break;
387 default:
388 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
389 retval = EXIT_FAILURE;
390 break;
391 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200392 break;
393 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200395 retval = EXIT_FAILURE;
396 break;
397 }
398 nc_reply_free(reply);
399 return (retval);
400 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100401 /* release lock on failure */
402 if (pthread_rwlock_unlock (&session_lock) != 0) {
403 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
404 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200405 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
406 return (EXIT_FAILURE);
407 }
408}
Radek Krejci80c10d92012-07-30 08:38:50 +0200409
410static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200411{
412 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200413 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100414 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200415 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100416 NC_MSG_TYPE msgt;
417 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200418
419 /* check requests */
420 if (rpc == NULL) {
421 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
422 return (NULL);
423 }
424
David Kupka8e60a372012-09-04 09:15:20 +0200425 /* get non-exclusive (read) access to sessions_list (conns) */
426 if (pthread_rwlock_rdlock (&session_lock) != 0) {
427 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
428 return NULL;
429 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200430 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200431 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
432 if (locked_session != NULL) {
433 session = locked_session->session;
434 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200435 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200436 /* get exclusive access to session */
437 if (pthread_mutex_lock(&locked_session->lock) != 0) {
438 /* unlock before returning error */
439 if (pthread_rwlock_unlock (&session_lock) != 0) {
440 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
441 return NULL;
442 }
443 return NULL;
444 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200445 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200446 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100447 msgt = nc_session_send_recv(session, rpc, &reply);
448
David Kupka8e60a372012-09-04 09:15:20 +0200449 /* first release exclusive lock for this session */
450 pthread_mutex_unlock(&locked_session->lock);
451 /* end of critical section */
452 if (pthread_rwlock_unlock (&session_lock) != 0) {
453 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Radek Krejcia332b692012-11-12 16:15:54 +0100454 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200455 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200456
Radek Krejcia332b692012-11-12 16:15:54 +0100457 /* process the result of the operation */
458 switch (msgt) {
459 case NC_MSG_UNKNOWN:
460 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
461 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
462 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200463 return (NULL);
464 }
Radek Krejcia332b692012-11-12 16:15:54 +0100465 /* no break */
466 case NC_MSG_NONE:
467 /* there is error handled by callback */
468 return (NULL);
469 break;
470 case NC_MSG_REPLY:
471 switch (replyt = nc_reply_get_type(reply)) {
472 case NC_REPLY_DATA:
473 if ((data = nc_reply_get_data (reply)) == NULL) {
474 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
475 data = NULL;
476 }
477 break;
478 default:
479 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
480 data = NULL;
481 break;
482 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200483 break;
484 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100485 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
486 data = NULL;
487 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200488 }
489 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490 return (data);
491 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100492 /* release lock on failure */
493 if (pthread_rwlock_unlock (&session_lock) != 0) {
494 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
495 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200496 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
497 return (NULL);
498 }
499}
500
Radek Krejci80c10d92012-07-30 08:38:50 +0200501static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200502{
503 nc_rpc* rpc;
504 struct nc_filter *f = NULL;
505 char* data = NULL;
506
507 /* create filter if set */
508 if (filter != NULL) {
509 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
510 }
511
512 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100513 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200514 nc_filter_free(f);
515 if (rpc == NULL) {
516 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
517 return (NULL);
518 }
519
520 data = netconf_opdata(server, conns, session_key, rpc);
521 nc_rpc_free (rpc);
522 return (data);
523}
524
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100525static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
526{
527 nc_rpc* rpc;
528 char* data = NULL;
529
530 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100531 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100532 if (rpc == NULL) {
533 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
534 return (NULL);
535 }
536
537 data = netconf_opdata(server, conns, session_key, rpc);
538 nc_rpc_free (rpc);
539 return (data);
540}
541
Radek Krejci80c10d92012-07-30 08:38:50 +0200542static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200543{
544 nc_rpc* rpc;
545 struct nc_filter *f = NULL;
546 char* data = NULL;
547
548 /* create filter if set */
549 if (filter != NULL) {
550 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
551 }
552
553 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100554 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200555 nc_filter_free(f);
556 if (rpc == NULL) {
557 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
558 return (NULL);
559 }
560
561 data = netconf_opdata(server, conns, session_key, rpc);
562 nc_rpc_free (rpc);
563 return (data);
564}
565
Tomas Cejka5064c232013-01-17 09:30:58 +0100566static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config, const char *url)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200567{
568 nc_rpc* rpc;
569 int retval = EXIT_SUCCESS;
570
571 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100572 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
573 if (target == NC_DATASTORE_URL) {
574 rpc = nc_rpc_copyconfig(source, target, config, url);
575 } else {
576 rpc = nc_rpc_copyconfig(source, target, config);
577 }
578 } else {
579 if (target == NC_DATASTORE_URL) {
580 rpc = nc_rpc_copyconfig(source, target, url);
581 } else {
582 rpc = nc_rpc_copyconfig(source, target);
583 }
584 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200585 if (rpc == NULL) {
586 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
587 return (EXIT_FAILURE);
588 }
589
590 retval = netconf_op(server, conns, session_key, rpc);
591 nc_rpc_free (rpc);
592 return (retval);
593}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200594
Tomas Cejka5064c232013-01-17 09:30:58 +0100595static int netconf_editconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, NC_EDIT_TESTOPT_TYPE testopt, const char* config)
Radek Krejci62ab34b2012-07-26 13:42:05 +0200596{
597 nc_rpc* rpc;
598 int retval = EXIT_SUCCESS;
599
600 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100601 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
602 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200603 if (rpc == NULL) {
604 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
605 return (EXIT_FAILURE);
606 }
607
608 retval = netconf_op(server, conns, session_key, rpc);
609 nc_rpc_free (rpc);
610 return (retval);
611}
612
Radek Krejci80c10d92012-07-30 08:38:50 +0200613static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200614{
615 nc_rpc* rpc;
616 int retval = EXIT_SUCCESS;
617
618 /* create requests */
619 rpc = nc_rpc_killsession(sid);
620 if (rpc == NULL) {
621 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
622 return (EXIT_FAILURE);
623 }
624
625 retval = netconf_op(server, conns, session_key, rpc);
626 nc_rpc_free (rpc);
627 return (retval);
628}
629
Radek Krejci80c10d92012-07-30 08:38:50 +0200630static int netconf_onlytargetop(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200631{
632 nc_rpc* rpc;
633 int retval = EXIT_SUCCESS;
634
635 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200636 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200637 if (rpc == NULL) {
638 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
639 return (EXIT_FAILURE);
640 }
641
642 retval = netconf_op(server, conns, session_key, rpc);
643 nc_rpc_free (rpc);
644 return (retval);
645}
646
Radek Krejci80c10d92012-07-30 08:38:50 +0200647static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200648{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200649 nc_rpc *rpc = NULL;
650 if (target != NC_DATASTORE_URL) {
651 rpc = nc_rpc_deleteconfig(target);
652 } else {
653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
654 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
655 return (EXIT_FAILURE);
656 }
657
658 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200659}
660
Radek Krejci80c10d92012-07-30 08:38:50 +0200661static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200662{
663 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
664}
665
Radek Krejci80c10d92012-07-30 08:38:50 +0200666static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200667{
668 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
669}
670
Radek Krejci80c10d92012-07-30 08:38:50 +0200671/**
672 * @return REPLY_OK: 0, *data == NULL
673 * REPLY_DATA: 0, *data != NULL
674 * REPLY_ERROR: 1, *data == NULL
675 */
676static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
677{
678 struct nc_session *session = NULL;
679 nc_reply* reply;
680 nc_rpc* rpc;
681 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100682 NC_MSG_TYPE msgt;
683 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200684
685 /* create requests */
686 rpc = nc_rpc_generic(content);
687 if (rpc == NULL) {
688 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
689 return (EXIT_FAILURE);
690 }
691
Radek Krejcia332b692012-11-12 16:15:54 +0100692 if (data != NULL) {
693 *data = NULL;
694 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200695
696 /* get session where send the RPC */
697 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
698 if (session != NULL) {
699 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100700 msgt = nc_session_send_recv(session, rpc, &reply);
701 nc_rpc_free (rpc);
702
703 /* process the result of the operation */
704 switch (msgt) {
705 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200706 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
708 netconf_close(server, conns, session_key);
709 return (EXIT_FAILURE);
710 }
Radek Krejcia332b692012-11-12 16:15:54 +0100711 /* no break */
712 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200713 /* there is error handled by callback */
714 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200715 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100716 case NC_MSG_REPLY:
717 switch (replyt = nc_reply_get_type(reply)) {
718 case NC_REPLY_DATA:
719 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
720 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
721 nc_reply_free(reply);
722 return (EXIT_FAILURE);
723 }
724 retval = EXIT_SUCCESS;
725 break;
726 case NC_REPLY_OK:
727 retval = EXIT_SUCCESS;
728 break;
729 default:
730 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
731 retval = EXIT_FAILURE;
732 break;
733 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200734 break;
735 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100736 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200737 retval = EXIT_FAILURE;
738 break;
739 }
740 nc_reply_free(reply);
741
742 return (retval);
743 } else {
744 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
745 return (EXIT_FAILURE);
746 }
747}
748
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200749/**
750 * @}
751 *//* netconf_operations */
752
Radek Krejci469aab82012-07-22 18:42:20 +0200753server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200754void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200755{
Radek Krejci7338bde2012-08-10 12:57:30 +0200756 switch (level) {
757 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200758 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200759 break;
760 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200761 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200762 break;
763 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200764 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200765 break;
766 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200767 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200768 break;
769 }
Radek Krejci469aab82012-07-22 18:42:20 +0200770}
771
David Kupka8e60a372012-09-04 09:15:20 +0200772void * thread_routine (void * arg)
773{
774 void * retval = NULL;
775
776 ssize_t buffer_len;
777 struct pollfd fds;
778 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200779 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200780 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200781 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200782 char* session_key, *data;
783 const char *host, *port, *user, *pass;
784 const char *msgtext, *cpbltstr;
785 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100786 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200787 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200788 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200789 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200790 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100791 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200792 NC_EDIT_ERROPT_TYPE erropt_type = 0;
793
794 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
795 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
796 server_rec * server = ((struct pass_to_thread*)arg)->server;
797 int client = ((struct pass_to_thread*)arg)->client;
798
799 char * buffer, chunk_len_str[12], *chunked_msg;
800 char c;
801
802 while (!isterminated) {
803 fds.fd = client;
804 fds.events = POLLIN;
805 fds.revents = 0;
806
807 status = poll(&fds, 1, 1000);
808
809 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
810 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
811 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
812 continue;
813 } else if (status < 0) {
814 /* 0: poll time outed
815 * close socket and ignore this request from the client, it can try it again
816 * -1: poll failed
817 * something wrong happend, close this socket and wait for another request
818 */
819 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
820 close(client);
821 break;
822 }
823 /* status > 0 */
824
825 /* check the status of the socket */
826
827 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
828 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
829 /* close client's socket (it's probably already closed by client */
830 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
831 close(client);
832 break;
833 }
834
835 /* read json in chunked framing */
836 buffer_size = 0;
837 buffer_len = 0;
838 buffer = NULL;
839 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200840 /* read chunk length */
841 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
842 free (buffer);
843 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200844 break;
845 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200846 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
847 free (buffer);
848 buffer = NULL;
849 break;
850 }
851 i=0;
852 memset (chunk_len_str, 0, 12);
853 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
854 if (i==0 && c == '#') {
855 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
856 /* end but invalid */
857 free (buffer);
858 buffer = NULL;
859 }
860 /* end of message, double-loop break */
861 goto msg_complete;
862 }
863 chunk_len_str[i++] = c;
864 }
865 if (c != '\n') {
866 free (buffer);
867 buffer = NULL;
868 break;
869 }
870 if ((chunk_len = atoi (chunk_len_str)) == 0) {
871 free (buffer);
872 buffer = NULL;
873 break;
874 }
875 buffer_size += chunk_len+1;
876 buffer = realloc (buffer, sizeof(char)*buffer_size);
877 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
878 free (buffer);
879 buffer = NULL;
880 break;
881 }
882 buffer_len += ret;
883 }
884msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200885
886 if (buffer != NULL) {
887 request = json_tokener_parse(buffer);
888 operation = json_object_get_int(json_object_object_get(request, "type"));
889
890 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
891 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
892 if (operation != MSG_CONNECT && session_key == NULL) {
893 reply = json_object_new_object();
894 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
895 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
896 msgtext = json_object_to_json_string(reply);
897 send(client, msgtext, strlen(msgtext) + 1, 0);
898 json_object_put(reply);
899 /* there is some stupid client, so close the connection to give a chance to some other client */
900 close(client);
901 break;
902 }
903
904 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100905 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200906 ds_type_t = -1;
907 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
908 if (strcmp(target, "running") == 0) {
909 ds_type_t = NC_DATASTORE_RUNNING;
910 } else if (strcmp(target, "startup") == 0) {
911 ds_type_t = NC_DATASTORE_STARTUP;
912 } else if (strcmp(target, "candidate") == 0) {
913 ds_type_t = NC_DATASTORE_CANDIDATE;
914 }
915 }
916 ds_type_s = -1;
917 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
918 if (strcmp(source, "running") == 0) {
919 ds_type_s = NC_DATASTORE_RUNNING;
920 } else if (strcmp(source, "startup") == 0) {
921 ds_type_s = NC_DATASTORE_STARTUP;
922 } else if (strcmp(source, "candidate") == 0) {
923 ds_type_s = NC_DATASTORE_CANDIDATE;
924 }
925 }
926
927 /* null global JSON error-reply */
928 err_reply = NULL;
929
930 /* prepare reply envelope */
931 reply = json_object_new_object();
932
933 /* process required operation */
934 switch (operation) {
935 case MSG_CONNECT:
936 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
937
938 host = json_object_get_string(json_object_object_get(request, "host"));
939 port = json_object_get_string(json_object_object_get(request, "port"));
940 user = json_object_get_string(json_object_object_get(request, "user"));
941 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200942 capabilities = json_object_object_get(request, "capabilities");
Tomas Cejkaae9efe52013-04-23 13:37:36 +0200943 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
944 cpblts = nc_cpblts_new (NULL);
945 for (i=0; i<len; i++) {
946 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
947 }
948 } else {
949 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
950 }
David Kupka8e60a372012-09-04 09:15:20 +0200951 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
952 if ((host == NULL) || (user == NULL)) {
953 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
954 session_key = NULL;
955 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200956 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
957 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200958 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
959 }
960
961 reply = json_object_new_object();
962 if (session_key == NULL) {
963 /* negative reply */
964 if (err_reply == NULL) {
965 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
966 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100967 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200968 } else {
969 /* use filled err_reply from libnetconf's callback */
970 json_object_put(reply);
971 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100972 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200973 }
974 } else {
975 /* positive reply */
976 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
977 json_object_object_add(reply, "session", json_object_new_string(session_key));
978
979 free(session_key);
980 }
981
982 break;
983 case MSG_GET:
984 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
985
986 filter = json_object_get_string(json_object_object_get(request, "filter"));
987
988 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
989
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200990 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200991 if (err_reply == NULL) {
992 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
993 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
994 } else {
995 /* use filled err_reply from libnetconf's callback */
996 json_object_put(reply);
997 reply = err_reply;
998 }
999 } else {
1000 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1001 json_object_object_add(reply, "data", json_object_new_string(data));
1002 }
1003 break;
1004 case MSG_GETCONFIG:
1005 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1006
1007 filter = json_object_get_string(json_object_object_get(request, "filter"));
1008
1009 if (ds_type_s == -1) {
1010 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1011 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1012 break;
1013 }
1014
1015 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1016 if (err_reply == NULL) {
1017 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1018 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1019 } else {
1020 /* use filled err_reply from libnetconf's callback */
1021 json_object_put(reply);
1022 reply = err_reply;
1023 }
1024 } else {
1025 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1026 json_object_object_add(reply, "data", json_object_new_string(data));
1027 }
1028 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001029 case MSG_GETSCHEMA:
1030 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1031 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1032 if (identifier == NULL) {
1033 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1034 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1035 break;
1036 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001037 version = json_object_get_string(json_object_object_get(request, "version"));
1038 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001039
Tomas Cejka94da2c52013-01-08 18:20:30 +01001040 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001041 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001042 if (err_reply == NULL) {
1043 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1044 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1045 } else {
1046 /* use filled err_reply from libnetconf's callback */
1047 json_object_put(reply);
1048 reply = err_reply;
1049 }
1050 } else {
1051 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1052 json_object_object_add(reply, "data", json_object_new_string(data));
1053 }
1054 break;
David Kupka8e60a372012-09-04 09:15:20 +02001055 case MSG_EDITCONFIG:
1056 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1057
1058 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1059 if (defop != NULL) {
1060 if (strcmp(defop, "merge") == 0) {
1061 defop_type = NC_EDIT_DEFOP_MERGE;
1062 } else if (strcmp(defop, "replace") == 0) {
1063 defop_type = NC_EDIT_DEFOP_REPLACE;
1064 } else if (strcmp(defop, "none") == 0) {
1065 defop_type = NC_EDIT_DEFOP_NONE;
1066 } else {
1067 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1068 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1069 break;
1070 }
1071 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001072 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001073 }
1074
1075 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1076 if (erropt != NULL) {
1077 if (strcmp(erropt, "continue-on-error") == 0) {
1078 erropt_type = NC_EDIT_ERROPT_CONT;
1079 } else if (strcmp(erropt, "stop-on-error") == 0) {
1080 erropt_type = NC_EDIT_ERROPT_STOP;
1081 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1082 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1083 } else {
1084 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1085 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1086 break;
1087 }
1088 } else {
1089 erropt_type = 0;
1090 }
1091
1092 if (ds_type_t == -1) {
1093 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1094 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1095 break;
1096 }
1097
1098 config = json_object_get_string(json_object_object_get(request, "config"));
1099 if (config == NULL) {
1100 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1101 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1102 break;
1103 }
1104
Tomas Cejka5064c232013-01-17 09:30:58 +01001105 /* TODO url capability see netconf_editconfig */
1106 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1107 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, NC_EDIT_TESTOPT_NOTSET, config) != EXIT_SUCCESS) {
David Kupka8e60a372012-09-04 09:15:20 +02001108 if (err_reply == NULL) {
1109 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1110 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1111 } else {
1112 /* use filled err_reply from libnetconf's callback */
1113 json_object_put(reply);
1114 reply = err_reply;
1115 }
1116 } else {
1117 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1118 }
1119 break;
1120 case MSG_COPYCONFIG:
1121 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1122 config = NULL;
1123
1124 if (source == NULL) {
1125 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001126 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001127 config = json_object_get_string(json_object_object_get(request, "config"));
1128 } else if (ds_type_s == -1) {
1129 /* source datastore specified, but it is invalid */
1130 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1131 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1132 break;
1133 }
1134
1135 if (ds_type_t == -1) {
1136 /* invalid target datastore specified */
1137 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1138 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1139 break;
1140 }
1141
1142 if (source == NULL && config == NULL) {
1143 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1144 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1145 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001146 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config, "") != EXIT_SUCCESS) {
David Kupka8e60a372012-09-04 09:15:20 +02001147 if (err_reply == NULL) {
1148 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1149 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1150 } else {
1151 /* use filled err_reply from libnetconf's callback */
1152 json_object_put(reply);
1153 reply = err_reply;
1154 }
1155 } else {
1156 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1157 }
1158 }
1159 break;
1160 case MSG_DELETECONFIG:
1161 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1162 /* no break - unifying code */
1163 case MSG_LOCK:
1164 if (operation == MSG_LOCK) {
1165 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1166 }
1167 /* no break - unifying code */
1168 case MSG_UNLOCK:
1169 if (operation == MSG_UNLOCK) {
1170 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1171 }
1172
1173 if (ds_type_t == -1) {
1174 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1175 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1176 break;
1177 }
1178
1179 switch(operation) {
1180 case MSG_DELETECONFIG:
1181 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1182 break;
1183 case MSG_LOCK:
1184 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1185 break;
1186 case MSG_UNLOCK:
1187 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1188 break;
1189 default:
1190 status = -1;
1191 break;
1192 }
1193
1194 if (status != EXIT_SUCCESS) {
1195 if (err_reply == NULL) {
1196 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1197 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1198 } else {
1199 /* use filled err_reply from libnetconf's callback */
1200 json_object_put(reply);
1201 reply = err_reply;
1202 }
1203 } else {
1204 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1205 }
1206 break;
1207 case MSG_KILL:
1208 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1209
1210 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1211
1212 if (sid == NULL) {
1213 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1214 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1215 break;
1216 }
1217
1218 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1219 if (err_reply == NULL) {
1220 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1221 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1222 } else {
1223 /* use filled err_reply from libnetconf's callback */
1224 json_object_put(reply);
1225 reply = err_reply;
1226 }
1227 } else {
1228 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1229 }
1230 break;
1231 case MSG_DISCONNECT:
1232 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1233
1234 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1235 if (err_reply == NULL) {
1236 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1237 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1238 } else {
1239 /* use filled err_reply from libnetconf's callback */
1240 json_object_put(reply);
1241 reply = err_reply;
1242 }
1243 } else {
1244 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1245 }
1246 break;
1247 case MSG_INFO:
1248 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1249
Kupka Davidda134a12012-09-06 14:12:16 +02001250 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1251 if (locked_session != NULL) {
1252 session = locked_session->session;
1253 } else {
1254 session = NULL;
1255 }
David Kupka8e60a372012-09-04 09:15:20 +02001256 if (session != NULL) {
1257 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1258 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1259 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1260 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1261 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1262 cpblts = nc_session_get_cpblts (session);
1263 if (cpblts != NULL) {
1264 json_obj = json_object_new_array();
1265 nc_cpblts_iter_start (cpblts);
1266 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1267 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1268 }
1269 json_object_object_add(reply, "capabilities", json_obj);
1270 }
1271 } else {
1272 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1273 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1274 }
1275
1276 break;
1277 case MSG_GENERIC:
1278 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1279
1280 config = json_object_get_string(json_object_object_get(request, "content"));
1281
1282 if (config == NULL) {
1283 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1284 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1285 break;
1286 }
1287
1288 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1289 if (err_reply == NULL) {
1290 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1291 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1292 } else {
1293 /* use filled err_reply from libnetconf's callback */
1294 json_object_put(reply);
1295 reply = err_reply;
1296 }
1297 } else {
1298 if (data == NULL) {
1299 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1300 } else {
1301 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1302 json_object_object_add(reply, "data", json_object_new_string(data));
1303 }
1304 }
1305 break;
1306 default:
1307 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1308 reply = json_object_new_object();
1309 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1310 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1311 break;
1312 }
1313 json_object_put(request);
1314
David Kupka1e3e4c82012-09-04 09:32:15 +02001315 /* send reply to caller */
1316 if (reply != NULL) {
1317 msgtext = json_object_to_json_string(reply);
1318 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1319 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001320 break;
1321 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001322 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1323 json_object_put(reply);
1324 free (chunked_msg);
1325 free (buffer);
1326 } else {
1327 break;
David Kupka8e60a372012-09-04 09:15:20 +02001328 }
1329 }
1330 }
David Kupka8e60a372012-09-04 09:15:20 +02001331 free (arg);
1332
1333 return retval;
1334}
1335
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001336/**
1337 * \brief Close all open NETCONF sessions.
1338 *
1339 * During termination of mod_netconf, it is useful to close all remaining
1340 * sessions. This function iterates over the list of sessions and close them
1341 * all.
1342 *
1343 * \param[in] server pointer to server_rec for logging
1344 * \param[in] p apr pool needed for hash table iterating
1345 * \param[in] ht hash table of session_with_mutex structs
1346 */
1347static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1348{
1349 apr_hash_index_t *hi;
1350 void *val = NULL;
1351 struct session_with_mutex *swm = NULL;
1352 struct nc_session *ns = NULL;
1353 const char *hashed_key = NULL;
1354 apr_ssize_t hashed_key_length;
1355
1356 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1357 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1358 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1359 swm = (struct session_with_mutex *) val;
1360 if (swm != NULL) {
1361 pthread_mutex_lock(&swm->lock);
1362 if (swm->session != NULL) {
1363 ns = swm->session;
1364 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1365 nc_session_free(ns);
1366 swm->session = NULL;
1367 }
1368 pthread_mutex_unlock(&swm->lock);
1369 }
1370 }
1371}
1372
1373static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1374{
1375 apr_hash_index_t *hi;
1376 void *val = NULL;
1377 struct nc_session *ns = NULL;
1378 struct session_with_mutex *swm = NULL;
1379 const char *hashed_key = NULL;
1380 apr_ssize_t hashed_key_length;
1381 apr_time_t current_time = apr_time_now();
1382
1383 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1384 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1385 swm = (struct session_with_mutex *) val;
1386 if (swm == NULL) {
1387 continue;
1388 }
1389 ns = swm->session;
1390 if (ns == NULL) {
1391 continue;
1392 }
1393 pthread_mutex_lock(&swm->lock);
1394 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1395 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1396 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1397 nc_session_free (ns);
1398 ns = NULL;
1399 /* remove session from the active sessions list */
1400 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1401 }
1402 pthread_mutex_unlock(&swm->lock);
1403 }
1404}
1405
1406
1407/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001408 * This is actually implementation of NETCONF client
1409 * - requests are received from UNIX socket in the predefined format
1410 * - results are replied through the same way
1411 * - the daemon run as a separate process, but it is started and stopped
1412 * automatically by Apache.
1413 *
1414 */
Radek Krejci469aab82012-07-22 18:42:20 +02001415static void forked_proc(apr_pool_t * pool, server_rec * server)
1416{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001417 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001418 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001419 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001420 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001421 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001422 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001423 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001424 struct pass_to_thread * arg;
1425 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1426 struct timespec maxtime;
1427 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001428 #ifdef WITH_NOTIFICATIONS
1429 char use_notifications = 0;
1430 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001431
Tomas Cejka404d37e2013-04-13 02:31:35 +02001432 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001433 maxtime.tv_sec = 5;
1434 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001435
Radek Krejcif23850c2012-07-23 16:14:17 +02001436 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001437 unixd_setup_child();
1438
Radek Krejciae021c12012-07-25 18:03:52 +02001439 cfg = ap_get_module_config(server->module_config, &netconf_module);
1440 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001441 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1442 return;
1443 }
Radek Krejci469aab82012-07-22 18:42:20 +02001444
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001445 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001446 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1447 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1448 return;
1449 }
1450
1451 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001452 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001453 unlink(local.sun_path);
1454 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1455
1456 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1457 if (errno == EADDRINUSE) {
1458 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1459 close(lsock);
1460 exit(0);
1461 }
1462 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1463 close(lsock);
1464 return;
1465 }
1466
1467 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1468 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1469 close(lsock);
1470 return;
1471 }
1472
Tomas Cejkaba21b382013-04-13 02:37:32 +02001473 /* prepare internal lists */
1474 netconf_sessions_list = apr_hash_make(pool);
1475
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001476 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001477 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001478 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1479 use_notifications = 0;
1480 } else {
1481 use_notifications = 1;
1482 }
1483 #endif
1484
Radek Krejci469aab82012-07-22 18:42:20 +02001485 /* setup libnetconf's callbacks */
1486 nc_verbosity(NC_VERB_DEBUG);
1487 clb_print_server = server;
1488 nc_callback_print(clb_print);
1489 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1490 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1491 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001492 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001493
1494 /* disable publickey authentication */
1495 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1496
Tomas Cejka44e71db2013-04-21 15:50:47 +02001497 ncdflt_set_basic_mode(NCWD_MODE_ALL);
1498
David Kupka8e60a372012-09-04 09:15:20 +02001499 /* create mutex protecting session list */
1500 pthread_rwlockattr_init(&lock_attrs);
1501 /* rwlock is shared only with threads in this process */
1502 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1503 /* create rw lock */
1504 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1505 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1506 close (lsock);
1507 return;
1508 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001509
1510 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001511 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001512 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001513 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001514 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001515 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001516 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1517 }
1518 if (use_notifications == 1) {
1519 notification_handle();
1520 }
1521 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001522 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1523 check_timeout_and_close(server, pool, netconf_sessions_list);
1524 }
Radek Krejci469aab82012-07-22 18:42:20 +02001525
1526 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001527 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001528 if (((unsigned int)tv.tv_sec - olds) > 60) {
1529 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1530 olds = tv.tv_sec;
1531 }
David Kupka8e60a372012-09-04 09:15:20 +02001532 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001533 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1534 apr_sleep(SLEEP_TIME);
1535 continue;
1536 } else if (client == -1 && (errno == EINTR)) {
1537 continue;
1538 } else if (client == -1) {
1539 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1540 continue;
1541 }
Radek Krejci469aab82012-07-22 18:42:20 +02001542
1543 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001544 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001545
David Kupka8e60a372012-09-04 09:15:20 +02001546 arg = malloc (sizeof(struct pass_to_thread));
1547 arg->client = client;
1548 arg->pool = pool;
1549 arg->server = server;
1550 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001551
David Kupka8e60a372012-09-04 09:15:20 +02001552 /* start new thread. It will serve this particular request and then terminate */
1553 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1554 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1555 } else {
1556 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1557 pthread_count++;
1558 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1559 ptids[pthread_count] = 0;
1560 }
Radek Krejci469aab82012-07-22 18:42:20 +02001561
David Kupka8e60a372012-09-04 09:15:20 +02001562 /* check if some thread already terminated, free some resources by joining it */
1563 for (i=0; i<pthread_count; i++) {
1564 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1566 pthread_count--;
1567 if (pthread_count > 0) {
1568 /* place last Thread ID on the place of joined one */
1569 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001570 }
Radek Krejci469aab82012-07-22 18:42:20 +02001571 }
1572 }
David Kupka8e60a372012-09-04 09:15:20 +02001573 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001574 }
1575
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001576 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001577 /* join all threads */
1578 for (i=0; i<pthread_count; i++) {
1579 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1580 }
1581 free (ptids);
1582
Radek Krejci469aab82012-07-22 18:42:20 +02001583 close(lsock);
1584
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001585 #ifdef WITH_NOTIFICATIONS
1586 notification_close();
1587 #endif
1588
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001589 /* close all NETCONF sessions */
1590 close_all_nc_sessions(server, pool, netconf_sessions_list);
1591
David Kupka8e60a372012-09-04 09:15:20 +02001592 /* destroy rwlock */
1593 pthread_rwlock_destroy(&session_lock);
1594 pthread_rwlockattr_destroy(&lock_attrs);
1595
Radek Krejci469aab82012-07-22 18:42:20 +02001596 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1597
1598 exit(APR_SUCCESS);
1599}
1600
Radek Krejcif23850c2012-07-23 16:14:17 +02001601static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001602{
Radek Krejcif23850c2012-07-23 16:14:17 +02001603 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1604
1605 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1606 apr_pool_create(&config->pool, pool);
1607 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001608 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001609
1610 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001611}
1612
1613static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1614 apr_pool_t * plog, server_rec * s)
1615{
Radek Krejcif23850c2012-07-23 16:14:17 +02001616 mod_netconf_cfg *config;
1617 apr_status_t res;
1618
Radek Krejci469aab82012-07-22 18:42:20 +02001619 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001620 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001621 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001622
1623 /*
1624 * The following checks if this routine has been called before.
1625 * This is necessary because the parent process gets initialized
1626 * a couple of times as the server starts up.
1627 */
1628 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1629 if (!data) {
1630 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1631 return (OK);
1632 }
1633
Radek Krejcif23850c2012-07-23 16:14:17 +02001634 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1635 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001636
Radek Krejcif23850c2012-07-23 16:14:17 +02001637 if (config && config->forkproc == NULL) {
1638 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1639 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001640 switch (res) {
1641 case APR_INCHILD:
1642 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001643 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001644 apr_signal(SIGTERM, signal_handler);
1645
1646 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001647 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001648
1649 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001650 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001651
Radek Krejcif23850c2012-07-23 16:14:17 +02001652 /* I never should be here, wtf?!? */
1653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001654 exit(APR_EGENERAL);
1655 break;
1656 case APR_INPARENT:
1657 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001658 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001659 break;
1660 default:
1661 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1662 break;
1663 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001664 } else {
1665 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001666 }
1667
1668 return OK;
1669}
1670
Radek Krejci469aab82012-07-22 18:42:20 +02001671/**
1672 * Register module hooks
1673 */
1674static void mod_netconf_register_hooks(apr_pool_t * p)
1675{
Radek Krejcif23850c2012-07-23 16:14:17 +02001676 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001677}
1678
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001679static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1680{
1681 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1682 return NULL;
1683}
1684
1685static const command_rec netconf_cmds[] = {
1686 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1687 {NULL}
1688};
1689
Radek Krejci469aab82012-07-22 18:42:20 +02001690/* Dispatch list for API hooks */
1691module AP_MODULE_DECLARE_DATA netconf_module = {
1692 STANDARD20_MODULE_STUFF,
1693 NULL, /* create per-dir config structures */
1694 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001695 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001696 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001697 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001698 mod_netconf_register_hooks /* register hooks */
1699};
Radek Krejcia332b692012-11-12 16:15:54 +01001700