blob: ac014ae08b80b3f4bd36d8c9a27f991742357980 [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
Kupka David00b9c5c2012-09-05 09:45:50 +0200208static 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 +0200209{
Radek Krejci469aab82012-07-22 18:42:20 +0200210 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200211 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200212 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200213
214 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200215 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200216 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
217
Radek Krejci469aab82012-07-22 18:42:20 +0200218 /* if connected successful, add session to the list */
219 if (session != NULL) {
220 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200221 session_key = gen_ncsession_hash(
222 (host==NULL) ? "localhost" : host,
223 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200224 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200225
Tomas Cejkaba21b382013-04-13 02:37:32 +0200226 /** \todo allocate from apr_pool */
David Kupka8e60a372012-09-04 09:15:20 +0200227 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
228 nc_session_free(session);
229 free (locked_session);
230 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
231 return NULL;
232 }
233 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200234 locked_session->last_activity = apr_time_now();
David Kupka8e60a372012-09-04 09:15:20 +0200235 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100236 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200237 /* get exclusive access to sessions_list (conns) */
238 if (pthread_rwlock_wrlock (&session_lock) != 0) {
239 nc_session_free(session);
240 free (locked_session);
241 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
242 return NULL;
243 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200244 locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100245 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200246 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100247 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200248 /* end of critical section */
249 if (pthread_rwlock_unlock (&session_lock) != 0) {
250 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
251 return NULL;
252 }
Radek Krejci469aab82012-07-22 18:42:20 +0200253 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200254 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200255 } else {
256 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200257 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200258 }
259
Radek Krejci469aab82012-07-22 18:42:20 +0200260}
261
Radek Krejci80c10d92012-07-30 08:38:50 +0200262static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200263{
264 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200265 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200266
Radek Krejcif23850c2012-07-23 16:14:17 +0200267 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200268 /* get exclusive (write) access to sessions_list (conns) */
269 if (pthread_rwlock_wrlock (&session_lock) != 0) {
270 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
271 return EXIT_FAILURE;
272 }
273 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
274 if (locked_session != NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200275 /** \todo free all notifications from queue */
276 apr_array_clear(locked_session->notifications);
David Kupka8e60a372012-09-04 09:15:20 +0200277 pthread_mutex_destroy(&locked_session->lock);
278 ns = locked_session->session;
279 free (locked_session);
280 }
Radek Krejci469aab82012-07-22 18:42:20 +0200281 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100282 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200283 nc_session_free (ns);
284 ns = NULL;
285
286 /* remove session from the active sessions list */
287 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200288 /* end of critical section */
289 if (pthread_rwlock_unlock (&session_lock) != 0) {
290 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
291 return EXIT_FAILURE;
292 }
Radek Krejci469aab82012-07-22 18:42:20 +0200293 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200294
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200295 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200296 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200297 if (pthread_rwlock_unlock (&session_lock) != 0) {
298 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
299 return EXIT_FAILURE;
300 }
Radek Krejci469aab82012-07-22 18:42:20 +0200301 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200302 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200303 }
304}
305
Radek Krejci80c10d92012-07-30 08:38:50 +0200306static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200307{
308 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200309 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200310 nc_reply* reply;
311 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100312 NC_MSG_TYPE msgt;
313 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200314
Radek Krejci8e4632a2012-07-26 13:40:34 +0200315 /* check requests */
316 if (rpc == NULL) {
317 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
318 return (EXIT_FAILURE);
319 }
320
David Kupka8e60a372012-09-04 09:15:20 +0200321 /* get non-exclusive (read) access to sessions_list (conns) */
322 if (pthread_rwlock_rdlock (&session_lock) != 0) {
323 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
324 return EXIT_FAILURE;
325 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200326 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200327 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
328 if (locked_session != NULL) {
329 session = locked_session->session;
330 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200331 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200332 /* get exclusive access to session */
333 if (pthread_mutex_lock(&locked_session->lock) != 0) {
334 /* unlock before returning error */
335 if (pthread_rwlock_unlock (&session_lock) != 0) {
336 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
337 return EXIT_FAILURE;
338 }
339 return EXIT_FAILURE;
340 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200341 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200342 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100343 msgt = nc_session_send_recv(session, rpc, &reply);
344
David Kupka8e60a372012-09-04 09:15:20 +0200345 /* first release exclusive lock for this session */
346 pthread_mutex_unlock(&locked_session->lock);
347 /* end of critical section */
348 if (pthread_rwlock_unlock (&session_lock) != 0) {
349 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
350 return EXIT_FAILURE;
351 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200352
Radek Krejcia332b692012-11-12 16:15:54 +0100353 /* process the result of the operation */
354 switch (msgt) {
355 case NC_MSG_UNKNOWN:
356 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
357 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
358 netconf_close(server, conns, session_key);
359 return (EXIT_FAILURE);
360 }
361 /* no break */
362 case NC_MSG_NONE:
363 /* there is error handled by callback */
364 return (EXIT_FAILURE);
365 break;
366 case NC_MSG_REPLY:
367 switch (replyt = nc_reply_get_type(reply)) {
368 case NC_REPLY_OK:
369 retval = EXIT_SUCCESS;
370 break;
371 default:
372 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
373 retval = EXIT_FAILURE;
374 break;
375 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200376 break;
377 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100378 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200379 retval = EXIT_FAILURE;
380 break;
381 }
382 nc_reply_free(reply);
383 return (retval);
384 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100385 /* release lock on failure */
386 if (pthread_rwlock_unlock (&session_lock) != 0) {
387 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
388 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200389 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
390 return (EXIT_FAILURE);
391 }
392}
Radek Krejci80c10d92012-07-30 08:38:50 +0200393
394static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200395{
396 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200397 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100398 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200399 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100400 NC_MSG_TYPE msgt;
401 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200402
403 /* check requests */
404 if (rpc == NULL) {
405 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
406 return (NULL);
407 }
408
David Kupka8e60a372012-09-04 09:15:20 +0200409 /* get non-exclusive (read) access to sessions_list (conns) */
410 if (pthread_rwlock_rdlock (&session_lock) != 0) {
411 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
412 return NULL;
413 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200414 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200415 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
416 if (locked_session != NULL) {
417 session = locked_session->session;
418 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200419 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200420 /* get exclusive access to session */
421 if (pthread_mutex_lock(&locked_session->lock) != 0) {
422 /* unlock before returning error */
423 if (pthread_rwlock_unlock (&session_lock) != 0) {
424 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
425 return NULL;
426 }
427 return NULL;
428 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200429 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200430 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100431 msgt = nc_session_send_recv(session, rpc, &reply);
432
David Kupka8e60a372012-09-04 09:15:20 +0200433 /* first release exclusive lock for this session */
434 pthread_mutex_unlock(&locked_session->lock);
435 /* end of critical section */
436 if (pthread_rwlock_unlock (&session_lock) != 0) {
437 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 +0100438 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200439 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200440
Radek Krejcia332b692012-11-12 16:15:54 +0100441 /* process the result of the operation */
442 switch (msgt) {
443 case NC_MSG_UNKNOWN:
444 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
445 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
446 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200447 return (NULL);
448 }
Radek Krejcia332b692012-11-12 16:15:54 +0100449 /* no break */
450 case NC_MSG_NONE:
451 /* there is error handled by callback */
452 return (NULL);
453 break;
454 case NC_MSG_REPLY:
455 switch (replyt = nc_reply_get_type(reply)) {
456 case NC_REPLY_DATA:
457 if ((data = nc_reply_get_data (reply)) == NULL) {
458 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
459 data = NULL;
460 }
461 break;
462 default:
463 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
464 data = NULL;
465 break;
466 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200467 break;
468 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100469 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
470 data = NULL;
471 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200472 }
473 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200474 return (data);
475 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100476 /* release lock on failure */
477 if (pthread_rwlock_unlock (&session_lock) != 0) {
478 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
479 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200480 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
481 return (NULL);
482 }
483}
484
Radek Krejci80c10d92012-07-30 08:38:50 +0200485static 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 +0200486{
487 nc_rpc* rpc;
488 struct nc_filter *f = NULL;
489 char* data = NULL;
490
491 /* create filter if set */
492 if (filter != NULL) {
493 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
494 }
495
496 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100497 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200498 nc_filter_free(f);
499 if (rpc == NULL) {
500 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
501 return (NULL);
502 }
503
504 data = netconf_opdata(server, conns, session_key, rpc);
505 nc_rpc_free (rpc);
506 return (data);
507}
508
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100509static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
510{
511 nc_rpc* rpc;
512 char* data = NULL;
513
514 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100515 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100516 if (rpc == NULL) {
517 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
518 return (NULL);
519 }
520
521 data = netconf_opdata(server, conns, session_key, rpc);
522 nc_rpc_free (rpc);
523 return (data);
524}
525
Radek Krejci80c10d92012-07-30 08:38:50 +0200526static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200527{
528 nc_rpc* rpc;
529 struct nc_filter *f = NULL;
530 char* data = NULL;
531
532 /* create filter if set */
533 if (filter != NULL) {
534 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
535 }
536
537 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100538 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200539 nc_filter_free(f);
540 if (rpc == NULL) {
541 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
542 return (NULL);
543 }
544
545 data = netconf_opdata(server, conns, session_key, rpc);
546 nc_rpc_free (rpc);
547 return (data);
548}
549
Tomas Cejka5064c232013-01-17 09:30:58 +0100550static 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 +0200551{
552 nc_rpc* rpc;
553 int retval = EXIT_SUCCESS;
554
555 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100556 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
557 if (target == NC_DATASTORE_URL) {
558 rpc = nc_rpc_copyconfig(source, target, config, url);
559 } else {
560 rpc = nc_rpc_copyconfig(source, target, config);
561 }
562 } else {
563 if (target == NC_DATASTORE_URL) {
564 rpc = nc_rpc_copyconfig(source, target, url);
565 } else {
566 rpc = nc_rpc_copyconfig(source, target);
567 }
568 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200569 if (rpc == NULL) {
570 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
571 return (EXIT_FAILURE);
572 }
573
574 retval = netconf_op(server, conns, session_key, rpc);
575 nc_rpc_free (rpc);
576 return (retval);
577}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200578
Tomas Cejka5064c232013-01-17 09:30:58 +0100579static 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 +0200580{
581 nc_rpc* rpc;
582 int retval = EXIT_SUCCESS;
583
584 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100585 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
586 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200587 if (rpc == NULL) {
588 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
589 return (EXIT_FAILURE);
590 }
591
592 retval = netconf_op(server, conns, session_key, rpc);
593 nc_rpc_free (rpc);
594 return (retval);
595}
596
Radek Krejci80c10d92012-07-30 08:38:50 +0200597static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200598{
599 nc_rpc* rpc;
600 int retval = EXIT_SUCCESS;
601
602 /* create requests */
603 rpc = nc_rpc_killsession(sid);
604 if (rpc == NULL) {
605 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
606 return (EXIT_FAILURE);
607 }
608
609 retval = netconf_op(server, conns, session_key, rpc);
610 nc_rpc_free (rpc);
611 return (retval);
612}
613
Radek Krejci80c10d92012-07-30 08:38:50 +0200614static 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 +0200615{
616 nc_rpc* rpc;
617 int retval = EXIT_SUCCESS;
618
619 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200620 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200621 if (rpc == NULL) {
622 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
623 return (EXIT_FAILURE);
624 }
625
626 retval = netconf_op(server, conns, session_key, rpc);
627 nc_rpc_free (rpc);
628 return (retval);
629}
630
Radek Krejci80c10d92012-07-30 08:38:50 +0200631static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200632{
Tomas Cejka404d37e2013-04-13 02:31:35 +0200633 nc_rpc *rpc = NULL;
634 if (target != NC_DATASTORE_URL) {
635 rpc = nc_rpc_deleteconfig(target);
636 } else {
637 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
638 /* rpc = nc_rpc_deleteconfig(target, const char *url); */
639 return (EXIT_FAILURE);
640 }
641
642 return netconf_op(server, conns, session_key, rpc);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200643}
644
Radek Krejci80c10d92012-07-30 08:38:50 +0200645static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200646{
647 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
648}
649
Radek Krejci80c10d92012-07-30 08:38:50 +0200650static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200651{
652 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
653}
654
Radek Krejci80c10d92012-07-30 08:38:50 +0200655/**
656 * @return REPLY_OK: 0, *data == NULL
657 * REPLY_DATA: 0, *data != NULL
658 * REPLY_ERROR: 1, *data == NULL
659 */
660static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
661{
662 struct nc_session *session = NULL;
663 nc_reply* reply;
664 nc_rpc* rpc;
665 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100666 NC_MSG_TYPE msgt;
667 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200668
669 /* create requests */
670 rpc = nc_rpc_generic(content);
671 if (rpc == NULL) {
672 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
673 return (EXIT_FAILURE);
674 }
675
Radek Krejcia332b692012-11-12 16:15:54 +0100676 if (data != NULL) {
677 *data = NULL;
678 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200679
680 /* get session where send the RPC */
681 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
682 if (session != NULL) {
683 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100684 msgt = nc_session_send_recv(session, rpc, &reply);
685 nc_rpc_free (rpc);
686
687 /* process the result of the operation */
688 switch (msgt) {
689 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200690 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
691 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
692 netconf_close(server, conns, session_key);
693 return (EXIT_FAILURE);
694 }
Radek Krejcia332b692012-11-12 16:15:54 +0100695 /* no break */
696 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200697 /* there is error handled by callback */
698 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200699 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100700 case NC_MSG_REPLY:
701 switch (replyt = nc_reply_get_type(reply)) {
702 case NC_REPLY_DATA:
703 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
704 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
705 nc_reply_free(reply);
706 return (EXIT_FAILURE);
707 }
708 retval = EXIT_SUCCESS;
709 break;
710 case NC_REPLY_OK:
711 retval = EXIT_SUCCESS;
712 break;
713 default:
714 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
715 retval = EXIT_FAILURE;
716 break;
717 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200718 break;
719 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100720 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200721 retval = EXIT_FAILURE;
722 break;
723 }
724 nc_reply_free(reply);
725
726 return (retval);
727 } else {
728 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
729 return (EXIT_FAILURE);
730 }
731}
732
Radek Krejci469aab82012-07-22 18:42:20 +0200733server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200734void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200735{
Radek Krejci7338bde2012-08-10 12:57:30 +0200736 switch (level) {
737 case NC_VERB_ERROR:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200738 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200739 break;
740 case NC_VERB_WARNING:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200741 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200742 break;
743 case NC_VERB_VERBOSE:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200744 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200745 break;
746 case NC_VERB_DEBUG:
Tomas Cejka404d37e2013-04-13 02:31:35 +0200747 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, "%s", msg);
Radek Krejci7338bde2012-08-10 12:57:30 +0200748 break;
749 }
Radek Krejci469aab82012-07-22 18:42:20 +0200750}
751
David Kupka8e60a372012-09-04 09:15:20 +0200752void * thread_routine (void * arg)
753{
754 void * retval = NULL;
755
756 ssize_t buffer_len;
757 struct pollfd fds;
758 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200759 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200760 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200761 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200762 char* session_key, *data;
763 const char *host, *port, *user, *pass;
764 const char *msgtext, *cpbltstr;
765 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100766 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200767 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200768 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200769 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200770 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100771 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200772 NC_EDIT_ERROPT_TYPE erropt_type = 0;
773
774 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
775 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
776 server_rec * server = ((struct pass_to_thread*)arg)->server;
777 int client = ((struct pass_to_thread*)arg)->client;
778
779 char * buffer, chunk_len_str[12], *chunked_msg;
780 char c;
781
782 while (!isterminated) {
783 fds.fd = client;
784 fds.events = POLLIN;
785 fds.revents = 0;
786
787 status = poll(&fds, 1, 1000);
788
789 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
790 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
791 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
792 continue;
793 } else if (status < 0) {
794 /* 0: poll time outed
795 * close socket and ignore this request from the client, it can try it again
796 * -1: poll failed
797 * something wrong happend, close this socket and wait for another request
798 */
799 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
800 close(client);
801 break;
802 }
803 /* status > 0 */
804
805 /* check the status of the socket */
806
807 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
808 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
809 /* close client's socket (it's probably already closed by client */
810 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
811 close(client);
812 break;
813 }
814
815 /* read json in chunked framing */
816 buffer_size = 0;
817 buffer_len = 0;
818 buffer = NULL;
819 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200820 /* read chunk length */
821 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
822 free (buffer);
823 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200824 break;
825 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200826 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
827 free (buffer);
828 buffer = NULL;
829 break;
830 }
831 i=0;
832 memset (chunk_len_str, 0, 12);
833 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
834 if (i==0 && c == '#') {
835 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
836 /* end but invalid */
837 free (buffer);
838 buffer = NULL;
839 }
840 /* end of message, double-loop break */
841 goto msg_complete;
842 }
843 chunk_len_str[i++] = c;
844 }
845 if (c != '\n') {
846 free (buffer);
847 buffer = NULL;
848 break;
849 }
850 if ((chunk_len = atoi (chunk_len_str)) == 0) {
851 free (buffer);
852 buffer = NULL;
853 break;
854 }
855 buffer_size += chunk_len+1;
856 buffer = realloc (buffer, sizeof(char)*buffer_size);
857 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
858 free (buffer);
859 buffer = NULL;
860 break;
861 }
862 buffer_len += ret;
863 }
864msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200865
866 if (buffer != NULL) {
867 request = json_tokener_parse(buffer);
868 operation = json_object_get_int(json_object_object_get(request, "type"));
869
870 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
871 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
872 if (operation != MSG_CONNECT && session_key == NULL) {
873 reply = json_object_new_object();
874 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
875 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
876 msgtext = json_object_to_json_string(reply);
877 send(client, msgtext, strlen(msgtext) + 1, 0);
878 json_object_put(reply);
879 /* there is some stupid client, so close the connection to give a chance to some other client */
880 close(client);
881 break;
882 }
883
884 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100885 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200886 ds_type_t = -1;
887 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
888 if (strcmp(target, "running") == 0) {
889 ds_type_t = NC_DATASTORE_RUNNING;
890 } else if (strcmp(target, "startup") == 0) {
891 ds_type_t = NC_DATASTORE_STARTUP;
892 } else if (strcmp(target, "candidate") == 0) {
893 ds_type_t = NC_DATASTORE_CANDIDATE;
894 }
895 }
896 ds_type_s = -1;
897 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
898 if (strcmp(source, "running") == 0) {
899 ds_type_s = NC_DATASTORE_RUNNING;
900 } else if (strcmp(source, "startup") == 0) {
901 ds_type_s = NC_DATASTORE_STARTUP;
902 } else if (strcmp(source, "candidate") == 0) {
903 ds_type_s = NC_DATASTORE_CANDIDATE;
904 }
905 }
906
907 /* null global JSON error-reply */
908 err_reply = NULL;
909
910 /* prepare reply envelope */
911 reply = json_object_new_object();
912
913 /* process required operation */
914 switch (operation) {
915 case MSG_CONNECT:
916 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
917
918 host = json_object_get_string(json_object_object_get(request, "host"));
919 port = json_object_get_string(json_object_object_get(request, "port"));
920 user = json_object_get_string(json_object_object_get(request, "user"));
921 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200922 capabilities = json_object_object_get(request, "capabilities");
923 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200924 cpblts = nc_cpblts_new (NULL);
925 for (i=0; i<len; i++) {
926 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
927 }
928 } else {
929 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
930 }
David Kupka8e60a372012-09-04 09:15:20 +0200931 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
932 if ((host == NULL) || (user == NULL)) {
933 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
934 session_key = NULL;
935 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200936 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
937 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200938 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
939 }
940
941 reply = json_object_new_object();
942 if (session_key == NULL) {
943 /* negative reply */
944 if (err_reply == NULL) {
945 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
946 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100947 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200948 } else {
949 /* use filled err_reply from libnetconf's callback */
950 json_object_put(reply);
951 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100952 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200953 }
954 } else {
955 /* positive reply */
956 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
957 json_object_object_add(reply, "session", json_object_new_string(session_key));
958
959 free(session_key);
960 }
961
962 break;
963 case MSG_GET:
964 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
965
966 filter = json_object_get_string(json_object_object_get(request, "filter"));
967
968 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
969
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200970 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200971 if (err_reply == NULL) {
972 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
973 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
974 } else {
975 /* use filled err_reply from libnetconf's callback */
976 json_object_put(reply);
977 reply = err_reply;
978 }
979 } else {
980 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
981 json_object_object_add(reply, "data", json_object_new_string(data));
982 }
983 break;
984 case MSG_GETCONFIG:
985 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
986
987 filter = json_object_get_string(json_object_object_get(request, "filter"));
988
989 if (ds_type_s == -1) {
990 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
991 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
992 break;
993 }
994
995 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
996 if (err_reply == NULL) {
997 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
998 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
999 } else {
1000 /* use filled err_reply from libnetconf's callback */
1001 json_object_put(reply);
1002 reply = err_reply;
1003 }
1004 } else {
1005 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1006 json_object_object_add(reply, "data", json_object_new_string(data));
1007 }
1008 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001009 case MSG_GETSCHEMA:
1010 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1011 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1012 if (identifier == NULL) {
1013 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1014 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1015 break;
1016 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001017 version = json_object_get_string(json_object_object_get(request, "version"));
1018 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001019
Tomas Cejka94da2c52013-01-08 18:20:30 +01001020 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001021 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001022 if (err_reply == NULL) {
1023 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1024 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1025 } else {
1026 /* use filled err_reply from libnetconf's callback */
1027 json_object_put(reply);
1028 reply = err_reply;
1029 }
1030 } else {
1031 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1032 json_object_object_add(reply, "data", json_object_new_string(data));
1033 }
1034 break;
David Kupka8e60a372012-09-04 09:15:20 +02001035 case MSG_EDITCONFIG:
1036 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1037
1038 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1039 if (defop != NULL) {
1040 if (strcmp(defop, "merge") == 0) {
1041 defop_type = NC_EDIT_DEFOP_MERGE;
1042 } else if (strcmp(defop, "replace") == 0) {
1043 defop_type = NC_EDIT_DEFOP_REPLACE;
1044 } else if (strcmp(defop, "none") == 0) {
1045 defop_type = NC_EDIT_DEFOP_NONE;
1046 } else {
1047 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1048 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1049 break;
1050 }
1051 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001052 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001053 }
1054
1055 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1056 if (erropt != NULL) {
1057 if (strcmp(erropt, "continue-on-error") == 0) {
1058 erropt_type = NC_EDIT_ERROPT_CONT;
1059 } else if (strcmp(erropt, "stop-on-error") == 0) {
1060 erropt_type = NC_EDIT_ERROPT_STOP;
1061 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1062 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1063 } else {
1064 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1065 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1066 break;
1067 }
1068 } else {
1069 erropt_type = 0;
1070 }
1071
1072 if (ds_type_t == -1) {
1073 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1074 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1075 break;
1076 }
1077
1078 config = json_object_get_string(json_object_object_get(request, "config"));
1079 if (config == NULL) {
1080 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1081 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1082 break;
1083 }
1084
Tomas Cejka5064c232013-01-17 09:30:58 +01001085 /* TODO url capability see netconf_editconfig */
1086 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1087 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 +02001088 if (err_reply == NULL) {
1089 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1090 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1091 } else {
1092 /* use filled err_reply from libnetconf's callback */
1093 json_object_put(reply);
1094 reply = err_reply;
1095 }
1096 } else {
1097 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1098 }
1099 break;
1100 case MSG_COPYCONFIG:
1101 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1102 config = NULL;
1103
1104 if (source == NULL) {
1105 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001106 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001107 config = json_object_get_string(json_object_object_get(request, "config"));
1108 } else if (ds_type_s == -1) {
1109 /* source datastore specified, but it is invalid */
1110 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1111 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1112 break;
1113 }
1114
1115 if (ds_type_t == -1) {
1116 /* invalid target datastore specified */
1117 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1118 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1119 break;
1120 }
1121
1122 if (source == NULL && config == NULL) {
1123 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1124 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1125 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001126 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 +02001127 if (err_reply == NULL) {
1128 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1129 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1130 } else {
1131 /* use filled err_reply from libnetconf's callback */
1132 json_object_put(reply);
1133 reply = err_reply;
1134 }
1135 } else {
1136 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1137 }
1138 }
1139 break;
1140 case MSG_DELETECONFIG:
1141 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1142 /* no break - unifying code */
1143 case MSG_LOCK:
1144 if (operation == MSG_LOCK) {
1145 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1146 }
1147 /* no break - unifying code */
1148 case MSG_UNLOCK:
1149 if (operation == MSG_UNLOCK) {
1150 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1151 }
1152
1153 if (ds_type_t == -1) {
1154 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1155 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1156 break;
1157 }
1158
1159 switch(operation) {
1160 case MSG_DELETECONFIG:
1161 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1162 break;
1163 case MSG_LOCK:
1164 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1165 break;
1166 case MSG_UNLOCK:
1167 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1168 break;
1169 default:
1170 status = -1;
1171 break;
1172 }
1173
1174 if (status != EXIT_SUCCESS) {
1175 if (err_reply == NULL) {
1176 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1177 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1178 } else {
1179 /* use filled err_reply from libnetconf's callback */
1180 json_object_put(reply);
1181 reply = err_reply;
1182 }
1183 } else {
1184 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1185 }
1186 break;
1187 case MSG_KILL:
1188 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1189
1190 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1191
1192 if (sid == NULL) {
1193 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1194 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1195 break;
1196 }
1197
1198 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1199 if (err_reply == NULL) {
1200 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1201 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1202 } else {
1203 /* use filled err_reply from libnetconf's callback */
1204 json_object_put(reply);
1205 reply = err_reply;
1206 }
1207 } else {
1208 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1209 }
1210 break;
1211 case MSG_DISCONNECT:
1212 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1213
1214 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1215 if (err_reply == NULL) {
1216 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1217 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1218 } else {
1219 /* use filled err_reply from libnetconf's callback */
1220 json_object_put(reply);
1221 reply = err_reply;
1222 }
1223 } else {
1224 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1225 }
1226 break;
1227 case MSG_INFO:
1228 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1229
Kupka Davidda134a12012-09-06 14:12:16 +02001230 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1231 if (locked_session != NULL) {
1232 session = locked_session->session;
1233 } else {
1234 session = NULL;
1235 }
David Kupka8e60a372012-09-04 09:15:20 +02001236 if (session != NULL) {
1237 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1238 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1239 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1240 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1241 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1242 cpblts = nc_session_get_cpblts (session);
1243 if (cpblts != NULL) {
1244 json_obj = json_object_new_array();
1245 nc_cpblts_iter_start (cpblts);
1246 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1247 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1248 }
1249 json_object_object_add(reply, "capabilities", json_obj);
1250 }
1251 } else {
1252 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1253 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1254 }
1255
1256 break;
1257 case MSG_GENERIC:
1258 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1259
1260 config = json_object_get_string(json_object_object_get(request, "content"));
1261
1262 if (config == NULL) {
1263 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1264 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1265 break;
1266 }
1267
1268 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1269 if (err_reply == NULL) {
1270 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1271 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1272 } else {
1273 /* use filled err_reply from libnetconf's callback */
1274 json_object_put(reply);
1275 reply = err_reply;
1276 }
1277 } else {
1278 if (data == NULL) {
1279 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1280 } else {
1281 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1282 json_object_object_add(reply, "data", json_object_new_string(data));
1283 }
1284 }
1285 break;
1286 default:
1287 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1288 reply = json_object_new_object();
1289 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1290 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1291 break;
1292 }
1293 json_object_put(request);
1294
David Kupka1e3e4c82012-09-04 09:32:15 +02001295 /* send reply to caller */
1296 if (reply != NULL) {
1297 msgtext = json_object_to_json_string(reply);
1298 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1299 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001300 break;
1301 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001302 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1303 json_object_put(reply);
1304 free (chunked_msg);
1305 free (buffer);
1306 } else {
1307 break;
David Kupka8e60a372012-09-04 09:15:20 +02001308 }
1309 }
1310 }
David Kupka8e60a372012-09-04 09:15:20 +02001311 free (arg);
1312
1313 return retval;
1314}
1315
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001316/**
1317 * \brief Close all open NETCONF sessions.
1318 *
1319 * During termination of mod_netconf, it is useful to close all remaining
1320 * sessions. This function iterates over the list of sessions and close them
1321 * all.
1322 *
1323 * \param[in] server pointer to server_rec for logging
1324 * \param[in] p apr pool needed for hash table iterating
1325 * \param[in] ht hash table of session_with_mutex structs
1326 */
1327static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1328{
1329 apr_hash_index_t *hi;
1330 void *val = NULL;
1331 struct session_with_mutex *swm = NULL;
1332 struct nc_session *ns = NULL;
1333 const char *hashed_key = NULL;
1334 apr_ssize_t hashed_key_length;
1335
1336 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1337 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1338 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1339 swm = (struct session_with_mutex *) val;
1340 if (swm != NULL) {
1341 pthread_mutex_lock(&swm->lock);
1342 if (swm->session != NULL) {
1343 ns = swm->session;
1344 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1345 nc_session_free(ns);
1346 swm->session = NULL;
1347 }
1348 pthread_mutex_unlock(&swm->lock);
1349 }
1350 }
1351}
1352
1353static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1354{
1355 apr_hash_index_t *hi;
1356 void *val = NULL;
1357 struct nc_session *ns = NULL;
1358 struct session_with_mutex *swm = NULL;
1359 const char *hashed_key = NULL;
1360 apr_ssize_t hashed_key_length;
1361 apr_time_t current_time = apr_time_now();
1362
1363 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1364 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1365 swm = (struct session_with_mutex *) val;
1366 if (swm == NULL) {
1367 continue;
1368 }
1369 ns = swm->session;
1370 if (ns == NULL) {
1371 continue;
1372 }
1373 pthread_mutex_lock(&swm->lock);
1374 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1375 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1376 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1377 nc_session_free (ns);
1378 ns = NULL;
1379 /* remove session from the active sessions list */
1380 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1381 }
1382 pthread_mutex_unlock(&swm->lock);
1383 }
1384}
1385
1386
1387/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001388 * This is actually implementation of NETCONF client
1389 * - requests are received from UNIX socket in the predefined format
1390 * - results are replied through the same way
1391 * - the daemon run as a separate process, but it is started and stopped
1392 * automatically by Apache.
1393 *
1394 */
Radek Krejci469aab82012-07-22 18:42:20 +02001395static void forked_proc(apr_pool_t * pool, server_rec * server)
1396{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001397 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001398 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001399 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001400 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001401 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001402 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001403 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001404 struct pass_to_thread * arg;
1405 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1406 struct timespec maxtime;
1407 pthread_rwlockattr_t lock_attrs;
Tomas Cejka404d37e2013-04-13 02:31:35 +02001408 #ifdef WITH_NOTIFICATIONS
1409 char use_notifications = 0;
1410 #endif
David Kupka8e60a372012-09-04 09:15:20 +02001411
Tomas Cejka404d37e2013-04-13 02:31:35 +02001412 /* wait at most 5 seconds for every thread to terminate */
David Kupka8e60a372012-09-04 09:15:20 +02001413 maxtime.tv_sec = 5;
1414 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001415
Radek Krejcif23850c2012-07-23 16:14:17 +02001416 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001417 unixd_setup_child();
1418
Radek Krejciae021c12012-07-25 18:03:52 +02001419 cfg = ap_get_module_config(server->module_config, &netconf_module);
1420 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001421 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1422 return;
1423 }
Radek Krejci469aab82012-07-22 18:42:20 +02001424
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001425 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001426 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1427 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1428 return;
1429 }
1430
1431 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001432 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001433 unlink(local.sun_path);
1434 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1435
1436 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1437 if (errno == EADDRINUSE) {
1438 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1439 close(lsock);
1440 exit(0);
1441 }
1442 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1443 close(lsock);
1444 return;
1445 }
1446
1447 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1448 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1449 close(lsock);
1450 return;
1451 }
1452
Tomas Cejkaba21b382013-04-13 02:37:32 +02001453 /* prepare internal lists */
1454 netconf_sessions_list = apr_hash_make(pool);
1455
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001456 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaba21b382013-04-13 02:37:32 +02001457 if (notification_init(pool, server, netconf_sessions_list) == -1) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001458 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1459 use_notifications = 0;
1460 } else {
1461 use_notifications = 1;
1462 }
1463 #endif
1464
Radek Krejci469aab82012-07-22 18:42:20 +02001465 /* setup libnetconf's callbacks */
1466 nc_verbosity(NC_VERB_DEBUG);
1467 clb_print_server = server;
1468 nc_callback_print(clb_print);
1469 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1470 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1471 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001472 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001473
1474 /* disable publickey authentication */
1475 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1476
David Kupka8e60a372012-09-04 09:15:20 +02001477 /* create mutex protecting session list */
1478 pthread_rwlockattr_init(&lock_attrs);
1479 /* rwlock is shared only with threads in this process */
1480 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1481 /* create rw lock */
1482 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1483 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1484 close (lsock);
1485 return;
1486 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001487
1488 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001489 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001490 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001491 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001492 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001493 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001494 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1495 }
1496 if (use_notifications == 1) {
1497 notification_handle();
1498 }
1499 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001500 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1501 check_timeout_and_close(server, pool, netconf_sessions_list);
1502 }
Radek Krejci469aab82012-07-22 18:42:20 +02001503
1504 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001505 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001506 if (((unsigned int)tv.tv_sec - olds) > 60) {
1507 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1508 olds = tv.tv_sec;
1509 }
David Kupka8e60a372012-09-04 09:15:20 +02001510 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001511 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1512 apr_sleep(SLEEP_TIME);
1513 continue;
1514 } else if (client == -1 && (errno == EINTR)) {
1515 continue;
1516 } else if (client == -1) {
1517 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1518 continue;
1519 }
Radek Krejci469aab82012-07-22 18:42:20 +02001520
1521 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001522 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001523
David Kupka8e60a372012-09-04 09:15:20 +02001524 arg = malloc (sizeof(struct pass_to_thread));
1525 arg->client = client;
1526 arg->pool = pool;
1527 arg->server = server;
1528 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001529
David Kupka8e60a372012-09-04 09:15:20 +02001530 /* start new thread. It will serve this particular request and then terminate */
1531 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1533 } else {
1534 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1535 pthread_count++;
1536 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1537 ptids[pthread_count] = 0;
1538 }
Radek Krejci469aab82012-07-22 18:42:20 +02001539
David Kupka8e60a372012-09-04 09:15:20 +02001540 /* check if some thread already terminated, free some resources by joining it */
1541 for (i=0; i<pthread_count; i++) {
1542 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1543 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1544 pthread_count--;
1545 if (pthread_count > 0) {
1546 /* place last Thread ID on the place of joined one */
1547 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001548 }
Radek Krejci469aab82012-07-22 18:42:20 +02001549 }
1550 }
David Kupka8e60a372012-09-04 09:15:20 +02001551 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001552 }
1553
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001554 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001555 /* join all threads */
1556 for (i=0; i<pthread_count; i++) {
1557 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1558 }
1559 free (ptids);
1560
Radek Krejci469aab82012-07-22 18:42:20 +02001561 close(lsock);
1562
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001563 #ifdef WITH_NOTIFICATIONS
1564 notification_close();
1565 #endif
1566
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001567 /* close all NETCONF sessions */
1568 close_all_nc_sessions(server, pool, netconf_sessions_list);
1569
David Kupka8e60a372012-09-04 09:15:20 +02001570 /* destroy rwlock */
1571 pthread_rwlock_destroy(&session_lock);
1572 pthread_rwlockattr_destroy(&lock_attrs);
1573
Radek Krejci469aab82012-07-22 18:42:20 +02001574 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1575
1576 exit(APR_SUCCESS);
1577}
1578
Radek Krejcif23850c2012-07-23 16:14:17 +02001579static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001580{
Radek Krejcif23850c2012-07-23 16:14:17 +02001581 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1582
1583 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1584 apr_pool_create(&config->pool, pool);
1585 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001586 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001587
1588 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001589}
1590
1591static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1592 apr_pool_t * plog, server_rec * s)
1593{
Radek Krejcif23850c2012-07-23 16:14:17 +02001594 mod_netconf_cfg *config;
1595 apr_status_t res;
1596
Radek Krejci469aab82012-07-22 18:42:20 +02001597 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001598 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001599 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001600
1601 /*
1602 * The following checks if this routine has been called before.
1603 * This is necessary because the parent process gets initialized
1604 * a couple of times as the server starts up.
1605 */
1606 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1607 if (!data) {
1608 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1609 return (OK);
1610 }
1611
Radek Krejcif23850c2012-07-23 16:14:17 +02001612 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1613 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001614
Radek Krejcif23850c2012-07-23 16:14:17 +02001615 if (config && config->forkproc == NULL) {
1616 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1617 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001618 switch (res) {
1619 case APR_INCHILD:
1620 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001621 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001622 apr_signal(SIGTERM, signal_handler);
1623
1624 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001625 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001626
1627 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001628 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001629
Radek Krejcif23850c2012-07-23 16:14:17 +02001630 /* I never should be here, wtf?!? */
1631 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001632 exit(APR_EGENERAL);
1633 break;
1634 case APR_INPARENT:
1635 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001636 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001637 break;
1638 default:
1639 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1640 break;
1641 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001642 } else {
1643 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001644 }
1645
1646 return OK;
1647}
1648
Radek Krejci469aab82012-07-22 18:42:20 +02001649/**
1650 * Register module hooks
1651 */
1652static void mod_netconf_register_hooks(apr_pool_t * p)
1653{
Radek Krejcif23850c2012-07-23 16:14:17 +02001654 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001655}
1656
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001657static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1658{
1659 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1660 return NULL;
1661}
1662
1663static const command_rec netconf_cmds[] = {
1664 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1665 {NULL}
1666};
1667
Radek Krejci469aab82012-07-22 18:42:20 +02001668/* Dispatch list for API hooks */
1669module AP_MODULE_DECLARE_DATA netconf_module = {
1670 STANDARD20_MODULE_STUFF,
1671 NULL, /* create per-dir config structures */
1672 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001673 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001674 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001675 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001676 mod_netconf_register_hooks /* register hooks */
1677};
Radek Krejcia332b692012-11-12 16:15:54 +01001678