blob: f3cedb0f4bb254d9a9e964c18fbd18c023cf7cf3 [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
8 */
9/*
10 * Copyright (C) 2011-2012 CESNET
11 *
12 * LICENSE TERMS
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * 3. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * ALTERNATIVELY, provided that this notice is retained in full, this
28 * product may be distributed under the terms of the GNU General Public
29 * License (GPL) version 2 or later, in which case the provisions
30 * of the GPL apply INSTEAD OF those given above.
31 *
32 * This software is provided ``as is'', and any express or implied
33 * warranties, including, but not limited to, the implied warranties of
34 * merchantability and fitness for a particular purpose are disclaimed.
35 * In no event shall the company or contributors be liable for any
36 * direct, indirect, incidental, special, exemplary, or consequential
37 * damages (including, but not limited to, procurement of substitute
38 * goods or services; loss of use, data, or profits; or business
39 * interruption) however caused and on any theory of liability, whether
40 * in contract, strict liability, or tort (including negligence or
41 * otherwise) arising in any way out of the use of this software, even
42 * if advised of the possibility of such damage.
43 *
44 */
45
Radek Krejci7b4ddd02012-07-30 08:09:58 +020046#include <unistd.h>
47#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020048#include <sys/types.h>
49#include <sys/socket.h>
50#include <sys/un.h>
David Kupka8e60a372012-09-04 09:15:20 +020051#include <pthread.h>
52#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020053
54#include <unixd.h>
55#include <httpd.h>
56#include <http_log.h>
57#include <http_config.h>
58
59#include <apr_sha1.h>
60#include <apr_hash.h>
61#include <apr_signal.h>
62#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020063
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020064#include <json/json.h>
65
Radek Krejci469aab82012-07-22 18:42:20 +020066#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020067
Radek Krejci469aab82012-07-22 18:42:20 +020068
69#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020070#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020071#define MAX_SOCKET_CL 10
72#define BUFFER_SIZE 4096
73
74/* sleep in master process for non-blocking socket reading */
75#define SLEEP_TIME 200
76
77#ifndef offsetof
78#define offsetof(type, member) ((size_t) ((type *) 0)->member)
79#endif
80
Tomas Cejka027f3bc2012-11-10 20:28:36 +010081/* timeout in msec */
Tomas Cejka027f3bc2012-11-10 20:28:36 +010082#define NCWITHDEFAULTS NCWD_MODE_DISABLED
83
Radek Krejci469aab82012-07-22 18:42:20 +020084struct timeval timeout = { 1, 0 };
85
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020086typedef enum MSG_TYPE {
87 REPLY_OK,
88 REPLY_DATA,
89 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020090 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020091 MSG_CONNECT,
92 MSG_DISCONNECT,
93 MSG_GET,
94 MSG_GETCONFIG,
95 MSG_EDITCONFIG,
96 MSG_COPYCONFIG,
97 MSG_DELETECONFIG,
98 MSG_LOCK,
99 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200100 MSG_KILL,
Radek Krejci80c10d92012-07-30 08:38:50 +0200101 MSG_INFO,
102 MSG_GENERIC
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200103} MSG_TYPE;
104
Radek Krejci469aab82012-07-22 18:42:20 +0200105#define MSG_OK 0
106#define MSG_OPEN 1
107#define MSG_DATA 2
108#define MSG_CLOSE 3
109#define MSG_ERROR 4
110#define MSG_UNKNOWN 5
111
Radek Krejci469aab82012-07-22 18:42:20 +0200112module AP_MODULE_DECLARE_DATA netconf_module;
113
114typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200115 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200116 apr_proc_t *forkproc;
117 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200118} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200119
David Kupka8e60a372012-09-04 09:15:20 +0200120struct pass_to_thread {
121 int client; /**< opened socket */
122 apr_pool_t * pool; /**< ?? */
123 server_rec * server; /**< ?? */
124 apr_hash_t * netconf_sessions_list; /**< ?? */
125};
126
127struct session_with_mutex {
128 struct nc_session * session; /**< netconf session */
129 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
130};
131
132pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
133
Radek Krejci469aab82012-07-22 18:42:20 +0200134volatile int isterminated = 0;
135
136static char* password;
137
138
139static void signal_handler(int sign)
140{
141 switch (sign) {
142 case SIGTERM:
143 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200144 break;
145 }
146}
147
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200148static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200149{
Radek Krejcif23850c2012-07-23 16:14:17 +0200150 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
151 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200152 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200153
Radek Krejci469aab82012-07-22 18:42:20 +0200154 apr_sha1_ctx_t sha1_ctx;
155 apr_sha1_init(&sha1_ctx);
156 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
157 apr_sha1_update(&sha1_ctx, port, strlen(port));
158 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200159 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200160
Radek Krejcif23850c2012-07-23 16:14:17 +0200161 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200162 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200163 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200164 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200165 }
166 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200167
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200168 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200169}
170
171int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
172{
173 /* always approve */
174 return (EXIT_SUCCESS);
175}
176
177char* netconf_callback_sshauth_password (const char* username, const char* hostname)
178{
179 char* buf;
180
181 buf = malloc ((strlen(password) + 1) * sizeof(char));
182 apr_cpystrn(buf, password, strlen(password) + 1);
183
184 return (buf);
185}
186
187void netconf_callback_sshauth_interactive (const char* name,
188 int name_len,
189 const char* instruction,
190 int instruction_len,
191 int num_prompts,
192 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
193 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
194 void** abstract)
195{
196 int i;
197
198 for (i=0; i<num_prompts; i++) {
199 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
200 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
201 responses[i].length = strlen(responses[i].text) + 1;
202 }
203
204 return;
205}
206
Radek Krejcic11fd862012-07-26 12:41:21 +0200207static json_object *err_reply = NULL;
208void netconf_callback_error_process(const char* tag,
209 const char* type,
210 const char* severity,
211 const char* apptag,
212 const char* path,
213 const char* message,
214 const char* attribute,
215 const char* element,
216 const char* ns,
217 const char* sid)
218{
219 err_reply = json_object_new_object();
220 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
221 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
222 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
223 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
224 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
225 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
226 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
227 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
228 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
229 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
230 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
231}
232
Kupka David00b9c5c2012-09-05 09:45:50 +0200233static 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 +0200234{
Radek Krejci469aab82012-07-22 18:42:20 +0200235 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200236 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200237 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200238
239 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200240 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200241 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
242
Radek Krejci469aab82012-07-22 18:42:20 +0200243 /* if connected successful, add session to the list */
244 if (session != NULL) {
245 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200246 session_key = gen_ncsession_hash(
247 (host==NULL) ? "localhost" : host,
248 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200249 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200250
251 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
252 nc_session_free(session);
253 free (locked_session);
254 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
255 return NULL;
256 }
257 locked_session->session = session;
258 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100259 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200260 /* get exclusive access to sessions_list (conns) */
261 if (pthread_rwlock_wrlock (&session_lock) != 0) {
262 nc_session_free(session);
263 free (locked_session);
264 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
265 return NULL;
266 }
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100267 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200268 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100269 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200270 /* end of critical section */
271 if (pthread_rwlock_unlock (&session_lock) != 0) {
272 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
273 return NULL;
274 }
Radek Krejci469aab82012-07-22 18:42:20 +0200275 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200276 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200277 } else {
278 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200279 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200280 }
281
Radek Krejci469aab82012-07-22 18:42:20 +0200282}
283
Radek Krejci80c10d92012-07-30 08:38:50 +0200284static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200285{
286 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200287 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200288
Radek Krejcif23850c2012-07-23 16:14:17 +0200289 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200290 /* get exclusive (write) access to sessions_list (conns) */
291 if (pthread_rwlock_wrlock (&session_lock) != 0) {
292 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
293 return EXIT_FAILURE;
294 }
295 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
296 if (locked_session != NULL) {
297 pthread_mutex_destroy(&locked_session->lock);
298 ns = locked_session->session;
299 free (locked_session);
300 }
Radek Krejci469aab82012-07-22 18:42:20 +0200301 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100302 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200303 nc_session_free (ns);
304 ns = NULL;
305
306 /* remove session from the active sessions list */
307 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200308 /* end of critical section */
309 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_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200314
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200315 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200316 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200317 if (pthread_rwlock_unlock (&session_lock) != 0) {
318 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
319 return EXIT_FAILURE;
320 }
Radek Krejci469aab82012-07-22 18:42:20 +0200321 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200322 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200323 }
324}
325
Radek Krejci80c10d92012-07-30 08:38:50 +0200326static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200327{
328 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200329 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200330 nc_reply* reply;
331 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100332 NC_MSG_TYPE msgt;
333 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200334
Radek Krejci8e4632a2012-07-26 13:40:34 +0200335 /* check requests */
336 if (rpc == NULL) {
337 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
338 return (EXIT_FAILURE);
339 }
340
David Kupka8e60a372012-09-04 09:15:20 +0200341 /* get non-exclusive (read) access to sessions_list (conns) */
342 if (pthread_rwlock_rdlock (&session_lock) != 0) {
343 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
344 return EXIT_FAILURE;
345 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200346 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200347 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
348 if (locked_session != NULL) {
349 session = locked_session->session;
350 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200351 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200352 /* get exclusive access to session */
353 if (pthread_mutex_lock(&locked_session->lock) != 0) {
354 /* unlock before returning error */
355 if (pthread_rwlock_unlock (&session_lock) != 0) {
356 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
357 return EXIT_FAILURE;
358 }
359 return EXIT_FAILURE;
360 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200361 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100362 msgt = nc_session_send_recv(session, rpc, &reply);
363
David Kupka8e60a372012-09-04 09:15:20 +0200364 /* first release exclusive lock for this session */
365 pthread_mutex_unlock(&locked_session->lock);
366 /* end of critical section */
367 if (pthread_rwlock_unlock (&session_lock) != 0) {
368 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
369 return EXIT_FAILURE;
370 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200371
Radek Krejcia332b692012-11-12 16:15:54 +0100372 /* process the result of the operation */
373 switch (msgt) {
374 case NC_MSG_UNKNOWN:
375 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
376 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
377 netconf_close(server, conns, session_key);
378 return (EXIT_FAILURE);
379 }
380 /* no break */
381 case NC_MSG_NONE:
382 /* there is error handled by callback */
383 return (EXIT_FAILURE);
384 break;
385 case NC_MSG_REPLY:
386 switch (replyt = nc_reply_get_type(reply)) {
387 case NC_REPLY_OK:
388 retval = EXIT_SUCCESS;
389 break;
390 default:
391 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
392 retval = EXIT_FAILURE;
393 break;
394 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200395 break;
396 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100397 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200398 retval = EXIT_FAILURE;
399 break;
400 }
401 nc_reply_free(reply);
402 return (retval);
403 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100404 /* release lock on failure */
405 if (pthread_rwlock_unlock (&session_lock) != 0) {
406 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
407 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200408 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
409 return (EXIT_FAILURE);
410 }
411}
Radek Krejci80c10d92012-07-30 08:38:50 +0200412
413static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200414{
415 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200416 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100417 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200418 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100419 NC_MSG_TYPE msgt;
420 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200421
422 /* check requests */
423 if (rpc == NULL) {
424 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
425 return (NULL);
426 }
427
David Kupka8e60a372012-09-04 09:15:20 +0200428 /* get non-exclusive (read) access to sessions_list (conns) */
429 if (pthread_rwlock_rdlock (&session_lock) != 0) {
430 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
431 return NULL;
432 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200433 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200434 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
435 if (locked_session != NULL) {
436 session = locked_session->session;
437 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200438 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200439 /* get exclusive access to session */
440 if (pthread_mutex_lock(&locked_session->lock) != 0) {
441 /* unlock before returning error */
442 if (pthread_rwlock_unlock (&session_lock) != 0) {
443 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
444 return NULL;
445 }
446 return NULL;
447 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200448 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100449 msgt = nc_session_send_recv(session, rpc, &reply);
450
David Kupka8e60a372012-09-04 09:15:20 +0200451 /* first release exclusive lock for this session */
452 pthread_mutex_unlock(&locked_session->lock);
453 /* end of critical section */
454 if (pthread_rwlock_unlock (&session_lock) != 0) {
455 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 +0100456 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200457 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200458
Radek Krejcia332b692012-11-12 16:15:54 +0100459 /* process the result of the operation */
460 switch (msgt) {
461 case NC_MSG_UNKNOWN:
462 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
463 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
464 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200465 return (NULL);
466 }
Radek Krejcia332b692012-11-12 16:15:54 +0100467 /* no break */
468 case NC_MSG_NONE:
469 /* there is error handled by callback */
470 return (NULL);
471 break;
472 case NC_MSG_REPLY:
473 switch (replyt = nc_reply_get_type(reply)) {
474 case NC_REPLY_DATA:
475 if ((data = nc_reply_get_data (reply)) == NULL) {
476 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
477 data = NULL;
478 }
479 break;
480 default:
481 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
482 data = NULL;
483 break;
484 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200485 break;
486 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100487 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
488 data = NULL;
489 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490 }
491 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200492 return (data);
493 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100494 /* release lock on failure */
495 if (pthread_rwlock_unlock (&session_lock) != 0) {
496 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
497 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200498 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
499 return (NULL);
500 }
501}
502
Radek Krejci80c10d92012-07-30 08:38:50 +0200503static 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 +0200504{
505 nc_rpc* rpc;
506 struct nc_filter *f = NULL;
507 char* data = NULL;
508
509 /* create filter if set */
510 if (filter != NULL) {
511 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
512 }
513
514 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100515 rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200516 nc_filter_free(f);
517 if (rpc == NULL) {
518 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
519 return (NULL);
520 }
521
522 data = netconf_opdata(server, conns, session_key, rpc);
523 nc_rpc_free (rpc);
524 return (data);
525}
526
Radek Krejci80c10d92012-07-30 08:38:50 +0200527static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200528{
529 nc_rpc* rpc;
530 struct nc_filter *f = NULL;
531 char* data = NULL;
532
533 /* create filter if set */
534 if (filter != NULL) {
535 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
536 }
537
538 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100539 rpc = nc_rpc_get (f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200540 nc_filter_free(f);
541 if (rpc == NULL) {
542 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
543 return (NULL);
544 }
545
546 data = netconf_opdata(server, conns, session_key, rpc);
547 nc_rpc_free (rpc);
548 return (data);
549}
550
Radek Krejci80c10d92012-07-30 08:38:50 +0200551static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200552{
553 nc_rpc* rpc;
554 int retval = EXIT_SUCCESS;
555
556 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100557 rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200558 if (rpc == NULL) {
559 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
560 return (EXIT_FAILURE);
561 }
562
563 retval = netconf_op(server, conns, session_key, rpc);
564 nc_rpc_free (rpc);
565 return (retval);
566}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200567
Radek Krejci80c10d92012-07-30 08:38:50 +0200568static 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, const char* config)
Radek Krejci62ab34b2012-07-26 13:42:05 +0200569{
570 nc_rpc* rpc;
571 int retval = EXIT_SUCCESS;
572
573 /* create requests */
574 rpc = nc_rpc_editconfig(target, defop, erropt, config);
575 if (rpc == NULL) {
576 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
577 return (EXIT_FAILURE);
578 }
579
580 retval = netconf_op(server, conns, session_key, rpc);
581 nc_rpc_free (rpc);
582 return (retval);
583}
584
Radek Krejci80c10d92012-07-30 08:38:50 +0200585static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200586{
587 nc_rpc* rpc;
588 int retval = EXIT_SUCCESS;
589
590 /* create requests */
591 rpc = nc_rpc_killsession(sid);
592 if (rpc == NULL) {
593 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
594 return (EXIT_FAILURE);
595 }
596
597 retval = netconf_op(server, conns, session_key, rpc);
598 nc_rpc_free (rpc);
599 return (retval);
600}
601
Radek Krejci80c10d92012-07-30 08:38:50 +0200602static 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 +0200603{
604 nc_rpc* rpc;
605 int retval = EXIT_SUCCESS;
606
607 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200608 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200609 if (rpc == NULL) {
610 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
611 return (EXIT_FAILURE);
612 }
613
614 retval = netconf_op(server, conns, session_key, rpc);
615 nc_rpc_free (rpc);
616 return (retval);
617}
618
Radek Krejci80c10d92012-07-30 08:38:50 +0200619static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200620{
621 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
622}
623
Radek Krejci80c10d92012-07-30 08:38:50 +0200624static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200625{
626 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
627}
628
Radek Krejci80c10d92012-07-30 08:38:50 +0200629static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200630{
631 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
632}
633
Radek Krejci80c10d92012-07-30 08:38:50 +0200634/**
635 * @return REPLY_OK: 0, *data == NULL
636 * REPLY_DATA: 0, *data != NULL
637 * REPLY_ERROR: 1, *data == NULL
638 */
639static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
640{
641 struct nc_session *session = NULL;
642 nc_reply* reply;
643 nc_rpc* rpc;
644 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100645 NC_MSG_TYPE msgt;
646 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200647
648 /* create requests */
649 rpc = nc_rpc_generic(content);
650 if (rpc == NULL) {
651 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
652 return (EXIT_FAILURE);
653 }
654
Radek Krejcia332b692012-11-12 16:15:54 +0100655 if (data != NULL) {
656 *data = NULL;
657 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200658
659 /* get session where send the RPC */
660 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
661 if (session != NULL) {
662 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100663 msgt = nc_session_send_recv(session, rpc, &reply);
664 nc_rpc_free (rpc);
665
666 /* process the result of the operation */
667 switch (msgt) {
668 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200669 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
670 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
671 netconf_close(server, conns, session_key);
672 return (EXIT_FAILURE);
673 }
Radek Krejcia332b692012-11-12 16:15:54 +0100674 /* no break */
675 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200676 /* there is error handled by callback */
677 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200678 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100679 case NC_MSG_REPLY:
680 switch (replyt = nc_reply_get_type(reply)) {
681 case NC_REPLY_DATA:
682 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
683 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
684 nc_reply_free(reply);
685 return (EXIT_FAILURE);
686 }
687 retval = EXIT_SUCCESS;
688 break;
689 case NC_REPLY_OK:
690 retval = EXIT_SUCCESS;
691 break;
692 default:
693 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
694 retval = EXIT_FAILURE;
695 break;
696 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200697 break;
698 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100699 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200700 retval = EXIT_FAILURE;
701 break;
702 }
703 nc_reply_free(reply);
704
705 return (retval);
706 } else {
707 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
708 return (EXIT_FAILURE);
709 }
710}
711
Radek Krejci469aab82012-07-22 18:42:20 +0200712server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200713void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200714{
Radek Krejci7338bde2012-08-10 12:57:30 +0200715 switch (level) {
716 case NC_VERB_ERROR:
717 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
718 break;
719 case NC_VERB_WARNING:
720 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
721 break;
722 case NC_VERB_VERBOSE:
723 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
724 break;
725 case NC_VERB_DEBUG:
726 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
727 break;
728 }
Radek Krejci469aab82012-07-22 18:42:20 +0200729}
730
David Kupka8e60a372012-09-04 09:15:20 +0200731void * thread_routine (void * arg)
732{
733 void * retval = NULL;
734
735 ssize_t buffer_len;
736 struct pollfd fds;
737 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200738 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200739 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200740 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200741 char* session_key, *data;
742 const char *host, *port, *user, *pass;
743 const char *msgtext, *cpbltstr;
744 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
745 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200746 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200747 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200748 NC_DATASTORE ds_type_s, ds_type_t;
749 NC_EDIT_DEFOP_TYPE defop_type = 0;
750 NC_EDIT_ERROPT_TYPE erropt_type = 0;
751
752 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
753 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
754 server_rec * server = ((struct pass_to_thread*)arg)->server;
755 int client = ((struct pass_to_thread*)arg)->client;
756
757 char * buffer, chunk_len_str[12], *chunked_msg;
758 char c;
759
760 while (!isterminated) {
761 fds.fd = client;
762 fds.events = POLLIN;
763 fds.revents = 0;
764
765 status = poll(&fds, 1, 1000);
766
767 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
768 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
769 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
770 continue;
771 } else if (status < 0) {
772 /* 0: poll time outed
773 * close socket and ignore this request from the client, it can try it again
774 * -1: poll failed
775 * something wrong happend, close this socket and wait for another request
776 */
777 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
778 close(client);
779 break;
780 }
781 /* status > 0 */
782
783 /* check the status of the socket */
784
785 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
786 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
787 /* close client's socket (it's probably already closed by client */
788 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
789 close(client);
790 break;
791 }
792
793 /* read json in chunked framing */
794 buffer_size = 0;
795 buffer_len = 0;
796 buffer = NULL;
797 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200798 /* read chunk length */
799 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
800 free (buffer);
801 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200802 break;
803 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200804 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
805 free (buffer);
806 buffer = NULL;
807 break;
808 }
809 i=0;
810 memset (chunk_len_str, 0, 12);
811 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
812 if (i==0 && c == '#') {
813 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
814 /* end but invalid */
815 free (buffer);
816 buffer = NULL;
817 }
818 /* end of message, double-loop break */
819 goto msg_complete;
820 }
821 chunk_len_str[i++] = c;
822 }
823 if (c != '\n') {
824 free (buffer);
825 buffer = NULL;
826 break;
827 }
828 if ((chunk_len = atoi (chunk_len_str)) == 0) {
829 free (buffer);
830 buffer = NULL;
831 break;
832 }
833 buffer_size += chunk_len+1;
834 buffer = realloc (buffer, sizeof(char)*buffer_size);
835 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
836 free (buffer);
837 buffer = NULL;
838 break;
839 }
840 buffer_len += ret;
841 }
842msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200843
844 if (buffer != NULL) {
845 request = json_tokener_parse(buffer);
846 operation = json_object_get_int(json_object_object_get(request, "type"));
847
848 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
849 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
850 if (operation != MSG_CONNECT && session_key == NULL) {
851 reply = json_object_new_object();
852 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
853 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
854 msgtext = json_object_to_json_string(reply);
855 send(client, msgtext, strlen(msgtext) + 1, 0);
856 json_object_put(reply);
857 /* there is some stupid client, so close the connection to give a chance to some other client */
858 close(client);
859 break;
860 }
861
862 /* get parameters */
863 ds_type_t = -1;
864 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
865 if (strcmp(target, "running") == 0) {
866 ds_type_t = NC_DATASTORE_RUNNING;
867 } else if (strcmp(target, "startup") == 0) {
868 ds_type_t = NC_DATASTORE_STARTUP;
869 } else if (strcmp(target, "candidate") == 0) {
870 ds_type_t = NC_DATASTORE_CANDIDATE;
871 }
872 }
873 ds_type_s = -1;
874 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
875 if (strcmp(source, "running") == 0) {
876 ds_type_s = NC_DATASTORE_RUNNING;
877 } else if (strcmp(source, "startup") == 0) {
878 ds_type_s = NC_DATASTORE_STARTUP;
879 } else if (strcmp(source, "candidate") == 0) {
880 ds_type_s = NC_DATASTORE_CANDIDATE;
881 }
882 }
883
884 /* null global JSON error-reply */
885 err_reply = NULL;
886
887 /* prepare reply envelope */
888 reply = json_object_new_object();
889
890 /* process required operation */
891 switch (operation) {
892 case MSG_CONNECT:
893 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
894
895 host = json_object_get_string(json_object_object_get(request, "host"));
896 port = json_object_get_string(json_object_object_get(request, "port"));
897 user = json_object_get_string(json_object_object_get(request, "user"));
898 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200899 capabilities = json_object_object_get(request, "capabilities");
900 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200901 cpblts = nc_cpblts_new (NULL);
902 for (i=0; i<len; i++) {
903 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
904 }
905 } else {
906 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
907 }
David Kupka8e60a372012-09-04 09:15:20 +0200908 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
909 if ((host == NULL) || (user == NULL)) {
910 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
911 session_key = NULL;
912 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200913 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
914 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200915 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
916 }
917
918 reply = json_object_new_object();
919 if (session_key == NULL) {
920 /* negative reply */
921 if (err_reply == NULL) {
922 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
923 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
924 } else {
925 /* use filled err_reply from libnetconf's callback */
926 json_object_put(reply);
927 reply = err_reply;
928 }
929 } else {
930 /* positive reply */
931 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
932 json_object_object_add(reply, "session", json_object_new_string(session_key));
933
934 free(session_key);
935 }
936
937 break;
938 case MSG_GET:
939 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
940
941 filter = json_object_get_string(json_object_object_get(request, "filter"));
942
943 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
944
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200945 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200946 if (err_reply == NULL) {
947 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
948 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
949 } else {
950 /* use filled err_reply from libnetconf's callback */
951 json_object_put(reply);
952 reply = err_reply;
953 }
954 } else {
955 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
956 json_object_object_add(reply, "data", json_object_new_string(data));
957 }
958 break;
959 case MSG_GETCONFIG:
960 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
961
962 filter = json_object_get_string(json_object_object_get(request, "filter"));
963
964 if (ds_type_s == -1) {
965 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
966 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
967 break;
968 }
969
970 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
971 if (err_reply == NULL) {
972 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
973 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
974 } else {
975 /* use filled err_reply from libnetconf's callback */
976 json_object_put(reply);
977 reply = err_reply;
978 }
979 } else {
980 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
981 json_object_object_add(reply, "data", json_object_new_string(data));
982 }
983 break;
984 case MSG_EDITCONFIG:
985 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
986
987 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
988 if (defop != NULL) {
989 if (strcmp(defop, "merge") == 0) {
990 defop_type = NC_EDIT_DEFOP_MERGE;
991 } else if (strcmp(defop, "replace") == 0) {
992 defop_type = NC_EDIT_DEFOP_REPLACE;
993 } else if (strcmp(defop, "none") == 0) {
994 defop_type = NC_EDIT_DEFOP_NONE;
995 } else {
996 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
997 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
998 break;
999 }
1000 } else {
1001 defop_type = 0;
1002 }
1003
1004 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1005 if (erropt != NULL) {
1006 if (strcmp(erropt, "continue-on-error") == 0) {
1007 erropt_type = NC_EDIT_ERROPT_CONT;
1008 } else if (strcmp(erropt, "stop-on-error") == 0) {
1009 erropt_type = NC_EDIT_ERROPT_STOP;
1010 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1011 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1012 } else {
1013 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1014 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1015 break;
1016 }
1017 } else {
1018 erropt_type = 0;
1019 }
1020
1021 if (ds_type_t == -1) {
1022 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1023 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1024 break;
1025 }
1026
1027 config = json_object_get_string(json_object_object_get(request, "config"));
1028 if (config == NULL) {
1029 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1030 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1031 break;
1032 }
1033
1034 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1035 if (err_reply == NULL) {
1036 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1037 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1038 } else {
1039 /* use filled err_reply from libnetconf's callback */
1040 json_object_put(reply);
1041 reply = err_reply;
1042 }
1043 } else {
1044 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1045 }
1046 break;
1047 case MSG_COPYCONFIG:
1048 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1049 config = NULL;
1050
1051 if (source == NULL) {
1052 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001053 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001054 config = json_object_get_string(json_object_object_get(request, "config"));
1055 } else if (ds_type_s == -1) {
1056 /* source datastore specified, but it is invalid */
1057 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1058 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1059 break;
1060 }
1061
1062 if (ds_type_t == -1) {
1063 /* invalid target datastore specified */
1064 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1065 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1066 break;
1067 }
1068
1069 if (source == NULL && config == NULL) {
1070 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1071 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1072 } else {
1073 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1074 if (err_reply == NULL) {
1075 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1076 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1077 } else {
1078 /* use filled err_reply from libnetconf's callback */
1079 json_object_put(reply);
1080 reply = err_reply;
1081 }
1082 } else {
1083 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1084 }
1085 }
1086 break;
1087 case MSG_DELETECONFIG:
1088 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1089 /* no break - unifying code */
1090 case MSG_LOCK:
1091 if (operation == MSG_LOCK) {
1092 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1093 }
1094 /* no break - unifying code */
1095 case MSG_UNLOCK:
1096 if (operation == MSG_UNLOCK) {
1097 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1098 }
1099
1100 if (ds_type_t == -1) {
1101 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1102 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1103 break;
1104 }
1105
1106 switch(operation) {
1107 case MSG_DELETECONFIG:
1108 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1109 break;
1110 case MSG_LOCK:
1111 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1112 break;
1113 case MSG_UNLOCK:
1114 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1115 break;
1116 default:
1117 status = -1;
1118 break;
1119 }
1120
1121 if (status != EXIT_SUCCESS) {
1122 if (err_reply == NULL) {
1123 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1124 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1125 } else {
1126 /* use filled err_reply from libnetconf's callback */
1127 json_object_put(reply);
1128 reply = err_reply;
1129 }
1130 } else {
1131 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1132 }
1133 break;
1134 case MSG_KILL:
1135 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1136
1137 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1138
1139 if (sid == NULL) {
1140 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1141 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1142 break;
1143 }
1144
1145 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1146 if (err_reply == NULL) {
1147 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1148 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1149 } else {
1150 /* use filled err_reply from libnetconf's callback */
1151 json_object_put(reply);
1152 reply = err_reply;
1153 }
1154 } else {
1155 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1156 }
1157 break;
1158 case MSG_DISCONNECT:
1159 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1160
1161 if (netconf_close(server, netconf_sessions_list, session_key) != 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("Invalid session identifier."));
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_INFO:
1175 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1176
Kupka Davidda134a12012-09-06 14:12:16 +02001177 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1178 if (locked_session != NULL) {
1179 session = locked_session->session;
1180 } else {
1181 session = NULL;
1182 }
David Kupka8e60a372012-09-04 09:15:20 +02001183 if (session != NULL) {
1184 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1185 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1186 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1187 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1188 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1189 cpblts = nc_session_get_cpblts (session);
1190 if (cpblts != NULL) {
1191 json_obj = json_object_new_array();
1192 nc_cpblts_iter_start (cpblts);
1193 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1194 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1195 }
1196 json_object_object_add(reply, "capabilities", json_obj);
1197 }
1198 } else {
1199 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1200 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1201 }
1202
1203 break;
1204 case MSG_GENERIC:
1205 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1206
1207 config = json_object_get_string(json_object_object_get(request, "content"));
1208
1209 if (config == NULL) {
1210 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1211 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1212 break;
1213 }
1214
1215 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != 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("kill-session failed."));
1219 } else {
1220 /* use filled err_reply from libnetconf's callback */
1221 json_object_put(reply);
1222 reply = err_reply;
1223 }
1224 } else {
1225 if (data == NULL) {
1226 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1227 } else {
1228 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1229 json_object_object_add(reply, "data", json_object_new_string(data));
1230 }
1231 }
1232 break;
1233 default:
1234 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1235 reply = json_object_new_object();
1236 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1237 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1238 break;
1239 }
1240 json_object_put(request);
1241
David Kupka1e3e4c82012-09-04 09:32:15 +02001242 /* send reply to caller */
1243 if (reply != NULL) {
1244 msgtext = json_object_to_json_string(reply);
1245 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1246 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001247 break;
1248 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001249 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1250 json_object_put(reply);
1251 free (chunked_msg);
1252 free (buffer);
1253 } else {
1254 break;
David Kupka8e60a372012-09-04 09:15:20 +02001255 }
1256 }
1257 }
1258
1259 free (arg);
1260
1261 return retval;
1262}
1263
Radek Krejcif23850c2012-07-23 16:14:17 +02001264/*
1265 * This is actually implementation of NETCONF client
1266 * - requests are received from UNIX socket in the predefined format
1267 * - results are replied through the same way
1268 * - the daemon run as a separate process, but it is started and stopped
1269 * automatically by Apache.
1270 *
1271 */
Radek Krejci469aab82012-07-22 18:42:20 +02001272static void forked_proc(apr_pool_t * pool, server_rec * server)
1273{
1274 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001275 int lsock, client, ret, i, pthread_count = 0;
1276 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001277 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001278 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001279 struct pass_to_thread * arg;
1280 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1281 struct timespec maxtime;
1282 pthread_rwlockattr_t lock_attrs;
1283
1284 /* wait at most 5 secons for every thread to terminate */
1285 maxtime.tv_sec = 5;
1286 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001287
Radek Krejcif23850c2012-07-23 16:14:17 +02001288 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001289 unixd_setup_child();
1290
Radek Krejciae021c12012-07-25 18:03:52 +02001291 cfg = ap_get_module_config(server->module_config, &netconf_module);
1292 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001293 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1294 return;
1295 }
Radek Krejci469aab82012-07-22 18:42:20 +02001296
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001297 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001298 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1299 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1300 return;
1301 }
1302
1303 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001304 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001305 unlink(local.sun_path);
1306 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1307
1308 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1309 if (errno == EADDRINUSE) {
1310 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1311 close(lsock);
1312 exit(0);
1313 }
1314 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1315 close(lsock);
1316 return;
1317 }
1318
1319 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1320 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1321 close(lsock);
1322 return;
1323 }
1324
1325 /* prepare internal lists */
1326 netconf_sessions_list = apr_hash_make(pool);
1327
1328 /* setup libnetconf's callbacks */
1329 nc_verbosity(NC_VERB_DEBUG);
1330 clb_print_server = server;
1331 nc_callback_print(clb_print);
1332 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1333 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1334 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001335 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001336
1337 /* disable publickey authentication */
1338 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1339
David Kupka8e60a372012-09-04 09:15:20 +02001340 /* create mutex protecting session list */
1341 pthread_rwlockattr_init(&lock_attrs);
1342 /* rwlock is shared only with threads in this process */
1343 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1344 /* create rw lock */
1345 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1346 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1347 close (lsock);
1348 return;
1349 }
1350
Radek Krejci469aab82012-07-22 18:42:20 +02001351 while (isterminated == 0) {
1352 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1353
1354 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001355 len = sizeof(remote);
1356 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001357 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1358 apr_sleep(SLEEP_TIME);
1359 continue;
1360 } else if (client == -1 && (errno == EINTR)) {
1361 continue;
1362 } else if (client == -1) {
1363 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1364 continue;
1365 }
Radek Krejci469aab82012-07-22 18:42:20 +02001366
1367 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001368 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001369
David Kupka8e60a372012-09-04 09:15:20 +02001370 arg = malloc (sizeof(struct pass_to_thread));
1371 arg->client = client;
1372 arg->pool = pool;
1373 arg->server = server;
1374 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001375
David Kupka8e60a372012-09-04 09:15:20 +02001376 /* start new thread. It will serve this particular request and then terminate */
1377 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1378 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1379 } else {
1380 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1381 pthread_count++;
1382 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1383 ptids[pthread_count] = 0;
1384 }
Radek Krejci469aab82012-07-22 18:42:20 +02001385
David Kupka8e60a372012-09-04 09:15:20 +02001386 /* check if some thread already terminated, free some resources by joining it */
1387 for (i=0; i<pthread_count; i++) {
1388 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1389 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1390 pthread_count--;
1391 if (pthread_count > 0) {
1392 /* place last Thread ID on the place of joined one */
1393 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001394 }
Radek Krejci469aab82012-07-22 18:42:20 +02001395 }
1396 }
David Kupka8e60a372012-09-04 09:15:20 +02001397 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001398 }
1399
David Kupka8e60a372012-09-04 09:15:20 +02001400 /* join all threads */
1401 for (i=0; i<pthread_count; i++) {
1402 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1403 }
1404 free (ptids);
1405
Radek Krejci469aab82012-07-22 18:42:20 +02001406 close(lsock);
1407
David Kupka8e60a372012-09-04 09:15:20 +02001408 /* destroy rwlock */
1409 pthread_rwlock_destroy(&session_lock);
1410 pthread_rwlockattr_destroy(&lock_attrs);
1411
Radek Krejci469aab82012-07-22 18:42:20 +02001412 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1413
1414 exit(APR_SUCCESS);
1415}
1416
Radek Krejcif23850c2012-07-23 16:14:17 +02001417static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001418{
Radek Krejcif23850c2012-07-23 16:14:17 +02001419 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1420
1421 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1422 apr_pool_create(&config->pool, pool);
1423 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001424 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001425
1426 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001427}
1428
1429static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1430 apr_pool_t * plog, server_rec * s)
1431{
Radek Krejcif23850c2012-07-23 16:14:17 +02001432 mod_netconf_cfg *config;
1433 apr_status_t res;
1434
Radek Krejci469aab82012-07-22 18:42:20 +02001435 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001436 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001437 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001438
1439 /*
1440 * The following checks if this routine has been called before.
1441 * This is necessary because the parent process gets initialized
1442 * a couple of times as the server starts up.
1443 */
1444 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1445 if (!data) {
1446 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1447 return (OK);
1448 }
1449
Radek Krejcif23850c2012-07-23 16:14:17 +02001450 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1451 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001452
Radek Krejcif23850c2012-07-23 16:14:17 +02001453 if (config && config->forkproc == NULL) {
1454 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1455 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001456 switch (res) {
1457 case APR_INCHILD:
1458 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001459 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001460 apr_signal(SIGTERM, signal_handler);
1461
1462 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001463 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001464
1465 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001466 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001467
Radek Krejcif23850c2012-07-23 16:14:17 +02001468 /* I never should be here, wtf?!? */
1469 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001470 exit(APR_EGENERAL);
1471 break;
1472 case APR_INPARENT:
1473 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001474 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001475 break;
1476 default:
1477 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1478 break;
1479 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001480 } else {
1481 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001482 }
1483
1484 return OK;
1485}
1486
Radek Krejci469aab82012-07-22 18:42:20 +02001487/**
1488 * Register module hooks
1489 */
1490static void mod_netconf_register_hooks(apr_pool_t * p)
1491{
Radek Krejcif23850c2012-07-23 16:14:17 +02001492 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001493}
1494
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001495static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1496{
1497 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1498 return NULL;
1499}
1500
1501static const command_rec netconf_cmds[] = {
1502 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1503 {NULL}
1504};
1505
Radek Krejci469aab82012-07-22 18:42:20 +02001506/* Dispatch list for API hooks */
1507module AP_MODULE_DECLARE_DATA netconf_module = {
1508 STANDARD20_MODULE_STUFF,
1509 NULL, /* create per-dir config structures */
1510 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001511 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001512 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001513 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001514 mod_netconf_register_hooks /* register hooks */
1515};
Radek Krejcia332b692012-11-12 16:15:54 +01001516