blob: 8d7476d6cd0c0a03915dedf28d8fa8a5a6f2ac21 [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 */
84#define ACTIVITY_TIMEOUT 180 /**< 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
226 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
227 nc_session_free(session);
228 free (locked_session);
229 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
230 return NULL;
231 }
232 locked_session->session = session;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200233 locked_session->last_activity = apr_time_now();
David Kupka8e60a372012-09-04 09:15:20 +0200234 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100235 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200236 /* get exclusive access to sessions_list (conns) */
237 if (pthread_rwlock_wrlock (&session_lock) != 0) {
238 nc_session_free(session);
239 free (locked_session);
240 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
241 return NULL;
242 }
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100243 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200244 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100245 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200246 /* end of critical section */
247 if (pthread_rwlock_unlock (&session_lock) != 0) {
248 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
249 return NULL;
250 }
Radek Krejci469aab82012-07-22 18:42:20 +0200251 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200252 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200253 } else {
254 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200255 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200256 }
257
Radek Krejci469aab82012-07-22 18:42:20 +0200258}
259
Radek Krejci80c10d92012-07-30 08:38:50 +0200260static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200261{
262 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200263 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200264
Radek Krejcif23850c2012-07-23 16:14:17 +0200265 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200266 /* get exclusive (write) access to sessions_list (conns) */
267 if (pthread_rwlock_wrlock (&session_lock) != 0) {
268 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
269 return EXIT_FAILURE;
270 }
271 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
272 if (locked_session != NULL) {
273 pthread_mutex_destroy(&locked_session->lock);
274 ns = locked_session->session;
275 free (locked_session);
276 }
Radek Krejci469aab82012-07-22 18:42:20 +0200277 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100278 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200279 nc_session_free (ns);
280 ns = NULL;
281
282 /* remove session from the active sessions list */
283 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200284 /* end of critical section */
285 if (pthread_rwlock_unlock (&session_lock) != 0) {
286 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
287 return EXIT_FAILURE;
288 }
Radek Krejci469aab82012-07-22 18:42:20 +0200289 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200290
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200291 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200292 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200293 if (pthread_rwlock_unlock (&session_lock) != 0) {
294 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
295 return EXIT_FAILURE;
296 }
Radek Krejci469aab82012-07-22 18:42:20 +0200297 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200298 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200299 }
300}
301
Radek Krejci80c10d92012-07-30 08:38:50 +0200302static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200303{
304 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200305 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200306 nc_reply* reply;
307 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100308 NC_MSG_TYPE msgt;
309 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200310
Radek Krejci8e4632a2012-07-26 13:40:34 +0200311 /* check requests */
312 if (rpc == NULL) {
313 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
314 return (EXIT_FAILURE);
315 }
316
David Kupka8e60a372012-09-04 09:15:20 +0200317 /* get non-exclusive (read) access to sessions_list (conns) */
318 if (pthread_rwlock_rdlock (&session_lock) != 0) {
319 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
320 return EXIT_FAILURE;
321 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200322 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200323 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
324 if (locked_session != NULL) {
325 session = locked_session->session;
326 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200327 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200328 /* get exclusive access to session */
329 if (pthread_mutex_lock(&locked_session->lock) != 0) {
330 /* unlock before returning error */
331 if (pthread_rwlock_unlock (&session_lock) != 0) {
332 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
333 return EXIT_FAILURE;
334 }
335 return EXIT_FAILURE;
336 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200337 locked_session->last_activity = apr_time_now();
Radek Krejci035bf4e2012-07-25 10:59:09 +0200338 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100339 msgt = nc_session_send_recv(session, rpc, &reply);
340
David Kupka8e60a372012-09-04 09:15:20 +0200341 /* first release exclusive lock for this session */
342 pthread_mutex_unlock(&locked_session->lock);
343 /* end of critical section */
344 if (pthread_rwlock_unlock (&session_lock) != 0) {
345 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
346 return EXIT_FAILURE;
347 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200348
Radek Krejcia332b692012-11-12 16:15:54 +0100349 /* process the result of the operation */
350 switch (msgt) {
351 case NC_MSG_UNKNOWN:
352 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
353 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
354 netconf_close(server, conns, session_key);
355 return (EXIT_FAILURE);
356 }
357 /* no break */
358 case NC_MSG_NONE:
359 /* there is error handled by callback */
360 return (EXIT_FAILURE);
361 break;
362 case NC_MSG_REPLY:
363 switch (replyt = nc_reply_get_type(reply)) {
364 case NC_REPLY_OK:
365 retval = EXIT_SUCCESS;
366 break;
367 default:
368 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
369 retval = EXIT_FAILURE;
370 break;
371 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200372 break;
373 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100374 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200375 retval = EXIT_FAILURE;
376 break;
377 }
378 nc_reply_free(reply);
379 return (retval);
380 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100381 /* release lock on failure */
382 if (pthread_rwlock_unlock (&session_lock) != 0) {
383 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
384 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200385 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
386 return (EXIT_FAILURE);
387 }
388}
Radek Krejci80c10d92012-07-30 08:38:50 +0200389
390static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200391{
392 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200393 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100394 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200395 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100396 NC_MSG_TYPE msgt;
397 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200398
399 /* check requests */
400 if (rpc == NULL) {
401 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
402 return (NULL);
403 }
404
David Kupka8e60a372012-09-04 09:15:20 +0200405 /* get non-exclusive (read) access to sessions_list (conns) */
406 if (pthread_rwlock_rdlock (&session_lock) != 0) {
407 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
408 return NULL;
409 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200410 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200411 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
412 if (locked_session != NULL) {
413 session = locked_session->session;
414 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200415 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200416 /* get exclusive access to session */
417 if (pthread_mutex_lock(&locked_session->lock) != 0) {
418 /* unlock before returning error */
419 if (pthread_rwlock_unlock (&session_lock) != 0) {
420 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
421 return NULL;
422 }
423 return NULL;
424 }
Tomas Cejkaaf7a1562013-04-13 02:27:43 +0200425 locked_session->last_activity = apr_time_now();
Radek Krejci8e4632a2012-07-26 13:40:34 +0200426 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100427 msgt = nc_session_send_recv(session, rpc, &reply);
428
David Kupka8e60a372012-09-04 09:15:20 +0200429 /* first release exclusive lock for this session */
430 pthread_mutex_unlock(&locked_session->lock);
431 /* end of critical section */
432 if (pthread_rwlock_unlock (&session_lock) != 0) {
433 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 +0100434 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200435 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200436
Radek Krejcia332b692012-11-12 16:15:54 +0100437 /* process the result of the operation */
438 switch (msgt) {
439 case NC_MSG_UNKNOWN:
440 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
441 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
442 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200443 return (NULL);
444 }
Radek Krejcia332b692012-11-12 16:15:54 +0100445 /* no break */
446 case NC_MSG_NONE:
447 /* there is error handled by callback */
448 return (NULL);
449 break;
450 case NC_MSG_REPLY:
451 switch (replyt = nc_reply_get_type(reply)) {
452 case NC_REPLY_DATA:
453 if ((data = nc_reply_get_data (reply)) == NULL) {
454 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
455 data = NULL;
456 }
457 break;
458 default:
459 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
460 data = NULL;
461 break;
462 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200463 break;
464 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100465 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
466 data = NULL;
467 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200468 }
469 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200470 return (data);
471 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100472 /* release lock on failure */
473 if (pthread_rwlock_unlock (&session_lock) != 0) {
474 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
475 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200476 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
477 return (NULL);
478 }
479}
480
Radek Krejci80c10d92012-07-30 08:38:50 +0200481static 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 +0200482{
483 nc_rpc* rpc;
484 struct nc_filter *f = NULL;
485 char* data = NULL;
486
487 /* create filter if set */
488 if (filter != NULL) {
489 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
490 }
491
492 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100493 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200494 nc_filter_free(f);
495 if (rpc == NULL) {
496 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
497 return (NULL);
498 }
499
500 data = netconf_opdata(server, conns, session_key, rpc);
501 nc_rpc_free (rpc);
502 return (data);
503}
504
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100505static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
506{
507 nc_rpc* rpc;
508 char* data = NULL;
509
510 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100511 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100512 if (rpc == NULL) {
513 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
514 return (NULL);
515 }
516
517 data = netconf_opdata(server, conns, session_key, rpc);
518 nc_rpc_free (rpc);
519 return (data);
520}
521
Radek Krejci80c10d92012-07-30 08:38:50 +0200522static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200523{
524 nc_rpc* rpc;
525 struct nc_filter *f = NULL;
526 char* data = NULL;
527
528 /* create filter if set */
529 if (filter != NULL) {
530 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
531 }
532
533 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100534 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200535 nc_filter_free(f);
536 if (rpc == NULL) {
537 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
538 return (NULL);
539 }
540
541 data = netconf_opdata(server, conns, session_key, rpc);
542 nc_rpc_free (rpc);
543 return (data);
544}
545
Tomas Cejka5064c232013-01-17 09:30:58 +0100546static 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 +0200547{
548 nc_rpc* rpc;
549 int retval = EXIT_SUCCESS;
550
551 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100552 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
553 if (target == NC_DATASTORE_URL) {
554 rpc = nc_rpc_copyconfig(source, target, config, url);
555 } else {
556 rpc = nc_rpc_copyconfig(source, target, config);
557 }
558 } else {
559 if (target == NC_DATASTORE_URL) {
560 rpc = nc_rpc_copyconfig(source, target, url);
561 } else {
562 rpc = nc_rpc_copyconfig(source, target);
563 }
564 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200565 if (rpc == NULL) {
566 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
567 return (EXIT_FAILURE);
568 }
569
570 retval = netconf_op(server, conns, session_key, rpc);
571 nc_rpc_free (rpc);
572 return (retval);
573}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200574
Tomas Cejka5064c232013-01-17 09:30:58 +0100575static 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 +0200576{
577 nc_rpc* rpc;
578 int retval = EXIT_SUCCESS;
579
580 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100581 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
582 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200583 if (rpc == NULL) {
584 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
585 return (EXIT_FAILURE);
586 }
587
588 retval = netconf_op(server, conns, session_key, rpc);
589 nc_rpc_free (rpc);
590 return (retval);
591}
592
Radek Krejci80c10d92012-07-30 08:38:50 +0200593static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200594{
595 nc_rpc* rpc;
596 int retval = EXIT_SUCCESS;
597
598 /* create requests */
599 rpc = nc_rpc_killsession(sid);
600 if (rpc == NULL) {
601 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
602 return (EXIT_FAILURE);
603 }
604
605 retval = netconf_op(server, conns, session_key, rpc);
606 nc_rpc_free (rpc);
607 return (retval);
608}
609
Radek Krejci80c10d92012-07-30 08:38:50 +0200610static 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 +0200611{
612 nc_rpc* rpc;
613 int retval = EXIT_SUCCESS;
614
615 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200616 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200617 if (rpc == NULL) {
618 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
619 return (EXIT_FAILURE);
620 }
621
622 retval = netconf_op(server, conns, session_key, rpc);
623 nc_rpc_free (rpc);
624 return (retval);
625}
626
Radek Krejci80c10d92012-07-30 08:38:50 +0200627static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200628{
629 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
630}
631
Radek Krejci80c10d92012-07-30 08:38:50 +0200632static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200633{
634 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
635}
636
Radek Krejci80c10d92012-07-30 08:38:50 +0200637static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200638{
639 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
640}
641
Radek Krejci80c10d92012-07-30 08:38:50 +0200642/**
643 * @return REPLY_OK: 0, *data == NULL
644 * REPLY_DATA: 0, *data != NULL
645 * REPLY_ERROR: 1, *data == NULL
646 */
647static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
648{
649 struct nc_session *session = NULL;
650 nc_reply* reply;
651 nc_rpc* rpc;
652 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100653 NC_MSG_TYPE msgt;
654 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200655
656 /* create requests */
657 rpc = nc_rpc_generic(content);
658 if (rpc == NULL) {
659 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
660 return (EXIT_FAILURE);
661 }
662
Radek Krejcia332b692012-11-12 16:15:54 +0100663 if (data != NULL) {
664 *data = NULL;
665 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200666
667 /* get session where send the RPC */
668 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
669 if (session != NULL) {
670 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100671 msgt = nc_session_send_recv(session, rpc, &reply);
672 nc_rpc_free (rpc);
673
674 /* process the result of the operation */
675 switch (msgt) {
676 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200677 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
678 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
679 netconf_close(server, conns, session_key);
680 return (EXIT_FAILURE);
681 }
Radek Krejcia332b692012-11-12 16:15:54 +0100682 /* no break */
683 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200684 /* there is error handled by callback */
685 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200686 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100687 case NC_MSG_REPLY:
688 switch (replyt = nc_reply_get_type(reply)) {
689 case NC_REPLY_DATA:
690 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
691 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
692 nc_reply_free(reply);
693 return (EXIT_FAILURE);
694 }
695 retval = EXIT_SUCCESS;
696 break;
697 case NC_REPLY_OK:
698 retval = EXIT_SUCCESS;
699 break;
700 default:
701 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
702 retval = EXIT_FAILURE;
703 break;
704 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200705 break;
706 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200708 retval = EXIT_FAILURE;
709 break;
710 }
711 nc_reply_free(reply);
712
713 return (retval);
714 } else {
715 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
716 return (EXIT_FAILURE);
717 }
718}
719
Radek Krejci469aab82012-07-22 18:42:20 +0200720server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200721void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200722{
Radek Krejci7338bde2012-08-10 12:57:30 +0200723 switch (level) {
724 case NC_VERB_ERROR:
725 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
726 break;
727 case NC_VERB_WARNING:
728 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
729 break;
730 case NC_VERB_VERBOSE:
731 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
732 break;
733 case NC_VERB_DEBUG:
734 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
735 break;
736 }
Radek Krejci469aab82012-07-22 18:42:20 +0200737}
738
David Kupka8e60a372012-09-04 09:15:20 +0200739void * thread_routine (void * arg)
740{
741 void * retval = NULL;
742
743 ssize_t buffer_len;
744 struct pollfd fds;
745 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200746 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200747 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200748 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200749 char* session_key, *data;
750 const char *host, *port, *user, *pass;
751 const char *msgtext, *cpbltstr;
752 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100753 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200754 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200755 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200756 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200757 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100758 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200759 NC_EDIT_ERROPT_TYPE erropt_type = 0;
760
761 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
762 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
763 server_rec * server = ((struct pass_to_thread*)arg)->server;
764 int client = ((struct pass_to_thread*)arg)->client;
765
766 char * buffer, chunk_len_str[12], *chunked_msg;
767 char c;
768
769 while (!isterminated) {
770 fds.fd = client;
771 fds.events = POLLIN;
772 fds.revents = 0;
773
774 status = poll(&fds, 1, 1000);
775
776 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
777 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
778 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
779 continue;
780 } else if (status < 0) {
781 /* 0: poll time outed
782 * close socket and ignore this request from the client, it can try it again
783 * -1: poll failed
784 * something wrong happend, close this socket and wait for another request
785 */
786 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
787 close(client);
788 break;
789 }
790 /* status > 0 */
791
792 /* check the status of the socket */
793
794 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
795 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
796 /* close client's socket (it's probably already closed by client */
797 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
798 close(client);
799 break;
800 }
801
802 /* read json in chunked framing */
803 buffer_size = 0;
804 buffer_len = 0;
805 buffer = NULL;
806 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200807 /* read chunk length */
808 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
809 free (buffer);
810 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200811 break;
812 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200813 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
814 free (buffer);
815 buffer = NULL;
816 break;
817 }
818 i=0;
819 memset (chunk_len_str, 0, 12);
820 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
821 if (i==0 && c == '#') {
822 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
823 /* end but invalid */
824 free (buffer);
825 buffer = NULL;
826 }
827 /* end of message, double-loop break */
828 goto msg_complete;
829 }
830 chunk_len_str[i++] = c;
831 }
832 if (c != '\n') {
833 free (buffer);
834 buffer = NULL;
835 break;
836 }
837 if ((chunk_len = atoi (chunk_len_str)) == 0) {
838 free (buffer);
839 buffer = NULL;
840 break;
841 }
842 buffer_size += chunk_len+1;
843 buffer = realloc (buffer, sizeof(char)*buffer_size);
844 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
845 free (buffer);
846 buffer = NULL;
847 break;
848 }
849 buffer_len += ret;
850 }
851msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200852
853 if (buffer != NULL) {
854 request = json_tokener_parse(buffer);
855 operation = json_object_get_int(json_object_object_get(request, "type"));
856
857 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
858 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
859 if (operation != MSG_CONNECT && session_key == NULL) {
860 reply = json_object_new_object();
861 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
862 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
863 msgtext = json_object_to_json_string(reply);
864 send(client, msgtext, strlen(msgtext) + 1, 0);
865 json_object_put(reply);
866 /* there is some stupid client, so close the connection to give a chance to some other client */
867 close(client);
868 break;
869 }
870
871 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100872 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200873 ds_type_t = -1;
874 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
875 if (strcmp(target, "running") == 0) {
876 ds_type_t = NC_DATASTORE_RUNNING;
877 } else if (strcmp(target, "startup") == 0) {
878 ds_type_t = NC_DATASTORE_STARTUP;
879 } else if (strcmp(target, "candidate") == 0) {
880 ds_type_t = NC_DATASTORE_CANDIDATE;
881 }
882 }
883 ds_type_s = -1;
884 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
885 if (strcmp(source, "running") == 0) {
886 ds_type_s = NC_DATASTORE_RUNNING;
887 } else if (strcmp(source, "startup") == 0) {
888 ds_type_s = NC_DATASTORE_STARTUP;
889 } else if (strcmp(source, "candidate") == 0) {
890 ds_type_s = NC_DATASTORE_CANDIDATE;
891 }
892 }
893
894 /* null global JSON error-reply */
895 err_reply = NULL;
896
897 /* prepare reply envelope */
898 reply = json_object_new_object();
899
900 /* process required operation */
901 switch (operation) {
902 case MSG_CONNECT:
903 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
904
905 host = json_object_get_string(json_object_object_get(request, "host"));
906 port = json_object_get_string(json_object_object_get(request, "port"));
907 user = json_object_get_string(json_object_object_get(request, "user"));
908 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200909 capabilities = json_object_object_get(request, "capabilities");
910 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200911 cpblts = nc_cpblts_new (NULL);
912 for (i=0; i<len; i++) {
913 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
914 }
915 } else {
916 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
917 }
David Kupka8e60a372012-09-04 09:15:20 +0200918 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
919 if ((host == NULL) || (user == NULL)) {
920 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
921 session_key = NULL;
922 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200923 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
924 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200925 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
926 }
927
928 reply = json_object_new_object();
929 if (session_key == NULL) {
930 /* negative reply */
931 if (err_reply == NULL) {
932 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
933 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100934 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200935 } else {
936 /* use filled err_reply from libnetconf's callback */
937 json_object_put(reply);
938 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100939 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200940 }
941 } else {
942 /* positive reply */
943 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
944 json_object_object_add(reply, "session", json_object_new_string(session_key));
945
946 free(session_key);
947 }
948
949 break;
950 case MSG_GET:
951 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
952
953 filter = json_object_get_string(json_object_object_get(request, "filter"));
954
955 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
956
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200957 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200958 if (err_reply == NULL) {
959 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
960 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
961 } else {
962 /* use filled err_reply from libnetconf's callback */
963 json_object_put(reply);
964 reply = err_reply;
965 }
966 } else {
967 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
968 json_object_object_add(reply, "data", json_object_new_string(data));
969 }
970 break;
971 case MSG_GETCONFIG:
972 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
973
974 filter = json_object_get_string(json_object_object_get(request, "filter"));
975
976 if (ds_type_s == -1) {
977 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
978 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
979 break;
980 }
981
982 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
983 if (err_reply == NULL) {
984 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
985 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
986 } else {
987 /* use filled err_reply from libnetconf's callback */
988 json_object_put(reply);
989 reply = err_reply;
990 }
991 } else {
992 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
993 json_object_object_add(reply, "data", json_object_new_string(data));
994 }
995 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100996 case MSG_GETSCHEMA:
997 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
998 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
999 if (identifier == NULL) {
1000 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1001 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1002 break;
1003 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001004 version = json_object_get_string(json_object_object_get(request, "version"));
1005 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001006
Tomas Cejka94da2c52013-01-08 18:20:30 +01001007 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001008 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001009 if (err_reply == NULL) {
1010 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1011 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1012 } else {
1013 /* use filled err_reply from libnetconf's callback */
1014 json_object_put(reply);
1015 reply = err_reply;
1016 }
1017 } else {
1018 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1019 json_object_object_add(reply, "data", json_object_new_string(data));
1020 }
1021 break;
David Kupka8e60a372012-09-04 09:15:20 +02001022 case MSG_EDITCONFIG:
1023 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1024
1025 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1026 if (defop != NULL) {
1027 if (strcmp(defop, "merge") == 0) {
1028 defop_type = NC_EDIT_DEFOP_MERGE;
1029 } else if (strcmp(defop, "replace") == 0) {
1030 defop_type = NC_EDIT_DEFOP_REPLACE;
1031 } else if (strcmp(defop, "none") == 0) {
1032 defop_type = NC_EDIT_DEFOP_NONE;
1033 } else {
1034 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1035 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1036 break;
1037 }
1038 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001039 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001040 }
1041
1042 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1043 if (erropt != NULL) {
1044 if (strcmp(erropt, "continue-on-error") == 0) {
1045 erropt_type = NC_EDIT_ERROPT_CONT;
1046 } else if (strcmp(erropt, "stop-on-error") == 0) {
1047 erropt_type = NC_EDIT_ERROPT_STOP;
1048 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1049 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1050 } else {
1051 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1052 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1053 break;
1054 }
1055 } else {
1056 erropt_type = 0;
1057 }
1058
1059 if (ds_type_t == -1) {
1060 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1061 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1062 break;
1063 }
1064
1065 config = json_object_get_string(json_object_object_get(request, "config"));
1066 if (config == NULL) {
1067 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1068 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1069 break;
1070 }
1071
Tomas Cejka5064c232013-01-17 09:30:58 +01001072 /* TODO url capability see netconf_editconfig */
1073 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1074 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 +02001075 if (err_reply == NULL) {
1076 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1077 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1078 } else {
1079 /* use filled err_reply from libnetconf's callback */
1080 json_object_put(reply);
1081 reply = err_reply;
1082 }
1083 } else {
1084 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1085 }
1086 break;
1087 case MSG_COPYCONFIG:
1088 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1089 config = NULL;
1090
1091 if (source == NULL) {
1092 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001093 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001094 config = json_object_get_string(json_object_object_get(request, "config"));
1095 } else if (ds_type_s == -1) {
1096 /* source datastore specified, but it is invalid */
1097 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1098 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1099 break;
1100 }
1101
1102 if (ds_type_t == -1) {
1103 /* invalid target datastore specified */
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("Invalid target repository type requested."));
1106 break;
1107 }
1108
1109 if (source == NULL && config == NULL) {
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 input parameters - one of source and config is required."));
1112 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001113 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 +02001114 if (err_reply == NULL) {
1115 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1116 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1117 } else {
1118 /* use filled err_reply from libnetconf's callback */
1119 json_object_put(reply);
1120 reply = err_reply;
1121 }
1122 } else {
1123 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1124 }
1125 }
1126 break;
1127 case MSG_DELETECONFIG:
1128 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1129 /* no break - unifying code */
1130 case MSG_LOCK:
1131 if (operation == MSG_LOCK) {
1132 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1133 }
1134 /* no break - unifying code */
1135 case MSG_UNLOCK:
1136 if (operation == MSG_UNLOCK) {
1137 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1138 }
1139
1140 if (ds_type_t == -1) {
1141 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1142 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1143 break;
1144 }
1145
1146 switch(operation) {
1147 case MSG_DELETECONFIG:
1148 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1149 break;
1150 case MSG_LOCK:
1151 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1152 break;
1153 case MSG_UNLOCK:
1154 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1155 break;
1156 default:
1157 status = -1;
1158 break;
1159 }
1160
1161 if (status != EXIT_SUCCESS) {
1162 if (err_reply == NULL) {
1163 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1164 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1165 } else {
1166 /* use filled err_reply from libnetconf's callback */
1167 json_object_put(reply);
1168 reply = err_reply;
1169 }
1170 } else {
1171 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1172 }
1173 break;
1174 case MSG_KILL:
1175 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1176
1177 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1178
1179 if (sid == NULL) {
1180 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1181 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1182 break;
1183 }
1184
1185 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1186 if (err_reply == NULL) {
1187 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1188 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1189 } else {
1190 /* use filled err_reply from libnetconf's callback */
1191 json_object_put(reply);
1192 reply = err_reply;
1193 }
1194 } else {
1195 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1196 }
1197 break;
1198 case MSG_DISCONNECT:
1199 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1200
1201 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1202 if (err_reply == NULL) {
1203 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1204 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1205 } else {
1206 /* use filled err_reply from libnetconf's callback */
1207 json_object_put(reply);
1208 reply = err_reply;
1209 }
1210 } else {
1211 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1212 }
1213 break;
1214 case MSG_INFO:
1215 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1216
Kupka Davidda134a12012-09-06 14:12:16 +02001217 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1218 if (locked_session != NULL) {
1219 session = locked_session->session;
1220 } else {
1221 session = NULL;
1222 }
David Kupka8e60a372012-09-04 09:15:20 +02001223 if (session != NULL) {
1224 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1225 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1226 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1227 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1228 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1229 cpblts = nc_session_get_cpblts (session);
1230 if (cpblts != NULL) {
1231 json_obj = json_object_new_array();
1232 nc_cpblts_iter_start (cpblts);
1233 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1234 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1235 }
1236 json_object_object_add(reply, "capabilities", json_obj);
1237 }
1238 } else {
1239 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1240 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1241 }
1242
1243 break;
1244 case MSG_GENERIC:
1245 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1246
1247 config = json_object_get_string(json_object_object_get(request, "content"));
1248
1249 if (config == NULL) {
1250 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1251 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1252 break;
1253 }
1254
1255 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1256 if (err_reply == NULL) {
1257 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1258 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1259 } else {
1260 /* use filled err_reply from libnetconf's callback */
1261 json_object_put(reply);
1262 reply = err_reply;
1263 }
1264 } else {
1265 if (data == NULL) {
1266 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1267 } else {
1268 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1269 json_object_object_add(reply, "data", json_object_new_string(data));
1270 }
1271 }
1272 break;
1273 default:
1274 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1275 reply = json_object_new_object();
1276 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1277 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1278 break;
1279 }
1280 json_object_put(request);
1281
David Kupka1e3e4c82012-09-04 09:32:15 +02001282 /* send reply to caller */
1283 if (reply != NULL) {
1284 msgtext = json_object_to_json_string(reply);
1285 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1286 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001287 break;
1288 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001289 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1290 json_object_put(reply);
1291 free (chunked_msg);
1292 free (buffer);
1293 } else {
1294 break;
David Kupka8e60a372012-09-04 09:15:20 +02001295 }
1296 }
1297 }
David Kupka8e60a372012-09-04 09:15:20 +02001298 free (arg);
1299
1300 return retval;
1301}
1302
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001303/**
1304 * \brief Close all open NETCONF sessions.
1305 *
1306 * During termination of mod_netconf, it is useful to close all remaining
1307 * sessions. This function iterates over the list of sessions and close them
1308 * all.
1309 *
1310 * \param[in] server pointer to server_rec for logging
1311 * \param[in] p apr pool needed for hash table iterating
1312 * \param[in] ht hash table of session_with_mutex structs
1313 */
1314static void close_all_nc_sessions(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1315{
1316 apr_hash_index_t *hi;
1317 void *val = NULL;
1318 struct session_with_mutex *swm = NULL;
1319 struct nc_session *ns = NULL;
1320 const char *hashed_key = NULL;
1321 apr_ssize_t hashed_key_length;
1322
1323 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1324 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1325 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1326 swm = (struct session_with_mutex *) val;
1327 if (swm != NULL) {
1328 pthread_mutex_lock(&swm->lock);
1329 if (swm->session != NULL) {
1330 ns = swm->session;
1331 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1332 nc_session_free(ns);
1333 swm->session = NULL;
1334 }
1335 pthread_mutex_unlock(&swm->lock);
1336 }
1337 }
1338}
1339
1340static void check_timeout_and_close(server_rec* server, apr_pool_t *p, apr_hash_t *ht)
1341{
1342 apr_hash_index_t *hi;
1343 void *val = NULL;
1344 struct nc_session *ns = NULL;
1345 struct session_with_mutex *swm = NULL;
1346 const char *hashed_key = NULL;
1347 apr_ssize_t hashed_key_length;
1348 apr_time_t current_time = apr_time_now();
1349
1350 for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
1351 apr_hash_this(hi, (const void **) &hashed_key, &hashed_key_length, &val);
1352 swm = (struct session_with_mutex *) val;
1353 if (swm == NULL) {
1354 continue;
1355 }
1356 ns = swm->session;
1357 if (ns == NULL) {
1358 continue;
1359 }
1360 pthread_mutex_lock(&swm->lock);
1361 if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
1362 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Closing NETCONF session (%s).", hashed_key);
1363 nc_session_close(ns, NC_SESSION_TERM_CLOSED);
1364 nc_session_free (ns);
1365 ns = NULL;
1366 /* remove session from the active sessions list */
1367 apr_hash_set(ht, hashed_key, APR_HASH_KEY_STRING, NULL);
1368 }
1369 pthread_mutex_unlock(&swm->lock);
1370 }
1371}
1372
1373
1374/**
Radek Krejcif23850c2012-07-23 16:14:17 +02001375 * This is actually implementation of NETCONF client
1376 * - requests are received from UNIX socket in the predefined format
1377 * - results are replied through the same way
1378 * - the daemon run as a separate process, but it is started and stopped
1379 * automatically by Apache.
1380 *
1381 */
Radek Krejci469aab82012-07-22 18:42:20 +02001382static void forked_proc(apr_pool_t * pool, server_rec * server)
1383{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001384 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001385 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001386 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001387 unsigned int olds = 0, timediff = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001388 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001389 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001390 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001391 struct pass_to_thread * arg;
1392 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1393 struct timespec maxtime;
1394 pthread_rwlockattr_t lock_attrs;
1395
1396 /* wait at most 5 secons for every thread to terminate */
1397 maxtime.tv_sec = 5;
1398 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001399
Radek Krejcif23850c2012-07-23 16:14:17 +02001400 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001401 unixd_setup_child();
1402
Radek Krejciae021c12012-07-25 18:03:52 +02001403 cfg = ap_get_module_config(server->module_config, &netconf_module);
1404 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001405 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1406 return;
1407 }
Radek Krejci469aab82012-07-22 18:42:20 +02001408
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001409 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001410 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1411 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1412 return;
1413 }
1414
1415 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001416 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001417 unlink(local.sun_path);
1418 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1419
1420 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1421 if (errno == EADDRINUSE) {
1422 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1423 close(lsock);
1424 exit(0);
1425 }
1426 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1427 close(lsock);
1428 return;
1429 }
1430
1431 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1432 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1433 close(lsock);
1434 return;
1435 }
1436
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001437 #ifdef WITH_NOTIFICATIONS
1438 if (notification_init(pool, server) == -1) {
1439 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1440 use_notifications = 0;
1441 } else {
1442 use_notifications = 1;
1443 }
1444 #endif
1445
Radek Krejci469aab82012-07-22 18:42:20 +02001446 /* prepare internal lists */
1447 netconf_sessions_list = apr_hash_make(pool);
1448
1449 /* setup libnetconf's callbacks */
1450 nc_verbosity(NC_VERB_DEBUG);
1451 clb_print_server = server;
1452 nc_callback_print(clb_print);
1453 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1454 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1455 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001456 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001457
1458 /* disable publickey authentication */
1459 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1460
David Kupka8e60a372012-09-04 09:15:20 +02001461 /* create mutex protecting session list */
1462 pthread_rwlockattr_init(&lock_attrs);
1463 /* rwlock is shared only with threads in this process */
1464 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1465 /* create rw lock */
1466 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1467 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1468 close (lsock);
1469 return;
1470 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001471
1472 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001473 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001474 gettimeofday(&tv, NULL);
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001475 timediff = (unsigned int)tv.tv_sec - olds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001476 #ifdef WITH_NOTIFICATIONS
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001477 if (timediff > 60) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001478 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1479 }
1480 if (use_notifications == 1) {
1481 notification_handle();
1482 }
1483 #endif
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001484 if (timediff > ACTIVITY_CHECK_INTERVAL) {
1485 check_timeout_and_close(server, pool, netconf_sessions_list);
1486 }
Radek Krejci469aab82012-07-22 18:42:20 +02001487
1488 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001489 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001490 if (((unsigned int)tv.tv_sec - olds) > 60) {
1491 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1492 olds = tv.tv_sec;
1493 }
David Kupka8e60a372012-09-04 09:15:20 +02001494 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001495 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1496 apr_sleep(SLEEP_TIME);
1497 continue;
1498 } else if (client == -1 && (errno == EINTR)) {
1499 continue;
1500 } else if (client == -1) {
1501 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1502 continue;
1503 }
Radek Krejci469aab82012-07-22 18:42:20 +02001504
1505 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001506 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001507
David Kupka8e60a372012-09-04 09:15:20 +02001508 arg = malloc (sizeof(struct pass_to_thread));
1509 arg->client = client;
1510 arg->pool = pool;
1511 arg->server = server;
1512 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001513
David Kupka8e60a372012-09-04 09:15:20 +02001514 /* start new thread. It will serve this particular request and then terminate */
1515 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1516 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1517 } else {
1518 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1519 pthread_count++;
1520 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1521 ptids[pthread_count] = 0;
1522 }
Radek Krejci469aab82012-07-22 18:42:20 +02001523
David Kupka8e60a372012-09-04 09:15:20 +02001524 /* check if some thread already terminated, free some resources by joining it */
1525 for (i=0; i<pthread_count; i++) {
1526 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1527 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1528 pthread_count--;
1529 if (pthread_count > 0) {
1530 /* place last Thread ID on the place of joined one */
1531 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001532 }
Radek Krejci469aab82012-07-22 18:42:20 +02001533 }
1534 }
David Kupka8e60a372012-09-04 09:15:20 +02001535 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001536 }
1537
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001538 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf terminating...");
David Kupka8e60a372012-09-04 09:15:20 +02001539 /* join all threads */
1540 for (i=0; i<pthread_count; i++) {
1541 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1542 }
1543 free (ptids);
1544
Radek Krejci469aab82012-07-22 18:42:20 +02001545 close(lsock);
1546
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001547 #ifdef WITH_NOTIFICATIONS
1548 notification_close();
1549 #endif
1550
Tomas Cejkaaf7a1562013-04-13 02:27:43 +02001551 /* close all NETCONF sessions */
1552 close_all_nc_sessions(server, pool, netconf_sessions_list);
1553
David Kupka8e60a372012-09-04 09:15:20 +02001554 /* destroy rwlock */
1555 pthread_rwlock_destroy(&session_lock);
1556 pthread_rwlockattr_destroy(&lock_attrs);
1557
Radek Krejci469aab82012-07-22 18:42:20 +02001558 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1559
1560 exit(APR_SUCCESS);
1561}
1562
Radek Krejcif23850c2012-07-23 16:14:17 +02001563static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001564{
Radek Krejcif23850c2012-07-23 16:14:17 +02001565 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1566
1567 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1568 apr_pool_create(&config->pool, pool);
1569 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001570 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001571
1572 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001573}
1574
1575static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1576 apr_pool_t * plog, server_rec * s)
1577{
Radek Krejcif23850c2012-07-23 16:14:17 +02001578 mod_netconf_cfg *config;
1579 apr_status_t res;
1580
Radek Krejci469aab82012-07-22 18:42:20 +02001581 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001582 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001583 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001584
1585 /*
1586 * The following checks if this routine has been called before.
1587 * This is necessary because the parent process gets initialized
1588 * a couple of times as the server starts up.
1589 */
1590 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1591 if (!data) {
1592 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1593 return (OK);
1594 }
1595
Radek Krejcif23850c2012-07-23 16:14:17 +02001596 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1597 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001598
Radek Krejcif23850c2012-07-23 16:14:17 +02001599 if (config && config->forkproc == NULL) {
1600 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1601 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001602 switch (res) {
1603 case APR_INCHILD:
1604 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001605 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001606 apr_signal(SIGTERM, signal_handler);
1607
1608 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001609 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001610
1611 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001612 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001613
Radek Krejcif23850c2012-07-23 16:14:17 +02001614 /* I never should be here, wtf?!? */
1615 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001616 exit(APR_EGENERAL);
1617 break;
1618 case APR_INPARENT:
1619 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001620 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001621 break;
1622 default:
1623 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1624 break;
1625 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001626 } else {
1627 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001628 }
1629
1630 return OK;
1631}
1632
Radek Krejci469aab82012-07-22 18:42:20 +02001633/**
1634 * Register module hooks
1635 */
1636static void mod_netconf_register_hooks(apr_pool_t * p)
1637{
Radek Krejcif23850c2012-07-23 16:14:17 +02001638 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001639}
1640
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001641static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1642{
1643 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1644 return NULL;
1645}
1646
1647static const command_rec netconf_cmds[] = {
1648 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1649 {NULL}
1650};
1651
Radek Krejci469aab82012-07-22 18:42:20 +02001652/* Dispatch list for API hooks */
1653module AP_MODULE_DECLARE_DATA netconf_module = {
1654 STANDARD20_MODULE_STUFF,
1655 NULL, /* create per-dir config structures */
1656 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001657 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001658 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001659 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001660 mod_netconf_register_hooks /* register hooks */
1661};
Radek Krejcia332b692012-11-12 16:15:54 +01001662