blob: d1e4394877a3a20c8ce32801e5b2604d158ab93d [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
8 */
9/*
10 * Copyright (C) 2011-2012 CESNET
11 *
12 * LICENSE TERMS
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * 3. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * ALTERNATIVELY, provided that this notice is retained in full, this
28 * product may be distributed under the terms of the GNU General Public
29 * License (GPL) version 2 or later, in which case the provisions
30 * of the GPL apply INSTEAD OF those given above.
31 *
32 * This software is provided ``as is'', and any express or implied
33 * warranties, including, but not limited to, the implied warranties of
34 * merchantability and fitness for a particular purpose are disclaimed.
35 * In no event shall the company or contributors be liable for any
36 * direct, indirect, incidental, special, exemplary, or consequential
37 * damages (including, but not limited to, procurement of substitute
38 * goods or services; loss of use, data, or profits; or business
39 * interruption) however caused and on any theory of liability, whether
40 * in contract, strict liability, or tort (including negligence or
41 * otherwise) arising in any way out of the use of this software, even
42 * if advised of the possibility of such damage.
43 *
44 */
45
Radek Krejci7b4ddd02012-07-30 08:09:58 +020046#include <unistd.h>
47#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020048#include <sys/types.h>
49#include <sys/socket.h>
50#include <sys/un.h>
David Kupka8e60a372012-09-04 09:15:20 +020051#include <pthread.h>
52#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020053
54#include <unixd.h>
55#include <httpd.h>
56#include <http_log.h>
57#include <http_config.h>
58
59#include <apr_sha1.h>
60#include <apr_hash.h>
61#include <apr_signal.h>
62#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020063
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020064#include <json/json.h>
65
Radek Krejci469aab82012-07-22 18:42:20 +020066#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020067
Radek Krejci469aab82012-07-22 18:42:20 +020068
69#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020070#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020071#define MAX_SOCKET_CL 10
72#define BUFFER_SIZE 4096
73
74/* sleep in master process for non-blocking socket reading */
75#define SLEEP_TIME 200
76
77#ifndef offsetof
78#define offsetof(type, member) ((size_t) ((type *) 0)->member)
79#endif
80
81struct timeval timeout = { 1, 0 };
82
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020083typedef enum MSG_TYPE {
84 REPLY_OK,
85 REPLY_DATA,
86 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020087 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020088 MSG_CONNECT,
89 MSG_DISCONNECT,
90 MSG_GET,
91 MSG_GETCONFIG,
92 MSG_EDITCONFIG,
93 MSG_COPYCONFIG,
94 MSG_DELETECONFIG,
95 MSG_LOCK,
96 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020097 MSG_KILL,
Radek Krejci80c10d92012-07-30 08:38:50 +020098 MSG_INFO,
99 MSG_GENERIC
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200100} MSG_TYPE;
101
Radek Krejci469aab82012-07-22 18:42:20 +0200102#define MSG_OK 0
103#define MSG_OPEN 1
104#define MSG_DATA 2
105#define MSG_CLOSE 3
106#define MSG_ERROR 4
107#define MSG_UNKNOWN 5
108
Radek Krejci469aab82012-07-22 18:42:20 +0200109module AP_MODULE_DECLARE_DATA netconf_module;
110
111typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200112 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200113 apr_proc_t *forkproc;
114 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200115} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200116
David Kupka8e60a372012-09-04 09:15:20 +0200117struct pass_to_thread {
118 int client; /**< opened socket */
119 apr_pool_t * pool; /**< ?? */
120 server_rec * server; /**< ?? */
121 apr_hash_t * netconf_sessions_list; /**< ?? */
122};
123
124struct session_with_mutex {
125 struct nc_session * session; /**< netconf session */
126 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
127};
128
129pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
130
Radek Krejci469aab82012-07-22 18:42:20 +0200131volatile int isterminated = 0;
132
133static char* password;
134
135
136static void signal_handler(int sign)
137{
138 switch (sign) {
139 case SIGTERM:
140 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200141 break;
142 }
143}
144
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200146{
Radek Krejcif23850c2012-07-23 16:14:17 +0200147 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
148 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200150
Radek Krejci469aab82012-07-22 18:42:20 +0200151 apr_sha1_ctx_t sha1_ctx;
152 apr_sha1_init(&sha1_ctx);
153 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
154 apr_sha1_update(&sha1_ctx, port, strlen(port));
155 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200156 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200157
Radek Krejcif23850c2012-07-23 16:14:17 +0200158 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200159 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200160 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200161 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200162 }
163 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200164
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200165 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200166}
167
168int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
169{
170 /* always approve */
171 return (EXIT_SUCCESS);
172}
173
174char* netconf_callback_sshauth_password (const char* username, const char* hostname)
175{
176 char* buf;
177
178 buf = malloc ((strlen(password) + 1) * sizeof(char));
179 apr_cpystrn(buf, password, strlen(password) + 1);
180
181 return (buf);
182}
183
184void netconf_callback_sshauth_interactive (const char* name,
185 int name_len,
186 const char* instruction,
187 int instruction_len,
188 int num_prompts,
189 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
190 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
191 void** abstract)
192{
193 int i;
194
195 for (i=0; i<num_prompts; i++) {
196 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
197 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
198 responses[i].length = strlen(responses[i].text) + 1;
199 }
200
201 return;
202}
203
Radek Krejcic11fd862012-07-26 12:41:21 +0200204static json_object *err_reply = NULL;
205void netconf_callback_error_process(const char* tag,
206 const char* type,
207 const char* severity,
208 const char* apptag,
209 const char* path,
210 const char* message,
211 const char* attribute,
212 const char* element,
213 const char* ns,
214 const char* sid)
215{
216 err_reply = json_object_new_object();
217 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
218 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
219 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
220 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
221 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
222 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
223 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
224 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
225 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
226 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
227 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
228}
229
Kupka David00b9c5c2012-09-05 09:45:50 +0200230static 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 +0200231{
Radek Krejci469aab82012-07-22 18:42:20 +0200232 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200233 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200234 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200235
236 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200237 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200238 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
239
Radek Krejci469aab82012-07-22 18:42:20 +0200240 /* if connected successful, add session to the list */
241 if (session != NULL) {
242 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200243 session_key = gen_ncsession_hash(
244 (host==NULL) ? "localhost" : host,
245 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200246 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200247
248 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
249 nc_session_free(session);
250 free (locked_session);
251 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
252 return NULL;
253 }
254 locked_session->session = session;
255 pthread_mutex_init (&locked_session->lock, NULL);
256 /* get exclusive access to sessions_list (conns) */
257 if (pthread_rwlock_wrlock (&session_lock) != 0) {
258 nc_session_free(session);
259 free (locked_session);
260 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
261 return NULL;
262 }
263 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
264 /* end of critical section */
265 if (pthread_rwlock_unlock (&session_lock) != 0) {
266 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
267 return NULL;
268 }
Radek Krejci469aab82012-07-22 18:42:20 +0200269 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200270 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200271 } else {
272 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200273 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200274 }
275
Radek Krejci469aab82012-07-22 18:42:20 +0200276}
277
Radek Krejci80c10d92012-07-30 08:38:50 +0200278static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200279{
280 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200281 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200282
Radek Krejcif23850c2012-07-23 16:14:17 +0200283 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200284 /* get exclusive (write) access to sessions_list (conns) */
285 if (pthread_rwlock_wrlock (&session_lock) != 0) {
286 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
287 return EXIT_FAILURE;
288 }
289 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
290 if (locked_session != NULL) {
291 pthread_mutex_destroy(&locked_session->lock);
292 ns = locked_session->session;
293 free (locked_session);
294 }
Radek Krejci469aab82012-07-22 18:42:20 +0200295 if (ns != NULL) {
296 nc_session_close (ns, "NETCONF session closed by client");
297 nc_session_free (ns);
298 ns = NULL;
299
300 /* remove session from the active sessions list */
301 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200302 /* end of critical section */
303 if (pthread_rwlock_unlock (&session_lock) != 0) {
304 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
305 return EXIT_FAILURE;
306 }
Radek Krejci469aab82012-07-22 18:42:20 +0200307 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200308
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200309 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200310 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200311 if (pthread_rwlock_unlock (&session_lock) != 0) {
312 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
313 return EXIT_FAILURE;
314 }
Radek Krejci469aab82012-07-22 18:42:20 +0200315 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200316 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200317 }
318}
319
Radek Krejci80c10d92012-07-30 08:38:50 +0200320static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200321{
322 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200323 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200324 nc_reply* reply;
325 int retval = EXIT_SUCCESS;
326
Radek Krejci8e4632a2012-07-26 13:40:34 +0200327 /* check requests */
328 if (rpc == NULL) {
329 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
330 return (EXIT_FAILURE);
331 }
332
David Kupka8e60a372012-09-04 09:15:20 +0200333 /* get non-exclusive (read) access to sessions_list (conns) */
334 if (pthread_rwlock_rdlock (&session_lock) != 0) {
335 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
336 return EXIT_FAILURE;
337 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200338 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200339 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
340 if (locked_session != NULL) {
341 session = locked_session->session;
342 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200343 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200344 /* get exclusive access to session */
345 if (pthread_mutex_lock(&locked_session->lock) != 0) {
346 /* unlock before returning error */
347 if (pthread_rwlock_unlock (&session_lock) != 0) {
348 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
349 return EXIT_FAILURE;
350 }
351 return EXIT_FAILURE;
352 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200353 /* send the request and get the reply */
354 nc_session_send_rpc (session, rpc);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200355 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200356 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200357 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
David Kupka8e60a372012-09-04 09:15:20 +0200358 /* first release exclusive lock for this session */
359 pthread_mutex_unlock(&locked_session->lock);
360 /* release read lock, netconf_close will get exclusive access and close this session */
361 if (pthread_rwlock_unlock (&session_lock) != 0) {
362 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
363 return EXIT_FAILURE;
364 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200365 netconf_close(server, conns, session_key);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200366 return (EXIT_FAILURE);
367 }
David Kupka8e60a372012-09-04 09:15:20 +0200368 /* first release exclusive lock for this session */
369 pthread_mutex_unlock(&locked_session->lock);
370 /* unlock before returning error */
371 if (pthread_rwlock_unlock (&session_lock) != 0) {
372 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
373 return EXIT_FAILURE;
374 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200375 /* there is error handled by callback */
376 return (EXIT_FAILURE);
377 }
David Kupka8e60a372012-09-04 09:15:20 +0200378 /* first release exclusive lock for this session */
379 pthread_mutex_unlock(&locked_session->lock);
380 /* end of critical section */
381 if (pthread_rwlock_unlock (&session_lock) != 0) {
382 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
383 return EXIT_FAILURE;
384 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200385
386 switch (nc_reply_get_type (reply)) {
387 case NC_REPLY_OK:
388 retval = EXIT_SUCCESS;
389 break;
390 default:
391 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
392 retval = EXIT_FAILURE;
393 break;
394 }
395 nc_reply_free(reply);
396 return (retval);
397 } else {
398 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
399 return (EXIT_FAILURE);
400 }
401}
Radek Krejci80c10d92012-07-30 08:38:50 +0200402
403static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200404{
405 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200406 struct session_with_mutex * locked_session;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200407 nc_reply* reply;
408 char* data;
409
410 /* check requests */
411 if (rpc == NULL) {
412 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
413 return (NULL);
414 }
415
David Kupka8e60a372012-09-04 09:15:20 +0200416 /* get non-exclusive (read) access to sessions_list (conns) */
417 if (pthread_rwlock_rdlock (&session_lock) != 0) {
418 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
419 return NULL;
420 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200421 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200422 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
423 if (locked_session != NULL) {
424 session = locked_session->session;
425 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200426 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200427 /* get exclusive access to session */
428 if (pthread_mutex_lock(&locked_session->lock) != 0) {
429 /* unlock before returning error */
430 if (pthread_rwlock_unlock (&session_lock) != 0) {
431 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
432 return NULL;
433 }
434 return NULL;
435 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200436 /* send the request and get the reply */
437 nc_session_send_rpc (session, rpc);
438 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci8e4632a2012-07-26 13:40:34 +0200439 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
440 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
David Kupka8e60a372012-09-04 09:15:20 +0200441 /* first release exclusive lock for this session */
442 pthread_mutex_unlock(&locked_session->lock);
443 /* release read lock, netconf_close will get exclusive access and close this session */
444 if (pthread_rwlock_unlock (&session_lock) != 0) {
445 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
446 return NULL;
447 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200448 netconf_close(server, conns, session_key);
449 return (NULL);
450 }
David Kupka8e60a372012-09-04 09:15:20 +0200451 /* first release exclusive lock for this session */
452 pthread_mutex_unlock(&locked_session->lock);
453 /* unlock before returning error */
454 if (pthread_rwlock_unlock (&session_lock) != 0) {
455 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
456 return NULL;
457 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200458 /* there is error handled by callback */
459 return (NULL);
460 }
David Kupka8e60a372012-09-04 09:15:20 +0200461 /* first release exclusive lock for this session */
462 pthread_mutex_unlock(&locked_session->lock);
463 /* end of critical section */
464 if (pthread_rwlock_unlock (&session_lock) != 0) {
465 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
466 return NULL;
467 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200468
469 switch (nc_reply_get_type (reply)) {
470 case NC_REPLY_DATA:
471 if ((data = nc_reply_get_data (reply)) == NULL) {
472 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
473 return (NULL);
474 }
475 break;
476 default:
477 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
478 return (NULL);
479 }
480 nc_reply_free(reply);
481
482 return (data);
483 } else {
484 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
485 return (NULL);
486 }
487}
488
Radek Krejci80c10d92012-07-30 08:38:50 +0200489static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200490{
491 nc_rpc* rpc;
492 struct nc_filter *f = NULL;
493 char* data = NULL;
494
495 /* create filter if set */
496 if (filter != NULL) {
497 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
498 }
499
500 /* create requests */
501 rpc = nc_rpc_getconfig (source, f);
502 nc_filter_free(f);
503 if (rpc == NULL) {
504 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
505 return (NULL);
506 }
507
508 data = netconf_opdata(server, conns, session_key, rpc);
509 nc_rpc_free (rpc);
510 return (data);
511}
512
Radek Krejci80c10d92012-07-30 08:38:50 +0200513static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200514{
515 nc_rpc* rpc;
516 struct nc_filter *f = NULL;
517 char* data = NULL;
518
519 /* create filter if set */
520 if (filter != NULL) {
521 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
522 }
523
524 /* create requests */
525 rpc = nc_rpc_get (f);
526 nc_filter_free(f);
527 if (rpc == NULL) {
528 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
529 return (NULL);
530 }
531
532 data = netconf_opdata(server, conns, session_key, rpc);
533 nc_rpc_free (rpc);
534 return (data);
535}
536
Radek Krejci80c10d92012-07-30 08:38:50 +0200537static 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 +0200538{
539 nc_rpc* rpc;
540 int retval = EXIT_SUCCESS;
541
542 /* create requests */
543 rpc = nc_rpc_copyconfig(source, target, config);
544 if (rpc == NULL) {
545 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
546 return (EXIT_FAILURE);
547 }
548
549 retval = netconf_op(server, conns, session_key, rpc);
550 nc_rpc_free (rpc);
551 return (retval);
552}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200553
Radek Krejci80c10d92012-07-30 08:38:50 +0200554static 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 +0200555{
556 nc_rpc* rpc;
557 int retval = EXIT_SUCCESS;
558
559 /* create requests */
560 rpc = nc_rpc_editconfig(target, defop, erropt, config);
561 if (rpc == NULL) {
562 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
563 return (EXIT_FAILURE);
564 }
565
566 retval = netconf_op(server, conns, session_key, rpc);
567 nc_rpc_free (rpc);
568 return (retval);
569}
570
Radek Krejci80c10d92012-07-30 08:38:50 +0200571static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200572{
573 nc_rpc* rpc;
574 int retval = EXIT_SUCCESS;
575
576 /* create requests */
577 rpc = nc_rpc_killsession(sid);
578 if (rpc == NULL) {
579 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
580 return (EXIT_FAILURE);
581 }
582
583 retval = netconf_op(server, conns, session_key, rpc);
584 nc_rpc_free (rpc);
585 return (retval);
586}
587
Radek Krejci80c10d92012-07-30 08:38:50 +0200588static int netconf_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 +0200589{
590 nc_rpc* rpc;
591 int retval = EXIT_SUCCESS;
592
593 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200594 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200595 if (rpc == NULL) {
596 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
597 return (EXIT_FAILURE);
598 }
599
600 retval = netconf_op(server, conns, session_key, rpc);
601 nc_rpc_free (rpc);
602 return (retval);
603}
604
Radek Krejci80c10d92012-07-30 08:38:50 +0200605static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200606{
607 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
608}
609
Radek Krejci80c10d92012-07-30 08:38:50 +0200610static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200611{
612 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
613}
614
Radek Krejci80c10d92012-07-30 08:38:50 +0200615static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200616{
617 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
618}
619
Radek Krejci80c10d92012-07-30 08:38:50 +0200620/**
621 * @return REPLY_OK: 0, *data == NULL
622 * REPLY_DATA: 0, *data != NULL
623 * REPLY_ERROR: 1, *data == NULL
624 */
625static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
626{
627 struct nc_session *session = NULL;
628 nc_reply* reply;
629 nc_rpc* rpc;
630 int retval = EXIT_SUCCESS;
631
632 /* create requests */
633 rpc = nc_rpc_generic(content);
634 if (rpc == NULL) {
635 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
636 return (EXIT_FAILURE);
637 }
638
639 *data = NULL;
640
641 /* get session where send the RPC */
642 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
643 if (session != NULL) {
644 /* send the request and get the reply */
645 nc_session_send_rpc (session, rpc);
646 if (nc_session_recv_reply (session, &reply) == 0) {
647 nc_rpc_free (rpc);
648 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
649 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
650 netconf_close(server, conns, session_key);
651 return (EXIT_FAILURE);
652 }
653
654 /* there is error handled by callback */
655 return (EXIT_FAILURE);
656 }
657 nc_rpc_free (rpc);
658
659 switch (nc_reply_get_type (reply)) {
660 case NC_REPLY_DATA:
661 if ((*data = nc_reply_get_data (reply)) == NULL) {
662 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
663 return (EXIT_FAILURE);
664 }
665 retval = EXIT_SUCCESS;
666 break;
667 case NC_REPLY_OK:
668 retval = EXIT_SUCCESS;
669 break;
670 default:
671 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
672 retval = EXIT_FAILURE;
673 break;
674 }
675 nc_reply_free(reply);
676
677 return (retval);
678 } else {
679 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
680 return (EXIT_FAILURE);
681 }
682}
683
Radek Krejci469aab82012-07-22 18:42:20 +0200684server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200685void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200686{
Radek Krejci7338bde2012-08-10 12:57:30 +0200687 switch (level) {
688 case NC_VERB_ERROR:
689 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
690 break;
691 case NC_VERB_WARNING:
692 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
693 break;
694 case NC_VERB_VERBOSE:
695 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
696 break;
697 case NC_VERB_DEBUG:
698 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
699 break;
700 }
Radek Krejci469aab82012-07-22 18:42:20 +0200701}
702
David Kupka8e60a372012-09-04 09:15:20 +0200703void * thread_routine (void * arg)
704{
705 void * retval = NULL;
706
707 ssize_t buffer_len;
708 struct pollfd fds;
709 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200710 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200711 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200712 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200713 char* session_key, *data;
714 const char *host, *port, *user, *pass;
715 const char *msgtext, *cpbltstr;
716 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
717 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200718 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200719 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200720 NC_DATASTORE ds_type_s, ds_type_t;
721 NC_EDIT_DEFOP_TYPE defop_type = 0;
722 NC_EDIT_ERROPT_TYPE erropt_type = 0;
723
724 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
725 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
726 server_rec * server = ((struct pass_to_thread*)arg)->server;
727 int client = ((struct pass_to_thread*)arg)->client;
728
729 char * buffer, chunk_len_str[12], *chunked_msg;
730 char c;
731
732 while (!isterminated) {
733 fds.fd = client;
734 fds.events = POLLIN;
735 fds.revents = 0;
736
737 status = poll(&fds, 1, 1000);
738
739 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
740 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
741 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
742 continue;
743 } else if (status < 0) {
744 /* 0: poll time outed
745 * close socket and ignore this request from the client, it can try it again
746 * -1: poll failed
747 * something wrong happend, close this socket and wait for another request
748 */
749 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
750 close(client);
751 break;
752 }
753 /* status > 0 */
754
755 /* check the status of the socket */
756
757 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
758 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
759 /* close client's socket (it's probably already closed by client */
760 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
761 close(client);
762 break;
763 }
764
765 /* read json in chunked framing */
766 buffer_size = 0;
767 buffer_len = 0;
768 buffer = NULL;
769 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200770 /* read chunk length */
771 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
772 free (buffer);
773 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200774 break;
775 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200776 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
777 free (buffer);
778 buffer = NULL;
779 break;
780 }
781 i=0;
782 memset (chunk_len_str, 0, 12);
783 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
784 if (i==0 && c == '#') {
785 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
786 /* end but invalid */
787 free (buffer);
788 buffer = NULL;
789 }
790 /* end of message, double-loop break */
791 goto msg_complete;
792 }
793 chunk_len_str[i++] = c;
794 }
795 if (c != '\n') {
796 free (buffer);
797 buffer = NULL;
798 break;
799 }
800 if ((chunk_len = atoi (chunk_len_str)) == 0) {
801 free (buffer);
802 buffer = NULL;
803 break;
804 }
805 buffer_size += chunk_len+1;
806 buffer = realloc (buffer, sizeof(char)*buffer_size);
807 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
808 free (buffer);
809 buffer = NULL;
810 break;
811 }
812 buffer_len += ret;
813 }
814msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200815
816 if (buffer != NULL) {
817 request = json_tokener_parse(buffer);
818 operation = json_object_get_int(json_object_object_get(request, "type"));
819
820 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
821 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
822 if (operation != MSG_CONNECT && session_key == NULL) {
823 reply = json_object_new_object();
824 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
825 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
826 msgtext = json_object_to_json_string(reply);
827 send(client, msgtext, strlen(msgtext) + 1, 0);
828 json_object_put(reply);
829 /* there is some stupid client, so close the connection to give a chance to some other client */
830 close(client);
831 break;
832 }
833
834 /* get parameters */
835 ds_type_t = -1;
836 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
837 if (strcmp(target, "running") == 0) {
838 ds_type_t = NC_DATASTORE_RUNNING;
839 } else if (strcmp(target, "startup") == 0) {
840 ds_type_t = NC_DATASTORE_STARTUP;
841 } else if (strcmp(target, "candidate") == 0) {
842 ds_type_t = NC_DATASTORE_CANDIDATE;
843 }
844 }
845 ds_type_s = -1;
846 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
847 if (strcmp(source, "running") == 0) {
848 ds_type_s = NC_DATASTORE_RUNNING;
849 } else if (strcmp(source, "startup") == 0) {
850 ds_type_s = NC_DATASTORE_STARTUP;
851 } else if (strcmp(source, "candidate") == 0) {
852 ds_type_s = NC_DATASTORE_CANDIDATE;
853 }
854 }
855
856 /* null global JSON error-reply */
857 err_reply = NULL;
858
859 /* prepare reply envelope */
860 reply = json_object_new_object();
861
862 /* process required operation */
863 switch (operation) {
864 case MSG_CONNECT:
865 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
866
867 host = json_object_get_string(json_object_object_get(request, "host"));
868 port = json_object_get_string(json_object_object_get(request, "port"));
869 user = json_object_get_string(json_object_object_get(request, "user"));
870 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200871 capabilities = json_object_object_get(request, "capabilities");
872 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200873 cpblts = nc_cpblts_new (NULL);
874 for (i=0; i<len; i++) {
875 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
876 }
877 } else {
878 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
879 }
David Kupka8e60a372012-09-04 09:15:20 +0200880 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
881 if ((host == NULL) || (user == NULL)) {
882 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
883 session_key = NULL;
884 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200885 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
886 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200887 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
888 }
889
890 reply = json_object_new_object();
891 if (session_key == NULL) {
892 /* negative reply */
893 if (err_reply == NULL) {
894 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
895 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
896 } else {
897 /* use filled err_reply from libnetconf's callback */
898 json_object_put(reply);
899 reply = err_reply;
900 }
901 } else {
902 /* positive reply */
903 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
904 json_object_object_add(reply, "session", json_object_new_string(session_key));
905
906 free(session_key);
907 }
908
909 break;
910 case MSG_GET:
911 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
912
913 filter = json_object_get_string(json_object_object_get(request, "filter"));
914
915 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
916
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200917 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200918 if (err_reply == NULL) {
919 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
920 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
921 } else {
922 /* use filled err_reply from libnetconf's callback */
923 json_object_put(reply);
924 reply = err_reply;
925 }
926 } else {
927 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
928 json_object_object_add(reply, "data", json_object_new_string(data));
929 }
930 break;
931 case MSG_GETCONFIG:
932 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
933
934 filter = json_object_get_string(json_object_object_get(request, "filter"));
935
936 if (ds_type_s == -1) {
937 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
938 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
939 break;
940 }
941
942 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
943 if (err_reply == NULL) {
944 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
945 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
946 } else {
947 /* use filled err_reply from libnetconf's callback */
948 json_object_put(reply);
949 reply = err_reply;
950 }
951 } else {
952 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
953 json_object_object_add(reply, "data", json_object_new_string(data));
954 }
955 break;
956 case MSG_EDITCONFIG:
957 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
958
959 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
960 if (defop != NULL) {
961 if (strcmp(defop, "merge") == 0) {
962 defop_type = NC_EDIT_DEFOP_MERGE;
963 } else if (strcmp(defop, "replace") == 0) {
964 defop_type = NC_EDIT_DEFOP_REPLACE;
965 } else if (strcmp(defop, "none") == 0) {
966 defop_type = NC_EDIT_DEFOP_NONE;
967 } else {
968 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
969 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
970 break;
971 }
972 } else {
973 defop_type = 0;
974 }
975
976 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
977 if (erropt != NULL) {
978 if (strcmp(erropt, "continue-on-error") == 0) {
979 erropt_type = NC_EDIT_ERROPT_CONT;
980 } else if (strcmp(erropt, "stop-on-error") == 0) {
981 erropt_type = NC_EDIT_ERROPT_STOP;
982 } else if (strcmp(erropt, "rollback-on-error") == 0) {
983 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
984 } else {
985 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
986 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
987 break;
988 }
989 } else {
990 erropt_type = 0;
991 }
992
993 if (ds_type_t == -1) {
994 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
995 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
996 break;
997 }
998
999 config = json_object_get_string(json_object_object_get(request, "config"));
1000 if (config == NULL) {
1001 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1002 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1003 break;
1004 }
1005
1006 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1007 if (err_reply == NULL) {
1008 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1009 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1010 } else {
1011 /* use filled err_reply from libnetconf's callback */
1012 json_object_put(reply);
1013 reply = err_reply;
1014 }
1015 } else {
1016 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1017 }
1018 break;
1019 case MSG_COPYCONFIG:
1020 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1021 config = NULL;
1022
1023 if (source == NULL) {
1024 /* no explicit source specified -> use config data */
1025 ds_type_s = NC_DATASTORE_NONE;
1026 config = json_object_get_string(json_object_object_get(request, "config"));
1027 } else if (ds_type_s == -1) {
1028 /* source datastore specified, but it is invalid */
1029 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1030 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1031 break;
1032 }
1033
1034 if (ds_type_t == -1) {
1035 /* invalid target datastore specified */
1036 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1037 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1038 break;
1039 }
1040
1041 if (source == NULL && config == NULL) {
1042 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1043 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1044 } else {
1045 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1046 if (err_reply == NULL) {
1047 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1048 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1049 } else {
1050 /* use filled err_reply from libnetconf's callback */
1051 json_object_put(reply);
1052 reply = err_reply;
1053 }
1054 } else {
1055 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1056 }
1057 }
1058 break;
1059 case MSG_DELETECONFIG:
1060 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1061 /* no break - unifying code */
1062 case MSG_LOCK:
1063 if (operation == MSG_LOCK) {
1064 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1065 }
1066 /* no break - unifying code */
1067 case MSG_UNLOCK:
1068 if (operation == MSG_UNLOCK) {
1069 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1070 }
1071
1072 if (ds_type_t == -1) {
1073 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1074 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1075 break;
1076 }
1077
1078 switch(operation) {
1079 case MSG_DELETECONFIG:
1080 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1081 break;
1082 case MSG_LOCK:
1083 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1084 break;
1085 case MSG_UNLOCK:
1086 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1087 break;
1088 default:
1089 status = -1;
1090 break;
1091 }
1092
1093 if (status != EXIT_SUCCESS) {
1094 if (err_reply == NULL) {
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("operation failed."));
1097 } else {
1098 /* use filled err_reply from libnetconf's callback */
1099 json_object_put(reply);
1100 reply = err_reply;
1101 }
1102 } else {
1103 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1104 }
1105 break;
1106 case MSG_KILL:
1107 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1108
1109 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1110
1111 if (sid == NULL) {
1112 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1113 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1114 break;
1115 }
1116
1117 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1118 if (err_reply == NULL) {
1119 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1120 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1121 } else {
1122 /* use filled err_reply from libnetconf's callback */
1123 json_object_put(reply);
1124 reply = err_reply;
1125 }
1126 } else {
1127 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1128 }
1129 break;
1130 case MSG_DISCONNECT:
1131 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1132
1133 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1134 if (err_reply == NULL) {
1135 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1136 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1137 } else {
1138 /* use filled err_reply from libnetconf's callback */
1139 json_object_put(reply);
1140 reply = err_reply;
1141 }
1142 } else {
1143 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1144 }
1145 break;
1146 case MSG_INFO:
1147 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1148
Kupka Davidda134a12012-09-06 14:12:16 +02001149 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1150 if (locked_session != NULL) {
1151 session = locked_session->session;
1152 } else {
1153 session = NULL;
1154 }
David Kupka8e60a372012-09-04 09:15:20 +02001155 if (session != NULL) {
1156 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1157 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1158 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1159 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1160 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1161 cpblts = nc_session_get_cpblts (session);
1162 if (cpblts != NULL) {
1163 json_obj = json_object_new_array();
1164 nc_cpblts_iter_start (cpblts);
1165 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1166 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1167 }
1168 json_object_object_add(reply, "capabilities", json_obj);
1169 }
1170 } else {
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("Invalid session identifier."));
1173 }
1174
1175 break;
1176 case MSG_GENERIC:
1177 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1178
1179 config = json_object_get_string(json_object_object_get(request, "content"));
1180
1181 if (config == NULL) {
1182 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1183 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1184 break;
1185 }
1186
1187 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1188 if (err_reply == NULL) {
1189 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1190 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1191 } else {
1192 /* use filled err_reply from libnetconf's callback */
1193 json_object_put(reply);
1194 reply = err_reply;
1195 }
1196 } else {
1197 if (data == NULL) {
1198 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1199 } else {
1200 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1201 json_object_object_add(reply, "data", json_object_new_string(data));
1202 }
1203 }
1204 break;
1205 default:
1206 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1207 reply = json_object_new_object();
1208 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1209 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1210 break;
1211 }
1212 json_object_put(request);
1213
David Kupka1e3e4c82012-09-04 09:32:15 +02001214 /* send reply to caller */
1215 if (reply != NULL) {
1216 msgtext = json_object_to_json_string(reply);
1217 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1218 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001219 break;
1220 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001221 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1222 json_object_put(reply);
1223 free (chunked_msg);
1224 free (buffer);
1225 } else {
1226 break;
David Kupka8e60a372012-09-04 09:15:20 +02001227 }
1228 }
1229 }
1230
1231 free (arg);
1232
1233 return retval;
1234}
1235
Radek Krejcif23850c2012-07-23 16:14:17 +02001236/*
1237 * This is actually implementation of NETCONF client
1238 * - requests are received from UNIX socket in the predefined format
1239 * - results are replied through the same way
1240 * - the daemon run as a separate process, but it is started and stopped
1241 * automatically by Apache.
1242 *
1243 */
Radek Krejci469aab82012-07-22 18:42:20 +02001244static void forked_proc(apr_pool_t * pool, server_rec * server)
1245{
1246 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001247 int lsock, client, ret, i, pthread_count = 0;
1248 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001249 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001250 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001251 struct pass_to_thread * arg;
1252 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1253 struct timespec maxtime;
1254 pthread_rwlockattr_t lock_attrs;
1255
1256 /* wait at most 5 secons for every thread to terminate */
1257 maxtime.tv_sec = 5;
1258 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001259
Radek Krejcif23850c2012-07-23 16:14:17 +02001260 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001261 unixd_setup_child();
1262
Radek Krejciae021c12012-07-25 18:03:52 +02001263 cfg = ap_get_module_config(server->module_config, &netconf_module);
1264 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001265 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1266 return;
1267 }
Radek Krejci469aab82012-07-22 18:42:20 +02001268
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001269 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001270 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1271 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1272 return;
1273 }
1274
1275 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001276 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001277 unlink(local.sun_path);
1278 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1279
1280 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1281 if (errno == EADDRINUSE) {
1282 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1283 close(lsock);
1284 exit(0);
1285 }
1286 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1287 close(lsock);
1288 return;
1289 }
1290
1291 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1292 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1293 close(lsock);
1294 return;
1295 }
1296
1297 /* prepare internal lists */
1298 netconf_sessions_list = apr_hash_make(pool);
1299
1300 /* setup libnetconf's callbacks */
1301 nc_verbosity(NC_VERB_DEBUG);
1302 clb_print_server = server;
1303 nc_callback_print(clb_print);
1304 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1305 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1306 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001307 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001308
1309 /* disable publickey authentication */
1310 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1311
David Kupka8e60a372012-09-04 09:15:20 +02001312 /* create mutex protecting session list */
1313 pthread_rwlockattr_init(&lock_attrs);
1314 /* rwlock is shared only with threads in this process */
1315 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1316 /* create rw lock */
1317 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1318 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1319 close (lsock);
1320 return;
1321 }
1322
Radek Krejci469aab82012-07-22 18:42:20 +02001323 while (isterminated == 0) {
1324 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1325
1326 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001327 len = sizeof(remote);
1328 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001329 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1330 apr_sleep(SLEEP_TIME);
1331 continue;
1332 } else if (client == -1 && (errno == EINTR)) {
1333 continue;
1334 } else if (client == -1) {
1335 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1336 continue;
1337 }
Radek Krejci469aab82012-07-22 18:42:20 +02001338
1339 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001340 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001341
David Kupka8e60a372012-09-04 09:15:20 +02001342 arg = malloc (sizeof(struct pass_to_thread));
1343 arg->client = client;
1344 arg->pool = pool;
1345 arg->server = server;
1346 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001347
David Kupka8e60a372012-09-04 09:15:20 +02001348 /* start new thread. It will serve this particular request and then terminate */
1349 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1350 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1351 } else {
1352 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1353 pthread_count++;
1354 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1355 ptids[pthread_count] = 0;
1356 }
Radek Krejci469aab82012-07-22 18:42:20 +02001357
David Kupka8e60a372012-09-04 09:15:20 +02001358 /* check if some thread already terminated, free some resources by joining it */
1359 for (i=0; i<pthread_count; i++) {
1360 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1362 pthread_count--;
1363 if (pthread_count > 0) {
1364 /* place last Thread ID on the place of joined one */
1365 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001366 }
Radek Krejci469aab82012-07-22 18:42:20 +02001367 }
1368 }
David Kupka8e60a372012-09-04 09:15:20 +02001369 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001370 }
1371
David Kupka8e60a372012-09-04 09:15:20 +02001372 /* join all threads */
1373 for (i=0; i<pthread_count; i++) {
1374 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1375 }
1376 free (ptids);
1377
Radek Krejci469aab82012-07-22 18:42:20 +02001378 close(lsock);
1379
David Kupka8e60a372012-09-04 09:15:20 +02001380 /* destroy rwlock */
1381 pthread_rwlock_destroy(&session_lock);
1382 pthread_rwlockattr_destroy(&lock_attrs);
1383
Radek Krejci469aab82012-07-22 18:42:20 +02001384 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1385
1386 exit(APR_SUCCESS);
1387}
1388
Radek Krejcif23850c2012-07-23 16:14:17 +02001389static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001390{
Radek Krejcif23850c2012-07-23 16:14:17 +02001391 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1392
1393 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1394 apr_pool_create(&config->pool, pool);
1395 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001396 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001397
1398 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001399}
1400
1401static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1402 apr_pool_t * plog, server_rec * s)
1403{
Radek Krejcif23850c2012-07-23 16:14:17 +02001404 mod_netconf_cfg *config;
1405 apr_status_t res;
1406
Radek Krejci469aab82012-07-22 18:42:20 +02001407 /* These two help ensure that we only init once. */
1408 void *data;
Radek Krejcif23850c2012-07-23 16:14:17 +02001409 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001410
1411 /*
1412 * The following checks if this routine has been called before.
1413 * This is necessary because the parent process gets initialized
1414 * a couple of times as the server starts up.
1415 */
1416 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1417 if (!data) {
1418 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1419 return (OK);
1420 }
1421
Radek Krejcif23850c2012-07-23 16:14:17 +02001422 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1423 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001424
Radek Krejcif23850c2012-07-23 16:14:17 +02001425 if (config && config->forkproc == NULL) {
1426 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1427 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001428 switch (res) {
1429 case APR_INCHILD:
1430 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001431 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001432 apr_signal(SIGTERM, signal_handler);
1433
1434 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001435 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001436
1437 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001438 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001439
Radek Krejcif23850c2012-07-23 16:14:17 +02001440 /* I never should be here, wtf?!? */
1441 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001442 exit(APR_EGENERAL);
1443 break;
1444 case APR_INPARENT:
1445 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001446 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001447 break;
1448 default:
1449 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1450 break;
1451 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001452 } else {
1453 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001454 }
1455
1456 return OK;
1457}
1458
Radek Krejci469aab82012-07-22 18:42:20 +02001459/**
1460 * Register module hooks
1461 */
1462static void mod_netconf_register_hooks(apr_pool_t * p)
1463{
Radek Krejcif23850c2012-07-23 16:14:17 +02001464 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001465}
1466
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001467static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1468{
1469 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1470 return NULL;
1471}
1472
1473static const command_rec netconf_cmds[] = {
1474 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1475 {NULL}
1476};
1477
Radek Krejci469aab82012-07-22 18:42:20 +02001478/* Dispatch list for API hooks */
1479module AP_MODULE_DECLARE_DATA netconf_module = {
1480 STANDARD20_MODULE_STUFF,
1481 NULL, /* create per-dir config structures */
1482 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001483 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001484 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001485 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001486 mod_netconf_register_hooks /* register hooks */
1487};