blob: 97ee5dcd2ca7149ce1ef10341e2f5d62807de616 [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);
Tomas Cejkaafe46072013-01-09 16:55:58 +01001001 if ((data = netconf_getschema(server, netconf_sessions_list, session_key, identifier, version, format)) == NULL) {
Tomas Cejka0aeca8b2012-12-22 19:56:03 +01001002 if (err_reply == NULL) {
1003 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1004 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
1005 } else {
1006 /* use filled err_reply from libnetconf's callback */
1007 json_object_put(reply);
1008 reply = err_reply;
1009 }
1010 } else {
1011 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1012 json_object_object_add(reply, "data", json_object_new_string(data));
1013 }
1014 break;
David Kupka8e60a372012-09-04 09:15:20 +02001015 case MSG_EDITCONFIG:
1016 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
1017
1018 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
1019 if (defop != NULL) {
1020 if (strcmp(defop, "merge") == 0) {
1021 defop_type = NC_EDIT_DEFOP_MERGE;
1022 } else if (strcmp(defop, "replace") == 0) {
1023 defop_type = NC_EDIT_DEFOP_REPLACE;
1024 } else if (strcmp(defop, "none") == 0) {
1025 defop_type = NC_EDIT_DEFOP_NONE;
1026 } else {
1027 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1028 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
1029 break;
1030 }
1031 } else {
1032 defop_type = 0;
1033 }
1034
1035 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
1036 if (erropt != NULL) {
1037 if (strcmp(erropt, "continue-on-error") == 0) {
1038 erropt_type = NC_EDIT_ERROPT_CONT;
1039 } else if (strcmp(erropt, "stop-on-error") == 0) {
1040 erropt_type = NC_EDIT_ERROPT_STOP;
1041 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1042 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1043 } else {
1044 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1045 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1046 break;
1047 }
1048 } else {
1049 erropt_type = 0;
1050 }
1051
1052 if (ds_type_t == -1) {
1053 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1054 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1055 break;
1056 }
1057
1058 config = json_object_get_string(json_object_object_get(request, "config"));
1059 if (config == NULL) {
1060 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1061 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1062 break;
1063 }
1064
1065 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1066 if (err_reply == NULL) {
1067 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1068 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1069 } else {
1070 /* use filled err_reply from libnetconf's callback */
1071 json_object_put(reply);
1072 reply = err_reply;
1073 }
1074 } else {
1075 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1076 }
1077 break;
1078 case MSG_COPYCONFIG:
1079 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1080 config = NULL;
1081
1082 if (source == NULL) {
1083 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001084 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001085 config = json_object_get_string(json_object_object_get(request, "config"));
1086 } else if (ds_type_s == -1) {
1087 /* source datastore specified, but it is invalid */
1088 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1089 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1090 break;
1091 }
1092
1093 if (ds_type_t == -1) {
1094 /* invalid target datastore specified */
1095 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1096 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1097 break;
1098 }
1099
1100 if (source == NULL && config == NULL) {
1101 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1102 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1103 } else {
1104 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1105 if (err_reply == NULL) {
1106 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1107 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1108 } else {
1109 /* use filled err_reply from libnetconf's callback */
1110 json_object_put(reply);
1111 reply = err_reply;
1112 }
1113 } else {
1114 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1115 }
1116 }
1117 break;
1118 case MSG_DELETECONFIG:
1119 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1120 /* no break - unifying code */
1121 case MSG_LOCK:
1122 if (operation == MSG_LOCK) {
1123 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1124 }
1125 /* no break - unifying code */
1126 case MSG_UNLOCK:
1127 if (operation == MSG_UNLOCK) {
1128 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1129 }
1130
1131 if (ds_type_t == -1) {
1132 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1133 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1134 break;
1135 }
1136
1137 switch(operation) {
1138 case MSG_DELETECONFIG:
1139 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1140 break;
1141 case MSG_LOCK:
1142 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1143 break;
1144 case MSG_UNLOCK:
1145 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1146 break;
1147 default:
1148 status = -1;
1149 break;
1150 }
1151
1152 if (status != EXIT_SUCCESS) {
1153 if (err_reply == NULL) {
1154 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1155 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1156 } else {
1157 /* use filled err_reply from libnetconf's callback */
1158 json_object_put(reply);
1159 reply = err_reply;
1160 }
1161 } else {
1162 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1163 }
1164 break;
1165 case MSG_KILL:
1166 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1167
1168 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1169
1170 if (sid == NULL) {
1171 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1172 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1173 break;
1174 }
1175
1176 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1177 if (err_reply == NULL) {
1178 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1179 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1180 } else {
1181 /* use filled err_reply from libnetconf's callback */
1182 json_object_put(reply);
1183 reply = err_reply;
1184 }
1185 } else {
1186 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1187 }
1188 break;
1189 case MSG_DISCONNECT:
1190 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1191
1192 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1193 if (err_reply == NULL) {
1194 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1195 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1196 } else {
1197 /* use filled err_reply from libnetconf's callback */
1198 json_object_put(reply);
1199 reply = err_reply;
1200 }
1201 } else {
1202 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1203 }
1204 break;
1205 case MSG_INFO:
1206 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1207
Kupka Davidda134a12012-09-06 14:12:16 +02001208 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1209 if (locked_session != NULL) {
1210 session = locked_session->session;
1211 } else {
1212 session = NULL;
1213 }
David Kupka8e60a372012-09-04 09:15:20 +02001214 if (session != NULL) {
1215 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1216 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1217 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1218 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1219 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1220 cpblts = nc_session_get_cpblts (session);
1221 if (cpblts != NULL) {
1222 json_obj = json_object_new_array();
1223 nc_cpblts_iter_start (cpblts);
1224 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1225 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1226 }
1227 json_object_object_add(reply, "capabilities", json_obj);
1228 }
1229 } else {
1230 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1231 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1232 }
1233
1234 break;
1235 case MSG_GENERIC:
1236 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1237
1238 config = json_object_get_string(json_object_object_get(request, "content"));
1239
1240 if (config == NULL) {
1241 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1242 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1243 break;
1244 }
1245
1246 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1247 if (err_reply == NULL) {
1248 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1249 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1250 } else {
1251 /* use filled err_reply from libnetconf's callback */
1252 json_object_put(reply);
1253 reply = err_reply;
1254 }
1255 } else {
1256 if (data == NULL) {
1257 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1258 } else {
1259 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1260 json_object_object_add(reply, "data", json_object_new_string(data));
1261 }
1262 }
1263 break;
1264 default:
1265 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1266 reply = json_object_new_object();
1267 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1268 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1269 break;
1270 }
1271 json_object_put(request);
1272
David Kupka1e3e4c82012-09-04 09:32:15 +02001273 /* send reply to caller */
1274 if (reply != NULL) {
1275 msgtext = json_object_to_json_string(reply);
1276 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1277 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001278 break;
1279 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001280 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1281 json_object_put(reply);
1282 free (chunked_msg);
1283 free (buffer);
1284 } else {
1285 break;
David Kupka8e60a372012-09-04 09:15:20 +02001286 }
1287 }
1288 }
1289
1290 free (arg);
1291
1292 return retval;
1293}
1294
Radek Krejcif23850c2012-07-23 16:14:17 +02001295/*
1296 * This is actually implementation of NETCONF client
1297 * - requests are received from UNIX socket in the predefined format
1298 * - results are replied through the same way
1299 * - the daemon run as a separate process, but it is started and stopped
1300 * automatically by Apache.
1301 *
1302 */
Radek Krejci469aab82012-07-22 18:42:20 +02001303static void forked_proc(apr_pool_t * pool, server_rec * server)
1304{
1305 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001306 int lsock, client, ret, i, pthread_count = 0;
1307 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001308 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001309 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001310 struct pass_to_thread * arg;
1311 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1312 struct timespec maxtime;
1313 pthread_rwlockattr_t lock_attrs;
1314
1315 /* wait at most 5 secons for every thread to terminate */
1316 maxtime.tv_sec = 5;
1317 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001318
Radek Krejcif23850c2012-07-23 16:14:17 +02001319 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001320 unixd_setup_child();
1321
Radek Krejciae021c12012-07-25 18:03:52 +02001322 cfg = ap_get_module_config(server->module_config, &netconf_module);
1323 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001324 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1325 return;
1326 }
Radek Krejci469aab82012-07-22 18:42:20 +02001327
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001328 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001329 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1330 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1331 return;
1332 }
1333
1334 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001335 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001336 unlink(local.sun_path);
1337 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1338
1339 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1340 if (errno == EADDRINUSE) {
1341 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1342 close(lsock);
1343 exit(0);
1344 }
1345 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1346 close(lsock);
1347 return;
1348 }
1349
1350 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1351 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1352 close(lsock);
1353 return;
1354 }
1355
1356 /* prepare internal lists */
1357 netconf_sessions_list = apr_hash_make(pool);
1358
1359 /* setup libnetconf's callbacks */
1360 nc_verbosity(NC_VERB_DEBUG);
1361 clb_print_server = server;
1362 nc_callback_print(clb_print);
1363 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1364 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1365 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001366 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001367
1368 /* disable publickey authentication */
1369 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1370
David Kupka8e60a372012-09-04 09:15:20 +02001371 /* create mutex protecting session list */
1372 pthread_rwlockattr_init(&lock_attrs);
1373 /* rwlock is shared only with threads in this process */
1374 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1375 /* create rw lock */
1376 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1377 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1378 close (lsock);
1379 return;
1380 }
1381
Radek Krejci469aab82012-07-22 18:42:20 +02001382 while (isterminated == 0) {
1383 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1384
1385 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001386 len = sizeof(remote);
1387 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001388 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1389 apr_sleep(SLEEP_TIME);
1390 continue;
1391 } else if (client == -1 && (errno == EINTR)) {
1392 continue;
1393 } else if (client == -1) {
1394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1395 continue;
1396 }
Radek Krejci469aab82012-07-22 18:42:20 +02001397
1398 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001399 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001400
David Kupka8e60a372012-09-04 09:15:20 +02001401 arg = malloc (sizeof(struct pass_to_thread));
1402 arg->client = client;
1403 arg->pool = pool;
1404 arg->server = server;
1405 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001406
David Kupka8e60a372012-09-04 09:15:20 +02001407 /* start new thread. It will serve this particular request and then terminate */
1408 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1409 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1410 } else {
1411 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1412 pthread_count++;
1413 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1414 ptids[pthread_count] = 0;
1415 }
Radek Krejci469aab82012-07-22 18:42:20 +02001416
David Kupka8e60a372012-09-04 09:15:20 +02001417 /* check if some thread already terminated, free some resources by joining it */
1418 for (i=0; i<pthread_count; i++) {
1419 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1420 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1421 pthread_count--;
1422 if (pthread_count > 0) {
1423 /* place last Thread ID on the place of joined one */
1424 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001425 }
Radek Krejci469aab82012-07-22 18:42:20 +02001426 }
1427 }
David Kupka8e60a372012-09-04 09:15:20 +02001428 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001429 }
1430
David Kupka8e60a372012-09-04 09:15:20 +02001431 /* join all threads */
1432 for (i=0; i<pthread_count; i++) {
1433 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1434 }
1435 free (ptids);
1436
Radek Krejci469aab82012-07-22 18:42:20 +02001437 close(lsock);
1438
David Kupka8e60a372012-09-04 09:15:20 +02001439 /* destroy rwlock */
1440 pthread_rwlock_destroy(&session_lock);
1441 pthread_rwlockattr_destroy(&lock_attrs);
1442
Radek Krejci469aab82012-07-22 18:42:20 +02001443 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1444
1445 exit(APR_SUCCESS);
1446}
1447
Radek Krejcif23850c2012-07-23 16:14:17 +02001448static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001449{
Radek Krejcif23850c2012-07-23 16:14:17 +02001450 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1451
1452 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1453 apr_pool_create(&config->pool, pool);
1454 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001455 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001456
1457 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001458}
1459
1460static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1461 apr_pool_t * plog, server_rec * s)
1462{
Radek Krejcif23850c2012-07-23 16:14:17 +02001463 mod_netconf_cfg *config;
1464 apr_status_t res;
1465
Radek Krejci469aab82012-07-22 18:42:20 +02001466 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001467 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001468 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001469
1470 /*
1471 * The following checks if this routine has been called before.
1472 * This is necessary because the parent process gets initialized
1473 * a couple of times as the server starts up.
1474 */
1475 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1476 if (!data) {
1477 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1478 return (OK);
1479 }
1480
Radek Krejcif23850c2012-07-23 16:14:17 +02001481 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1482 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001483
Radek Krejcif23850c2012-07-23 16:14:17 +02001484 if (config && config->forkproc == NULL) {
1485 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1486 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001487 switch (res) {
1488 case APR_INCHILD:
1489 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001490 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001491 apr_signal(SIGTERM, signal_handler);
1492
1493 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001494 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001495
1496 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001497 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001498
Radek Krejcif23850c2012-07-23 16:14:17 +02001499 /* I never should be here, wtf?!? */
1500 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001501 exit(APR_EGENERAL);
1502 break;
1503 case APR_INPARENT:
1504 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001505 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001506 break;
1507 default:
1508 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1509 break;
1510 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001511 } else {
1512 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001513 }
1514
1515 return OK;
1516}
1517
Radek Krejci469aab82012-07-22 18:42:20 +02001518/**
1519 * Register module hooks
1520 */
1521static void mod_netconf_register_hooks(apr_pool_t * p)
1522{
Radek Krejcif23850c2012-07-23 16:14:17 +02001523 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001524}
1525
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001526static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1527{
1528 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1529 return NULL;
1530}
1531
1532static const command_rec netconf_cmds[] = {
1533 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1534 {NULL}
1535};
1536
Radek Krejci469aab82012-07-22 18:42:20 +02001537/* Dispatch list for API hooks */
1538module AP_MODULE_DECLARE_DATA netconf_module = {
1539 STANDARD20_MODULE_STUFF,
1540 NULL, /* create per-dir config structures */
1541 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001542 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001543 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001544 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001545 mod_netconf_register_hooks /* register hooks */
1546};
Radek Krejcia332b692012-11-12 16:15:54 +01001547