blob: 0710f55f2de3792da8c4c34c36ab45552b9bc158 [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01008 * \date 2013
Radek Krejci469aab82012-07-22 18:42:20 +02009 */
10/*
11 * Copyright (C) 2011-2012 CESNET
12 *
13 * LICENSE TERMS
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 * 3. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * ALTERNATIVELY, provided that this notice is retained in full, this
29 * product may be distributed under the terms of the GNU General Public
30 * License (GPL) version 2 or later, in which case the provisions
31 * of the GPL apply INSTEAD OF those given above.
32 *
33 * This software is provided ``as is'', and any express or implied
34 * warranties, including, but not limited to, the implied warranties of
35 * merchantability and fitness for a particular purpose are disclaimed.
36 * In no event shall the company or contributors be liable for any
37 * direct, indirect, incidental, special, exemplary, or consequential
38 * damages (including, but not limited to, procurement of substitute
39 * goods or services; loss of use, data, or profits; or business
40 * interruption) however caused and on any theory of liability, whether
41 * in contract, strict liability, or tort (including negligence or
42 * otherwise) arising in any way out of the use of this software, even
43 * if advised of the possibility of such damage.
44 *
45 */
Tomas Cejka689a1042013-01-16 15:08:25 +010046static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "ARCSID" $";
Radek Krejci469aab82012-07-22 18:42:20 +020047
Radek Krejci7b4ddd02012-07-30 08:09:58 +020048#include <unistd.h>
49#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020050#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/un.h>
David Kupka8e60a372012-09-04 09:15:20 +020053#include <pthread.h>
54#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020055
56#include <unixd.h>
57#include <httpd.h>
58#include <http_log.h>
59#include <http_config.h>
60
61#include <apr_sha1.h>
62#include <apr_hash.h>
63#include <apr_signal.h>
64#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020065
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020066#include <json/json.h>
67
Radek Krejci469aab82012-07-22 18:42:20 +020068#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020069
Tomas Cejka94da2c52013-01-08 18:20:30 +010070#include "message_type.h"
71
Radek Krejci469aab82012-07-22 18:42:20 +020072
73#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020074#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020075#define MAX_SOCKET_CL 10
76#define BUFFER_SIZE 4096
77
78/* sleep in master process for non-blocking socket reading */
79#define SLEEP_TIME 200
80
81#ifndef offsetof
82#define offsetof(type, member) ((size_t) ((type *) 0)->member)
83#endif
84
Tomas Cejka027f3bc2012-11-10 20:28:36 +010085/* timeout in msec */
Tomas Cejka027f3bc2012-11-10 20:28:36 +010086#define NCWITHDEFAULTS NCWD_MODE_DISABLED
87
Radek Krejci469aab82012-07-22 18:42:20 +020088struct timeval timeout = { 1, 0 };
89
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020090
Radek Krejci469aab82012-07-22 18:42:20 +020091#define MSG_OK 0
92#define MSG_OPEN 1
93#define MSG_DATA 2
94#define MSG_CLOSE 3
95#define MSG_ERROR 4
96#define MSG_UNKNOWN 5
97
Radek Krejci469aab82012-07-22 18:42:20 +020098module AP_MODULE_DECLARE_DATA netconf_module;
99
100typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200101 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200102 apr_proc_t *forkproc;
103 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200104} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200105
David Kupka8e60a372012-09-04 09:15:20 +0200106struct pass_to_thread {
107 int client; /**< opened socket */
108 apr_pool_t * pool; /**< ?? */
109 server_rec * server; /**< ?? */
110 apr_hash_t * netconf_sessions_list; /**< ?? */
111};
112
113struct session_with_mutex {
114 struct nc_session * session; /**< netconf session */
115 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
116};
117
118pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
119
Radek Krejci469aab82012-07-22 18:42:20 +0200120volatile int isterminated = 0;
121
122static char* password;
123
124
125static void signal_handler(int sign)
126{
127 switch (sign) {
128 case SIGTERM:
129 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200130 break;
131 }
132}
133
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200134static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200135{
Radek Krejcif23850c2012-07-23 16:14:17 +0200136 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
137 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200138 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200139
Radek Krejci469aab82012-07-22 18:42:20 +0200140 apr_sha1_ctx_t sha1_ctx;
141 apr_sha1_init(&sha1_ctx);
142 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
143 apr_sha1_update(&sha1_ctx, port, strlen(port));
144 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200145 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200146
Radek Krejcif23850c2012-07-23 16:14:17 +0200147 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200148 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200149 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200150 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200151 }
152 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200153
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200154 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200155}
156
157int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
158{
159 /* always approve */
160 return (EXIT_SUCCESS);
161}
162
163char* netconf_callback_sshauth_password (const char* username, const char* hostname)
164{
165 char* buf;
166
167 buf = malloc ((strlen(password) + 1) * sizeof(char));
168 apr_cpystrn(buf, password, strlen(password) + 1);
169
170 return (buf);
171}
172
173void netconf_callback_sshauth_interactive (const char* name,
174 int name_len,
175 const char* instruction,
176 int instruction_len,
177 int num_prompts,
178 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
179 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
180 void** abstract)
181{
182 int i;
183
184 for (i=0; i<num_prompts; i++) {
185 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
186 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
187 responses[i].length = strlen(responses[i].text) + 1;
188 }
189
190 return;
191}
192
Radek Krejcic11fd862012-07-26 12:41:21 +0200193static json_object *err_reply = NULL;
194void netconf_callback_error_process(const char* tag,
195 const char* type,
196 const char* severity,
197 const char* apptag,
198 const char* path,
199 const char* message,
200 const char* attribute,
201 const char* element,
202 const char* ns,
203 const char* sid)
204{
205 err_reply = json_object_new_object();
206 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
207 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
208 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
209 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
210 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
211 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
212 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
213 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
214 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
215 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
216 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
217}
218
Kupka David00b9c5c2012-09-05 09:45:50 +0200219static 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 +0200220{
Radek Krejci469aab82012-07-22 18:42:20 +0200221 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200222 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200223 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200224
225 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200226 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200227 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
228
Radek Krejci469aab82012-07-22 18:42:20 +0200229 /* if connected successful, add session to the list */
230 if (session != NULL) {
231 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200232 session_key = gen_ncsession_hash(
233 (host==NULL) ? "localhost" : host,
234 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200235 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200236
237 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
238 nc_session_free(session);
239 free (locked_session);
240 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
241 return NULL;
242 }
243 locked_session->session = session;
244 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100245 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200246 /* get exclusive access to sessions_list (conns) */
247 if (pthread_rwlock_wrlock (&session_lock) != 0) {
248 nc_session_free(session);
249 free (locked_session);
250 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
251 return NULL;
252 }
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100253 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200254 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100255 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200256 /* end of critical section */
257 if (pthread_rwlock_unlock (&session_lock) != 0) {
258 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
259 return NULL;
260 }
Radek Krejci469aab82012-07-22 18:42:20 +0200261 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200262 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200263 } else {
264 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200265 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200266 }
267
Radek Krejci469aab82012-07-22 18:42:20 +0200268}
269
Radek Krejci80c10d92012-07-30 08:38:50 +0200270static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200271{
272 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200273 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200274
Radek Krejcif23850c2012-07-23 16:14:17 +0200275 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200276 /* get exclusive (write) access to sessions_list (conns) */
277 if (pthread_rwlock_wrlock (&session_lock) != 0) {
278 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
279 return EXIT_FAILURE;
280 }
281 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
282 if (locked_session != NULL) {
283 pthread_mutex_destroy(&locked_session->lock);
284 ns = locked_session->session;
285 free (locked_session);
286 }
Radek Krejci469aab82012-07-22 18:42:20 +0200287 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100288 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200289 nc_session_free (ns);
290 ns = NULL;
291
292 /* remove session from the active sessions list */
293 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200294 /* end of critical section */
295 if (pthread_rwlock_unlock (&session_lock) != 0) {
296 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
297 return EXIT_FAILURE;
298 }
Radek Krejci469aab82012-07-22 18:42:20 +0200299 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200300
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200301 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200302 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200303 if (pthread_rwlock_unlock (&session_lock) != 0) {
304 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
305 return EXIT_FAILURE;
306 }
Radek Krejci469aab82012-07-22 18:42:20 +0200307 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200308 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200309 }
310}
311
Radek Krejci80c10d92012-07-30 08:38:50 +0200312static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200313{
314 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200315 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200316 nc_reply* reply;
317 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100318 NC_MSG_TYPE msgt;
319 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200320
Radek Krejci8e4632a2012-07-26 13:40:34 +0200321 /* check requests */
322 if (rpc == NULL) {
323 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
324 return (EXIT_FAILURE);
325 }
326
David Kupka8e60a372012-09-04 09:15:20 +0200327 /* get non-exclusive (read) access to sessions_list (conns) */
328 if (pthread_rwlock_rdlock (&session_lock) != 0) {
329 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
330 return EXIT_FAILURE;
331 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200332 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200333 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
334 if (locked_session != NULL) {
335 session = locked_session->session;
336 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200337 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200338 /* get exclusive access to session */
339 if (pthread_mutex_lock(&locked_session->lock) != 0) {
340 /* unlock before returning error */
341 if (pthread_rwlock_unlock (&session_lock) != 0) {
342 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
343 return EXIT_FAILURE;
344 }
345 return EXIT_FAILURE;
346 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200347 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100348 msgt = nc_session_send_recv(session, rpc, &reply);
349
David Kupka8e60a372012-09-04 09:15:20 +0200350 /* first release exclusive lock for this session */
351 pthread_mutex_unlock(&locked_session->lock);
352 /* end of critical section */
353 if (pthread_rwlock_unlock (&session_lock) != 0) {
354 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
355 return EXIT_FAILURE;
356 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200357
Radek Krejcia332b692012-11-12 16:15:54 +0100358 /* process the result of the operation */
359 switch (msgt) {
360 case NC_MSG_UNKNOWN:
361 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
362 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
363 netconf_close(server, conns, session_key);
364 return (EXIT_FAILURE);
365 }
366 /* no break */
367 case NC_MSG_NONE:
368 /* there is error handled by callback */
369 return (EXIT_FAILURE);
370 break;
371 case NC_MSG_REPLY:
372 switch (replyt = nc_reply_get_type(reply)) {
373 case NC_REPLY_OK:
374 retval = EXIT_SUCCESS;
375 break;
376 default:
377 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
378 retval = EXIT_FAILURE;
379 break;
380 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200381 break;
382 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100383 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200384 retval = EXIT_FAILURE;
385 break;
386 }
387 nc_reply_free(reply);
388 return (retval);
389 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100390 /* release lock on failure */
391 if (pthread_rwlock_unlock (&session_lock) != 0) {
392 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
393 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
395 return (EXIT_FAILURE);
396 }
397}
Radek Krejci80c10d92012-07-30 08:38:50 +0200398
399static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200400{
401 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200402 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100403 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200404 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100405 NC_MSG_TYPE msgt;
406 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200407
408 /* check requests */
409 if (rpc == NULL) {
410 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
411 return (NULL);
412 }
413
David Kupka8e60a372012-09-04 09:15:20 +0200414 /* get non-exclusive (read) access to sessions_list (conns) */
415 if (pthread_rwlock_rdlock (&session_lock) != 0) {
416 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
417 return NULL;
418 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200419 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200420 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
421 if (locked_session != NULL) {
422 session = locked_session->session;
423 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200424 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200425 /* get exclusive access to session */
426 if (pthread_mutex_lock(&locked_session->lock) != 0) {
427 /* unlock before returning error */
428 if (pthread_rwlock_unlock (&session_lock) != 0) {
429 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
430 return NULL;
431 }
432 return NULL;
433 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200434 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100435 msgt = nc_session_send_recv(session, rpc, &reply);
436
David Kupka8e60a372012-09-04 09:15:20 +0200437 /* first release exclusive lock for this session */
438 pthread_mutex_unlock(&locked_session->lock);
439 /* end of critical section */
440 if (pthread_rwlock_unlock (&session_lock) != 0) {
441 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 +0100442 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200443 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200444
Radek Krejcia332b692012-11-12 16:15:54 +0100445 /* process the result of the operation */
446 switch (msgt) {
447 case NC_MSG_UNKNOWN:
448 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
450 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200451 return (NULL);
452 }
Radek Krejcia332b692012-11-12 16:15:54 +0100453 /* no break */
454 case NC_MSG_NONE:
455 /* there is error handled by callback */
456 return (NULL);
457 break;
458 case NC_MSG_REPLY:
459 switch (replyt = nc_reply_get_type(reply)) {
460 case NC_REPLY_DATA:
461 if ((data = nc_reply_get_data (reply)) == NULL) {
462 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
463 data = NULL;
464 }
465 break;
466 default:
467 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
468 data = NULL;
469 break;
470 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200471 break;
472 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100473 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
474 data = NULL;
475 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200476 }
477 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200478 return (data);
479 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100480 /* release lock on failure */
481 if (pthread_rwlock_unlock (&session_lock) != 0) {
482 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
483 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200484 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
485 return (NULL);
486 }
487}
488
Radek Krejci80c10d92012-07-30 08:38:50 +0200489static 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 +0200490{
491 nc_rpc* rpc;
492 struct nc_filter *f = NULL;
493 char* data = NULL;
494
495 /* create filter if set */
496 if (filter != NULL) {
497 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
498 }
499
500 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100501 rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200502 nc_filter_free(f);
503 if (rpc == NULL) {
504 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
505 return (NULL);
506 }
507
508 data = netconf_opdata(server, conns, session_key, rpc);
509 nc_rpc_free (rpc);
510 return (data);
511}
512
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100513static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
514{
515 nc_rpc* rpc;
516 char* data = NULL;
517
518 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100519 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100520 if (rpc == NULL) {
521 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
522 return (NULL);
523 }
524
525 data = netconf_opdata(server, conns, session_key, rpc);
526 nc_rpc_free (rpc);
527 return (data);
528}
529
Radek Krejci80c10d92012-07-30 08:38:50 +0200530static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200531{
532 nc_rpc* rpc;
533 struct nc_filter *f = NULL;
534 char* data = NULL;
535
536 /* create filter if set */
537 if (filter != NULL) {
538 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
539 }
540
541 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100542 rpc = nc_rpc_get (f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200543 nc_filter_free(f);
544 if (rpc == NULL) {
545 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
546 return (NULL);
547 }
548
549 data = netconf_opdata(server, conns, session_key, rpc);
550 nc_rpc_free (rpc);
551 return (data);
552}
553
Radek Krejci80c10d92012-07-30 08:38:50 +0200554static 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 +0200555{
556 nc_rpc* rpc;
557 int retval = EXIT_SUCCESS;
558
559 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100560 rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200561 if (rpc == NULL) {
562 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
563 return (EXIT_FAILURE);
564 }
565
566 retval = netconf_op(server, conns, session_key, rpc);
567 nc_rpc_free (rpc);
568 return (retval);
569}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200570
Radek Krejci80c10d92012-07-30 08:38:50 +0200571static 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 +0200572{
573 nc_rpc* rpc;
574 int retval = EXIT_SUCCESS;
575
576 /* create requests */
577 rpc = nc_rpc_editconfig(target, defop, erropt, config);
578 if (rpc == NULL) {
579 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
580 return (EXIT_FAILURE);
581 }
582
583 retval = netconf_op(server, conns, session_key, rpc);
584 nc_rpc_free (rpc);
585 return (retval);
586}
587
Radek Krejci80c10d92012-07-30 08:38:50 +0200588static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200589{
590 nc_rpc* rpc;
591 int retval = EXIT_SUCCESS;
592
593 /* create requests */
594 rpc = nc_rpc_killsession(sid);
595 if (rpc == NULL) {
596 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
597 return (EXIT_FAILURE);
598 }
599
600 retval = netconf_op(server, conns, session_key, rpc);
601 nc_rpc_free (rpc);
602 return (retval);
603}
604
Radek Krejci80c10d92012-07-30 08:38:50 +0200605static 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 +0200606{
607 nc_rpc* rpc;
608 int retval = EXIT_SUCCESS;
609
610 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200611 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200612 if (rpc == NULL) {
613 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
614 return (EXIT_FAILURE);
615 }
616
617 retval = netconf_op(server, conns, session_key, rpc);
618 nc_rpc_free (rpc);
619 return (retval);
620}
621
Radek Krejci80c10d92012-07-30 08:38:50 +0200622static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200623{
624 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
625}
626
Radek Krejci80c10d92012-07-30 08:38:50 +0200627static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200628{
629 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
630}
631
Radek Krejci80c10d92012-07-30 08:38:50 +0200632static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200633{
634 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
635}
636
Radek Krejci80c10d92012-07-30 08:38:50 +0200637/**
638 * @return REPLY_OK: 0, *data == NULL
639 * REPLY_DATA: 0, *data != NULL
640 * REPLY_ERROR: 1, *data == NULL
641 */
642static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
643{
644 struct nc_session *session = NULL;
645 nc_reply* reply;
646 nc_rpc* rpc;
647 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100648 NC_MSG_TYPE msgt;
649 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200650
651 /* create requests */
652 rpc = nc_rpc_generic(content);
653 if (rpc == NULL) {
654 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
655 return (EXIT_FAILURE);
656 }
657
Radek Krejcia332b692012-11-12 16:15:54 +0100658 if (data != NULL) {
659 *data = NULL;
660 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200661
662 /* get session where send the RPC */
663 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
664 if (session != NULL) {
665 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100666 msgt = nc_session_send_recv(session, rpc, &reply);
667 nc_rpc_free (rpc);
668
669 /* process the result of the operation */
670 switch (msgt) {
671 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200672 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
673 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
674 netconf_close(server, conns, session_key);
675 return (EXIT_FAILURE);
676 }
Radek Krejcia332b692012-11-12 16:15:54 +0100677 /* no break */
678 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200679 /* there is error handled by callback */
680 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200681 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100682 case NC_MSG_REPLY:
683 switch (replyt = nc_reply_get_type(reply)) {
684 case NC_REPLY_DATA:
685 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
686 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
687 nc_reply_free(reply);
688 return (EXIT_FAILURE);
689 }
690 retval = EXIT_SUCCESS;
691 break;
692 case NC_REPLY_OK:
693 retval = EXIT_SUCCESS;
694 break;
695 default:
696 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
697 retval = EXIT_FAILURE;
698 break;
699 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200700 break;
701 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100702 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200703 retval = EXIT_FAILURE;
704 break;
705 }
706 nc_reply_free(reply);
707
708 return (retval);
709 } else {
710 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
711 return (EXIT_FAILURE);
712 }
713}
714
Radek Krejci469aab82012-07-22 18:42:20 +0200715server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200716void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200717{
Radek Krejci7338bde2012-08-10 12:57:30 +0200718 switch (level) {
719 case NC_VERB_ERROR:
720 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
721 break;
722 case NC_VERB_WARNING:
723 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
724 break;
725 case NC_VERB_VERBOSE:
726 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
727 break;
728 case NC_VERB_DEBUG:
729 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
730 break;
731 }
Radek Krejci469aab82012-07-22 18:42:20 +0200732}
733
David Kupka8e60a372012-09-04 09:15:20 +0200734void * thread_routine (void * arg)
735{
736 void * retval = NULL;
737
738 ssize_t buffer_len;
739 struct pollfd fds;
740 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200741 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200742 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200743 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200744 char* session_key, *data;
745 const char *host, *port, *user, *pass;
746 const char *msgtext, *cpbltstr;
747 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100748 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200749 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200750 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200751 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200752 NC_DATASTORE ds_type_s, ds_type_t;
753 NC_EDIT_DEFOP_TYPE defop_type = 0;
754 NC_EDIT_ERROPT_TYPE erropt_type = 0;
755
756 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
757 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
758 server_rec * server = ((struct pass_to_thread*)arg)->server;
759 int client = ((struct pass_to_thread*)arg)->client;
760
761 char * buffer, chunk_len_str[12], *chunked_msg;
762 char c;
763
764 while (!isterminated) {
765 fds.fd = client;
766 fds.events = POLLIN;
767 fds.revents = 0;
768
769 status = poll(&fds, 1, 1000);
770
771 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
772 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
773 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
774 continue;
775 } else if (status < 0) {
776 /* 0: poll time outed
777 * close socket and ignore this request from the client, it can try it again
778 * -1: poll failed
779 * something wrong happend, close this socket and wait for another request
780 */
781 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
782 close(client);
783 break;
784 }
785 /* status > 0 */
786
787 /* check the status of the socket */
788
789 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
790 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
791 /* close client's socket (it's probably already closed by client */
792 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
793 close(client);
794 break;
795 }
796
797 /* read json in chunked framing */
798 buffer_size = 0;
799 buffer_len = 0;
800 buffer = NULL;
801 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200802 /* read chunk length */
803 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
804 free (buffer);
805 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200806 break;
807 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200808 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
809 free (buffer);
810 buffer = NULL;
811 break;
812 }
813 i=0;
814 memset (chunk_len_str, 0, 12);
815 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
816 if (i==0 && c == '#') {
817 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
818 /* end but invalid */
819 free (buffer);
820 buffer = NULL;
821 }
822 /* end of message, double-loop break */
823 goto msg_complete;
824 }
825 chunk_len_str[i++] = c;
826 }
827 if (c != '\n') {
828 free (buffer);
829 buffer = NULL;
830 break;
831 }
832 if ((chunk_len = atoi (chunk_len_str)) == 0) {
833 free (buffer);
834 buffer = NULL;
835 break;
836 }
837 buffer_size += chunk_len+1;
838 buffer = realloc (buffer, sizeof(char)*buffer_size);
839 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
840 free (buffer);
841 buffer = NULL;
842 break;
843 }
844 buffer_len += ret;
845 }
846msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200847
848 if (buffer != NULL) {
849 request = json_tokener_parse(buffer);
850 operation = json_object_get_int(json_object_object_get(request, "type"));
851
852 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
853 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
854 if (operation != MSG_CONNECT && session_key == NULL) {
855 reply = json_object_new_object();
856 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
857 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
858 msgtext = json_object_to_json_string(reply);
859 send(client, msgtext, strlen(msgtext) + 1, 0);
860 json_object_put(reply);
861 /* there is some stupid client, so close the connection to give a chance to some other client */
862 close(client);
863 break;
864 }
865
866 /* get parameters */
867 ds_type_t = -1;
868 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
869 if (strcmp(target, "running") == 0) {
870 ds_type_t = NC_DATASTORE_RUNNING;
871 } else if (strcmp(target, "startup") == 0) {
872 ds_type_t = NC_DATASTORE_STARTUP;
873 } else if (strcmp(target, "candidate") == 0) {
874 ds_type_t = NC_DATASTORE_CANDIDATE;
875 }
876 }
877 ds_type_s = -1;
878 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
879 if (strcmp(source, "running") == 0) {
880 ds_type_s = NC_DATASTORE_RUNNING;
881 } else if (strcmp(source, "startup") == 0) {
882 ds_type_s = NC_DATASTORE_STARTUP;
883 } else if (strcmp(source, "candidate") == 0) {
884 ds_type_s = NC_DATASTORE_CANDIDATE;
885 }
886 }
887
888 /* null global JSON error-reply */
889 err_reply = NULL;
890
891 /* prepare reply envelope */
892 reply = json_object_new_object();
893
894 /* process required operation */
895 switch (operation) {
896 case MSG_CONNECT:
897 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
898
899 host = json_object_get_string(json_object_object_get(request, "host"));
900 port = json_object_get_string(json_object_object_get(request, "port"));
901 user = json_object_get_string(json_object_object_get(request, "user"));
902 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200903 capabilities = json_object_object_get(request, "capabilities");
904 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200905 cpblts = nc_cpblts_new (NULL);
906 for (i=0; i<len; i++) {
907 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
908 }
909 } else {
910 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
911 }
David Kupka8e60a372012-09-04 09:15:20 +0200912 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
913 if ((host == NULL) || (user == NULL)) {
914 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
915 session_key = NULL;
916 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200917 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
918 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200919 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
920 }
921
922 reply = json_object_new_object();
923 if (session_key == NULL) {
924 /* negative reply */
925 if (err_reply == NULL) {
926 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
927 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100928 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200929 } else {
930 /* use filled err_reply from libnetconf's callback */
931 json_object_put(reply);
932 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100933 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200934 }
935 } else {
936 /* positive reply */
937 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
938 json_object_object_add(reply, "session", json_object_new_string(session_key));
939
940 free(session_key);
941 }
942
943 break;
944 case MSG_GET:
945 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
946
947 filter = json_object_get_string(json_object_object_get(request, "filter"));
948
949 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
950
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200951 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200952 if (err_reply == NULL) {
953 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
954 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
955 } else {
956 /* use filled err_reply from libnetconf's callback */
957 json_object_put(reply);
958 reply = err_reply;
959 }
960 } else {
961 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
962 json_object_object_add(reply, "data", json_object_new_string(data));
963 }
964 break;
965 case MSG_GETCONFIG:
966 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
967
968 filter = json_object_get_string(json_object_object_get(request, "filter"));
969
970 if (ds_type_s == -1) {
971 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
972 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
973 break;
974 }
975
976 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
977 if (err_reply == NULL) {
978 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
979 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
980 } else {
981 /* use filled err_reply from libnetconf's callback */
982 json_object_put(reply);
983 reply = err_reply;
984 }
985 } else {
986 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
987 json_object_object_add(reply, "data", json_object_new_string(data));
988 }
989 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100990 case MSG_GETSCHEMA:
991 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
992 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
993 if (identifier == NULL) {
994 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
995 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
996 break;
997 }
Tomas Cejka94da2c52013-01-08 18:20:30 +0100998 version = json_object_get_string(json_object_object_get(request, "version"));
999 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001000
Tomas Cejka94da2c52013-01-08 18:20:30 +01001001 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001002 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001003 if (err_reply == NULL) {
1004 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1005 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1006 } else {
1007 /* use filled err_reply from libnetconf's callback */
1008 json_object_put(reply);
1009 reply = err_reply;
1010 }
1011 } else {
1012 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1013 json_object_object_add(reply, "data", json_object_new_string(data));
1014 }
1015 break;
David Kupka8e60a372012-09-04 09:15:20 +02001016 case MSG_EDITCONFIG:
1017 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1018
1019 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1020 if (defop != NULL) {
1021 if (strcmp(defop, "merge") == 0) {
1022 defop_type = NC_EDIT_DEFOP_MERGE;
1023 } else if (strcmp(defop, "replace") == 0) {
1024 defop_type = NC_EDIT_DEFOP_REPLACE;
1025 } else if (strcmp(defop, "none") == 0) {
1026 defop_type = NC_EDIT_DEFOP_NONE;
1027 } else {
1028 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1029 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1030 break;
1031 }
1032 } else {
1033 defop_type = 0;
1034 }
1035
1036 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1037 if (erropt != NULL) {
1038 if (strcmp(erropt, "continue-on-error") == 0) {
1039 erropt_type = NC_EDIT_ERROPT_CONT;
1040 } else if (strcmp(erropt, "stop-on-error") == 0) {
1041 erropt_type = NC_EDIT_ERROPT_STOP;
1042 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1043 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1044 } else {
1045 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1046 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1047 break;
1048 }
1049 } else {
1050 erropt_type = 0;
1051 }
1052
1053 if (ds_type_t == -1) {
1054 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1055 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1056 break;
1057 }
1058
1059 config = json_object_get_string(json_object_object_get(request, "config"));
1060 if (config == NULL) {
1061 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1062 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1063 break;
1064 }
1065
1066 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1067 if (err_reply == NULL) {
1068 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1069 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1070 } else {
1071 /* use filled err_reply from libnetconf's callback */
1072 json_object_put(reply);
1073 reply = err_reply;
1074 }
1075 } else {
1076 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1077 }
1078 break;
1079 case MSG_COPYCONFIG:
1080 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1081 config = NULL;
1082
1083 if (source == NULL) {
1084 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001085 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001086 config = json_object_get_string(json_object_object_get(request, "config"));
1087 } else if (ds_type_s == -1) {
1088 /* source datastore specified, but it is invalid */
1089 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1090 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1091 break;
1092 }
1093
1094 if (ds_type_t == -1) {
1095 /* invalid target datastore specified */
1096 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1097 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1098 break;
1099 }
1100
1101 if (source == NULL && config == NULL) {
1102 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1103 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1104 } else {
1105 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1106 if (err_reply == NULL) {
1107 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1108 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1109 } else {
1110 /* use filled err_reply from libnetconf's callback */
1111 json_object_put(reply);
1112 reply = err_reply;
1113 }
1114 } else {
1115 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1116 }
1117 }
1118 break;
1119 case MSG_DELETECONFIG:
1120 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1121 /* no break - unifying code */
1122 case MSG_LOCK:
1123 if (operation == MSG_LOCK) {
1124 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1125 }
1126 /* no break - unifying code */
1127 case MSG_UNLOCK:
1128 if (operation == MSG_UNLOCK) {
1129 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1130 }
1131
1132 if (ds_type_t == -1) {
1133 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1134 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1135 break;
1136 }
1137
1138 switch(operation) {
1139 case MSG_DELETECONFIG:
1140 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1141 break;
1142 case MSG_LOCK:
1143 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1144 break;
1145 case MSG_UNLOCK:
1146 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1147 break;
1148 default:
1149 status = -1;
1150 break;
1151 }
1152
1153 if (status != EXIT_SUCCESS) {
1154 if (err_reply == NULL) {
1155 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1156 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1157 } else {
1158 /* use filled err_reply from libnetconf's callback */
1159 json_object_put(reply);
1160 reply = err_reply;
1161 }
1162 } else {
1163 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1164 }
1165 break;
1166 case MSG_KILL:
1167 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1168
1169 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1170
1171 if (sid == NULL) {
1172 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1173 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1174 break;
1175 }
1176
1177 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1178 if (err_reply == NULL) {
1179 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1180 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1181 } else {
1182 /* use filled err_reply from libnetconf's callback */
1183 json_object_put(reply);
1184 reply = err_reply;
1185 }
1186 } else {
1187 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1188 }
1189 break;
1190 case MSG_DISCONNECT:
1191 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1192
1193 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1194 if (err_reply == NULL) {
1195 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1196 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1197 } else {
1198 /* use filled err_reply from libnetconf's callback */
1199 json_object_put(reply);
1200 reply = err_reply;
1201 }
1202 } else {
1203 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1204 }
1205 break;
1206 case MSG_INFO:
1207 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1208
Kupka Davidda134a12012-09-06 14:12:16 +02001209 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1210 if (locked_session != NULL) {
1211 session = locked_session->session;
1212 } else {
1213 session = NULL;
1214 }
David Kupka8e60a372012-09-04 09:15:20 +02001215 if (session != NULL) {
1216 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1217 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1218 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1219 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1220 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1221 cpblts = nc_session_get_cpblts (session);
1222 if (cpblts != NULL) {
1223 json_obj = json_object_new_array();
1224 nc_cpblts_iter_start (cpblts);
1225 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1226 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1227 }
1228 json_object_object_add(reply, "capabilities", json_obj);
1229 }
1230 } else {
1231 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1232 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1233 }
1234
1235 break;
1236 case MSG_GENERIC:
1237 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1238
1239 config = json_object_get_string(json_object_object_get(request, "content"));
1240
1241 if (config == NULL) {
1242 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1243 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1244 break;
1245 }
1246
1247 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1248 if (err_reply == NULL) {
1249 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1250 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1251 } else {
1252 /* use filled err_reply from libnetconf's callback */
1253 json_object_put(reply);
1254 reply = err_reply;
1255 }
1256 } else {
1257 if (data == NULL) {
1258 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1259 } else {
1260 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1261 json_object_object_add(reply, "data", json_object_new_string(data));
1262 }
1263 }
1264 break;
1265 default:
1266 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1267 reply = json_object_new_object();
1268 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1269 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1270 break;
1271 }
1272 json_object_put(request);
1273
David Kupka1e3e4c82012-09-04 09:32:15 +02001274 /* send reply to caller */
1275 if (reply != NULL) {
1276 msgtext = json_object_to_json_string(reply);
1277 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1278 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001279 break;
1280 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001281 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1282 json_object_put(reply);
1283 free (chunked_msg);
1284 free (buffer);
1285 } else {
1286 break;
David Kupka8e60a372012-09-04 09:15:20 +02001287 }
1288 }
1289 }
1290
1291 free (arg);
1292
1293 return retval;
1294}
1295
Radek Krejcif23850c2012-07-23 16:14:17 +02001296/*
1297 * This is actually implementation of NETCONF client
1298 * - requests are received from UNIX socket in the predefined format
1299 * - results are replied through the same way
1300 * - the daemon run as a separate process, but it is started and stopped
1301 * automatically by Apache.
1302 *
1303 */
Radek Krejci469aab82012-07-22 18:42:20 +02001304static void forked_proc(apr_pool_t * pool, server_rec * server)
1305{
1306 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001307 int lsock, client, ret, i, pthread_count = 0;
1308 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001309 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001310 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001311 struct pass_to_thread * arg;
1312 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1313 struct timespec maxtime;
1314 pthread_rwlockattr_t lock_attrs;
1315
1316 /* wait at most 5 secons for every thread to terminate */
1317 maxtime.tv_sec = 5;
1318 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001319
Radek Krejcif23850c2012-07-23 16:14:17 +02001320 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001321 unixd_setup_child();
1322
Radek Krejciae021c12012-07-25 18:03:52 +02001323 cfg = ap_get_module_config(server->module_config, &netconf_module);
1324 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001325 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1326 return;
1327 }
Radek Krejci469aab82012-07-22 18:42:20 +02001328
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001329 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001330 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1331 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1332 return;
1333 }
1334
1335 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001336 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001337 unlink(local.sun_path);
1338 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1339
1340 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1341 if (errno == EADDRINUSE) {
1342 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1343 close(lsock);
1344 exit(0);
1345 }
1346 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1347 close(lsock);
1348 return;
1349 }
1350
1351 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1352 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1353 close(lsock);
1354 return;
1355 }
1356
1357 /* prepare internal lists */
1358 netconf_sessions_list = apr_hash_make(pool);
1359
1360 /* setup libnetconf's callbacks */
1361 nc_verbosity(NC_VERB_DEBUG);
1362 clb_print_server = server;
1363 nc_callback_print(clb_print);
1364 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1365 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1366 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001367 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001368
1369 /* disable publickey authentication */
1370 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1371
David Kupka8e60a372012-09-04 09:15:20 +02001372 /* create mutex protecting session list */
1373 pthread_rwlockattr_init(&lock_attrs);
1374 /* rwlock is shared only with threads in this process */
1375 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1376 /* create rw lock */
1377 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1378 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1379 close (lsock);
1380 return;
1381 }
1382
Radek Krejci469aab82012-07-22 18:42:20 +02001383 while (isterminated == 0) {
1384 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1385
1386 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001387 len = sizeof(remote);
1388 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001389 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1390 apr_sleep(SLEEP_TIME);
1391 continue;
1392 } else if (client == -1 && (errno == EINTR)) {
1393 continue;
1394 } else if (client == -1) {
1395 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1396 continue;
1397 }
Radek Krejci469aab82012-07-22 18:42:20 +02001398
1399 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001400 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001401
David Kupka8e60a372012-09-04 09:15:20 +02001402 arg = malloc (sizeof(struct pass_to_thread));
1403 arg->client = client;
1404 arg->pool = pool;
1405 arg->server = server;
1406 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001407
David Kupka8e60a372012-09-04 09:15:20 +02001408 /* start new thread. It will serve this particular request and then terminate */
1409 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1410 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1411 } else {
1412 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1413 pthread_count++;
1414 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1415 ptids[pthread_count] = 0;
1416 }
Radek Krejci469aab82012-07-22 18:42:20 +02001417
David Kupka8e60a372012-09-04 09:15:20 +02001418 /* check if some thread already terminated, free some resources by joining it */
1419 for (i=0; i<pthread_count; i++) {
1420 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1421 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1422 pthread_count--;
1423 if (pthread_count > 0) {
1424 /* place last Thread ID on the place of joined one */
1425 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001426 }
Radek Krejci469aab82012-07-22 18:42:20 +02001427 }
1428 }
David Kupka8e60a372012-09-04 09:15:20 +02001429 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001430 }
1431
David Kupka8e60a372012-09-04 09:15:20 +02001432 /* join all threads */
1433 for (i=0; i<pthread_count; i++) {
1434 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1435 }
1436 free (ptids);
1437
Radek Krejci469aab82012-07-22 18:42:20 +02001438 close(lsock);
1439
David Kupka8e60a372012-09-04 09:15:20 +02001440 /* destroy rwlock */
1441 pthread_rwlock_destroy(&session_lock);
1442 pthread_rwlockattr_destroy(&lock_attrs);
1443
Radek Krejci469aab82012-07-22 18:42:20 +02001444 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1445
1446 exit(APR_SUCCESS);
1447}
1448
Radek Krejcif23850c2012-07-23 16:14:17 +02001449static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001450{
Radek Krejcif23850c2012-07-23 16:14:17 +02001451 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1452
1453 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1454 apr_pool_create(&config->pool, pool);
1455 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001456 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001457
1458 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001459}
1460
1461static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1462 apr_pool_t * plog, server_rec * s)
1463{
Radek Krejcif23850c2012-07-23 16:14:17 +02001464 mod_netconf_cfg *config;
1465 apr_status_t res;
1466
Radek Krejci469aab82012-07-22 18:42:20 +02001467 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001468 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001469 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001470
1471 /*
1472 * The following checks if this routine has been called before.
1473 * This is necessary because the parent process gets initialized
1474 * a couple of times as the server starts up.
1475 */
1476 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1477 if (!data) {
1478 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1479 return (OK);
1480 }
1481
Radek Krejcif23850c2012-07-23 16:14:17 +02001482 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1483 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001484
Radek Krejcif23850c2012-07-23 16:14:17 +02001485 if (config && config->forkproc == NULL) {
1486 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1487 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001488 switch (res) {
1489 case APR_INCHILD:
1490 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001491 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001492 apr_signal(SIGTERM, signal_handler);
1493
1494 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001495 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001496
1497 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001498 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001499
Radek Krejcif23850c2012-07-23 16:14:17 +02001500 /* I never should be here, wtf?!? */
1501 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001502 exit(APR_EGENERAL);
1503 break;
1504 case APR_INPARENT:
1505 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001506 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001507 break;
1508 default:
1509 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1510 break;
1511 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001512 } else {
1513 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001514 }
1515
1516 return OK;
1517}
1518
Radek Krejci469aab82012-07-22 18:42:20 +02001519/**
1520 * Register module hooks
1521 */
1522static void mod_netconf_register_hooks(apr_pool_t * p)
1523{
Radek Krejcif23850c2012-07-23 16:14:17 +02001524 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001525}
1526
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001527static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1528{
1529 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1530 return NULL;
1531}
1532
1533static const command_rec netconf_cmds[] = {
1534 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1535 {NULL}
1536};
1537
Radek Krejci469aab82012-07-22 18:42:20 +02001538/* Dispatch list for API hooks */
1539module AP_MODULE_DECLARE_DATA netconf_module = {
1540 STANDARD20_MODULE_STUFF,
1541 NULL, /* create per-dir config structures */
1542 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001543 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001544 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001545 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001546 mod_netconf_register_hooks /* register hooks */
1547};
Radek Krejcia332b692012-11-12 16:15:54 +01001548