blob: e09b4a6ca1d14443137720bfad38ffdaa6f630bd [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 */
46
Radek Krejci7b4ddd02012-07-30 08:09:58 +020047#include <unistd.h>
48#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020049#include <sys/types.h>
50#include <sys/socket.h>
51#include <sys/un.h>
David Kupka8e60a372012-09-04 09:15:20 +020052#include <pthread.h>
53#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020054
55#include <unixd.h>
56#include <httpd.h>
57#include <http_log.h>
58#include <http_config.h>
59
60#include <apr_sha1.h>
61#include <apr_hash.h>
62#include <apr_signal.h>
63#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020064
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020065#include <json/json.h>
66
Radek Krejci469aab82012-07-22 18:42:20 +020067#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020068
Tomas Cejka94da2c52013-01-08 18:20:30 +010069#include "message_type.h"
70
Radek Krejci469aab82012-07-22 18:42:20 +020071
72#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020073#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020074#define MAX_SOCKET_CL 10
75#define BUFFER_SIZE 4096
76
77/* sleep in master process for non-blocking socket reading */
78#define SLEEP_TIME 200
79
80#ifndef offsetof
81#define offsetof(type, member) ((size_t) ((type *) 0)->member)
82#endif
83
Tomas Cejka027f3bc2012-11-10 20:28:36 +010084/* timeout in msec */
Tomas Cejka027f3bc2012-11-10 20:28:36 +010085#define NCWITHDEFAULTS NCWD_MODE_DISABLED
86
Radek Krejci469aab82012-07-22 18:42:20 +020087struct timeval timeout = { 1, 0 };
88
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020089
Radek Krejci469aab82012-07-22 18:42:20 +020090#define MSG_OK 0
91#define MSG_OPEN 1
92#define MSG_DATA 2
93#define MSG_CLOSE 3
94#define MSG_ERROR 4
95#define MSG_UNKNOWN 5
96
Radek Krejci469aab82012-07-22 18:42:20 +020097module AP_MODULE_DECLARE_DATA netconf_module;
98
99typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200100 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200101 apr_proc_t *forkproc;
102 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200103} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200104
David Kupka8e60a372012-09-04 09:15:20 +0200105struct pass_to_thread {
106 int client; /**< opened socket */
107 apr_pool_t * pool; /**< ?? */
108 server_rec * server; /**< ?? */
109 apr_hash_t * netconf_sessions_list; /**< ?? */
110};
111
112struct session_with_mutex {
113 struct nc_session * session; /**< netconf session */
114 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
115};
116
117pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
118
Radek Krejci469aab82012-07-22 18:42:20 +0200119volatile int isterminated = 0;
120
121static char* password;
122
123
124static void signal_handler(int sign)
125{
126 switch (sign) {
127 case SIGTERM:
128 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200129 break;
130 }
131}
132
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200133static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200134{
Radek Krejcif23850c2012-07-23 16:14:17 +0200135 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
136 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200137 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200138
Radek Krejci469aab82012-07-22 18:42:20 +0200139 apr_sha1_ctx_t sha1_ctx;
140 apr_sha1_init(&sha1_ctx);
141 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
142 apr_sha1_update(&sha1_ctx, port, strlen(port));
143 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200144 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200145
Radek Krejcif23850c2012-07-23 16:14:17 +0200146 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200147 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200148 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200150 }
151 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200152
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200153 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200154}
155
156int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
157{
158 /* always approve */
159 return (EXIT_SUCCESS);
160}
161
162char* netconf_callback_sshauth_password (const char* username, const char* hostname)
163{
164 char* buf;
165
166 buf = malloc ((strlen(password) + 1) * sizeof(char));
167 apr_cpystrn(buf, password, strlen(password) + 1);
168
169 return (buf);
170}
171
172void netconf_callback_sshauth_interactive (const char* name,
173 int name_len,
174 const char* instruction,
175 int instruction_len,
176 int num_prompts,
177 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
178 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
179 void** abstract)
180{
181 int i;
182
183 for (i=0; i<num_prompts; i++) {
184 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
185 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
186 responses[i].length = strlen(responses[i].text) + 1;
187 }
188
189 return;
190}
191
Radek Krejcic11fd862012-07-26 12:41:21 +0200192static json_object *err_reply = NULL;
193void netconf_callback_error_process(const char* tag,
194 const char* type,
195 const char* severity,
196 const char* apptag,
197 const char* path,
198 const char* message,
199 const char* attribute,
200 const char* element,
201 const char* ns,
202 const char* sid)
203{
204 err_reply = json_object_new_object();
205 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
206 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
207 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
208 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
209 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
210 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
211 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
212 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
213 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
214 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
215 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
216}
217
Kupka David00b9c5c2012-09-05 09:45:50 +0200218static 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 +0200219{
Radek Krejci469aab82012-07-22 18:42:20 +0200220 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200221 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200222 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200223
224 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200225 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200226 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
227
Radek Krejci469aab82012-07-22 18:42:20 +0200228 /* if connected successful, add session to the list */
229 if (session != NULL) {
230 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200231 session_key = gen_ncsession_hash(
232 (host==NULL) ? "localhost" : host,
233 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200234 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200235
236 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
237 nc_session_free(session);
238 free (locked_session);
239 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
240 return NULL;
241 }
242 locked_session->session = session;
243 pthread_mutex_init (&locked_session->lock, NULL);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100244 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_lock");
David Kupka8e60a372012-09-04 09:15:20 +0200245 /* get exclusive access to sessions_list (conns) */
246 if (pthread_rwlock_wrlock (&session_lock) != 0) {
247 nc_session_free(session);
248 free (locked_session);
249 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
250 return NULL;
251 }
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100252 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Add connection to the list");
David Kupka8e60a372012-09-04 09:15:20 +0200253 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100254 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "Before session_unlock");
David Kupka8e60a372012-09-04 09:15:20 +0200255 /* end of critical section */
256 if (pthread_rwlock_unlock (&session_lock) != 0) {
257 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
258 return NULL;
259 }
Radek Krejci469aab82012-07-22 18:42:20 +0200260 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200261 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200262 } else {
263 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200264 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200265 }
266
Radek Krejci469aab82012-07-22 18:42:20 +0200267}
268
Radek Krejci80c10d92012-07-30 08:38:50 +0200269static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200270{
271 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200272 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200273
Radek Krejcif23850c2012-07-23 16:14:17 +0200274 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200275 /* get exclusive (write) access to sessions_list (conns) */
276 if (pthread_rwlock_wrlock (&session_lock) != 0) {
277 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
278 return EXIT_FAILURE;
279 }
280 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
281 if (locked_session != NULL) {
282 pthread_mutex_destroy(&locked_session->lock);
283 ns = locked_session->session;
284 free (locked_session);
285 }
Radek Krejci469aab82012-07-22 18:42:20 +0200286 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100287 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200288 nc_session_free (ns);
289 ns = NULL;
290
291 /* remove session from the active sessions list */
292 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200293 /* end of critical section */
294 if (pthread_rwlock_unlock (&session_lock) != 0) {
295 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
296 return EXIT_FAILURE;
297 }
Radek Krejci469aab82012-07-22 18:42:20 +0200298 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200299
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200300 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200301 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200302 if (pthread_rwlock_unlock (&session_lock) != 0) {
303 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
304 return EXIT_FAILURE;
305 }
Radek Krejci469aab82012-07-22 18:42:20 +0200306 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200307 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200308 }
309}
310
Radek Krejci80c10d92012-07-30 08:38:50 +0200311static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200312{
313 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200314 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200315 nc_reply* reply;
316 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100317 NC_MSG_TYPE msgt;
318 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200319
Radek Krejci8e4632a2012-07-26 13:40:34 +0200320 /* check requests */
321 if (rpc == NULL) {
322 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
323 return (EXIT_FAILURE);
324 }
325
David Kupka8e60a372012-09-04 09:15:20 +0200326 /* get non-exclusive (read) access to sessions_list (conns) */
327 if (pthread_rwlock_rdlock (&session_lock) != 0) {
328 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
329 return EXIT_FAILURE;
330 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200331 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200332 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
333 if (locked_session != NULL) {
334 session = locked_session->session;
335 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200336 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200337 /* get exclusive access to session */
338 if (pthread_mutex_lock(&locked_session->lock) != 0) {
339 /* unlock before returning error */
340 if (pthread_rwlock_unlock (&session_lock) != 0) {
341 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
342 return EXIT_FAILURE;
343 }
344 return EXIT_FAILURE;
345 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200346 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100347 msgt = nc_session_send_recv(session, rpc, &reply);
348
David Kupka8e60a372012-09-04 09:15:20 +0200349 /* first release exclusive lock for this session */
350 pthread_mutex_unlock(&locked_session->lock);
351 /* end of critical section */
352 if (pthread_rwlock_unlock (&session_lock) != 0) {
353 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
354 return EXIT_FAILURE;
355 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200356
Radek Krejcia332b692012-11-12 16:15:54 +0100357 /* process the result of the operation */
358 switch (msgt) {
359 case NC_MSG_UNKNOWN:
360 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
362 netconf_close(server, conns, session_key);
363 return (EXIT_FAILURE);
364 }
365 /* no break */
366 case NC_MSG_NONE:
367 /* there is error handled by callback */
368 return (EXIT_FAILURE);
369 break;
370 case NC_MSG_REPLY:
371 switch (replyt = nc_reply_get_type(reply)) {
372 case NC_REPLY_OK:
373 retval = EXIT_SUCCESS;
374 break;
375 default:
376 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
377 retval = EXIT_FAILURE;
378 break;
379 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200380 break;
381 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100382 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200383 retval = EXIT_FAILURE;
384 break;
385 }
386 nc_reply_free(reply);
387 return (retval);
388 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100389 /* release lock on failure */
390 if (pthread_rwlock_unlock (&session_lock) != 0) {
391 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
392 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200393 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
394 return (EXIT_FAILURE);
395 }
396}
Radek Krejci80c10d92012-07-30 08:38:50 +0200397
398static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200399{
400 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200401 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100402 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200403 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100404 NC_MSG_TYPE msgt;
405 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200406
407 /* check requests */
408 if (rpc == NULL) {
409 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
410 return (NULL);
411 }
412
David Kupka8e60a372012-09-04 09:15:20 +0200413 /* get non-exclusive (read) access to sessions_list (conns) */
414 if (pthread_rwlock_rdlock (&session_lock) != 0) {
415 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
416 return NULL;
417 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200418 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200419 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
420 if (locked_session != NULL) {
421 session = locked_session->session;
422 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200423 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200424 /* get exclusive access to session */
425 if (pthread_mutex_lock(&locked_session->lock) != 0) {
426 /* unlock before returning error */
427 if (pthread_rwlock_unlock (&session_lock) != 0) {
428 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
429 return NULL;
430 }
431 return NULL;
432 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200433 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100434 msgt = nc_session_send_recv(session, rpc, &reply);
435
David Kupka8e60a372012-09-04 09:15:20 +0200436 /* first release exclusive lock for this session */
437 pthread_mutex_unlock(&locked_session->lock);
438 /* end of critical section */
439 if (pthread_rwlock_unlock (&session_lock) != 0) {
440 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 +0100441 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200442 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200443
Radek Krejcia332b692012-11-12 16:15:54 +0100444 /* process the result of the operation */
445 switch (msgt) {
446 case NC_MSG_UNKNOWN:
447 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
448 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
449 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200450 return (NULL);
451 }
Radek Krejcia332b692012-11-12 16:15:54 +0100452 /* no break */
453 case NC_MSG_NONE:
454 /* there is error handled by callback */
455 return (NULL);
456 break;
457 case NC_MSG_REPLY:
458 switch (replyt = nc_reply_get_type(reply)) {
459 case NC_REPLY_DATA:
460 if ((data = nc_reply_get_data (reply)) == NULL) {
461 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
462 data = NULL;
463 }
464 break;
465 default:
466 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
467 data = NULL;
468 break;
469 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200470 break;
471 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100472 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
473 data = NULL;
474 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200475 }
476 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200477 return (data);
478 } else {
Tomas Cejkabcdc1142012-11-14 01:12:43 +0100479 /* release lock on failure */
480 if (pthread_rwlock_unlock (&session_lock) != 0) {
481 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
482 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200483 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
484 return (NULL);
485 }
486}
487
Radek Krejci80c10d92012-07-30 08:38:50 +0200488static 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 +0200489{
490 nc_rpc* rpc;
491 struct nc_filter *f = NULL;
492 char* data = NULL;
493
494 /* create filter if set */
495 if (filter != NULL) {
496 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
497 }
498
499 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100500 rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200501 nc_filter_free(f);
502 if (rpc == NULL) {
503 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
504 return (NULL);
505 }
506
507 data = netconf_opdata(server, conns, session_key, rpc);
508 nc_rpc_free (rpc);
509 return (data);
510}
511
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100512static char* netconf_getschema(server_rec* server, apr_hash_t* conns, const char* session_key, const char* identifier, const char* version, const char* format)
513{
514 nc_rpc* rpc;
515 char* data = NULL;
516
517 /* create requests */
Tomas Cejka94da2c52013-01-08 18:20:30 +0100518 rpc = nc_rpc_getschema(identifier, version, format);
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100519 if (rpc == NULL) {
520 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
521 return (NULL);
522 }
523
524 data = netconf_opdata(server, conns, session_key, rpc);
525 nc_rpc_free (rpc);
526 return (data);
527}
528
Radek Krejci80c10d92012-07-30 08:38:50 +0200529static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200530{
531 nc_rpc* rpc;
532 struct nc_filter *f = NULL;
533 char* data = NULL;
534
535 /* create filter if set */
536 if (filter != NULL) {
537 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
538 }
539
540 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100541 rpc = nc_rpc_get (f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200542 nc_filter_free(f);
543 if (rpc == NULL) {
544 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
545 return (NULL);
546 }
547
548 data = netconf_opdata(server, conns, session_key, rpc);
549 nc_rpc_free (rpc);
550 return (data);
551}
552
Radek Krejci80c10d92012-07-30 08:38:50 +0200553static 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 +0200554{
555 nc_rpc* rpc;
556 int retval = EXIT_SUCCESS;
557
558 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100559 rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200560 if (rpc == NULL) {
561 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
562 return (EXIT_FAILURE);
563 }
564
565 retval = netconf_op(server, conns, session_key, rpc);
566 nc_rpc_free (rpc);
567 return (retval);
568}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200569
Radek Krejci80c10d92012-07-30 08:38:50 +0200570static 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 +0200571{
572 nc_rpc* rpc;
573 int retval = EXIT_SUCCESS;
574
575 /* create requests */
576 rpc = nc_rpc_editconfig(target, defop, erropt, config);
577 if (rpc == NULL) {
578 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
579 return (EXIT_FAILURE);
580 }
581
582 retval = netconf_op(server, conns, session_key, rpc);
583 nc_rpc_free (rpc);
584 return (retval);
585}
586
Radek Krejci80c10d92012-07-30 08:38:50 +0200587static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200588{
589 nc_rpc* rpc;
590 int retval = EXIT_SUCCESS;
591
592 /* create requests */
593 rpc = nc_rpc_killsession(sid);
594 if (rpc == NULL) {
595 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
596 return (EXIT_FAILURE);
597 }
598
599 retval = netconf_op(server, conns, session_key, rpc);
600 nc_rpc_free (rpc);
601 return (retval);
602}
603
Radek Krejci80c10d92012-07-30 08:38:50 +0200604static 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 +0200605{
606 nc_rpc* rpc;
607 int retval = EXIT_SUCCESS;
608
609 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200610 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200611 if (rpc == NULL) {
612 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
613 return (EXIT_FAILURE);
614 }
615
616 retval = netconf_op(server, conns, session_key, rpc);
617 nc_rpc_free (rpc);
618 return (retval);
619}
620
Radek Krejci80c10d92012-07-30 08:38:50 +0200621static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200622{
623 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
624}
625
Radek Krejci80c10d92012-07-30 08:38:50 +0200626static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200627{
628 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
629}
630
Radek Krejci80c10d92012-07-30 08:38:50 +0200631static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200632{
633 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
634}
635
Radek Krejci80c10d92012-07-30 08:38:50 +0200636/**
637 * @return REPLY_OK: 0, *data == NULL
638 * REPLY_DATA: 0, *data != NULL
639 * REPLY_ERROR: 1, *data == NULL
640 */
641static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
642{
643 struct nc_session *session = NULL;
644 nc_reply* reply;
645 nc_rpc* rpc;
646 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100647 NC_MSG_TYPE msgt;
648 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200649
650 /* create requests */
651 rpc = nc_rpc_generic(content);
652 if (rpc == NULL) {
653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
654 return (EXIT_FAILURE);
655 }
656
Radek Krejcia332b692012-11-12 16:15:54 +0100657 if (data != NULL) {
658 *data = NULL;
659 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200660
661 /* get session where send the RPC */
662 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
663 if (session != NULL) {
664 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100665 msgt = nc_session_send_recv(session, rpc, &reply);
666 nc_rpc_free (rpc);
667
668 /* process the result of the operation */
669 switch (msgt) {
670 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200671 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
672 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
673 netconf_close(server, conns, session_key);
674 return (EXIT_FAILURE);
675 }
Radek Krejcia332b692012-11-12 16:15:54 +0100676 /* no break */
677 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200678 /* there is error handled by callback */
679 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200680 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100681 case NC_MSG_REPLY:
682 switch (replyt = nc_reply_get_type(reply)) {
683 case NC_REPLY_DATA:
684 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
685 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
686 nc_reply_free(reply);
687 return (EXIT_FAILURE);
688 }
689 retval = EXIT_SUCCESS;
690 break;
691 case NC_REPLY_OK:
692 retval = EXIT_SUCCESS;
693 break;
694 default:
695 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
696 retval = EXIT_FAILURE;
697 break;
698 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200699 break;
700 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100701 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200702 retval = EXIT_FAILURE;
703 break;
704 }
705 nc_reply_free(reply);
706
707 return (retval);
708 } else {
709 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
710 return (EXIT_FAILURE);
711 }
712}
713
Radek Krejci469aab82012-07-22 18:42:20 +0200714server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200715void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200716{
Radek Krejci7338bde2012-08-10 12:57:30 +0200717 switch (level) {
718 case NC_VERB_ERROR:
719 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
720 break;
721 case NC_VERB_WARNING:
722 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
723 break;
724 case NC_VERB_VERBOSE:
725 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
726 break;
727 case NC_VERB_DEBUG:
728 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
729 break;
730 }
Radek Krejci469aab82012-07-22 18:42:20 +0200731}
732
David Kupka8e60a372012-09-04 09:15:20 +0200733void * thread_routine (void * arg)
734{
735 void * retval = NULL;
736
737 ssize_t buffer_len;
738 struct pollfd fds;
739 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200740 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200741 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200742 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200743 char* session_key, *data;
744 const char *host, *port, *user, *pass;
745 const char *msgtext, *cpbltstr;
746 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Tomas Cejka94da2c52013-01-08 18:20:30 +0100747 const char *identifier, *version, *format;
David Kupka8e60a372012-09-04 09:15:20 +0200748 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200749 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200750 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200751 NC_DATASTORE ds_type_s, ds_type_t;
752 NC_EDIT_DEFOP_TYPE defop_type = 0;
753 NC_EDIT_ERROPT_TYPE erropt_type = 0;
754
755 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
756 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
757 server_rec * server = ((struct pass_to_thread*)arg)->server;
758 int client = ((struct pass_to_thread*)arg)->client;
759
760 char * buffer, chunk_len_str[12], *chunked_msg;
761 char c;
762
763 while (!isterminated) {
764 fds.fd = client;
765 fds.events = POLLIN;
766 fds.revents = 0;
767
768 status = poll(&fds, 1, 1000);
769
770 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
771 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
772 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
773 continue;
774 } else if (status < 0) {
775 /* 0: poll time outed
776 * close socket and ignore this request from the client, it can try it again
777 * -1: poll failed
778 * something wrong happend, close this socket and wait for another request
779 */
780 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
781 close(client);
782 break;
783 }
784 /* status > 0 */
785
786 /* check the status of the socket */
787
788 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
789 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
790 /* close client's socket (it's probably already closed by client */
791 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
792 close(client);
793 break;
794 }
795
796 /* read json in chunked framing */
797 buffer_size = 0;
798 buffer_len = 0;
799 buffer = NULL;
800 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200801 /* read chunk length */
802 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
803 free (buffer);
804 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200805 break;
806 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200807 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
808 free (buffer);
809 buffer = NULL;
810 break;
811 }
812 i=0;
813 memset (chunk_len_str, 0, 12);
814 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
815 if (i==0 && c == '#') {
816 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
817 /* end but invalid */
818 free (buffer);
819 buffer = NULL;
820 }
821 /* end of message, double-loop break */
822 goto msg_complete;
823 }
824 chunk_len_str[i++] = c;
825 }
826 if (c != '\n') {
827 free (buffer);
828 buffer = NULL;
829 break;
830 }
831 if ((chunk_len = atoi (chunk_len_str)) == 0) {
832 free (buffer);
833 buffer = NULL;
834 break;
835 }
836 buffer_size += chunk_len+1;
837 buffer = realloc (buffer, sizeof(char)*buffer_size);
838 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
839 free (buffer);
840 buffer = NULL;
841 break;
842 }
843 buffer_len += ret;
844 }
845msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200846
847 if (buffer != NULL) {
848 request = json_tokener_parse(buffer);
849 operation = json_object_get_int(json_object_object_get(request, "type"));
850
851 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
852 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
853 if (operation != MSG_CONNECT && session_key == NULL) {
854 reply = json_object_new_object();
855 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
856 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
857 msgtext = json_object_to_json_string(reply);
858 send(client, msgtext, strlen(msgtext) + 1, 0);
859 json_object_put(reply);
860 /* there is some stupid client, so close the connection to give a chance to some other client */
861 close(client);
862 break;
863 }
864
865 /* get parameters */
866 ds_type_t = -1;
867 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
868 if (strcmp(target, "running") == 0) {
869 ds_type_t = NC_DATASTORE_RUNNING;
870 } else if (strcmp(target, "startup") == 0) {
871 ds_type_t = NC_DATASTORE_STARTUP;
872 } else if (strcmp(target, "candidate") == 0) {
873 ds_type_t = NC_DATASTORE_CANDIDATE;
874 }
875 }
876 ds_type_s = -1;
877 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
878 if (strcmp(source, "running") == 0) {
879 ds_type_s = NC_DATASTORE_RUNNING;
880 } else if (strcmp(source, "startup") == 0) {
881 ds_type_s = NC_DATASTORE_STARTUP;
882 } else if (strcmp(source, "candidate") == 0) {
883 ds_type_s = NC_DATASTORE_CANDIDATE;
884 }
885 }
886
887 /* null global JSON error-reply */
888 err_reply = NULL;
889
890 /* prepare reply envelope */
891 reply = json_object_new_object();
892
893 /* process required operation */
894 switch (operation) {
895 case MSG_CONNECT:
896 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
897
898 host = json_object_get_string(json_object_object_get(request, "host"));
899 port = json_object_get_string(json_object_object_get(request, "port"));
900 user = json_object_get_string(json_object_object_get(request, "user"));
901 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200902 capabilities = json_object_object_get(request, "capabilities");
903 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200904 cpblts = nc_cpblts_new (NULL);
905 for (i=0; i<len; i++) {
906 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
907 }
908 } else {
909 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
910 }
David Kupka8e60a372012-09-04 09:15:20 +0200911 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
912 if ((host == NULL) || (user == NULL)) {
913 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
914 session_key = NULL;
915 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200916 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
917 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200918 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
919 }
920
921 reply = json_object_new_object();
922 if (session_key == NULL) {
923 /* negative reply */
924 if (err_reply == NULL) {
925 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
926 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
Tomas Cejka1490ef12012-12-10 00:16:13 +0100927 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connection failed.");
David Kupka8e60a372012-09-04 09:15:20 +0200928 } else {
929 /* use filled err_reply from libnetconf's callback */
930 json_object_put(reply);
931 reply = err_reply;
Tomas Cejka1490ef12012-12-10 00:16:13 +0100932 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Connect - error from libnetconf's callback.");
David Kupka8e60a372012-09-04 09:15:20 +0200933 }
934 } else {
935 /* positive reply */
936 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
937 json_object_object_add(reply, "session", json_object_new_string(session_key));
938
939 free(session_key);
940 }
941
942 break;
943 case MSG_GET:
944 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
945
946 filter = json_object_get_string(json_object_object_get(request, "filter"));
947
948 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
949
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200950 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200951 if (err_reply == NULL) {
952 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
953 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
954 } else {
955 /* use filled err_reply from libnetconf's callback */
956 json_object_put(reply);
957 reply = err_reply;
958 }
959 } else {
960 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
961 json_object_object_add(reply, "data", json_object_new_string(data));
962 }
963 break;
964 case MSG_GETCONFIG:
965 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
966
967 filter = json_object_get_string(json_object_object_get(request, "filter"));
968
969 if (ds_type_s == -1) {
970 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
971 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
972 break;
973 }
974
975 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
976 if (err_reply == NULL) {
977 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
978 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
979 } else {
980 /* use filled err_reply from libnetconf's callback */
981 json_object_put(reply);
982 reply = err_reply;
983 }
984 } else {
985 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
986 json_object_object_add(reply, "data", json_object_new_string(data));
987 }
988 break;
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100989 case MSG_GETSCHEMA:
990 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-schema (session %s)", session_key);
991 identifier = json_object_get_string(json_object_object_get(request, "identifier"));
992 if (identifier == NULL) {
993 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
994 json_object_object_add(reply, "error-message", json_object_new_string("No identifier for get-schema supplied."));
995 break;
996 }
Tomas Cejka94da2c52013-01-08 18:20:30 +0100997 version = json_object_get_string(json_object_object_get(request, "version"));
998 format = json_object_get_string(json_object_object_get(request, "format"));
Tomas Cejka0aeca8b2012-12-22 19:56:03 +0100999
Tomas Cejka94da2c52013-01-08 18:20:30 +01001000 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "get-schema(version: %s, format: %s)", version, format);
1001//version, format
1002 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, NULL, NULL)) == 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