blob: 31422251dd7ea594e08d51ccfbd172c05291dfbf [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"
76
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
82
83/* sleep in master process for non-blocking socket reading */
84#define SLEEP_TIME 200
85
86#ifndef offsetof
87#define offsetof(type, member) ((size_t) ((type *) 0)->member)
88#endif
89
Tomas Cejka027f3bc2012-11-10 20:28:36 +010090/* timeout in msec */
Radek Krejci469aab82012-07-22 18:42:20 +020091struct timeval timeout = { 1, 0 };
92
Tomas Cejka5064c232013-01-17 09:30:58 +010093#define NCWITHDEFAULTS NCWD_MODE_NOTSET
94
95
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020096
Radek Krejci469aab82012-07-22 18:42:20 +020097#define MSG_OK 0
98#define MSG_OPEN 1
99#define MSG_DATA 2
100#define MSG_CLOSE 3
101#define MSG_ERROR 4
102#define MSG_UNKNOWN 5
103
Radek Krejci469aab82012-07-22 18:42:20 +0200104module AP_MODULE_DECLARE_DATA netconf_module;
105
106typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200107 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200108 apr_proc_t *forkproc;
109 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200110} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200111
David Kupka8e60a372012-09-04 09:15:20 +0200112struct pass_to_thread {
113 int client; /**< opened socket */
114 apr_pool_t * pool; /**< ?? */
115 server_rec * server; /**< ?? */
116 apr_hash_t * netconf_sessions_list; /**< ?? */
117};
118
119struct session_with_mutex {
120 struct nc_session * session; /**< netconf session */
121 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
122};
123
124pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
125
Radek Krejci469aab82012-07-22 18:42:20 +0200126volatile int isterminated = 0;
127
128static char* password;
129
130
131static void signal_handler(int sign)
132{
133 switch (sign) {
134 case SIGTERM:
135 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200136 break;
137 }
138}
139
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200140static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200141{
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
143 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200144 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200145
Radek Krejci469aab82012-07-22 18:42:20 +0200146 apr_sha1_ctx_t sha1_ctx;
147 apr_sha1_init(&sha1_ctx);
148 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
149 apr_sha1_update(&sha1_ctx, port, strlen(port));
150 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200151 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200152
Radek Krejcif23850c2012-07-23 16:14:17 +0200153 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200154 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200155 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200156 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200157 }
158 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200159
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200160 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200161}
162
163int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
164{
165 /* always approve */
166 return (EXIT_SUCCESS);
167}
168
169char* netconf_callback_sshauth_password (const char* username, const char* hostname)
170{
171 char* buf;
172
173 buf = malloc ((strlen(password) + 1) * sizeof(char));
174 apr_cpystrn(buf, password, strlen(password) + 1);
175
176 return (buf);
177}
178
179void netconf_callback_sshauth_interactive (const char* name,
180 int name_len,
181 const char* instruction,
182 int instruction_len,
183 int num_prompts,
184 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
185 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
186 void** abstract)
187{
188 int i;
189
190 for (i=0; i<num_prompts; i++) {
191 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
192 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
193 responses[i].length = strlen(responses[i].text) + 1;
194 }
195
196 return;
197}
198
Radek Krejcic11fd862012-07-26 12:41:21 +0200199static json_object *err_reply = NULL;
200void netconf_callback_error_process(const char* tag,
201 const char* type,
202 const char* severity,
203 const char* apptag,
204 const char* path,
205 const char* message,
206 const char* attribute,
207 const char* element,
208 const char* ns,
209 const char* sid)
210{
211 err_reply = json_object_new_object();
212 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
213 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
214 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
215 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
216 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
217 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
218 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
219 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
220 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
221 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
222 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
223}
224
Kupka David00b9c5c2012-09-05 09:45:50 +0200225static 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 +0200226{
Radek Krejci469aab82012-07-22 18:42:20 +0200227 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200228 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200229 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200230
231 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200232 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200233 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
234
Radek Krejci469aab82012-07-22 18:42:20 +0200235 /* if connected successful, add session to the list */
236 if (session != NULL) {
237 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200238 session_key = gen_ncsession_hash(
239 (host==NULL) ? "localhost" : host,
240 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200241 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200242
243 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
244 nc_session_free(session);
245 free (locked_session);
246 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
247 return NULL;
248 }
249 locked_session->session = session;
250 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100251 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200252 /* get exclusive access to sessions_list (conns) */
253 if (pthread_rwlock_wrlock (&session_lock) != 0) {
254 nc_session_free(session);
255 free (locked_session);
256 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
257 return NULL;
258 }
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100259 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200260 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100261 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200262 /* end of critical section */
263 if (pthread_rwlock_unlock (&session_lock) != 0) {
264 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
265 return NULL;
266 }
Radek Krejci469aab82012-07-22 18:42:20 +0200267 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200268 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200269 } else {
270 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200271 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200272 }
273
Radek Krejci469aab82012-07-22 18:42:20 +0200274}
275
Radek Krejci80c10d92012-07-30 08:38:50 +0200276static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200277{
278 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200279 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200280
Radek Krejcif23850c2012-07-23 16:14:17 +0200281 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200282 /* get exclusive (write) access to sessions_list (conns) */
283 if (pthread_rwlock_wrlock (&session_lock) != 0) {
284 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
285 return EXIT_FAILURE;
286 }
287 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
288 if (locked_session != NULL) {
289 pthread_mutex_destroy(&locked_session->lock);
290 ns = locked_session->session;
291 free (locked_session);
292 }
Radek Krejci469aab82012-07-22 18:42:20 +0200293 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100294 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200295 nc_session_free (ns);
296 ns = NULL;
297
298 /* remove session from the active sessions list */
299 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200300 /* end of critical section */
301 if (pthread_rwlock_unlock (&session_lock) != 0) {
302 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
303 return EXIT_FAILURE;
304 }
Radek Krejci469aab82012-07-22 18:42:20 +0200305 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200306
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200307 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200308 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200309 if (pthread_rwlock_unlock (&session_lock) != 0) {
310 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
311 return EXIT_FAILURE;
312 }
Radek Krejci469aab82012-07-22 18:42:20 +0200313 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200314 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200315 }
316}
317
Radek Krejci80c10d92012-07-30 08:38:50 +0200318static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200319{
320 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200321 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200322 nc_reply* reply;
323 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100324 NC_MSG_TYPE msgt;
325 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200326
Radek Krejci8e4632a2012-07-26 13:40:34 +0200327 /* check requests */
328 if (rpc == NULL) {
329 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
330 return (EXIT_FAILURE);
331 }
332
David Kupka8e60a372012-09-04 09:15:20 +0200333 /* get non-exclusive (read) access to sessions_list (conns) */
334 if (pthread_rwlock_rdlock (&session_lock) != 0) {
335 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
336 return EXIT_FAILURE;
337 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200338 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200339 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
340 if (locked_session != NULL) {
341 session = locked_session->session;
342 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200343 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200344 /* get exclusive access to session */
345 if (pthread_mutex_lock(&locked_session->lock) != 0) {
346 /* unlock before returning error */
347 if (pthread_rwlock_unlock (&session_lock) != 0) {
348 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
349 return EXIT_FAILURE;
350 }
351 return EXIT_FAILURE;
352 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200353 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100354 msgt = nc_session_send_recv(session, rpc, &reply);
355
David Kupka8e60a372012-09-04 09:15:20 +0200356 /* first release exclusive lock for this session */
357 pthread_mutex_unlock(&locked_session->lock);
358 /* end of critical section */
359 if (pthread_rwlock_unlock (&session_lock) != 0) {
360 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
361 return EXIT_FAILURE;
362 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200363
Radek Krejcia332b692012-11-12 16:15:54 +0100364 /* process the result of the operation */
365 switch (msgt) {
366 case NC_MSG_UNKNOWN:
367 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
368 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
369 netconf_close(server, conns, session_key);
370 return (EXIT_FAILURE);
371 }
372 /* no break */
373 case NC_MSG_NONE:
374 /* there is error handled by callback */
375 return (EXIT_FAILURE);
376 break;
377 case NC_MSG_REPLY:
378 switch (replyt = nc_reply_get_type(reply)) {
379 case NC_REPLY_OK:
380 retval = EXIT_SUCCESS;
381 break;
382 default:
383 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
384 retval = EXIT_FAILURE;
385 break;
386 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200387 break;
388 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100389 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200390 retval = EXIT_FAILURE;
391 break;
392 }
393 nc_reply_free(reply);
394 return (retval);
395 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100396 /* release lock on failure */
397 if (pthread_rwlock_unlock (&session_lock) != 0) {
398 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
399 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200400 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
401 return (EXIT_FAILURE);
402 }
403}
Radek Krejci80c10d92012-07-30 08:38:50 +0200404
405static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200406{
407 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200408 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100409 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200410 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100411 NC_MSG_TYPE msgt;
412 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200413
414 /* check requests */
415 if (rpc == NULL) {
416 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
417 return (NULL);
418 }
419
David Kupka8e60a372012-09-04 09:15:20 +0200420 /* get non-exclusive (read) access to sessions_list (conns) */
421 if (pthread_rwlock_rdlock (&session_lock) != 0) {
422 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
423 return NULL;
424 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200425 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200426 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
427 if (locked_session != NULL) {
428 session = locked_session->session;
429 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200430 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200431 /* get exclusive access to session */
432 if (pthread_mutex_lock(&locked_session->lock) != 0) {
433 /* unlock before returning error */
434 if (pthread_rwlock_unlock (&session_lock) != 0) {
435 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
436 return NULL;
437 }
438 return NULL;
439 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200440 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100441 msgt = nc_session_send_recv(session, rpc, &reply);
442
David Kupka8e60a372012-09-04 09:15:20 +0200443 /* first release exclusive lock for this session */
444 pthread_mutex_unlock(&locked_session->lock);
445 /* end of critical section */
446 if (pthread_rwlock_unlock (&session_lock) != 0) {
447 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 +0100448 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200449 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200450
Radek Krejcia332b692012-11-12 16:15:54 +0100451 /* process the result of the operation */
452 switch (msgt) {
453 case NC_MSG_UNKNOWN:
454 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
455 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
456 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200457 return (NULL);
458 }
Radek Krejcia332b692012-11-12 16:15:54 +0100459 /* no break */
460 case NC_MSG_NONE:
461 /* there is error handled by callback */
462 return (NULL);
463 break;
464 case NC_MSG_REPLY:
465 switch (replyt = nc_reply_get_type(reply)) {
466 case NC_REPLY_DATA:
467 if ((data = nc_reply_get_data (reply)) == NULL) {
468 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
469 data = NULL;
470 }
471 break;
472 default:
473 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
474 data = NULL;
475 break;
476 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200477 break;
478 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100479 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
480 data = NULL;
481 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200482 }
483 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200484 return (data);
485 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100486 /* release lock on failure */
487 if (pthread_rwlock_unlock (&session_lock) != 0) {
488 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
489 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
491 return (NULL);
492 }
493}
494
Radek Krejci80c10d92012-07-30 08:38:50 +0200495static 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 +0200496{
497 nc_rpc* rpc;
498 struct nc_filter *f = NULL;
499 char* data = NULL;
500
501 /* create filter if set */
502 if (filter != NULL) {
503 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
504 }
505
506 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100507 rpc = nc_rpc_getconfig (source, f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200508 nc_filter_free(f);
509 if (rpc == NULL) {
510 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
511 return (NULL);
512 }
513
514 data = netconf_opdata(server, conns, session_key, rpc);
515 nc_rpc_free (rpc);
516 return (data);
517}
518
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100519static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
520{
521 nc_rpc* rpc;
522 char* data = NULL;
523
524 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100525 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100526 if (rpc == NULL) {
527 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
528 return (NULL);
529 }
530
531 data = netconf_opdata(server, conns, session_key, rpc);
532 nc_rpc_free (rpc);
533 return (data);
534}
535
Radek Krejci80c10d92012-07-30 08:38:50 +0200536static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200537{
538 nc_rpc* rpc;
539 struct nc_filter *f = NULL;
540 char* data = NULL;
541
542 /* create filter if set */
543 if (filter != NULL) {
544 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
545 }
546
547 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100548 rpc = nc_rpc_get (f);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200549 nc_filter_free(f);
550 if (rpc == NULL) {
551 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
552 return (NULL);
553 }
554
555 data = netconf_opdata(server, conns, session_key, rpc);
556 nc_rpc_free (rpc);
557 return (data);
558}
559
Tomas Cejka5064c232013-01-17 09:30:58 +0100560static 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 +0200561{
562 nc_rpc* rpc;
563 int retval = EXIT_SUCCESS;
564
565 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100566 if ((source == NC_DATASTORE_CONFIG) || (source == NC_DATASTORE_URL)) {
567 if (target == NC_DATASTORE_URL) {
568 rpc = nc_rpc_copyconfig(source, target, config, url);
569 } else {
570 rpc = nc_rpc_copyconfig(source, target, config);
571 }
572 } else {
573 if (target == NC_DATASTORE_URL) {
574 rpc = nc_rpc_copyconfig(source, target, url);
575 } else {
576 rpc = nc_rpc_copyconfig(source, target);
577 }
578 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200579 if (rpc == NULL) {
580 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
581 return (EXIT_FAILURE);
582 }
583
584 retval = netconf_op(server, conns, session_key, rpc);
585 nc_rpc_free (rpc);
586 return (retval);
587}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200588
Tomas Cejka5064c232013-01-17 09:30:58 +0100589static 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 +0200590{
591 nc_rpc* rpc;
592 int retval = EXIT_SUCCESS;
593
594 /* create requests */
Tomas Cejka5064c232013-01-17 09:30:58 +0100595 /* TODO source NC_DATASTORE_CONFIG / NC_DATASTORE_URL */
596 rpc = nc_rpc_editconfig(target, NC_DATASTORE_CONFIG, defop, erropt, testopt, config);
Radek Krejci62ab34b2012-07-26 13:42:05 +0200597 if (rpc == NULL) {
598 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
599 return (EXIT_FAILURE);
600 }
601
602 retval = netconf_op(server, conns, session_key, rpc);
603 nc_rpc_free (rpc);
604 return (retval);
605}
606
Radek Krejci80c10d92012-07-30 08:38:50 +0200607static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200608{
609 nc_rpc* rpc;
610 int retval = EXIT_SUCCESS;
611
612 /* create requests */
613 rpc = nc_rpc_killsession(sid);
614 if (rpc == NULL) {
615 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
616 return (EXIT_FAILURE);
617 }
618
619 retval = netconf_op(server, conns, session_key, rpc);
620 nc_rpc_free (rpc);
621 return (retval);
622}
623
Radek Krejci80c10d92012-07-30 08:38:50 +0200624static 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 +0200625{
626 nc_rpc* rpc;
627 int retval = EXIT_SUCCESS;
628
629 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200630 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200631 if (rpc == NULL) {
632 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
633 return (EXIT_FAILURE);
634 }
635
636 retval = netconf_op(server, conns, session_key, rpc);
637 nc_rpc_free (rpc);
638 return (retval);
639}
640
Radek Krejci80c10d92012-07-30 08:38:50 +0200641static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200642{
643 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
644}
645
Radek Krejci80c10d92012-07-30 08:38:50 +0200646static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200647{
648 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
649}
650
Radek Krejci80c10d92012-07-30 08:38:50 +0200651static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200652{
653 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
654}
655
Radek Krejci80c10d92012-07-30 08:38:50 +0200656/**
657 * @return REPLY_OK: 0, *data == NULL
658 * REPLY_DATA: 0, *data != NULL
659 * REPLY_ERROR: 1, *data == NULL
660 */
661static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
662{
663 struct nc_session *session = NULL;
664 nc_reply* reply;
665 nc_rpc* rpc;
666 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100667 NC_MSG_TYPE msgt;
668 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200669
670 /* create requests */
671 rpc = nc_rpc_generic(content);
672 if (rpc == NULL) {
673 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
674 return (EXIT_FAILURE);
675 }
676
Radek Krejcia332b692012-11-12 16:15:54 +0100677 if (data != NULL) {
678 *data = NULL;
679 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200680
681 /* get session where send the RPC */
682 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
683 if (session != NULL) {
684 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100685 msgt = nc_session_send_recv(session, rpc, &reply);
686 nc_rpc_free (rpc);
687
688 /* process the result of the operation */
689 switch (msgt) {
690 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200691 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
692 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
693 netconf_close(server, conns, session_key);
694 return (EXIT_FAILURE);
695 }
Radek Krejcia332b692012-11-12 16:15:54 +0100696 /* no break */
697 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200698 /* there is error handled by callback */
699 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200700 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100701 case NC_MSG_REPLY:
702 switch (replyt = nc_reply_get_type(reply)) {
703 case NC_REPLY_DATA:
704 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
705 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
706 nc_reply_free(reply);
707 return (EXIT_FAILURE);
708 }
709 retval = EXIT_SUCCESS;
710 break;
711 case NC_REPLY_OK:
712 retval = EXIT_SUCCESS;
713 break;
714 default:
715 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
716 retval = EXIT_FAILURE;
717 break;
718 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200719 break;
720 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100721 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200722 retval = EXIT_FAILURE;
723 break;
724 }
725 nc_reply_free(reply);
726
727 return (retval);
728 } else {
729 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
730 return (EXIT_FAILURE);
731 }
732}
733
Radek Krejci469aab82012-07-22 18:42:20 +0200734server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200735void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200736{
Radek Krejci7338bde2012-08-10 12:57:30 +0200737 switch (level) {
738 case NC_VERB_ERROR:
739 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
740 break;
741 case NC_VERB_WARNING:
742 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
743 break;
744 case NC_VERB_VERBOSE:
745 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
746 break;
747 case NC_VERB_DEBUG:
748 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
749 break;
750 }
Radek Krejci469aab82012-07-22 18:42:20 +0200751}
752
David Kupka8e60a372012-09-04 09:15:20 +0200753void * thread_routine (void * arg)
754{
755 void * retval = NULL;
756
757 ssize_t buffer_len;
758 struct pollfd fds;
759 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200760 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200761 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200762 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200763 char* session_key, *data;
764 const char *host, *port, *user, *pass;
765 const char *msgtext, *cpbltstr;
766 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100767 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200768 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200769 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200770 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200771 NC_DATASTORE ds_type_s, ds_type_t;
Tomas Cejka5064c232013-01-17 09:30:58 +0100772 NC_EDIT_DEFOP_TYPE defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +0200773 NC_EDIT_ERROPT_TYPE erropt_type = 0;
774
775 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
776 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
777 server_rec * server = ((struct pass_to_thread*)arg)->server;
778 int client = ((struct pass_to_thread*)arg)->client;
779
780 char * buffer, chunk_len_str[12], *chunked_msg;
781 char c;
782
783 while (!isterminated) {
784 fds.fd = client;
785 fds.events = POLLIN;
786 fds.revents = 0;
787
788 status = poll(&fds, 1, 1000);
789
790 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
791 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
792 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
793 continue;
794 } else if (status < 0) {
795 /* 0: poll time outed
796 * close socket and ignore this request from the client, it can try it again
797 * -1: poll failed
798 * something wrong happend, close this socket and wait for another request
799 */
800 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
801 close(client);
802 break;
803 }
804 /* status > 0 */
805
806 /* check the status of the socket */
807
808 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
809 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
810 /* close client's socket (it's probably already closed by client */
811 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
812 close(client);
813 break;
814 }
815
816 /* read json in chunked framing */
817 buffer_size = 0;
818 buffer_len = 0;
819 buffer = NULL;
820 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200821 /* read chunk length */
822 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
823 free (buffer);
824 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200825 break;
826 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200827 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
828 free (buffer);
829 buffer = NULL;
830 break;
831 }
832 i=0;
833 memset (chunk_len_str, 0, 12);
834 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
835 if (i==0 && c == '#') {
836 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
837 /* end but invalid */
838 free (buffer);
839 buffer = NULL;
840 }
841 /* end of message, double-loop break */
842 goto msg_complete;
843 }
844 chunk_len_str[i++] = c;
845 }
846 if (c != '\n') {
847 free (buffer);
848 buffer = NULL;
849 break;
850 }
851 if ((chunk_len = atoi (chunk_len_str)) == 0) {
852 free (buffer);
853 buffer = NULL;
854 break;
855 }
856 buffer_size += chunk_len+1;
857 buffer = realloc (buffer, sizeof(char)*buffer_size);
858 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
859 free (buffer);
860 buffer = NULL;
861 break;
862 }
863 buffer_len += ret;
864 }
865msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200866
867 if (buffer != NULL) {
868 request = json_tokener_parse(buffer);
869 operation = json_object_get_int(json_object_object_get(request, "type"));
870
871 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
872 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
873 if (operation != MSG_CONNECT && session_key == NULL) {
874 reply = json_object_new_object();
875 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
876 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
877 msgtext = json_object_to_json_string(reply);
878 send(client, msgtext, strlen(msgtext) + 1, 0);
879 json_object_put(reply);
880 /* there is some stupid client, so close the connection to give a chance to some other client */
881 close(client);
882 break;
883 }
884
885 /* get parameters */
Tomas Cejka5064c232013-01-17 09:30:58 +0100886 /* TODO NC_DATASTORE_URL */
David Kupka8e60a372012-09-04 09:15:20 +0200887 ds_type_t = -1;
888 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
889 if (strcmp(target, "running") == 0) {
890 ds_type_t = NC_DATASTORE_RUNNING;
891 } else if (strcmp(target, "startup") == 0) {
892 ds_type_t = NC_DATASTORE_STARTUP;
893 } else if (strcmp(target, "candidate") == 0) {
894 ds_type_t = NC_DATASTORE_CANDIDATE;
895 }
896 }
897 ds_type_s = -1;
898 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
899 if (strcmp(source, "running") == 0) {
900 ds_type_s = NC_DATASTORE_RUNNING;
901 } else if (strcmp(source, "startup") == 0) {
902 ds_type_s = NC_DATASTORE_STARTUP;
903 } else if (strcmp(source, "candidate") == 0) {
904 ds_type_s = NC_DATASTORE_CANDIDATE;
905 }
906 }
907
908 /* null global JSON error-reply */
909 err_reply = NULL;
910
911 /* prepare reply envelope */
912 reply = json_object_new_object();
913
914 /* process required operation */
915 switch (operation) {
916 case MSG_CONNECT:
917 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
918
919 host = json_object_get_string(json_object_object_get(request, "host"));
920 port = json_object_get_string(json_object_object_get(request, "port"));
921 user = json_object_get_string(json_object_object_get(request, "user"));
922 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200923 capabilities = json_object_object_get(request, "capabilities");
924 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200925 cpblts = nc_cpblts_new (NULL);
926 for (i=0; i<len; i++) {
927 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
928 }
929 } else {
930 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
931 }
David Kupka8e60a372012-09-04 09:15:20 +0200932 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
933 if ((host == NULL) || (user == NULL)) {
934 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
935 session_key = NULL;
936 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200937 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
938 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200939 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
940 }
941
942 reply = json_object_new_object();
943 if (session_key == NULL) {
944 /* negative reply */
945 if (err_reply == NULL) {
946 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
947 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100948 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200949 } else {
950 /* use filled err_reply from libnetconf's callback */
951 json_object_put(reply);
952 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100953 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200954 }
955 } else {
956 /* positive reply */
957 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
958 json_object_object_add(reply, "session", json_object_new_string(session_key));
959
960 free(session_key);
961 }
962
963 break;
964 case MSG_GET:
965 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
966
967 filter = json_object_get_string(json_object_object_get(request, "filter"));
968
969 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
970
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200971 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200972 if (err_reply == NULL) {
973 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
974 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
975 } else {
976 /* use filled err_reply from libnetconf's callback */
977 json_object_put(reply);
978 reply = err_reply;
979 }
980 } else {
981 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
982 json_object_object_add(reply, "data", json_object_new_string(data));
983 }
984 break;
985 case MSG_GETCONFIG:
986 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
987
988 filter = json_object_get_string(json_object_object_get(request, "filter"));
989
990 if (ds_type_s == -1) {
991 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
992 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
993 break;
994 }
995
996 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
997 if (err_reply == NULL) {
998 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
999 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1000 } else {
1001 /* use filled err_reply from libnetconf's callback */
1002 json_object_put(reply);
1003 reply = err_reply;
1004 }
1005 } else {
1006 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1007 json_object_object_add(reply, "data", json_object_new_string(data));
1008 }
1009 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001010 case MSG_GETSCHEMA:
1011 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
1012 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
1013 if (identifier == NULL) {
1014 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1015 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
1016 break;
1017 }
Tomas Cejka94da2c52013-01-08 18:20:30 +01001018 version = json_object_get_string(json_object_object_get(request, "version"));
1019 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001020
Tomas Cejka94da2c52013-01-08 18:20:30 +01001021 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001022 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001023 if (err_reply == NULL) {
1024 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1025 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1026 } else {
1027 /* use filled err_reply from libnetconf's callback */
1028 json_object_put(reply);
1029 reply = err_reply;
1030 }
1031 } else {
1032 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1033 json_object_object_add(reply, "data", json_object_new_string(data));
1034 }
1035 break;
David Kupka8e60a372012-09-04 09:15:20 +02001036 case MSG_EDITCONFIG:
1037 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1038
1039 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1040 if (defop != NULL) {
1041 if (strcmp(defop, "merge") == 0) {
1042 defop_type = NC_EDIT_DEFOP_MERGE;
1043 } else if (strcmp(defop, "replace") == 0) {
1044 defop_type = NC_EDIT_DEFOP_REPLACE;
1045 } else if (strcmp(defop, "none") == 0) {
1046 defop_type = NC_EDIT_DEFOP_NONE;
1047 } else {
1048 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1049 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1050 break;
1051 }
1052 } else {
Tomas Cejka5064c232013-01-17 09:30:58 +01001053 defop_type = NC_EDIT_DEFOP_NOTSET;
David Kupka8e60a372012-09-04 09:15:20 +02001054 }
1055
1056 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1057 if (erropt != NULL) {
1058 if (strcmp(erropt, "continue-on-error") == 0) {
1059 erropt_type = NC_EDIT_ERROPT_CONT;
1060 } else if (strcmp(erropt, "stop-on-error") == 0) {
1061 erropt_type = NC_EDIT_ERROPT_STOP;
1062 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1063 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1064 } else {
1065 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1066 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1067 break;
1068 }
1069 } else {
1070 erropt_type = 0;
1071 }
1072
1073 if (ds_type_t == -1) {
1074 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1075 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1076 break;
1077 }
1078
1079 config = json_object_get_string(json_object_object_get(request, "config"));
1080 if (config == NULL) {
1081 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1082 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1083 break;
1084 }
1085
Tomas Cejka5064c232013-01-17 09:30:58 +01001086 /* TODO url capability see netconf_editconfig */
1087 /* TODO TESTSET - :validate:1.1 capability? http://tools.ietf.org/html/rfc6241#section-7.2 */
1088 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 +02001089 if (err_reply == NULL) {
1090 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1091 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1092 } else {
1093 /* use filled err_reply from libnetconf's callback */
1094 json_object_put(reply);
1095 reply = err_reply;
1096 }
1097 } else {
1098 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1099 }
1100 break;
1101 case MSG_COPYCONFIG:
1102 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1103 config = NULL;
1104
1105 if (source == NULL) {
1106 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001107 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001108 config = json_object_get_string(json_object_object_get(request, "config"));
1109 } else if (ds_type_s == -1) {
1110 /* source datastore specified, but it is invalid */
1111 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1112 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1113 break;
1114 }
1115
1116 if (ds_type_t == -1) {
1117 /* invalid target datastore specified */
1118 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1119 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1120 break;
1121 }
1122
1123 if (source == NULL && config == NULL) {
1124 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1125 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1126 } else {
Tomas Cejka4ce5d0a2013-01-17 19:23:54 +01001127 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 +02001128 if (err_reply == NULL) {
1129 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1130 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1131 } else {
1132 /* use filled err_reply from libnetconf's callback */
1133 json_object_put(reply);
1134 reply = err_reply;
1135 }
1136 } else {
1137 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1138 }
1139 }
1140 break;
1141 case MSG_DELETECONFIG:
1142 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1143 /* no break - unifying code */
1144 case MSG_LOCK:
1145 if (operation == MSG_LOCK) {
1146 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1147 }
1148 /* no break - unifying code */
1149 case MSG_UNLOCK:
1150 if (operation == MSG_UNLOCK) {
1151 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1152 }
1153
1154 if (ds_type_t == -1) {
1155 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1156 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1157 break;
1158 }
1159
1160 switch(operation) {
1161 case MSG_DELETECONFIG:
1162 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1163 break;
1164 case MSG_LOCK:
1165 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1166 break;
1167 case MSG_UNLOCK:
1168 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1169 break;
1170 default:
1171 status = -1;
1172 break;
1173 }
1174
1175 if (status != EXIT_SUCCESS) {
1176 if (err_reply == NULL) {
1177 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1178 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1179 } else {
1180 /* use filled err_reply from libnetconf's callback */
1181 json_object_put(reply);
1182 reply = err_reply;
1183 }
1184 } else {
1185 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1186 }
1187 break;
1188 case MSG_KILL:
1189 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1190
1191 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1192
1193 if (sid == NULL) {
1194 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1195 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1196 break;
1197 }
1198
1199 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1200 if (err_reply == NULL) {
1201 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1202 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1203 } else {
1204 /* use filled err_reply from libnetconf's callback */
1205 json_object_put(reply);
1206 reply = err_reply;
1207 }
1208 } else {
1209 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1210 }
1211 break;
1212 case MSG_DISCONNECT:
1213 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1214
1215 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1216 if (err_reply == NULL) {
1217 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1218 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1219 } else {
1220 /* use filled err_reply from libnetconf's callback */
1221 json_object_put(reply);
1222 reply = err_reply;
1223 }
1224 } else {
1225 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1226 }
1227 break;
1228 case MSG_INFO:
1229 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1230
Kupka Davidda134a12012-09-06 14:12:16 +02001231 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1232 if (locked_session != NULL) {
1233 session = locked_session->session;
1234 } else {
1235 session = NULL;
1236 }
David Kupka8e60a372012-09-04 09:15:20 +02001237 if (session != NULL) {
1238 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1239 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1240 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1241 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1242 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1243 cpblts = nc_session_get_cpblts (session);
1244 if (cpblts != NULL) {
1245 json_obj = json_object_new_array();
1246 nc_cpblts_iter_start (cpblts);
1247 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1248 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1249 }
1250 json_object_object_add(reply, "capabilities", json_obj);
1251 }
1252 } else {
1253 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1254 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1255 }
1256
1257 break;
1258 case MSG_GENERIC:
1259 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1260
1261 config = json_object_get_string(json_object_object_get(request, "content"));
1262
1263 if (config == NULL) {
1264 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1265 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1266 break;
1267 }
1268
1269 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1270 if (err_reply == NULL) {
1271 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1272 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1273 } else {
1274 /* use filled err_reply from libnetconf's callback */
1275 json_object_put(reply);
1276 reply = err_reply;
1277 }
1278 } else {
1279 if (data == NULL) {
1280 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1281 } else {
1282 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1283 json_object_object_add(reply, "data", json_object_new_string(data));
1284 }
1285 }
1286 break;
1287 default:
1288 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1289 reply = json_object_new_object();
1290 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1291 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1292 break;
1293 }
1294 json_object_put(request);
1295
David Kupka1e3e4c82012-09-04 09:32:15 +02001296 /* send reply to caller */
1297 if (reply != NULL) {
1298 msgtext = json_object_to_json_string(reply);
1299 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1300 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001301 break;
1302 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001303 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1304 json_object_put(reply);
1305 free (chunked_msg);
1306 free (buffer);
1307 } else {
1308 break;
David Kupka8e60a372012-09-04 09:15:20 +02001309 }
1310 }
1311 }
1312
1313 free (arg);
1314
1315 return retval;
1316}
1317
Radek Krejcif23850c2012-07-23 16:14:17 +02001318/*
1319 * This is actually implementation of NETCONF client
1320 * - requests are received from UNIX socket in the predefined format
1321 * - results are replied through the same way
1322 * - the daemon run as a separate process, but it is started and stopped
1323 * automatically by Apache.
1324 *
1325 */
Radek Krejci469aab82012-07-22 18:42:20 +02001326static void forked_proc(apr_pool_t * pool, server_rec * server)
1327{
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001328 struct timeval tv;
Radek Krejci469aab82012-07-22 18:42:20 +02001329 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001330 int lsock, client, ret, i, pthread_count = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001331 char use_notifications = 0;
1332 unsigned int olds = 0;
David Kupka8e60a372012-09-04 09:15:20 +02001333 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001334 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001335 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001336 struct pass_to_thread * arg;
1337 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1338 struct timespec maxtime;
1339 pthread_rwlockattr_t lock_attrs;
1340
1341 /* wait at most 5 secons for every thread to terminate */
1342 maxtime.tv_sec = 5;
1343 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001344
Radek Krejcif23850c2012-07-23 16:14:17 +02001345 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001346 unixd_setup_child();
1347
Radek Krejciae021c12012-07-25 18:03:52 +02001348 cfg = ap_get_module_config(server->module_config, &netconf_module);
1349 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001350 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1351 return;
1352 }
Radek Krejci469aab82012-07-22 18:42:20 +02001353
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001354 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001355 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1356 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1357 return;
1358 }
1359
1360 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001361 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001362 unlink(local.sun_path);
1363 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1364
1365 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1366 if (errno == EADDRINUSE) {
1367 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1368 close(lsock);
1369 exit(0);
1370 }
1371 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1372 close(lsock);
1373 return;
1374 }
1375
1376 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1377 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1378 close(lsock);
1379 return;
1380 }
1381
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001382 #ifdef WITH_NOTIFICATIONS
1383 if (notification_init(pool, server) == -1) {
1384 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "libwebsockets initialization failed");
1385 use_notifications = 0;
1386 } else {
1387 use_notifications = 1;
1388 }
1389 #endif
1390
Radek Krejci469aab82012-07-22 18:42:20 +02001391 /* prepare internal lists */
1392 netconf_sessions_list = apr_hash_make(pool);
1393
1394 /* setup libnetconf's callbacks */
1395 nc_verbosity(NC_VERB_DEBUG);
1396 clb_print_server = server;
1397 nc_callback_print(clb_print);
1398 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1399 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1400 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001401 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001402
1403 /* disable publickey authentication */
1404 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1405
David Kupka8e60a372012-09-04 09:15:20 +02001406 /* create mutex protecting session list */
1407 pthread_rwlockattr_init(&lock_attrs);
1408 /* rwlock is shared only with threads in this process */
1409 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1410 /* create rw lock */
1411 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1412 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1413 close (lsock);
1414 return;
1415 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001416
1417 fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001418 while (isterminated == 0) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001419 gettimeofday(&tv, NULL);
1420 #ifdef WITH_NOTIFICATIONS
1421 if (((unsigned int)tv.tv_sec - olds) > 60) {
1422 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "handling notifications");
1423 }
1424 if (use_notifications == 1) {
1425 notification_handle();
1426 }
1427 #endif
Radek Krejci469aab82012-07-22 18:42:20 +02001428
1429 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001430 len = sizeof(remote);
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001431 if (((unsigned int)tv.tv_sec - olds) > 60) {
1432 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "accepting another client");
1433 olds = tv.tv_sec;
1434 }
David Kupka8e60a372012-09-04 09:15:20 +02001435 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001436 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1437 apr_sleep(SLEEP_TIME);
1438 continue;
1439 } else if (client == -1 && (errno == EINTR)) {
1440 continue;
1441 } else if (client == -1) {
1442 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1443 continue;
1444 }
Radek Krejci469aab82012-07-22 18:42:20 +02001445
1446 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001447 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001448
David Kupka8e60a372012-09-04 09:15:20 +02001449 arg = malloc (sizeof(struct pass_to_thread));
1450 arg->client = client;
1451 arg->pool = pool;
1452 arg->server = server;
1453 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001454
David Kupka8e60a372012-09-04 09:15:20 +02001455 /* start new thread. It will serve this particular request and then terminate */
1456 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1457 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1458 } else {
1459 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1460 pthread_count++;
1461 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1462 ptids[pthread_count] = 0;
1463 }
Radek Krejci469aab82012-07-22 18:42:20 +02001464
David Kupka8e60a372012-09-04 09:15:20 +02001465 /* check if some thread already terminated, free some resources by joining it */
1466 for (i=0; i<pthread_count; i++) {
1467 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1468 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1469 pthread_count--;
1470 if (pthread_count > 0) {
1471 /* place last Thread ID on the place of joined one */
1472 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001473 }
Radek Krejci469aab82012-07-22 18:42:20 +02001474 }
1475 }
David Kupka8e60a372012-09-04 09:15:20 +02001476 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001477 }
1478
David Kupka8e60a372012-09-04 09:15:20 +02001479 /* join all threads */
1480 for (i=0; i<pthread_count; i++) {
1481 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1482 }
1483 free (ptids);
1484
Radek Krejci469aab82012-07-22 18:42:20 +02001485 close(lsock);
1486
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001487 #ifdef WITH_NOTIFICATIONS
1488 notification_close();
1489 #endif
1490
David Kupka8e60a372012-09-04 09:15:20 +02001491 /* destroy rwlock */
1492 pthread_rwlock_destroy(&session_lock);
1493 pthread_rwlockattr_destroy(&lock_attrs);
1494
Radek Krejci469aab82012-07-22 18:42:20 +02001495 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1496
1497 exit(APR_SUCCESS);
1498}
1499
Radek Krejcif23850c2012-07-23 16:14:17 +02001500static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001501{
Radek Krejcif23850c2012-07-23 16:14:17 +02001502 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1503
1504 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1505 apr_pool_create(&config->pool, pool);
1506 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001507 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001508
1509 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001510}
1511
1512static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1513 apr_pool_t * plog, server_rec * s)
1514{
Radek Krejcif23850c2012-07-23 16:14:17 +02001515 mod_netconf_cfg *config;
1516 apr_status_t res;
1517
Radek Krejci469aab82012-07-22 18:42:20 +02001518 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001519 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001520 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001521
1522 /*
1523 * The following checks if this routine has been called before.
1524 * This is necessary because the parent process gets initialized
1525 * a couple of times as the server starts up.
1526 */
1527 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1528 if (!data) {
1529 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1530 return (OK);
1531 }
1532
Radek Krejcif23850c2012-07-23 16:14:17 +02001533 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1534 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001535
Radek Krejcif23850c2012-07-23 16:14:17 +02001536 if (config && config->forkproc == NULL) {
1537 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1538 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001539 switch (res) {
1540 case APR_INCHILD:
1541 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001542 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001543 apr_signal(SIGTERM, signal_handler);
1544
1545 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001546 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001547
1548 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001549 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001550
Radek Krejcif23850c2012-07-23 16:14:17 +02001551 /* I never should be here, wtf?!? */
1552 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001553 exit(APR_EGENERAL);
1554 break;
1555 case APR_INPARENT:
1556 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001557 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001558 break;
1559 default:
1560 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1561 break;
1562 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001563 } else {
1564 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001565 }
1566
1567 return OK;
1568}
1569
Radek Krejci469aab82012-07-22 18:42:20 +02001570/**
1571 * Register module hooks
1572 */
1573static void mod_netconf_register_hooks(apr_pool_t * p)
1574{
Radek Krejcif23850c2012-07-23 16:14:17 +02001575 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001576}
1577
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001578static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1579{
1580 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1581 return NULL;
1582}
1583
1584static const command_rec netconf_cmds[] = {
1585 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1586 {NULL}
1587};
1588
Radek Krejci469aab82012-07-22 18:42:20 +02001589/* Dispatch list for API hooks */
1590module AP_MODULE_DECLARE_DATA netconf_module = {
1591 STANDARD20_MODULE_STUFF,
1592 NULL, /* create per-dir config structures */
1593 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001594 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001595 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001596 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001597 mod_netconf_register_hooks /* register hooks */
1598};
Radek Krejcia332b692012-11-12 16:15:54 +01001599