blob: 932fb1f1777a4ef618b0cfc89e7487d93a84a7a6 [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>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020070
Tomas Cejkad340dbf2013-03-24 20:36:57 +010071#ifdef WITH_NOTIFICATIONS
72#include "notification_module.h"
73#endif
74
Tomas Cejka94da2c52013-01-08 18:20:30 +010075#include "message_type.h"
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020076#include "mod_netconf.h"
Radek Krejci469aab82012-07-22 18:42:20 +020077
78#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020079#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020080#define MAX_SOCKET_CL 10
81#define BUFFER_SIZE 4096
Tomas Cejkaaf7a1562013-04-13 02:27:43 +020082#define NOTIFICATION_QUEUE_SIZE 10
83#define ACTIVITY_CHECK_INTERVAL 10 /**< timeout in seconds, how often activity is checked */
Tomas Cejka04e39952013-04-19 11:49:38 +020084#define ACTIVITY_TIMEOUT (60*60) /**< timeout in seconds, after this time, session is automaticaly closed. */
Radek Krejci469aab82012-07-22 18:42:20 +020085
86/* sleep in master process for non-blocking socket reading */
87#define SLEEP_TIME 200
88
89#ifndef offsetof
90#define offsetof(type, member) ((size_t) ((type *) 0)->member)
91#endif
92
Tomas Cejka027f3bc2012-11-10 20:28:36 +010093/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020094struct timeval timeout = { 1, 0 };
95
Tomas Cejka5064c232013-01-17 09:30:58 +010096#define NCWITHDEFAULTS NCWD_MODE_NOTSET
97
98
Radek Krejci469aab82012-07-22 18:42:20 +020099#define MSG_OK 0
100#define MSG_OPEN 1
101#define MSG_DATA 2
102#define MSG_CLOSE 3
103#define MSG_ERROR 4
104#define MSG_UNKNOWN 5
105
Radek Krejci469aab82012-07-22 18:42:20 +0200106module AP_MODULE_DECLARE_DATA netconf_module;
107
David Kupka8e60a372012-09-04 09:15:20 +0200108pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
109
Radek Krejci469aab82012-07-22 18:42:20 +0200110volatile int isterminated = 0;
111
112static char* password;
113
Radek Krejci469aab82012-07-22 18:42:20 +0200114static void signal_handler(int sign)
115{
116 switch (sign) {
117 case SIGTERM:
118 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200119 break;
120 }
121}
122
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200123static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200124{
Radek Krejcif23850c2012-07-23 16:14:17 +0200125 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
126 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200127 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200128
Radek Krejci469aab82012-07-22 18:42:20 +0200129 apr_sha1_ctx_t sha1_ctx;
130 apr_sha1_init(&sha1_ctx);
131 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
132 apr_sha1_update(&sha1_ctx, port, strlen(port));
133 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200134 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200135
Radek Krejcif23850c2012-07-23 16:14:17 +0200136 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200137 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200138 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200139 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200140 }
141 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200142
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200144}
145
146int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
147{
148 /* always approve */
149 return (EXIT_SUCCESS);
150}
151
152char* netconf_callback_sshauth_password (const char* username, const char* hostname)
153{
154 char* buf;
155
156 buf = malloc ((strlen(password) + 1) * sizeof(char));
157 apr_cpystrn(buf, password, strlen(password) + 1);
158
159 return (buf);
160}
161
162void netconf_callback_sshauth_interactive (const char* name,
163 int name_len,
164 const char* instruction,
165 int instruction_len,
166 int num_prompts,
167 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
168 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
169 void** abstract)
170{
171 int i;
172
173 for (i=0; i<num_prompts; i++) {
174 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
175 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
176 responses[i].length = strlen(responses[i].text) + 1;
177 }
178
179 return;
180}
181
Radek Krejcic11fd862012-07-26 12:41:21 +0200182static json_object *err_reply = NULL;
183void netconf_callback_error_process(const char* tag,
184 const char* type,
185 const char* severity,
186 const char* apptag,
187 const char* path,
188 const char* message,
189 const char* attribute,
190 const char* element,
191 const char* ns,
192 const char* sid)
193{
194 err_reply = json_object_new_object();
195 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
196 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
197 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
198 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
199 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
200 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
201 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
202 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
203 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
204 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
205 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
206}
207
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200208/**
209 * \defgroup netconf_operations NETCONF operations
210 * The list of NETCONF operations that mod_netconf supports.
211 * @{
212 */
213
214/**
215 * \brief Connect to NETCONF server
216 *
217 * \warning Session_key hash is not bound with caller identification. This could be potential security risk.
218 */
Kupka David00b9c5c2012-09-05 09:45:50 +0200219static 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 +0200220{
Radek Krejci469aab82012-07-22 18:42:20 +0200221 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200222 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200223 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200224
225 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200226 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200227 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
228
Radek Krejci469aab82012-07-22 18:42:20 +0200229 /* if connected successful, add session to the list */
230 if (session != NULL) {
231 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200232 session_key = gen_ncsession_hash(
233 (host==NULL) ? "localhost" : host,
234 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200235 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200236
Tomas Cejkaba21b382013-04-13 02:37:32 +0200237 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200238 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
239 nc_session_free(session);
240 free (locked_session);
241 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
242 return NULL;
243 }
244 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200245 locked_session->last_activity = apr_time_now();
David Kupka8e60a372012-09-04 09:15:20 +0200246 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100247 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200248 /* get exclusive access to sessions_list (conns) */
249 if (pthread_rwlock_wrlock (&session_lock) != 0) {
250 nc_session_free(session);
251 free (locked_session);
252 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
253 return NULL;
254 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200255 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100256 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200257 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100258 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200259 /* end of critical section */
260 if (pthread_rwlock_unlock (&session_lock) != 0) {
261 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
262 return NULL;
263 }
Radek Krejci469aab82012-07-22 18:42:20 +0200264 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200265 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200266 } else {
267 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200268 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200269 }
270
Radek Krejci469aab82012-07-22 18:42:20 +0200271}
272
Radek Krejci80c10d92012-07-30 08:38:50 +0200273static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200274{
275 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200276 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200277
Radek Krejcif23850c2012-07-23 16:14:17 +0200278 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200279 /* get exclusive (write) access to sessions_list (conns) */
280 if (pthread_rwlock_wrlock (&session_lock) != 0) {
281 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
282 return EXIT_FAILURE;
283 }
284 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
285 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200286 /** \todo free all notifications from queue */
287 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200288 pthread_mutex_destroy(&locked_session->lock);
289 ns = locked_session->session;
290 free (locked_session);
291 }
Radek Krejci469aab82012-07-22 18:42:20 +0200292 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100293 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200294 nc_session_free (ns);
295 ns = NULL;
296
297 /* remove session from the active sessions list */
298 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200299 /* end of critical section */
300 if (pthread_rwlock_unlock (&session_lock) != 0) {
301 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
302 return EXIT_FAILURE;
303 }
Radek Krejci469aab82012-07-22 18:42:20 +0200304 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200305
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200306 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200307 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200308 if (pthread_rwlock_unlock (&session_lock) != 0) {
309 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
310 return EXIT_FAILURE;
311 }
Radek Krejci469aab82012-07-22 18:42:20 +0200312 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200313 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200314 }
315}
316
Radek Krejci80c10d92012-07-30 08:38:50 +0200317static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200318{
319 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200320 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200321 nc_reply* reply;
322 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100323 NC_MSG_TYPE msgt;
324 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200325
Radek Krejci8e4632a2012-07-26 13:40:34 +0200326 /* check requests */
327 if (rpc == NULL) {
328 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
329 return (EXIT_FAILURE);
330 }
331
David Kupka8e60a372012-09-04 09:15:20 +0200332 /* get non-exclusive (read) access to sessions_list (conns) */
333 if (pthread_rwlock_rdlock (&session_lock) != 0) {
334 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
335 return EXIT_FAILURE;
336 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200337 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200338 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
339 if (locked_session != NULL) {
340 session = locked_session->session;
341 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200342 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200343 /* get exclusive access to session */
344 if (pthread_mutex_lock(&locked_session->lock) != 0) {
345 /* unlock before returning error */
346 if (pthread_rwlock_unlock (&session_lock) != 0) {
347 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
348 return EXIT_FAILURE;
349 }
350 return EXIT_FAILURE;
351 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200352 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200353 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100354 msgt = nc_session_send_recv(session, rpc, &reply);
355
David Kupka8e60a372012-09-04 09:15:20 +0200356 /* first release exclusive lock for this session */
357 pthread_mutex_unlock(&locked_session->lock);
358 /* end of critical section */
359 if (pthread_rwlock_unlock (&session_lock) != 0) {
360 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
361 return EXIT_FAILURE;
362 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200363
Radek Krejcia332b692012-11-12 16:15:54 +0100364 /* process the result of the operation */
365 switch (msgt) {
366 case NC_MSG_UNKNOWN:
367 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
368 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
369 netconf_close(server, conns, session_key);
370 return (EXIT_FAILURE);
371 }
372 /* no break */
373 case NC_MSG_NONE:
374 /* there is error handled by callback */
375 return (EXIT_FAILURE);
376 break;
377 case NC_MSG_REPLY:
378 switch (replyt = nc_reply_get_type(reply)) {
379 case NC_REPLY_OK:
380 retval = EXIT_SUCCESS;
381 break;
382 default:
383 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
384 retval = EXIT_FAILURE;
385 break;
386 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200387 break;
388 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100389 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200390 retval = EXIT_FAILURE;
391 break;
392 }
393 nc_reply_free(reply);
394 return (retval);
395 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100396 /* release lock on failure */
397 if (pthread_rwlock_unlock (&session_lock) != 0) {
398 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
399 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200400 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
401 return (EXIT_FAILURE);
402 }
403}
Radek Krejci80c10d92012-07-30 08:38:50 +0200404
405static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200406{
407 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200408 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100409 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200410 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100411 NC_MSG_TYPE msgt;
412 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200413
414 /* check requests */
415 if (rpc == NULL) {
416 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
417 return (NULL);
418 }
419
David Kupka8e60a372012-09-04 09:15:20 +0200420 /* get non-exclusive (read) access to sessions_list (conns) */
421 if (pthread_rwlock_rdlock (&session_lock) != 0) {
422 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
423 return NULL;
424 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200425 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200426 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
427 if (locked_session != NULL) {
428 session = locked_session->session;
429 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200430 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200431 /* get exclusive access to session */
432 if (pthread_mutex_lock(&locked_session->lock) != 0) {
433 /* unlock before returning error */
434 if (pthread_rwlock_unlock (&session_lock) != 0) {
435 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
436 return NULL;
437 }
438 return NULL;
439 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200440 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200441 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100442 msgt = nc_session_send_recv(session, rpc, &reply);
443
David Kupka8e60a372012-09-04 09:15:20 +0200444 /* first release exclusive lock for this session */
445 pthread_mutex_unlock(&locked_session->lock);
446 /* end of critical section */
447 if (pthread_rwlock_unlock (&session_lock) != 0) {
448 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 +0100449 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200450 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200451
Radek Krejcia332b692012-11-12 16:15:54 +0100452 /* process the result of the operation */
453 switch (msgt) {
454 case NC_MSG_UNKNOWN:
455 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
456 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
457 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200458 return (NULL);
459 }
Radek Krejcia332b692012-11-12 16:15:54 +0100460 /* no break */
461 case NC_MSG_NONE:
462 /* there is error handled by callback */
463 return (NULL);
464 break;
465 case NC_MSG_REPLY:
466 switch (replyt = nc_reply_get_type(reply)) {
467 case NC_REPLY_DATA:
468 if ((data = nc_reply_get_data (reply)) == NULL) {
469 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
470 data = NULL;
471 }
472 break;
473 default:
474 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
475 data = NULL;
476 break;
477 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200478 break;
479 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100480 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
481 data = NULL;
482 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200483 }
484 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200485 return (data);
486 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100487 /* release lock on failure */
488 if (pthread_rwlock_unlock (&session_lock) != 0) {
489 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
490 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200491 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
492 return (NULL);
493 }
494}
495
Radek Krejci80c10d92012-07-30 08:38:50 +0200496static 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 +0200497{
498 nc_rpc* rpc;
499 struct nc_filter *f = NULL;
500 char* data = NULL;
501
502 /* create filter if set */
503 if (filter != NULL) {
504 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
505 }
506
507 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100508 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200509 nc_filter_free(f);
510 if (rpc == NULL) {
511 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
512 return (NULL);
513 }
514
515 data = netconf_opdata(server, conns, session_key, rpc);
516 nc_rpc_free (rpc);
517 return (data);
518}
519
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100520static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
521{
522 nc_rpc* rpc;
523 char* data = NULL;
524
525 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100526 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100527 if (rpc == NULL) {
528 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
529 return (NULL);
530 }
531
532 data = netconf_opdata(server, conns, session_key, rpc);
533 nc_rpc_free (rpc);
534 return (data);
535}
536
Radek Krejci80c10d92012-07-30 08:38:50 +0200537static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200538{
539 nc_rpc* rpc;
540 struct nc_filter *f = NULL;
541 char* data = NULL;
542
543 /* create filter if set */
544 if (filter != NULL) {
545 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
546 }
547
548 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100549 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200550 nc_filter_free(f);
551 if (rpc == NULL) {
552 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
553 return (NULL);
554 }
555
556 data = netconf_opdata(server, conns, session_key, rpc);
557 nc_rpc_free (rpc);
558 return (data);
559}
560
Tomas Cejka5064c232013-01-17 09:30:58 +0100561static 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 +0200562{
563 nc_rpc* rpc;
564 int retval = EXIT_SUCCESS;
565
566 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100567 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
568 if (target == NC_DATASTORE_URL) {
569 rpc = nc_rpc_copyconfig(source, target, config, url);
570 } else {
571 rpc = nc_rpc_copyconfig(source, target, config);
572 }
573 } else {
574 if (target == NC_DATASTORE_URL) {
575 rpc = nc_rpc_copyconfig(source, target, url);
576 } else {
577 rpc = nc_rpc_copyconfig(source, target);
578 }
579 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200580 if (rpc == NULL) {
581 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
582 return (EXIT_FAILURE);
583 }
584
585 retval = netconf_op(server, conns, session_key, rpc);
586 nc_rpc_free (rpc);
587 return (retval);
588}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200589
Tomas Cejka5064c232013-01-17 09:30:58 +0100590static 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 +0200591{
592 nc_rpc* rpc;
593 int retval = EXIT_SUCCESS;
594
595 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100596 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
597 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200598 if (rpc == NULL) {
599 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
600 return (EXIT_FAILURE);
601 }
602
603 retval = netconf_op(server, conns, session_key, rpc);
604 nc_rpc_free (rpc);
605 return (retval);
606}
607
Radek Krejci80c10d92012-07-30 08:38:50 +0200608static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200609{
610 nc_rpc* rpc;
611 int retval = EXIT_SUCCESS;
612
613 /* create requests */
614 rpc = nc_rpc_killsession(sid);
615 if (rpc == NULL) {
616 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
617 return (EXIT_FAILURE);
618 }
619
620 retval = netconf_op(server, conns, session_key, rpc);
621 nc_rpc_free (rpc);
622 return (retval);
623}
624
Radek Krejci80c10d92012-07-30 08:38:50 +0200625static 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 +0200626{
627 nc_rpc* rpc;
628 int retval = EXIT_SUCCESS;
629
630 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200631 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200632 if (rpc == NULL) {
633 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
634 return (EXIT_FAILURE);
635 }
636
637 retval = netconf_op(server, conns, session_key, rpc);
638 nc_rpc_free (rpc);
639 return (retval);
640}
641
Radek Krejci80c10d92012-07-30 08:38:50 +0200642static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200643{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200644 nc_rpc *rpc = NULL;
645 if (target != NC_DATASTORE_URL) {
646 rpc = nc_rpc_deleteconfig(target);
647 } else {
648 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
649 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
650 return (EXIT_FAILURE);
651 }
652
653 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200654}
655
Radek Krejci80c10d92012-07-30 08:38:50 +0200656static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200657{
658 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
659}
660
Radek Krejci80c10d92012-07-30 08:38:50 +0200661static int netconf_unlock(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_unlock));
664}
665
Radek Krejci80c10d92012-07-30 08:38:50 +0200666/**
667 * @return REPLY_OK: 0, *data == NULL
668 * REPLY_DATA: 0, *data != NULL
669 * REPLY_ERROR: 1, *data == NULL
670 */
671static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
672{
673 struct nc_session *session = NULL;
674 nc_reply* reply;
675 nc_rpc* rpc;
676 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100677 NC_MSG_TYPE msgt;
678 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200679
680 /* create requests */
681 rpc = nc_rpc_generic(content);
682 if (rpc == NULL) {
683 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
684 return (EXIT_FAILURE);
685 }
686
Radek Krejcia332b692012-11-12 16:15:54 +0100687 if (data != NULL) {
688 *data = NULL;
689 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200690
691 /* get session where send the RPC */
692 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
693 if (session != NULL) {
694 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100695 msgt = nc_session_send_recv(session, rpc, &reply);
696 nc_rpc_free (rpc);
697
698 /* process the result of the operation */
699 switch (msgt) {
700 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200701 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
702 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
703 netconf_close(server, conns, session_key);
704 return (EXIT_FAILURE);
705 }
Radek Krejcia332b692012-11-12 16:15:54 +0100706 /* no break */
707 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200708 /* there is error handled by callback */
709 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200710 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100711 case NC_MSG_REPLY:
712 switch (replyt = nc_reply_get_type(reply)) {
713 case NC_REPLY_DATA:
714 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
715 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
716 nc_reply_free(reply);
717 return (EXIT_FAILURE);
718 }
719 retval = EXIT_SUCCESS;
720 break;
721 case NC_REPLY_OK:
722 retval = EXIT_SUCCESS;
723 break;
724 default:
725 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
726 retval = EXIT_FAILURE;
727 break;
728 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200729 break;
730 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100731 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200732 retval = EXIT_FAILURE;
733 break;
734 }
735 nc_reply_free(reply);
736
737 return (retval);
738 } else {
739 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
740 return (EXIT_FAILURE);
741 }
742}
743
Tomas Cejka0a4bba82013-04-19 11:51:28 +0200744/**
745 * @}
746 *//* netconf_operations */
747
Radek Krejci469aab82012-07-22 18:42:20 +0200748server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200749void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200750{
Radek Krejci7338bde2012-08-10 12:57:30 +0200751 switch (level) {
752 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200753 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200754 break;
755 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200756 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200757 break;
758 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200759 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200760 break;
761 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200762 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200763 break;
764 }
Radek Krejci469aab82012-07-22 18:42:20 +0200765}
766
David Kupka8e60a372012-09-04 09:15:20 +0200767void * thread_routine (void * arg)
768{
769 void * retval = NULL;
770
771 ssize_t buffer_len;
772 struct pollfd fds;
773 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200774 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200775 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200776 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200777 char* session_key, *data;
778 const char *host, *port, *user, *pass;
779 const char *msgtext, *cpbltstr;
780 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100781 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200782 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200783 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200784 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200785 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100786 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200787 NC_EDIT_ERROPT_TYPE erropt_type = 0;
788
789 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
790 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
791 server_rec * server = ((struct pass_to_thread*)arg)->server;
792 int client = ((struct pass_to_thread*)arg)->client;
793
794 char * buffer, chunk_len_str[12], *chunked_msg;
795 char c;
796
797 while (!isterminated) {
798 fds.fd = client;
799 fds.events = POLLIN;
800 fds.revents = 0;
801
802 status = poll(&fds, 1, 1000);
803
804 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
805 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
806 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
807 continue;
808 } else if (status < 0) {
809 /* 0: poll time outed
810 * close socket and ignore this request from the client, it can try it again
811 * -1: poll failed
812 * something wrong happend, close this socket and wait for another request
813 */
814 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
815 close(client);
816 break;
817 }
818 /* status > 0 */
819
820 /* check the status of the socket */
821
822 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
823 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
824 /* close client's socket (it's probably already closed by client */
825 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
826 close(client);
827 break;
828 }
829
830 /* read json in chunked framing */
831 buffer_size = 0;
832 buffer_len = 0;
833 buffer = NULL;
834 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200835 /* read chunk length */
836 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
837 free (buffer);
838 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200839 break;
840 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200841 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
842 free (buffer);
843 buffer = NULL;
844 break;
845 }
846 i=0;
847 memset (chunk_len_str, 0, 12);
848 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
849 if (i==0 && c == '#') {
850 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
851 /* end but invalid */
852 free (buffer);
853 buffer = NULL;
854 }
855 /* end of message, double-loop break */
856 goto msg_complete;
857 }
858 chunk_len_str[i++] = c;
859 }
860 if (c != '\n') {
861 free (buffer);
862 buffer = NULL;
863 break;
864 }
865 if ((chunk_len = atoi (chunk_len_str)) == 0) {
866 free (buffer);
867 buffer = NULL;
868 break;
869 }
870 buffer_size += chunk_len+1;
871 buffer = realloc (buffer, sizeof(char)*buffer_size);
872 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
873 free (buffer);
874 buffer = NULL;
875 break;
876 }
877 buffer_len += ret;
878 }
879msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200880
881 if (buffer != NULL) {
882 request = json_tokener_parse(buffer);
883 operation = json_object_get_int(json_object_object_get(request, "type"));
884
885 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
886 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
887 if (operation != MSG_CONNECT && session_key == NULL) {
888 reply = json_object_new_object();
889 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
890 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
891 msgtext = json_object_to_json_string(reply);
892 send(client, msgtext, strlen(msgtext) + 1, 0);
893 json_object_put(reply);
894 /* there is some stupid client, so close the connection to give a chance to some other client */
895 close(client);
896 break;
897 }
898
899 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100900 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200901 ds_type_t = -1;
902 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
903 if (strcmp(target, "running") == 0) {
904 ds_type_t = NC_DATASTORE_RUNNING;
905 } else if (strcmp(target, "startup") == 0) {
906 ds_type_t = NC_DATASTORE_STARTUP;
907 } else if (strcmp(target, "candidate") == 0) {
908 ds_type_t = NC_DATASTORE_CANDIDATE;
909 }
910 }
911 ds_type_s = -1;
912 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
913 if (strcmp(source, "running") == 0) {
914 ds_type_s = NC_DATASTORE_RUNNING;
915 } else if (strcmp(source, "startup") == 0) {
916 ds_type_s = NC_DATASTORE_STARTUP;
917 } else if (strcmp(source, "candidate") == 0) {
918 ds_type_s = NC_DATASTORE_CANDIDATE;
919 }
920 }
921
922 /* null global JSON error-reply */
923 err_reply = NULL;
924
925 /* prepare reply envelope */
926 reply = json_object_new_object();
927
928 /* process required operation */
929 switch (operation) {
930 case MSG_CONNECT:
931 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
932
933 host = json_object_get_string(json_object_object_get(request, "host"));
934 port = json_object_get_string(json_object_object_get(request, "port"));
935 user = json_object_get_string(json_object_object_get(request, "user"));
936 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200937 capabilities = json_object_object_get(request, "capabilities");
938 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200939 cpblts = nc_cpblts_new (NULL);
940 for (i=0; i<len; i++) {
941 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
942 }
943 } else {
944 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
945 }
David Kupka8e60a372012-09-04 09:15:20 +0200946 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
947 if ((host == NULL) || (user == NULL)) {
948 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
949 session_key = NULL;
950 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200951 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
952 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200953 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
954 }
955
956 reply = json_object_new_object();
957 if (session_key == NULL) {
958 /* negative reply */
959 if (err_reply == NULL) {
960 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
961 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100962 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200963 } else {
964 /* use filled err_reply from libnetconf's callback */
965 json_object_put(reply);
966 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100967 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200968 }
969 } else {
970 /* positive reply */
971 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
972 json_object_object_add(reply, "session", json_object_new_string(session_key));
973
974 free(session_key);
975 }
976
977 break;
978 case MSG_GET:
979 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
980
981 filter = json_object_get_string(json_object_object_get(request, "filter"));
982
983 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
984
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200985 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200986 if (err_reply == NULL) {
987 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
988 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
989 } else {
990 /* use filled err_reply from libnetconf's callback */
991 json_object_put(reply);
992 reply = err_reply;
993 }
994 } else {
995 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
996 json_object_object_add(reply, "data", json_object_new_string(data));
997 }
998 break;
999 case MSG_GETCONFIG:
1000 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
1001
1002 filter = json_object_get_string(json_object_object_get(request, "filter"));
1003
1004 if (ds_type_s == -1) {
1005 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1006 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1007 break;
1008 }
1009
1010 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
1011 if (err_reply == NULL) {
1012 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1013 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1014 } else {
1015 /* use filled err_reply from libnetconf's callback */
1016 json_object_put(reply);
1017 reply = err_reply;
1018 }
1019 } else {
1020 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1021 json_object_object_add(reply, "data", json_object_new_string(data));
1022 }
1023 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001024 case MSG_GETSCHEMA:
1025 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1026 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1027 if (identifier == NULL) {
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("No identifier for get-schema supplied."));
1030 break;
1031 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001032 version = json_object_get_string(json_object_object_get(request, "version"));
1033 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001034
Tomas Cejka94da2c52013-01-08 18:20:30 +01001035 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001036 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001037 if (err_reply == NULL) {
1038 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1039 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1040 } else {
1041 /* use filled err_reply from libnetconf's callback */
1042 json_object_put(reply);
1043 reply = err_reply;
1044 }
1045 } else {
1046 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1047 json_object_object_add(reply, "data", json_object_new_string(data));
1048 }
1049 break;
David Kupka8e60a372012-09-04 09:15:20 +02001050 case MSG_EDITCONFIG:
1051 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1052
1053 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1054 if (defop != NULL) {
1055 if (strcmp(defop, "merge") == 0) {
1056 defop_type = NC_EDIT_DEFOP_MERGE;
1057 } else if (strcmp(defop, "replace") == 0) {
1058 defop_type = NC_EDIT_DEFOP_REPLACE;
1059 } else if (strcmp(defop, "none") == 0) {
1060 defop_type = NC_EDIT_DEFOP_NONE;
1061 } else {
1062 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1063 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1064 break;
1065 }
1066 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001067 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001068 }
1069
1070 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1071 if (erropt != NULL) {
1072 if (strcmp(erropt, "continue-on-error") == 0) {
1073 erropt_type = NC_EDIT_ERROPT_CONT;
1074 } else if (strcmp(erropt, "stop-on-error") == 0) {
1075 erropt_type = NC_EDIT_ERROPT_STOP;
1076 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1077 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1078 } else {
1079 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1080 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1081 break;
1082 }
1083 } else {
1084 erropt_type = 0;
1085 }
1086
1087 if (ds_type_t == -1) {
1088 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1089 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1090 break;
1091 }
1092
1093 config = json_object_get_string(json_object_object_get(request, "config"));
1094 if (config == NULL) {
1095 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1096 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1097 break;
1098 }
1099
Tomas Cejka5064c232013-01-17 09:30:58 +01001100 /* TODO url capability see netconf_editconfig */
1101 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1102 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 +02001103 if (err_reply == NULL) {
1104 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1105 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1106 } else {
1107 /* use filled err_reply from libnetconf's callback */
1108 json_object_put(reply);
1109 reply = err_reply;
1110 }
1111 } else {
1112 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1113 }
1114 break;
1115 case MSG_COPYCONFIG:
1116 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1117 config = NULL;
1118
1119 if (source == NULL) {
1120 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001121 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001122 config = json_object_get_string(json_object_object_get(request, "config"));
1123 } else if (ds_type_s == -1) {
1124 /* source datastore specified, but it is invalid */
1125 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1126 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1127 break;
1128 }
1129
1130 if (ds_type_t == -1) {
1131 /* invalid target datastore specified */
1132 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1133 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1134 break;
1135 }
1136
1137 if (source == NULL && config == NULL) {
1138 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1139 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1140 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001141 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 +02001142 if (err_reply == 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("copy-config failed."));
1145 } else {
1146 /* use filled err_reply from libnetconf's callback */
1147 json_object_put(reply);
1148 reply = err_reply;
1149 }
1150 } else {
1151 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1152 }
1153 }
1154 break;
1155 case MSG_DELETECONFIG:
1156 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1157 /* no break - unifying code */
1158 case MSG_LOCK:
1159 if (operation == MSG_LOCK) {
1160 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1161 }
1162 /* no break - unifying code */
1163 case MSG_UNLOCK:
1164 if (operation == MSG_UNLOCK) {
1165 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1166 }
1167
1168 if (ds_type_t == -1) {
1169 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1170 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1171 break;
1172 }
1173
1174 switch(operation) {
1175 case MSG_DELETECONFIG:
1176 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1177 break;
1178 case MSG_LOCK:
1179 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1180 break;
1181 case MSG_UNLOCK:
1182 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1183 break;
1184 default:
1185 status = -1;
1186 break;
1187 }
1188
1189 if (status != EXIT_SUCCESS) {
1190 if (err_reply == NULL) {
1191 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1192 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1193 } else {
1194 /* use filled err_reply from libnetconf's callback */
1195 json_object_put(reply);
1196 reply = err_reply;
1197 }
1198 } else {
1199 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1200 }
1201 break;
1202 case MSG_KILL:
1203 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1204
1205 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1206
1207 if (sid == NULL) {
1208 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1209 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1210 break;
1211 }
1212
1213 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1214 if (err_reply == NULL) {
1215 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1216 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1217 } else {
1218 /* use filled err_reply from libnetconf's callback */
1219 json_object_put(reply);
1220 reply = err_reply;
1221 }
1222 } else {
1223 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1224 }
1225 break;
1226 case MSG_DISCONNECT:
1227 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1228
1229 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1230 if (err_reply == NULL) {
1231 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1232 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1233 } else {
1234 /* use filled err_reply from libnetconf's callback */
1235 json_object_put(reply);
1236 reply = err_reply;
1237 }
1238 } else {
1239 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1240 }
1241 break;
1242 case MSG_INFO:
1243 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1244
Kupka Davidda134a12012-09-06 14:12:16 +02001245 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1246 if (locked_session != NULL) {
1247 session = locked_session->session;
1248 } else {
1249 session = NULL;
1250 }
David Kupka8e60a372012-09-04 09:15:20 +02001251 if (session != NULL) {
1252 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1253 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1254 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1255 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1256 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1257 cpblts = nc_session_get_cpblts (session);
1258 if (cpblts != NULL) {
1259 json_obj = json_object_new_array();
1260 nc_cpblts_iter_start (cpblts);
1261 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1262 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1263 }
1264 json_object_object_add(reply, "capabilities", json_obj);
1265 }
1266 } else {
1267 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1268 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1269 }
1270
1271 break;
1272 case MSG_GENERIC:
1273 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1274
1275 config = json_object_get_string(json_object_object_get(request, "content"));
1276
1277 if (config == NULL) {
1278 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1279 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1280 break;
1281 }
1282
1283 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1284 if (err_reply == NULL) {
1285 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1286 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1287 } else {
1288 /* use filled err_reply from libnetconf's callback */
1289 json_object_put(reply);
1290 reply = err_reply;
1291 }
1292 } else {
1293 if (data == NULL) {
1294 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1295 } else {
1296 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1297 json_object_object_add(reply, "data", json_object_new_string(data));
1298 }
1299 }
1300 break;
1301 default:
1302 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1303 reply = json_object_new_object();
1304 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1305 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1306 break;
1307 }
1308 json_object_put(request);
1309
David Kupka1e3e4c82012-09-04 09:32:15 +02001310 /* send reply to caller */
1311 if (reply != NULL) {
1312 msgtext = json_object_to_json_string(reply);
1313 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1314 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001315 break;
1316 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001317 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1318 json_object_put(reply);
1319 free (chunked_msg);
1320 free (buffer);
1321 } else {
1322 break;
David Kupka8e60a372012-09-04 09:15:20 +02001323 }
1324 }
1325 }
David Kupka8e60a372012-09-04 09:15:20 +02001326 free (arg);
1327
1328 return retval;
1329}
1330
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001331/**
1332 * \brief Close all open NETCONF sessions.
1333 *
1334 * During termination of mod_netconf, it is useful to close all remaining
1335 * sessions. This function iterates over the list of sessions and close them
1336 * all.
1337 *
1338 * \param[in] server pointer to server_rec for logging
1339 * \param[in] p apr pool needed for hash table iterating
1340 * \param[in] ht hash table of session_with_mutex structs
1341 */
1342static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1343{
1344 apr_hash_index_t *hi;
1345 void *val = NULL;
1346 struct session_with_mutex *swm = NULL;
1347 struct nc_session *ns = NULL;
1348 const char *hashed_key = NULL;
1349 apr_ssize_t hashed_key_length;
1350
1351 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1352 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1353 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1354 swm = (struct session_with_mutex *) val;
1355 if (swm != NULL) {
1356 pthread_mutex_lock(&swm->lock);
1357 if (swm->session != NULL) {
1358 ns = swm->session;
1359 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1360 nc_session_free(ns);
1361 swm->session = NULL;
1362 }
1363 pthread_mutex_unlock(&swm->lock);
1364 }
1365 }
1366}
1367
1368static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1369{
1370 apr_hash_index_t *hi;
1371 void *val = NULL;
1372 struct nc_session *ns = NULL;
1373 struct session_with_mutex *swm = NULL;
1374 const char *hashed_key = NULL;
1375 apr_ssize_t hashed_key_length;
1376 apr_time_t current_time = apr_time_now();
1377
1378 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1379 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1380 swm = (struct session_with_mutex *) val;
1381 if (swm == NULL) {
1382 continue;
1383 }
1384 ns = swm->session;
1385 if (ns == NULL) {
1386 continue;
1387 }
1388 pthread_mutex_lock(&swm->lock);
1389 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1390 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1391 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1392 nc_session_free (ns);
1393 ns = NULL;
1394 /* remove session from the active sessions list */
1395 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1396 }
1397 pthread_mutex_unlock(&swm->lock);
1398 }
1399}
1400
1401
1402/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001403 * This is actually implementation of NETCONF client
1404 * - requests are received from UNIX socket in the predefined format
1405 * - results are replied through the same way
1406 * - the daemon run as a separate process, but it is started and stopped
1407 * automatically by Apache.
1408 *
1409 */
Radek Krejci469aab82012-07-22 18:42:20 +02001410static void forked_proc(apr_pool_t * pool, server_rec * server)
1411{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001412 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001413 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001414 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001415 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001416 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001417 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001418 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001419 struct pass_to_thread * arg;
1420 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1421 struct timespec maxtime;
1422 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001423 #ifdef WITH_NOTIFICATIONS
1424 char use_notifications = 0;
1425 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001426
Tomas Cejka404d37e2013-04-13 02:31:35 +02001427 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001428 maxtime.tv_sec = 5;
1429 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001430
Radek Krejcif23850c2012-07-23 16:14:17 +02001431 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001432 unixd_setup_child();
1433
Radek Krejciae021c12012-07-25 18:03:52 +02001434 cfg = ap_get_module_config(server->module_config, &netconf_module);
1435 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001436 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1437 return;
1438 }
Radek Krejci469aab82012-07-22 18:42:20 +02001439
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001440 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001441 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1442 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1443 return;
1444 }
1445
1446 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001447 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001448 unlink(local.sun_path);
1449 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1450
1451 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1452 if (errno == EADDRINUSE) {
1453 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1454 close(lsock);
1455 exit(0);
1456 }
1457 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1458 close(lsock);
1459 return;
1460 }
1461
1462 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1463 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1464 close(lsock);
1465 return;
1466 }
1467
Tomas Cejkaba21b382013-04-13 02:37:32 +02001468 /* prepare internal lists */
1469 netconf_sessions_list = apr_hash_make(pool);
1470
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001471 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001472 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001473 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1474 use_notifications = 0;
1475 } else {
1476 use_notifications = 1;
1477 }
1478 #endif
1479
Radek Krejci469aab82012-07-22 18:42:20 +02001480 /* setup libnetconf's callbacks */
1481 nc_verbosity(NC_VERB_DEBUG);
1482 clb_print_server = server;
1483 nc_callback_print(clb_print);
1484 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1485 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1486 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001487 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001488
1489 /* disable publickey authentication */
1490 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1491
David Kupka8e60a372012-09-04 09:15:20 +02001492 /* create mutex protecting session list */
1493 pthread_rwlockattr_init(&lock_attrs);
1494 /* rwlock is shared only with threads in this process */
1495 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1496 /* create rw lock */
1497 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1498 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1499 close (lsock);
1500 return;
1501 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001502
1503 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001504 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001505 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001506 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001507 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001508 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001509 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1510 }
1511 if (use_notifications == 1) {
1512 notification_handle();
1513 }
1514 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001515 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1516 check_timeout_and_close(server, pool, netconf_sessions_list);
1517 }
Radek Krejci469aab82012-07-22 18:42:20 +02001518
1519 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001520 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001521 if (((unsigned int)tv.tv_sec - olds) > 60) {
1522 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1523 olds = tv.tv_sec;
1524 }
David Kupka8e60a372012-09-04 09:15:20 +02001525 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001526 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1527 apr_sleep(SLEEP_TIME);
1528 continue;
1529 } else if (client == -1 && (errno == EINTR)) {
1530 continue;
1531 } else if (client == -1) {
1532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1533 continue;
1534 }
Radek Krejci469aab82012-07-22 18:42:20 +02001535
1536 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001537 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001538
David Kupka8e60a372012-09-04 09:15:20 +02001539 arg = malloc (sizeof(struct pass_to_thread));
1540 arg->client = client;
1541 arg->pool = pool;
1542 arg->server = server;
1543 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001544
David Kupka8e60a372012-09-04 09:15:20 +02001545 /* start new thread. It will serve this particular request and then terminate */
1546 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1547 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1548 } else {
1549 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1550 pthread_count++;
1551 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1552 ptids[pthread_count] = 0;
1553 }
Radek Krejci469aab82012-07-22 18:42:20 +02001554
David Kupka8e60a372012-09-04 09:15:20 +02001555 /* check if some thread already terminated, free some resources by joining it */
1556 for (i=0; i<pthread_count; i++) {
1557 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1558 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1559 pthread_count--;
1560 if (pthread_count > 0) {
1561 /* place last Thread ID on the place of joined one */
1562 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001563 }
Radek Krejci469aab82012-07-22 18:42:20 +02001564 }
1565 }
David Kupka8e60a372012-09-04 09:15:20 +02001566 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001567 }
1568
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001569 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001570 /* join all threads */
1571 for (i=0; i<pthread_count; i++) {
1572 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1573 }
1574 free (ptids);
1575
Radek Krejci469aab82012-07-22 18:42:20 +02001576 close(lsock);
1577
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001578 #ifdef WITH_NOTIFICATIONS
1579 notification_close();
1580 #endif
1581
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001582 /* close all NETCONF sessions */
1583 close_all_nc_sessions(server, pool, netconf_sessions_list);
1584
David Kupka8e60a372012-09-04 09:15:20 +02001585 /* destroy rwlock */
1586 pthread_rwlock_destroy(&session_lock);
1587 pthread_rwlockattr_destroy(&lock_attrs);
1588
Radek Krejci469aab82012-07-22 18:42:20 +02001589 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1590
1591 exit(APR_SUCCESS);
1592}
1593
Radek Krejcif23850c2012-07-23 16:14:17 +02001594static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001595{
Radek Krejcif23850c2012-07-23 16:14:17 +02001596 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1597
1598 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1599 apr_pool_create(&config->pool, pool);
1600 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001601 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001602
1603 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001604}
1605
1606static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1607 apr_pool_t * plog, server_rec * s)
1608{
Radek Krejcif23850c2012-07-23 16:14:17 +02001609 mod_netconf_cfg *config;
1610 apr_status_t res;
1611
Radek Krejci469aab82012-07-22 18:42:20 +02001612 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001613 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001614 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001615
1616 /*
1617 * The following checks if this routine has been called before.
1618 * This is necessary because the parent process gets initialized
1619 * a couple of times as the server starts up.
1620 */
1621 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1622 if (!data) {
1623 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1624 return (OK);
1625 }
1626
Radek Krejcif23850c2012-07-23 16:14:17 +02001627 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1628 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001629
Radek Krejcif23850c2012-07-23 16:14:17 +02001630 if (config && config->forkproc == NULL) {
1631 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1632 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001633 switch (res) {
1634 case APR_INCHILD:
1635 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001636 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001637 apr_signal(SIGTERM, signal_handler);
1638
1639 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001640 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001641
1642 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001643 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001644
Radek Krejcif23850c2012-07-23 16:14:17 +02001645 /* I never should be here, wtf?!? */
1646 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001647 exit(APR_EGENERAL);
1648 break;
1649 case APR_INPARENT:
1650 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001651 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001652 break;
1653 default:
1654 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1655 break;
1656 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001657 } else {
1658 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001659 }
1660
1661 return OK;
1662}
1663
Radek Krejci469aab82012-07-22 18:42:20 +02001664/**
1665 * Register module hooks
1666 */
1667static void mod_netconf_register_hooks(apr_pool_t * p)
1668{
Radek Krejcif23850c2012-07-23 16:14:17 +02001669 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001670}
1671
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001672static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1673{
1674 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1675 return NULL;
1676}
1677
1678static const command_rec netconf_cmds[] = {
1679 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1680 {NULL}
1681};
1682
Radek Krejci469aab82012-07-22 18:42:20 +02001683/* Dispatch list for API hooks */
1684module AP_MODULE_DECLARE_DATA netconf_module = {
1685 STANDARD20_MODULE_STUFF,
1686 NULL, /* create per-dir config structures */
1687 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001688 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001689 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001690 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001691 mod_netconf_register_hooks /* register hooks */
1692};
Radek Krejcia332b692012-11-12 16:15:54 +01001693