blob: 627fb6de961d7e1970a261e4054fabb9eaf8bbb7 [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."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100924 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200925 } else {
926 /* use filled err_reply from libnetconf's callback */
927 json_object_put(reply);
928 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100929 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200930 }
931 } else {
932 /* positive reply */
933 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
934 json_object_object_add(reply, "session", json_object_new_string(session_key));
935
936 free(session_key);
937 }
938
939 break;
940 case MSG_GET:
941 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
942
943 filter = json_object_get_string(json_object_object_get(request, "filter"));
944
945 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
946
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200947 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200948 if (err_reply == NULL) {
949 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
950 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
951 } else {
952 /* use filled err_reply from libnetconf's callback */
953 json_object_put(reply);
954 reply = err_reply;
955 }
956 } else {
957 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
958 json_object_object_add(reply, "data", json_object_new_string(data));
959 }
960 break;
961 case MSG_GETCONFIG:
962 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
963
964 filter = json_object_get_string(json_object_object_get(request, "filter"));
965
966 if (ds_type_s == -1) {
967 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
968 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
969 break;
970 }
971
972 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
973 if (err_reply == NULL) {
974 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
975 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
976 } else {
977 /* use filled err_reply from libnetconf's callback */
978 json_object_put(reply);
979 reply = err_reply;
980 }
981 } else {
982 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
983 json_object_object_add(reply, "data", json_object_new_string(data));
984 }
985 break;
986 case MSG_EDITCONFIG:
987 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
988
989 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
990 if (defop != NULL) {
991 if (strcmp(defop, "merge") == 0) {
992 defop_type = NC_EDIT_DEFOP_MERGE;
993 } else if (strcmp(defop, "replace") == 0) {
994 defop_type = NC_EDIT_DEFOP_REPLACE;
995 } else if (strcmp(defop, "none") == 0) {
996 defop_type = NC_EDIT_DEFOP_NONE;
997 } else {
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("Invalid default-operation parameter."));
1000 break;
1001 }
1002 } else {
1003 defop_type = 0;
1004 }
1005
1006 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1007 if (erropt != NULL) {
1008 if (strcmp(erropt, "continue-on-error") == 0) {
1009 erropt_type = NC_EDIT_ERROPT_CONT;
1010 } else if (strcmp(erropt, "stop-on-error") == 0) {
1011 erropt_type = NC_EDIT_ERROPT_STOP;
1012 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1013 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1014 } else {
1015 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1016 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1017 break;
1018 }
1019 } else {
1020 erropt_type = 0;
1021 }
1022
1023 if (ds_type_t == -1) {
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("Invalid target repository type requested."));
1026 break;
1027 }
1028
1029 config = json_object_get_string(json_object_object_get(request, "config"));
1030 if (config == NULL) {
1031 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1032 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1033 break;
1034 }
1035
1036 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1037 if (err_reply == NULL) {
1038 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1039 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1040 } else {
1041 /* use filled err_reply from libnetconf's callback */
1042 json_object_put(reply);
1043 reply = err_reply;
1044 }
1045 } else {
1046 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1047 }
1048 break;
1049 case MSG_COPYCONFIG:
1050 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1051 config = NULL;
1052
1053 if (source == NULL) {
1054 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001055 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001056 config = json_object_get_string(json_object_object_get(request, "config"));
1057 } else if (ds_type_s == -1) {
1058 /* source datastore specified, but it is invalid */
1059 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1060 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1061 break;
1062 }
1063
1064 if (ds_type_t == -1) {
1065 /* invalid target datastore specified */
1066 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1067 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1068 break;
1069 }
1070
1071 if (source == NULL && config == NULL) {
1072 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1073 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1074 } else {
1075 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1076 if (err_reply == NULL) {
1077 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1078 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1079 } else {
1080 /* use filled err_reply from libnetconf's callback */
1081 json_object_put(reply);
1082 reply = err_reply;
1083 }
1084 } else {
1085 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1086 }
1087 }
1088 break;
1089 case MSG_DELETECONFIG:
1090 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1091 /* no break - unifying code */
1092 case MSG_LOCK:
1093 if (operation == MSG_LOCK) {
1094 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1095 }
1096 /* no break - unifying code */
1097 case MSG_UNLOCK:
1098 if (operation == MSG_UNLOCK) {
1099 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1100 }
1101
1102 if (ds_type_t == -1) {
1103 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1104 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1105 break;
1106 }
1107
1108 switch(operation) {
1109 case MSG_DELETECONFIG:
1110 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1111 break;
1112 case MSG_LOCK:
1113 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1114 break;
1115 case MSG_UNLOCK:
1116 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1117 break;
1118 default:
1119 status = -1;
1120 break;
1121 }
1122
1123 if (status != EXIT_SUCCESS) {
1124 if (err_reply == NULL) {
1125 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1126 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1127 } else {
1128 /* use filled err_reply from libnetconf's callback */
1129 json_object_put(reply);
1130 reply = err_reply;
1131 }
1132 } else {
1133 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1134 }
1135 break;
1136 case MSG_KILL:
1137 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1138
1139 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1140
1141 if (sid == NULL) {
1142 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1143 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1144 break;
1145 }
1146
1147 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1148 if (err_reply == NULL) {
1149 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1150 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1151 } else {
1152 /* use filled err_reply from libnetconf's callback */
1153 json_object_put(reply);
1154 reply = err_reply;
1155 }
1156 } else {
1157 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1158 }
1159 break;
1160 case MSG_DISCONNECT:
1161 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1162
1163 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1164 if (err_reply == NULL) {
1165 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1166 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1167 } else {
1168 /* use filled err_reply from libnetconf's callback */
1169 json_object_put(reply);
1170 reply = err_reply;
1171 }
1172 } else {
1173 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1174 }
1175 break;
1176 case MSG_INFO:
1177 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1178
Kupka Davidda134a12012-09-06 14:12:16 +02001179 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1180 if (locked_session != NULL) {
1181 session = locked_session->session;
1182 } else {
1183 session = NULL;
1184 }
David Kupka8e60a372012-09-04 09:15:20 +02001185 if (session != NULL) {
1186 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1187 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1188 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1189 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1190 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1191 cpblts = nc_session_get_cpblts (session);
1192 if (cpblts != NULL) {
1193 json_obj = json_object_new_array();
1194 nc_cpblts_iter_start (cpblts);
1195 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1196 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1197 }
1198 json_object_object_add(reply, "capabilities", json_obj);
1199 }
1200 } else {
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("Invalid session identifier."));
1203 }
1204
1205 break;
1206 case MSG_GENERIC:
1207 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1208
1209 config = json_object_get_string(json_object_object_get(request, "content"));
1210
1211 if (config == NULL) {
1212 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1213 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1214 break;
1215 }
1216
1217 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1218 if (err_reply == NULL) {
1219 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1220 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1221 } else {
1222 /* use filled err_reply from libnetconf's callback */
1223 json_object_put(reply);
1224 reply = err_reply;
1225 }
1226 } else {
1227 if (data == NULL) {
1228 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1229 } else {
1230 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1231 json_object_object_add(reply, "data", json_object_new_string(data));
1232 }
1233 }
1234 break;
1235 default:
1236 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1237 reply = json_object_new_object();
1238 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1239 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1240 break;
1241 }
1242 json_object_put(request);
1243
David Kupka1e3e4c82012-09-04 09:32:15 +02001244 /* send reply to caller */
1245 if (reply != NULL) {
1246 msgtext = json_object_to_json_string(reply);
1247 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1248 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001249 break;
1250 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001251 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1252 json_object_put(reply);
1253 free (chunked_msg);
1254 free (buffer);
1255 } else {
1256 break;
David Kupka8e60a372012-09-04 09:15:20 +02001257 }
1258 }
1259 }
1260
1261 free (arg);
1262
1263 return retval;
1264}
1265
Radek Krejcif23850c2012-07-23 16:14:17 +02001266/*
1267 * This is actually implementation of NETCONF client
1268 * - requests are received from UNIX socket in the predefined format
1269 * - results are replied through the same way
1270 * - the daemon run as a separate process, but it is started and stopped
1271 * automatically by Apache.
1272 *
1273 */
Radek Krejci469aab82012-07-22 18:42:20 +02001274static void forked_proc(apr_pool_t * pool, server_rec * server)
1275{
1276 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001277 int lsock, client, ret, i, pthread_count = 0;
1278 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001279 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001280 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001281 struct pass_to_thread * arg;
1282 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1283 struct timespec maxtime;
1284 pthread_rwlockattr_t lock_attrs;
1285
1286 /* wait at most 5 secons for every thread to terminate */
1287 maxtime.tv_sec = 5;
1288 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001289
Radek Krejcif23850c2012-07-23 16:14:17 +02001290 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001291 unixd_setup_child();
1292
Radek Krejciae021c12012-07-25 18:03:52 +02001293 cfg = ap_get_module_config(server->module_config, &netconf_module);
1294 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001295 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1296 return;
1297 }
Radek Krejci469aab82012-07-22 18:42:20 +02001298
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001299 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001300 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1301 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1302 return;
1303 }
1304
1305 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001306 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001307 unlink(local.sun_path);
1308 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1309
1310 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1311 if (errno == EADDRINUSE) {
1312 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1313 close(lsock);
1314 exit(0);
1315 }
1316 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1317 close(lsock);
1318 return;
1319 }
1320
1321 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1322 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1323 close(lsock);
1324 return;
1325 }
1326
1327 /* prepare internal lists */
1328 netconf_sessions_list = apr_hash_make(pool);
1329
1330 /* setup libnetconf's callbacks */
1331 nc_verbosity(NC_VERB_DEBUG);
1332 clb_print_server = server;
1333 nc_callback_print(clb_print);
1334 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1335 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1336 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001337 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001338
1339 /* disable publickey authentication */
1340 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1341
David Kupka8e60a372012-09-04 09:15:20 +02001342 /* create mutex protecting session list */
1343 pthread_rwlockattr_init(&lock_attrs);
1344 /* rwlock is shared only with threads in this process */
1345 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1346 /* create rw lock */
1347 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1348 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1349 close (lsock);
1350 return;
1351 }
1352
Radek Krejci469aab82012-07-22 18:42:20 +02001353 while (isterminated == 0) {
1354 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1355
1356 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001357 len = sizeof(remote);
1358 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001359 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1360 apr_sleep(SLEEP_TIME);
1361 continue;
1362 } else if (client == -1 && (errno == EINTR)) {
1363 continue;
1364 } else if (client == -1) {
1365 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1366 continue;
1367 }
Radek Krejci469aab82012-07-22 18:42:20 +02001368
1369 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001370 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001371
David Kupka8e60a372012-09-04 09:15:20 +02001372 arg = malloc (sizeof(struct pass_to_thread));
1373 arg->client = client;
1374 arg->pool = pool;
1375 arg->server = server;
1376 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001377
David Kupka8e60a372012-09-04 09:15:20 +02001378 /* start new thread. It will serve this particular request and then terminate */
1379 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1380 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1381 } else {
1382 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1383 pthread_count++;
1384 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1385 ptids[pthread_count] = 0;
1386 }
Radek Krejci469aab82012-07-22 18:42:20 +02001387
David Kupka8e60a372012-09-04 09:15:20 +02001388 /* check if some thread already terminated, free some resources by joining it */
1389 for (i=0; i<pthread_count; i++) {
1390 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1391 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1392 pthread_count--;
1393 if (pthread_count > 0) {
1394 /* place last Thread ID on the place of joined one */
1395 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001396 }
Radek Krejci469aab82012-07-22 18:42:20 +02001397 }
1398 }
David Kupka8e60a372012-09-04 09:15:20 +02001399 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001400 }
1401
David Kupka8e60a372012-09-04 09:15:20 +02001402 /* join all threads */
1403 for (i=0; i<pthread_count; i++) {
1404 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1405 }
1406 free (ptids);
1407
Radek Krejci469aab82012-07-22 18:42:20 +02001408 close(lsock);
1409
David Kupka8e60a372012-09-04 09:15:20 +02001410 /* destroy rwlock */
1411 pthread_rwlock_destroy(&session_lock);
1412 pthread_rwlockattr_destroy(&lock_attrs);
1413
Radek Krejci469aab82012-07-22 18:42:20 +02001414 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1415
1416 exit(APR_SUCCESS);
1417}
1418
Radek Krejcif23850c2012-07-23 16:14:17 +02001419static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001420{
Radek Krejcif23850c2012-07-23 16:14:17 +02001421 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1422
1423 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1424 apr_pool_create(&config->pool, pool);
1425 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001426 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001427
1428 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001429}
1430
1431static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1432 apr_pool_t * plog, server_rec * s)
1433{
Radek Krejcif23850c2012-07-23 16:14:17 +02001434 mod_netconf_cfg *config;
1435 apr_status_t res;
1436
Radek Krejci469aab82012-07-22 18:42:20 +02001437 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001438 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001439 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001440
1441 /*
1442 * The following checks if this routine has been called before.
1443 * This is necessary because the parent process gets initialized
1444 * a couple of times as the server starts up.
1445 */
1446 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1447 if (!data) {
1448 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1449 return (OK);
1450 }
1451
Radek Krejcif23850c2012-07-23 16:14:17 +02001452 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1453 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001454
Radek Krejcif23850c2012-07-23 16:14:17 +02001455 if (config && config->forkproc == NULL) {
1456 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1457 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001458 switch (res) {
1459 case APR_INCHILD:
1460 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001461 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001462 apr_signal(SIGTERM, signal_handler);
1463
1464 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001465 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001466
1467 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001468 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001469
Radek Krejcif23850c2012-07-23 16:14:17 +02001470 /* I never should be here, wtf?!? */
1471 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001472 exit(APR_EGENERAL);
1473 break;
1474 case APR_INPARENT:
1475 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001476 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001477 break;
1478 default:
1479 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1480 break;
1481 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001482 } else {
1483 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001484 }
1485
1486 return OK;
1487}
1488
Radek Krejci469aab82012-07-22 18:42:20 +02001489/**
1490 * Register module hooks
1491 */
1492static void mod_netconf_register_hooks(apr_pool_t * p)
1493{
Radek Krejcif23850c2012-07-23 16:14:17 +02001494 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001495}
1496
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001497static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1498{
1499 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1500 return NULL;
1501}
1502
1503static const command_rec netconf_cmds[] = {
1504 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1505 {NULL}
1506};
1507
Radek Krejci469aab82012-07-22 18:42:20 +02001508/* Dispatch list for API hooks */
1509module AP_MODULE_DECLARE_DATA netconf_module = {
1510 STANDARD20_MODULE_STUFF,
1511 NULL, /* create per-dir config structures */
1512 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001513 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001514 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001515 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001516 mod_netconf_register_hooks /* register hooks */
1517};
Radek Krejcia332b692012-11-12 16:15:54 +01001518