blob: 163a16f537b3c8a3e651bf0e00cf57aac123cfd8 [file] [log] [blame]
Radek Krejci469aab82012-07-22 18:42:20 +02001/*!
2 * \file mod_netconf.c
3 * \brief NETCONF Apache modul for Netopeer
4 * \author Tomas Cejka <cejkat@cesnet.cz>
5 * \author Radek Krejci <rkrejci@cesnet.cz>
6 * \date 2011
7 * \date 2012
8 */
9/*
10 * Copyright (C) 2011-2012 CESNET
11 *
12 * LICENSE TERMS
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * 3. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * ALTERNATIVELY, provided that this notice is retained in full, this
28 * product may be distributed under the terms of the GNU General Public
29 * License (GPL) version 2 or later, in which case the provisions
30 * of the GPL apply INSTEAD OF those given above.
31 *
32 * This software is provided ``as is'', and any express or implied
33 * warranties, including, but not limited to, the implied warranties of
34 * merchantability and fitness for a particular purpose are disclaimed.
35 * In no event shall the company or contributors be liable for any
36 * direct, indirect, incidental, special, exemplary, or consequential
37 * damages (including, but not limited to, procurement of substitute
38 * goods or services; loss of use, data, or profits; or business
39 * interruption) however caused and on any theory of liability, whether
40 * in contract, strict liability, or tort (including negligence or
41 * otherwise) arising in any way out of the use of this software, even
42 * if advised of the possibility of such damage.
43 *
44 */
45
Radek Krejci7b4ddd02012-07-30 08:09:58 +020046#include <unistd.h>
47#include <poll.h>
Radek Krejci469aab82012-07-22 18:42:20 +020048#include <sys/types.h>
49#include <sys/socket.h>
50#include <sys/un.h>
David Kupka8e60a372012-09-04 09:15:20 +020051#include <pthread.h>
52#include <ctype.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020053
54#include <unixd.h>
55#include <httpd.h>
56#include <http_log.h>
57#include <http_config.h>
58
59#include <apr_sha1.h>
60#include <apr_hash.h>
61#include <apr_signal.h>
62#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020063
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020064#include <json/json.h>
65
Radek Krejci469aab82012-07-22 18:42:20 +020066#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020067
Radek Krejci469aab82012-07-22 18:42:20 +020068
69#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020070#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020071#define MAX_SOCKET_CL 10
72#define BUFFER_SIZE 4096
73
74/* sleep in master process for non-blocking socket reading */
75#define SLEEP_TIME 200
76
77#ifndef offsetof
78#define offsetof(type, member) ((size_t) ((type *) 0)->member)
79#endif
80
Tomas Cejka027f3bc2012-11-10 20:28:36 +010081/* timeout in msec */
82#define NCRPCTIMEOUT 1000
83#define NCWITHDEFAULTS NCWD_MODE_DISABLED
84
Radek Krejci469aab82012-07-22 18:42:20 +020085struct timeval timeout = { 1, 0 };
86
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020087typedef enum MSG_TYPE {
88 REPLY_OK,
89 REPLY_DATA,
90 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020091 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020092 MSG_CONNECT,
93 MSG_DISCONNECT,
94 MSG_GET,
95 MSG_GETCONFIG,
96 MSG_EDITCONFIG,
97 MSG_COPYCONFIG,
98 MSG_DELETECONFIG,
99 MSG_LOCK,
100 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200101 MSG_KILL,
Radek Krejci80c10d92012-07-30 08:38:50 +0200102 MSG_INFO,
103 MSG_GENERIC
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200104} MSG_TYPE;
105
Radek Krejci469aab82012-07-22 18:42:20 +0200106#define MSG_OK 0
107#define MSG_OPEN 1
108#define MSG_DATA 2
109#define MSG_CLOSE 3
110#define MSG_ERROR 4
111#define MSG_UNKNOWN 5
112
Radek Krejci469aab82012-07-22 18:42:20 +0200113module AP_MODULE_DECLARE_DATA netconf_module;
114
115typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200116 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200117 apr_proc_t *forkproc;
118 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200119} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200120
David Kupka8e60a372012-09-04 09:15:20 +0200121struct pass_to_thread {
122 int client; /**< opened socket */
123 apr_pool_t * pool; /**< ?? */
124 server_rec * server; /**< ?? */
125 apr_hash_t * netconf_sessions_list; /**< ?? */
126};
127
128struct session_with_mutex {
129 struct nc_session * session; /**< netconf session */
130 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
131};
132
133pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
134
Radek Krejci469aab82012-07-22 18:42:20 +0200135volatile int isterminated = 0;
136
137static char* password;
138
139
140static void signal_handler(int sign)
141{
142 switch (sign) {
143 case SIGTERM:
144 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200145 break;
146 }
147}
148
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200150{
Radek Krejcif23850c2012-07-23 16:14:17 +0200151 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
152 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200153 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200154
Radek Krejci469aab82012-07-22 18:42:20 +0200155 apr_sha1_ctx_t sha1_ctx;
156 apr_sha1_init(&sha1_ctx);
157 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
158 apr_sha1_update(&sha1_ctx, port, strlen(port));
159 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200160 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200161
Radek Krejcif23850c2012-07-23 16:14:17 +0200162 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200163 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200164 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200165 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200166 }
167 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200168
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200169 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200170}
171
172int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
173{
174 /* always approve */
175 return (EXIT_SUCCESS);
176}
177
178char* netconf_callback_sshauth_password (const char* username, const char* hostname)
179{
180 char* buf;
181
182 buf = malloc ((strlen(password) + 1) * sizeof(char));
183 apr_cpystrn(buf, password, strlen(password) + 1);
184
185 return (buf);
186}
187
188void netconf_callback_sshauth_interactive (const char* name,
189 int name_len,
190 const char* instruction,
191 int instruction_len,
192 int num_prompts,
193 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
194 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
195 void** abstract)
196{
197 int i;
198
199 for (i=0; i<num_prompts; i++) {
200 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
201 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
202 responses[i].length = strlen(responses[i].text) + 1;
203 }
204
205 return;
206}
207
Radek Krejcic11fd862012-07-26 12:41:21 +0200208static json_object *err_reply = NULL;
209void netconf_callback_error_process(const char* tag,
210 const char* type,
211 const char* severity,
212 const char* apptag,
213 const char* path,
214 const char* message,
215 const char* attribute,
216 const char* element,
217 const char* ns,
218 const char* sid)
219{
220 err_reply = json_object_new_object();
221 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
222 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
223 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
224 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
225 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
226 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
227 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
228 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
229 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
230 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
231 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
232}
233
Kupka David00b9c5c2012-09-05 09:45:50 +0200234static 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 +0200235{
Radek Krejci469aab82012-07-22 18:42:20 +0200236 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200237 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200238 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200239
240 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200241 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200242 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
243
Radek Krejci469aab82012-07-22 18:42:20 +0200244 /* if connected successful, add session to the list */
245 if (session != NULL) {
246 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200247 session_key = gen_ncsession_hash(
248 (host==NULL) ? "localhost" : host,
249 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200250 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200251
252 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
253 nc_session_free(session);
254 free (locked_session);
255 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
256 return NULL;
257 }
258 locked_session->session = session;
259 pthread_mutex_init (&locked_session->lock, NULL);
260 /* get exclusive access to sessions_list (conns) */
261 if (pthread_rwlock_wrlock (&session_lock) != 0) {
262 nc_session_free(session);
263 free (locked_session);
264 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
265 return NULL;
266 }
267 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
268 /* end of critical section */
269 if (pthread_rwlock_unlock (&session_lock) != 0) {
270 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
271 return NULL;
272 }
Radek Krejci469aab82012-07-22 18:42:20 +0200273 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200274 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200275 } else {
276 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200277 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200278 }
279
Radek Krejci469aab82012-07-22 18:42:20 +0200280}
281
Radek Krejci80c10d92012-07-30 08:38:50 +0200282static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200283{
284 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200285 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200286
Radek Krejcif23850c2012-07-23 16:14:17 +0200287 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200288 /* get exclusive (write) access to sessions_list (conns) */
289 if (pthread_rwlock_wrlock (&session_lock) != 0) {
290 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
291 return EXIT_FAILURE;
292 }
293 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
294 if (locked_session != NULL) {
295 pthread_mutex_destroy(&locked_session->lock);
296 ns = locked_session->session;
297 free (locked_session);
298 }
Radek Krejci469aab82012-07-22 18:42:20 +0200299 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100300 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200301 nc_session_free (ns);
302 ns = NULL;
303
304 /* remove session from the active sessions list */
305 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200306 /* end of critical section */
307 if (pthread_rwlock_unlock (&session_lock) != 0) {
308 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
309 return EXIT_FAILURE;
310 }
Radek Krejci469aab82012-07-22 18:42:20 +0200311 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200312
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200313 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200314 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200315 if (pthread_rwlock_unlock (&session_lock) != 0) {
316 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
317 return EXIT_FAILURE;
318 }
Radek Krejci469aab82012-07-22 18:42:20 +0200319 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200320 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200321 }
322}
323
Radek Krejci80c10d92012-07-30 08:38:50 +0200324static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200325{
326 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200327 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200328 nc_reply* reply;
329 int retval = EXIT_SUCCESS;
330
Radek Krejci8e4632a2012-07-26 13:40:34 +0200331 /* check requests */
332 if (rpc == NULL) {
333 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
334 return (EXIT_FAILURE);
335 }
336
David Kupka8e60a372012-09-04 09:15:20 +0200337 /* get non-exclusive (read) access to sessions_list (conns) */
338 if (pthread_rwlock_rdlock (&session_lock) != 0) {
339 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
340 return EXIT_FAILURE;
341 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200342 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200343 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
344 if (locked_session != NULL) {
345 session = locked_session->session;
346 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200347 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200348 /* get exclusive access to session */
349 if (pthread_mutex_lock(&locked_session->lock) != 0) {
350 /* unlock before returning error */
351 if (pthread_rwlock_unlock (&session_lock) != 0) {
352 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
353 return EXIT_FAILURE;
354 }
355 return EXIT_FAILURE;
356 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200357 /* send the request and get the reply */
358 nc_session_send_rpc (session, rpc);
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100359 if (nc_session_recv_reply (session, NCRPCTIMEOUT, &reply) == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200360 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200361 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
David Kupka8e60a372012-09-04 09:15:20 +0200362 /* first release exclusive lock for this session */
363 pthread_mutex_unlock(&locked_session->lock);
364 /* release read lock, netconf_close will get exclusive access and close this session */
365 if (pthread_rwlock_unlock (&session_lock) != 0) {
366 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
367 return EXIT_FAILURE;
368 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200369 netconf_close(server, conns, session_key);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200370 return (EXIT_FAILURE);
371 }
David Kupka8e60a372012-09-04 09:15:20 +0200372 /* first release exclusive lock for this session */
373 pthread_mutex_unlock(&locked_session->lock);
374 /* unlock before returning error */
375 if (pthread_rwlock_unlock (&session_lock) != 0) {
376 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
377 return EXIT_FAILURE;
378 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200379 /* there is error handled by callback */
380 return (EXIT_FAILURE);
381 }
David Kupka8e60a372012-09-04 09:15:20 +0200382 /* first release exclusive lock for this session */
383 pthread_mutex_unlock(&locked_session->lock);
384 /* end of critical section */
385 if (pthread_rwlock_unlock (&session_lock) != 0) {
386 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
387 return EXIT_FAILURE;
388 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200389
390 switch (nc_reply_get_type (reply)) {
391 case NC_REPLY_OK:
392 retval = EXIT_SUCCESS;
393 break;
394 default:
395 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
396 retval = EXIT_FAILURE;
397 break;
398 }
399 nc_reply_free(reply);
400 return (retval);
401 } else {
402 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
403 return (EXIT_FAILURE);
404 }
405}
Radek Krejci80c10d92012-07-30 08:38:50 +0200406
407static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200408{
409 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200410 struct session_with_mutex * locked_session;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200411 nc_reply* reply;
412 char* data;
413
414 /* check requests */
415 if (rpc == NULL) {
416 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
417 return (NULL);
418 }
419
David Kupka8e60a372012-09-04 09:15:20 +0200420 /* get non-exclusive (read) access to sessions_list (conns) */
421 if (pthread_rwlock_rdlock (&session_lock) != 0) {
422 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
423 return NULL;
424 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200425 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200426 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
427 if (locked_session != NULL) {
428 session = locked_session->session;
429 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200430 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200431 /* get exclusive access to session */
432 if (pthread_mutex_lock(&locked_session->lock) != 0) {
433 /* unlock before returning error */
434 if (pthread_rwlock_unlock (&session_lock) != 0) {
435 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
436 return NULL;
437 }
438 return NULL;
439 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200440 /* send the request and get the reply */
441 nc_session_send_rpc (session, rpc);
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100442 if (nc_session_recv_reply (session, NCRPCTIMEOUT, &reply) == 0) {
Radek Krejci8e4632a2012-07-26 13:40:34 +0200443 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
444 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
David Kupka8e60a372012-09-04 09:15:20 +0200445 /* first release exclusive lock for this session */
446 pthread_mutex_unlock(&locked_session->lock);
447 /* release read lock, netconf_close will get exclusive access and close this session */
448 if (pthread_rwlock_unlock (&session_lock) != 0) {
449 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
450 return NULL;
451 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200452 netconf_close(server, conns, session_key);
453 return (NULL);
454 }
David Kupka8e60a372012-09-04 09:15:20 +0200455 /* first release exclusive lock for this session */
456 pthread_mutex_unlock(&locked_session->lock);
457 /* unlock before returning error */
458 if (pthread_rwlock_unlock (&session_lock) != 0) {
459 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
460 return NULL;
461 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200462 /* there is error handled by callback */
463 return (NULL);
464 }
David Kupka8e60a372012-09-04 09:15:20 +0200465 /* first release exclusive lock for this session */
466 pthread_mutex_unlock(&locked_session->lock);
467 /* end of critical section */
468 if (pthread_rwlock_unlock (&session_lock) != 0) {
469 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
470 return NULL;
471 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200472
473 switch (nc_reply_get_type (reply)) {
474 case NC_REPLY_DATA:
475 if ((data = nc_reply_get_data (reply)) == NULL) {
476 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
477 return (NULL);
478 }
479 break;
480 default:
481 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
482 return (NULL);
483 }
484 nc_reply_free(reply);
485
486 return (data);
487 } else {
488 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
489 return (NULL);
490 }
491}
492
Radek Krejci80c10d92012-07-30 08:38:50 +0200493static 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 +0200494{
495 nc_rpc* rpc;
496 struct nc_filter *f = NULL;
497 char* data = NULL;
498
499 /* create filter if set */
500 if (filter != NULL) {
501 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
502 }
503
504 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100505 rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200506 nc_filter_free(f);
507 if (rpc == NULL) {
508 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
509 return (NULL);
510 }
511
512 data = netconf_opdata(server, conns, session_key, rpc);
513 nc_rpc_free (rpc);
514 return (data);
515}
516
Radek Krejci80c10d92012-07-30 08:38:50 +0200517static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200518{
519 nc_rpc* rpc;
520 struct nc_filter *f = NULL;
521 char* data = NULL;
522
523 /* create filter if set */
524 if (filter != NULL) {
525 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
526 }
527
528 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100529 rpc = nc_rpc_get (f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200530 nc_filter_free(f);
531 if (rpc == NULL) {
532 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
533 return (NULL);
534 }
535
536 data = netconf_opdata(server, conns, session_key, rpc);
537 nc_rpc_free (rpc);
538 return (data);
539}
540
Radek Krejci80c10d92012-07-30 08:38:50 +0200541static 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 +0200542{
543 nc_rpc* rpc;
544 int retval = EXIT_SUCCESS;
545
546 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100547 rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200548 if (rpc == NULL) {
549 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
550 return (EXIT_FAILURE);
551 }
552
553 retval = netconf_op(server, conns, session_key, rpc);
554 nc_rpc_free (rpc);
555 return (retval);
556}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200557
Radek Krejci80c10d92012-07-30 08:38:50 +0200558static 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 +0200559{
560 nc_rpc* rpc;
561 int retval = EXIT_SUCCESS;
562
563 /* create requests */
564 rpc = nc_rpc_editconfig(target, defop, erropt, config);
565 if (rpc == NULL) {
566 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
567 return (EXIT_FAILURE);
568 }
569
570 retval = netconf_op(server, conns, session_key, rpc);
571 nc_rpc_free (rpc);
572 return (retval);
573}
574
Radek Krejci80c10d92012-07-30 08:38:50 +0200575static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200576{
577 nc_rpc* rpc;
578 int retval = EXIT_SUCCESS;
579
580 /* create requests */
581 rpc = nc_rpc_killsession(sid);
582 if (rpc == NULL) {
583 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
584 return (EXIT_FAILURE);
585 }
586
587 retval = netconf_op(server, conns, session_key, rpc);
588 nc_rpc_free (rpc);
589 return (retval);
590}
591
Radek Krejci80c10d92012-07-30 08:38:50 +0200592static 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 +0200593{
594 nc_rpc* rpc;
595 int retval = EXIT_SUCCESS;
596
597 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200598 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200599 if (rpc == NULL) {
600 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
601 return (EXIT_FAILURE);
602 }
603
604 retval = netconf_op(server, conns, session_key, rpc);
605 nc_rpc_free (rpc);
606 return (retval);
607}
608
Radek Krejci80c10d92012-07-30 08:38:50 +0200609static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200610{
611 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
612}
613
Radek Krejci80c10d92012-07-30 08:38:50 +0200614static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200615{
616 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
617}
618
Radek Krejci80c10d92012-07-30 08:38:50 +0200619static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200620{
621 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
622}
623
Radek Krejci80c10d92012-07-30 08:38:50 +0200624/**
625 * @return REPLY_OK: 0, *data == NULL
626 * REPLY_DATA: 0, *data != NULL
627 * REPLY_ERROR: 1, *data == NULL
628 */
629static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
630{
631 struct nc_session *session = NULL;
632 nc_reply* reply;
633 nc_rpc* rpc;
634 int retval = EXIT_SUCCESS;
635
636 /* create requests */
637 rpc = nc_rpc_generic(content);
638 if (rpc == NULL) {
639 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
640 return (EXIT_FAILURE);
641 }
642
643 *data = NULL;
644
645 /* get session where send the RPC */
646 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
647 if (session != NULL) {
648 /* send the request and get the reply */
649 nc_session_send_rpc (session, rpc);
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100650 if (nc_session_recv_reply (session, NCRPCTIMEOUT, &reply) == 0) {
Radek Krejci80c10d92012-07-30 08:38:50 +0200651 nc_rpc_free (rpc);
652 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
653 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
654 netconf_close(server, conns, session_key);
655 return (EXIT_FAILURE);
656 }
657
658 /* there is error handled by callback */
659 return (EXIT_FAILURE);
660 }
661 nc_rpc_free (rpc);
662
663 switch (nc_reply_get_type (reply)) {
664 case NC_REPLY_DATA:
665 if ((*data = nc_reply_get_data (reply)) == NULL) {
666 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
667 return (EXIT_FAILURE);
668 }
669 retval = EXIT_SUCCESS;
670 break;
671 case NC_REPLY_OK:
672 retval = EXIT_SUCCESS;
673 break;
674 default:
675 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
676 retval = EXIT_FAILURE;
677 break;
678 }
679 nc_reply_free(reply);
680
681 return (retval);
682 } else {
683 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
684 return (EXIT_FAILURE);
685 }
686}
687
Radek Krejci469aab82012-07-22 18:42:20 +0200688server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200689void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200690{
Radek Krejci7338bde2012-08-10 12:57:30 +0200691 switch (level) {
692 case NC_VERB_ERROR:
693 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
694 break;
695 case NC_VERB_WARNING:
696 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
697 break;
698 case NC_VERB_VERBOSE:
699 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
700 break;
701 case NC_VERB_DEBUG:
702 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
703 break;
704 }
Radek Krejci469aab82012-07-22 18:42:20 +0200705}
706
David Kupka8e60a372012-09-04 09:15:20 +0200707void * thread_routine (void * arg)
708{
709 void * retval = NULL;
710
711 ssize_t buffer_len;
712 struct pollfd fds;
713 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200714 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200715 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200716 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200717 char* session_key, *data;
718 const char *host, *port, *user, *pass;
719 const char *msgtext, *cpbltstr;
720 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
721 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200722 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200723 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200724 NC_DATASTORE ds_type_s, ds_type_t;
725 NC_EDIT_DEFOP_TYPE defop_type = 0;
726 NC_EDIT_ERROPT_TYPE erropt_type = 0;
727
728 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
729 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
730 server_rec * server = ((struct pass_to_thread*)arg)->server;
731 int client = ((struct pass_to_thread*)arg)->client;
732
733 char * buffer, chunk_len_str[12], *chunked_msg;
734 char c;
735
736 while (!isterminated) {
737 fds.fd = client;
738 fds.events = POLLIN;
739 fds.revents = 0;
740
741 status = poll(&fds, 1, 1000);
742
743 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
744 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
745 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
746 continue;
747 } else if (status < 0) {
748 /* 0: poll time outed
749 * close socket and ignore this request from the client, it can try it again
750 * -1: poll failed
751 * something wrong happend, close this socket and wait for another request
752 */
753 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
754 close(client);
755 break;
756 }
757 /* status > 0 */
758
759 /* check the status of the socket */
760
761 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
762 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
763 /* close client's socket (it's probably already closed by client */
764 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
765 close(client);
766 break;
767 }
768
769 /* read json in chunked framing */
770 buffer_size = 0;
771 buffer_len = 0;
772 buffer = NULL;
773 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200774 /* read chunk length */
775 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
776 free (buffer);
777 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200778 break;
779 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200780 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
781 free (buffer);
782 buffer = NULL;
783 break;
784 }
785 i=0;
786 memset (chunk_len_str, 0, 12);
787 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
788 if (i==0 && c == '#') {
789 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
790 /* end but invalid */
791 free (buffer);
792 buffer = NULL;
793 }
794 /* end of message, double-loop break */
795 goto msg_complete;
796 }
797 chunk_len_str[i++] = c;
798 }
799 if (c != '\n') {
800 free (buffer);
801 buffer = NULL;
802 break;
803 }
804 if ((chunk_len = atoi (chunk_len_str)) == 0) {
805 free (buffer);
806 buffer = NULL;
807 break;
808 }
809 buffer_size += chunk_len+1;
810 buffer = realloc (buffer, sizeof(char)*buffer_size);
811 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
812 free (buffer);
813 buffer = NULL;
814 break;
815 }
816 buffer_len += ret;
817 }
818msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200819
820 if (buffer != NULL) {
821 request = json_tokener_parse(buffer);
822 operation = json_object_get_int(json_object_object_get(request, "type"));
823
824 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
825 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
826 if (operation != MSG_CONNECT && session_key == NULL) {
827 reply = json_object_new_object();
828 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
829 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
830 msgtext = json_object_to_json_string(reply);
831 send(client, msgtext, strlen(msgtext) + 1, 0);
832 json_object_put(reply);
833 /* there is some stupid client, so close the connection to give a chance to some other client */
834 close(client);
835 break;
836 }
837
838 /* get parameters */
839 ds_type_t = -1;
840 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
841 if (strcmp(target, "running") == 0) {
842 ds_type_t = NC_DATASTORE_RUNNING;
843 } else if (strcmp(target, "startup") == 0) {
844 ds_type_t = NC_DATASTORE_STARTUP;
845 } else if (strcmp(target, "candidate") == 0) {
846 ds_type_t = NC_DATASTORE_CANDIDATE;
847 }
848 }
849 ds_type_s = -1;
850 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
851 if (strcmp(source, "running") == 0) {
852 ds_type_s = NC_DATASTORE_RUNNING;
853 } else if (strcmp(source, "startup") == 0) {
854 ds_type_s = NC_DATASTORE_STARTUP;
855 } else if (strcmp(source, "candidate") == 0) {
856 ds_type_s = NC_DATASTORE_CANDIDATE;
857 }
858 }
859
860 /* null global JSON error-reply */
861 err_reply = NULL;
862
863 /* prepare reply envelope */
864 reply = json_object_new_object();
865
866 /* process required operation */
867 switch (operation) {
868 case MSG_CONNECT:
869 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
870
871 host = json_object_get_string(json_object_object_get(request, "host"));
872 port = json_object_get_string(json_object_object_get(request, "port"));
873 user = json_object_get_string(json_object_object_get(request, "user"));
874 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200875 capabilities = json_object_object_get(request, "capabilities");
876 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200877 cpblts = nc_cpblts_new (NULL);
878 for (i=0; i<len; i++) {
879 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
880 }
881 } else {
882 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
883 }
David Kupka8e60a372012-09-04 09:15:20 +0200884 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
885 if ((host == NULL) || (user == NULL)) {
886 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
887 session_key = NULL;
888 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200889 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
890 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200891 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
892 }
893
894 reply = json_object_new_object();
895 if (session_key == NULL) {
896 /* negative reply */
897 if (err_reply == NULL) {
898 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
899 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
900 } else {
901 /* use filled err_reply from libnetconf's callback */
902 json_object_put(reply);
903 reply = err_reply;
904 }
905 } else {
906 /* positive reply */
907 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
908 json_object_object_add(reply, "session", json_object_new_string(session_key));
909
910 free(session_key);
911 }
912
913 break;
914 case MSG_GET:
915 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
916
917 filter = json_object_get_string(json_object_object_get(request, "filter"));
918
919 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
920
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200921 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200922 if (err_reply == NULL) {
923 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
924 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
925 } else {
926 /* use filled err_reply from libnetconf's callback */
927 json_object_put(reply);
928 reply = err_reply;
929 }
930 } else {
931 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
932 json_object_object_add(reply, "data", json_object_new_string(data));
933 }
934 break;
935 case MSG_GETCONFIG:
936 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
937
938 filter = json_object_get_string(json_object_object_get(request, "filter"));
939
940 if (ds_type_s == -1) {
941 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
942 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
943 break;
944 }
945
946 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
947 if (err_reply == NULL) {
948 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
949 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
950 } else {
951 /* use filled err_reply from libnetconf's callback */
952 json_object_put(reply);
953 reply = err_reply;
954 }
955 } else {
956 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
957 json_object_object_add(reply, "data", json_object_new_string(data));
958 }
959 break;
960 case MSG_EDITCONFIG:
961 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
962
963 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
964 if (defop != NULL) {
965 if (strcmp(defop, "merge") == 0) {
966 defop_type = NC_EDIT_DEFOP_MERGE;
967 } else if (strcmp(defop, "replace") == 0) {
968 defop_type = NC_EDIT_DEFOP_REPLACE;
969 } else if (strcmp(defop, "none") == 0) {
970 defop_type = NC_EDIT_DEFOP_NONE;
971 } else {
972 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
973 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
974 break;
975 }
976 } else {
977 defop_type = 0;
978 }
979
980 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
981 if (erropt != NULL) {
982 if (strcmp(erropt, "continue-on-error") == 0) {
983 erropt_type = NC_EDIT_ERROPT_CONT;
984 } else if (strcmp(erropt, "stop-on-error") == 0) {
985 erropt_type = NC_EDIT_ERROPT_STOP;
986 } else if (strcmp(erropt, "rollback-on-error") == 0) {
987 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
988 } else {
989 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
990 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
991 break;
992 }
993 } else {
994 erropt_type = 0;
995 }
996
997 if (ds_type_t == -1) {
998 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
999 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1000 break;
1001 }
1002
1003 config = json_object_get_string(json_object_object_get(request, "config"));
1004 if (config == NULL) {
1005 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1006 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1007 break;
1008 }
1009
1010 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1011 if (err_reply == NULL) {
1012 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1013 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1014 } else {
1015 /* use filled err_reply from libnetconf's callback */
1016 json_object_put(reply);
1017 reply = err_reply;
1018 }
1019 } else {
1020 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1021 }
1022 break;
1023 case MSG_COPYCONFIG:
1024 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1025 config = NULL;
1026
1027 if (source == NULL) {
1028 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001029 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001030 config = json_object_get_string(json_object_object_get(request, "config"));
1031 } else if (ds_type_s == -1) {
1032 /* source datastore specified, but it is invalid */
1033 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1034 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
1035 break;
1036 }
1037
1038 if (ds_type_t == -1) {
1039 /* invalid target datastore specified */
1040 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1041 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1042 break;
1043 }
1044
1045 if (source == NULL && config == NULL) {
1046 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1047 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1048 } else {
1049 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1050 if (err_reply == NULL) {
1051 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1052 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1053 } else {
1054 /* use filled err_reply from libnetconf's callback */
1055 json_object_put(reply);
1056 reply = err_reply;
1057 }
1058 } else {
1059 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1060 }
1061 }
1062 break;
1063 case MSG_DELETECONFIG:
1064 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1065 /* no break - unifying code */
1066 case MSG_LOCK:
1067 if (operation == MSG_LOCK) {
1068 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1069 }
1070 /* no break - unifying code */
1071 case MSG_UNLOCK:
1072 if (operation == MSG_UNLOCK) {
1073 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1074 }
1075
1076 if (ds_type_t == -1) {
1077 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1078 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1079 break;
1080 }
1081
1082 switch(operation) {
1083 case MSG_DELETECONFIG:
1084 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1085 break;
1086 case MSG_LOCK:
1087 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1088 break;
1089 case MSG_UNLOCK:
1090 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1091 break;
1092 default:
1093 status = -1;
1094 break;
1095 }
1096
1097 if (status != EXIT_SUCCESS) {
1098 if (err_reply == NULL) {
1099 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1100 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
1101 } else {
1102 /* use filled err_reply from libnetconf's callback */
1103 json_object_put(reply);
1104 reply = err_reply;
1105 }
1106 } else {
1107 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1108 }
1109 break;
1110 case MSG_KILL:
1111 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1112
1113 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1114
1115 if (sid == NULL) {
1116 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1117 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1118 break;
1119 }
1120
1121 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1122 if (err_reply == NULL) {
1123 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1124 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1125 } else {
1126 /* use filled err_reply from libnetconf's callback */
1127 json_object_put(reply);
1128 reply = err_reply;
1129 }
1130 } else {
1131 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1132 }
1133 break;
1134 case MSG_DISCONNECT:
1135 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1136
1137 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1138 if (err_reply == NULL) {
1139 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1140 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1141 } else {
1142 /* use filled err_reply from libnetconf's callback */
1143 json_object_put(reply);
1144 reply = err_reply;
1145 }
1146 } else {
1147 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1148 }
1149 break;
1150 case MSG_INFO:
1151 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1152
Kupka Davidda134a12012-09-06 14:12:16 +02001153 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1154 if (locked_session != NULL) {
1155 session = locked_session->session;
1156 } else {
1157 session = NULL;
1158 }
David Kupka8e60a372012-09-04 09:15:20 +02001159 if (session != NULL) {
1160 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1161 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1162 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1163 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1164 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1165 cpblts = nc_session_get_cpblts (session);
1166 if (cpblts != NULL) {
1167 json_obj = json_object_new_array();
1168 nc_cpblts_iter_start (cpblts);
1169 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1170 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1171 }
1172 json_object_object_add(reply, "capabilities", json_obj);
1173 }
1174 } else {
1175 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1176 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1177 }
1178
1179 break;
1180 case MSG_GENERIC:
1181 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1182
1183 config = json_object_get_string(json_object_object_get(request, "content"));
1184
1185 if (config == NULL) {
1186 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1187 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1188 break;
1189 }
1190
1191 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1192 if (err_reply == NULL) {
1193 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1194 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1195 } else {
1196 /* use filled err_reply from libnetconf's callback */
1197 json_object_put(reply);
1198 reply = err_reply;
1199 }
1200 } else {
1201 if (data == NULL) {
1202 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1203 } else {
1204 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1205 json_object_object_add(reply, "data", json_object_new_string(data));
1206 }
1207 }
1208 break;
1209 default:
1210 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1211 reply = json_object_new_object();
1212 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1213 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1214 break;
1215 }
1216 json_object_put(request);
1217
David Kupka1e3e4c82012-09-04 09:32:15 +02001218 /* send reply to caller */
1219 if (reply != NULL) {
1220 msgtext = json_object_to_json_string(reply);
1221 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1222 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001223 break;
1224 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001225 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1226 json_object_put(reply);
1227 free (chunked_msg);
1228 free (buffer);
1229 } else {
1230 break;
David Kupka8e60a372012-09-04 09:15:20 +02001231 }
1232 }
1233 }
1234
1235 free (arg);
1236
1237 return retval;
1238}
1239
Radek Krejcif23850c2012-07-23 16:14:17 +02001240/*
1241 * This is actually implementation of NETCONF client
1242 * - requests are received from UNIX socket in the predefined format
1243 * - results are replied through the same way
1244 * - the daemon run as a separate process, but it is started and stopped
1245 * automatically by Apache.
1246 *
1247 */
Radek Krejci469aab82012-07-22 18:42:20 +02001248static void forked_proc(apr_pool_t * pool, server_rec * server)
1249{
1250 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001251 int lsock, client, ret, i, pthread_count = 0;
1252 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001253 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001254 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001255 struct pass_to_thread * arg;
1256 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1257 struct timespec maxtime;
1258 pthread_rwlockattr_t lock_attrs;
1259
1260 /* wait at most 5 secons for every thread to terminate */
1261 maxtime.tv_sec = 5;
1262 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001263
Radek Krejcif23850c2012-07-23 16:14:17 +02001264 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001265 unixd_setup_child();
1266
Radek Krejciae021c12012-07-25 18:03:52 +02001267 cfg = ap_get_module_config(server->module_config, &netconf_module);
1268 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001269 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1270 return;
1271 }
Radek Krejci469aab82012-07-22 18:42:20 +02001272
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001273 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001274 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1275 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1276 return;
1277 }
1278
1279 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001280 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001281 unlink(local.sun_path);
1282 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1283
1284 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1285 if (errno == EADDRINUSE) {
1286 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1287 close(lsock);
1288 exit(0);
1289 }
1290 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1291 close(lsock);
1292 return;
1293 }
1294
1295 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1296 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1297 close(lsock);
1298 return;
1299 }
1300
1301 /* prepare internal lists */
1302 netconf_sessions_list = apr_hash_make(pool);
1303
1304 /* setup libnetconf's callbacks */
1305 nc_verbosity(NC_VERB_DEBUG);
1306 clb_print_server = server;
1307 nc_callback_print(clb_print);
1308 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1309 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1310 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001311 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001312
1313 /* disable publickey authentication */
1314 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1315
David Kupka8e60a372012-09-04 09:15:20 +02001316 /* create mutex protecting session list */
1317 pthread_rwlockattr_init(&lock_attrs);
1318 /* rwlock is shared only with threads in this process */
1319 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1320 /* create rw lock */
1321 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1322 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1323 close (lsock);
1324 return;
1325 }
1326
Radek Krejci469aab82012-07-22 18:42:20 +02001327 while (isterminated == 0) {
1328 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1329
1330 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001331 len = sizeof(remote);
1332 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001333 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1334 apr_sleep(SLEEP_TIME);
1335 continue;
1336 } else if (client == -1 && (errno == EINTR)) {
1337 continue;
1338 } else if (client == -1) {
1339 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1340 continue;
1341 }
Radek Krejci469aab82012-07-22 18:42:20 +02001342
1343 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001344 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001345
David Kupka8e60a372012-09-04 09:15:20 +02001346 arg = malloc (sizeof(struct pass_to_thread));
1347 arg->client = client;
1348 arg->pool = pool;
1349 arg->server = server;
1350 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001351
David Kupka8e60a372012-09-04 09:15:20 +02001352 /* start new thread. It will serve this particular request and then terminate */
1353 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1354 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1355 } else {
1356 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1357 pthread_count++;
1358 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1359 ptids[pthread_count] = 0;
1360 }
Radek Krejci469aab82012-07-22 18:42:20 +02001361
David Kupka8e60a372012-09-04 09:15:20 +02001362 /* check if some thread already terminated, free some resources by joining it */
1363 for (i=0; i<pthread_count; i++) {
1364 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1365 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1366 pthread_count--;
1367 if (pthread_count > 0) {
1368 /* place last Thread ID on the place of joined one */
1369 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001370 }
Radek Krejci469aab82012-07-22 18:42:20 +02001371 }
1372 }
David Kupka8e60a372012-09-04 09:15:20 +02001373 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001374 }
1375
David Kupka8e60a372012-09-04 09:15:20 +02001376 /* join all threads */
1377 for (i=0; i<pthread_count; i++) {
1378 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1379 }
1380 free (ptids);
1381
Radek Krejci469aab82012-07-22 18:42:20 +02001382 close(lsock);
1383
David Kupka8e60a372012-09-04 09:15:20 +02001384 /* destroy rwlock */
1385 pthread_rwlock_destroy(&session_lock);
1386 pthread_rwlockattr_destroy(&lock_attrs);
1387
Radek Krejci469aab82012-07-22 18:42:20 +02001388 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1389
1390 exit(APR_SUCCESS);
1391}
1392
Radek Krejcif23850c2012-07-23 16:14:17 +02001393static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001394{
Radek Krejcif23850c2012-07-23 16:14:17 +02001395 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1396
1397 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1398 apr_pool_create(&config->pool, pool);
1399 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001400 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001401
1402 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001403}
1404
1405static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1406 apr_pool_t * plog, server_rec * s)
1407{
Radek Krejcif23850c2012-07-23 16:14:17 +02001408 mod_netconf_cfg *config;
1409 apr_status_t res;
1410
Radek Krejci469aab82012-07-22 18:42:20 +02001411 /* These two help ensure that we only init once. */
1412 void *data;
Radek Krejcif23850c2012-07-23 16:14:17 +02001413 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001414
1415 /*
1416 * The following checks if this routine has been called before.
1417 * This is necessary because the parent process gets initialized
1418 * a couple of times as the server starts up.
1419 */
1420 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1421 if (!data) {
1422 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1423 return (OK);
1424 }
1425
Radek Krejcif23850c2012-07-23 16:14:17 +02001426 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1427 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001428
Radek Krejcif23850c2012-07-23 16:14:17 +02001429 if (config && config->forkproc == NULL) {
1430 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1431 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001432 switch (res) {
1433 case APR_INCHILD:
1434 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001435 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001436 apr_signal(SIGTERM, signal_handler);
1437
1438 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001439 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001440
1441 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001442 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001443
Radek Krejcif23850c2012-07-23 16:14:17 +02001444 /* I never should be here, wtf?!? */
1445 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001446 exit(APR_EGENERAL);
1447 break;
1448 case APR_INPARENT:
1449 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001450 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001451 break;
1452 default:
1453 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1454 break;
1455 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001456 } else {
1457 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001458 }
1459
1460 return OK;
1461}
1462
Radek Krejci469aab82012-07-22 18:42:20 +02001463/**
1464 * Register module hooks
1465 */
1466static void mod_netconf_register_hooks(apr_pool_t * p)
1467{
Radek Krejcif23850c2012-07-23 16:14:17 +02001468 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001469}
1470
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001471static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1472{
1473 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1474 return NULL;
1475}
1476
1477static const command_rec netconf_cmds[] = {
1478 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1479 {NULL}
1480};
1481
Radek Krejci469aab82012-07-22 18:42:20 +02001482/* Dispatch list for API hooks */
1483module AP_MODULE_DECLARE_DATA netconf_module = {
1484 STANDARD20_MODULE_STUFF,
1485 NULL, /* create per-dir config structures */
1486 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001487 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001488 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001489 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001490 mod_netconf_register_hooks /* register hooks */
1491};