blob: 5230bf8ff71e82c99174589d6a6d5895ebeb70de [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 */
Tomas Cejka027f3bc2012-11-10 20:28:36 +010082#define NCWITHDEFAULTS NCWD_MODE_DISABLED
83
Radek Krejci469aab82012-07-22 18:42:20 +020084struct timeval timeout = { 1, 0 };
85
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020086typedef enum MSG_TYPE {
87 REPLY_OK,
88 REPLY_DATA,
89 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020090 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020091 MSG_CONNECT,
92 MSG_DISCONNECT,
93 MSG_GET,
94 MSG_GETCONFIG,
95 MSG_EDITCONFIG,
96 MSG_COPYCONFIG,
97 MSG_DELETECONFIG,
98 MSG_LOCK,
99 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200100 MSG_KILL,
Radek Krejci80c10d92012-07-30 08:38:50 +0200101 MSG_INFO,
102 MSG_GENERIC
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200103} MSG_TYPE;
104
Radek Krejci469aab82012-07-22 18:42:20 +0200105#define MSG_OK 0
106#define MSG_OPEN 1
107#define MSG_DATA 2
108#define MSG_CLOSE 3
109#define MSG_ERROR 4
110#define MSG_UNKNOWN 5
111
Radek Krejci469aab82012-07-22 18:42:20 +0200112module AP_MODULE_DECLARE_DATA netconf_module;
113
114typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200115 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200116 apr_proc_t *forkproc;
117 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200118} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200119
David Kupka8e60a372012-09-04 09:15:20 +0200120struct pass_to_thread {
121 int client; /**< opened socket */
122 apr_pool_t * pool; /**< ?? */
123 server_rec * server; /**< ?? */
124 apr_hash_t * netconf_sessions_list; /**< ?? */
125};
126
127struct session_with_mutex {
128 struct nc_session * session; /**< netconf session */
129 pthread_mutex_t lock; /**< mutex protecting the session from multiple access */
130};
131
132pthread_rwlock_t session_lock; /**< mutex protecting netconf_session_list from multiple access errors */
133
Radek Krejci469aab82012-07-22 18:42:20 +0200134volatile int isterminated = 0;
135
136static char* password;
137
138
139static void signal_handler(int sign)
140{
141 switch (sign) {
142 case SIGTERM:
143 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200144 break;
145 }
146}
147
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200148static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200149{
Radek Krejcif23850c2012-07-23 16:14:17 +0200150 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
151 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200152 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200153
Radek Krejci469aab82012-07-22 18:42:20 +0200154 apr_sha1_ctx_t sha1_ctx;
155 apr_sha1_init(&sha1_ctx);
156 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
157 apr_sha1_update(&sha1_ctx, port, strlen(port));
158 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200159 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200160
Radek Krejcif23850c2012-07-23 16:14:17 +0200161 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200162 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200163 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200164 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200165 }
166 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200167
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200168 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200169}
170
171int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
172{
173 /* always approve */
174 return (EXIT_SUCCESS);
175}
176
177char* netconf_callback_sshauth_password (const char* username, const char* hostname)
178{
179 char* buf;
180
181 buf = malloc ((strlen(password) + 1) * sizeof(char));
182 apr_cpystrn(buf, password, strlen(password) + 1);
183
184 return (buf);
185}
186
187void netconf_callback_sshauth_interactive (const char* name,
188 int name_len,
189 const char* instruction,
190 int instruction_len,
191 int num_prompts,
192 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
193 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
194 void** abstract)
195{
196 int i;
197
198 for (i=0; i<num_prompts; i++) {
199 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
200 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
201 responses[i].length = strlen(responses[i].text) + 1;
202 }
203
204 return;
205}
206
Radek Krejcic11fd862012-07-26 12:41:21 +0200207static json_object *err_reply = NULL;
208void netconf_callback_error_process(const char* tag,
209 const char* type,
210 const char* severity,
211 const char* apptag,
212 const char* path,
213 const char* message,
214 const char* attribute,
215 const char* element,
216 const char* ns,
217 const char* sid)
218{
219 err_reply = json_object_new_object();
220 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
221 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
222 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
223 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
224 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
225 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
226 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
227 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
228 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
229 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
230 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
231}
232
Kupka David00b9c5c2012-09-05 09:45:50 +0200233static 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 +0200234{
Radek Krejci469aab82012-07-22 18:42:20 +0200235 struct nc_session* session;
David Kupka8e60a372012-09-04 09:15:20 +0200236 struct session_with_mutex * locked_session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200237 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200238
239 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200240 password = (char*)pass;
David Kupka8e60a372012-09-04 09:15:20 +0200241 session = nc_session_connect(host, (unsigned short) atoi (port), user, cpblts);
242
Radek Krejci469aab82012-07-22 18:42:20 +0200243 /* if connected successful, add session to the list */
244 if (session != NULL) {
245 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200246 session_key = gen_ncsession_hash(
247 (host==NULL) ? "localhost" : host,
248 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200249 nc_session_get_id(session));
David Kupka8e60a372012-09-04 09:15:20 +0200250
251 if ((locked_session = malloc (sizeof (struct session_with_mutex))) == NULL || pthread_mutex_init (&locked_session->lock, NULL) != 0) {
252 nc_session_free(session);
253 free (locked_session);
254 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
255 return NULL;
256 }
257 locked_session->session = session;
258 pthread_mutex_init (&locked_session->lock, NULL);
259 /* get exclusive access to sessions_list (conns) */
260 if (pthread_rwlock_wrlock (&session_lock) != 0) {
261 nc_session_free(session);
262 free (locked_session);
263 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
264 return NULL;
265 }
266 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) locked_session);
267 /* end of critical section */
268 if (pthread_rwlock_unlock (&session_lock) != 0) {
269 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
270 return NULL;
271 }
Radek Krejci469aab82012-07-22 18:42:20 +0200272 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200273 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200274 } else {
275 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200276 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200277 }
278
Radek Krejci469aab82012-07-22 18:42:20 +0200279}
280
Radek Krejci80c10d92012-07-30 08:38:50 +0200281static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200282{
283 struct nc_session *ns = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200284 struct session_with_mutex * locked_session;
Radek Krejci469aab82012-07-22 18:42:20 +0200285
Radek Krejcif23850c2012-07-23 16:14:17 +0200286 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
David Kupka8e60a372012-09-04 09:15:20 +0200287 /* get exclusive (write) access to sessions_list (conns) */
288 if (pthread_rwlock_wrlock (&session_lock) != 0) {
289 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
290 return EXIT_FAILURE;
291 }
292 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
293 if (locked_session != NULL) {
294 pthread_mutex_destroy(&locked_session->lock);
295 ns = locked_session->session;
296 free (locked_session);
297 }
Radek Krejci469aab82012-07-22 18:42:20 +0200298 if (ns != NULL) {
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100299 nc_session_close (ns, NC_SESSION_TERM_CLOSED);
Radek Krejci469aab82012-07-22 18:42:20 +0200300 nc_session_free (ns);
301 ns = NULL;
302
303 /* remove session from the active sessions list */
304 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200305 /* end of critical section */
306 if (pthread_rwlock_unlock (&session_lock) != 0) {
307 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
308 return EXIT_FAILURE;
309 }
Radek Krejci469aab82012-07-22 18:42:20 +0200310 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200311
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200312 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200313 } else {
Tomas Cejka6c4609b2012-10-12 22:29:47 +0200314 if (pthread_rwlock_unlock (&session_lock) != 0) {
315 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
316 return EXIT_FAILURE;
317 }
Radek Krejci469aab82012-07-22 18:42:20 +0200318 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200319 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200320 }
321}
322
Radek Krejci80c10d92012-07-30 08:38:50 +0200323static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200324{
325 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200326 struct session_with_mutex * locked_session;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200327 nc_reply* reply;
328 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100329 NC_MSG_TYPE msgt;
330 NC_REPLY_TYPE replyt;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200331
Radek Krejci8e4632a2012-07-26 13:40:34 +0200332 /* check requests */
333 if (rpc == NULL) {
334 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
335 return (EXIT_FAILURE);
336 }
337
David Kupka8e60a372012-09-04 09:15:20 +0200338 /* get non-exclusive (read) access to sessions_list (conns) */
339 if (pthread_rwlock_rdlock (&session_lock) != 0) {
340 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
341 return EXIT_FAILURE;
342 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200343 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200344 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
345 if (locked_session != NULL) {
346 session = locked_session->session;
347 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200348 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200349 /* get exclusive access to session */
350 if (pthread_mutex_lock(&locked_session->lock) != 0) {
351 /* unlock before returning error */
352 if (pthread_rwlock_unlock (&session_lock) != 0) {
353 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
354 return EXIT_FAILURE;
355 }
356 return EXIT_FAILURE;
357 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200358 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100359 msgt = nc_session_send_recv(session, rpc, &reply);
360
David Kupka8e60a372012-09-04 09:15:20 +0200361 /* first release exclusive lock for this session */
362 pthread_mutex_unlock(&locked_session->lock);
363 /* end of critical section */
364 if (pthread_rwlock_unlock (&session_lock) != 0) {
365 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
366 return EXIT_FAILURE;
367 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200368
Radek Krejcia332b692012-11-12 16:15:54 +0100369 /* process the result of the operation */
370 switch (msgt) {
371 case NC_MSG_UNKNOWN:
372 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
373 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
374 netconf_close(server, conns, session_key);
375 return (EXIT_FAILURE);
376 }
377 /* no break */
378 case NC_MSG_NONE:
379 /* there is error handled by callback */
380 return (EXIT_FAILURE);
381 break;
382 case NC_MSG_REPLY:
383 switch (replyt = nc_reply_get_type(reply)) {
384 case NC_REPLY_OK:
385 retval = EXIT_SUCCESS;
386 break;
387 default:
388 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
389 retval = EXIT_FAILURE;
390 break;
391 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200392 break;
393 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200395 retval = EXIT_FAILURE;
396 break;
397 }
398 nc_reply_free(reply);
399 return (retval);
400 } else {
401 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
402 return (EXIT_FAILURE);
403 }
404}
Radek Krejci80c10d92012-07-30 08:38:50 +0200405
406static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200407{
408 struct nc_session *session = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200409 struct session_with_mutex * locked_session;
Radek Krejcia332b692012-11-12 16:15:54 +0100410 nc_reply* reply = NULL;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200411 char* data;
Radek Krejcia332b692012-11-12 16:15:54 +0100412 NC_MSG_TYPE msgt;
413 NC_REPLY_TYPE replyt;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200414
415 /* check requests */
416 if (rpc == NULL) {
417 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
418 return (NULL);
419 }
420
David Kupka8e60a372012-09-04 09:15:20 +0200421 /* get non-exclusive (read) access to sessions_list (conns) */
422 if (pthread_rwlock_rdlock (&session_lock) != 0) {
423 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
424 return NULL;
425 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200426 /* get session where send the RPC */
David Kupka8e60a372012-09-04 09:15:20 +0200427 locked_session = (struct session_with_mutex *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
428 if (locked_session != NULL) {
429 session = locked_session->session;
430 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200431 if (session != NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200432 /* get exclusive access to session */
433 if (pthread_mutex_lock(&locked_session->lock) != 0) {
434 /* unlock before returning error */
435 if (pthread_rwlock_unlock (&session_lock) != 0) {
436 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
437 return NULL;
438 }
439 return NULL;
440 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200441 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100442 msgt = nc_session_send_recv(session, rpc, &reply);
443
David Kupka8e60a372012-09-04 09:15:20 +0200444 /* first release exclusive lock for this session */
445 pthread_mutex_unlock(&locked_session->lock);
446 /* end of critical section */
447 if (pthread_rwlock_unlock (&session_lock) != 0) {
448 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Radek Krejcia332b692012-11-12 16:15:54 +0100449 return (NULL);
David Kupka8e60a372012-09-04 09:15:20 +0200450 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200451
Radek Krejcia332b692012-11-12 16:15:54 +0100452 /* process the result of the operation */
453 switch (msgt) {
454 case NC_MSG_UNKNOWN:
455 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
456 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
457 netconf_close(server, conns, session_key);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200458 return (NULL);
459 }
Radek Krejcia332b692012-11-12 16:15:54 +0100460 /* no break */
461 case NC_MSG_NONE:
462 /* there is error handled by callback */
463 return (NULL);
464 break;
465 case NC_MSG_REPLY:
466 switch (replyt = nc_reply_get_type(reply)) {
467 case NC_REPLY_DATA:
468 if ((data = nc_reply_get_data (reply)) == NULL) {
469 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
470 data = NULL;
471 }
472 break;
473 default:
474 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
475 data = NULL;
476 break;
477 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200478 break;
479 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100480 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
481 data = NULL;
482 break;
Radek Krejci8e4632a2012-07-26 13:40:34 +0200483 }
484 nc_reply_free(reply);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200485 return (data);
486 } else {
487 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
488 return (NULL);
489 }
490}
491
Radek Krejci80c10d92012-07-30 08:38:50 +0200492static 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 +0200493{
494 nc_rpc* rpc;
495 struct nc_filter *f = NULL;
496 char* data = NULL;
497
498 /* create filter if set */
499 if (filter != NULL) {
500 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
501 }
502
503 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100504 rpc = nc_rpc_getconfig (source, f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200505 nc_filter_free(f);
506 if (rpc == NULL) {
507 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
508 return (NULL);
509 }
510
511 data = netconf_opdata(server, conns, session_key, rpc);
512 nc_rpc_free (rpc);
513 return (data);
514}
515
Radek Krejci80c10d92012-07-30 08:38:50 +0200516static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200517{
518 nc_rpc* rpc;
519 struct nc_filter *f = NULL;
520 char* data = NULL;
521
522 /* create filter if set */
523 if (filter != NULL) {
524 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
525 }
526
527 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100528 rpc = nc_rpc_get (f, NCWITHDEFAULTS);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200529 nc_filter_free(f);
530 if (rpc == NULL) {
531 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
532 return (NULL);
533 }
534
535 data = netconf_opdata(server, conns, session_key, rpc);
536 nc_rpc_free (rpc);
537 return (data);
538}
539
Radek Krejci80c10d92012-07-30 08:38:50 +0200540static 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 +0200541{
542 nc_rpc* rpc;
543 int retval = EXIT_SUCCESS;
544
545 /* create requests */
Tomas Cejka027f3bc2012-11-10 20:28:36 +0100546 rpc = nc_rpc_copyconfig(source, target, NCWITHDEFAULTS, config);
Radek Krejci8e4632a2012-07-26 13:40:34 +0200547 if (rpc == NULL) {
548 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
549 return (EXIT_FAILURE);
550 }
551
552 retval = netconf_op(server, conns, session_key, rpc);
553 nc_rpc_free (rpc);
554 return (retval);
555}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200556
Radek Krejci80c10d92012-07-30 08:38:50 +0200557static 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 +0200558{
559 nc_rpc* rpc;
560 int retval = EXIT_SUCCESS;
561
562 /* create requests */
563 rpc = nc_rpc_editconfig(target, defop, erropt, config);
564 if (rpc == NULL) {
565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
566 return (EXIT_FAILURE);
567 }
568
569 retval = netconf_op(server, conns, session_key, rpc);
570 nc_rpc_free (rpc);
571 return (retval);
572}
573
Radek Krejci80c10d92012-07-30 08:38:50 +0200574static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200575{
576 nc_rpc* rpc;
577 int retval = EXIT_SUCCESS;
578
579 /* create requests */
580 rpc = nc_rpc_killsession(sid);
581 if (rpc == NULL) {
582 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
583 return (EXIT_FAILURE);
584 }
585
586 retval = netconf_op(server, conns, session_key, rpc);
587 nc_rpc_free (rpc);
588 return (retval);
589}
590
Radek Krejci80c10d92012-07-30 08:38:50 +0200591static 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 +0200592{
593 nc_rpc* rpc;
594 int retval = EXIT_SUCCESS;
595
596 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200597 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200598 if (rpc == NULL) {
599 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
600 return (EXIT_FAILURE);
601 }
602
603 retval = netconf_op(server, conns, session_key, rpc);
604 nc_rpc_free (rpc);
605 return (retval);
606}
607
Radek Krejci80c10d92012-07-30 08:38:50 +0200608static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200609{
610 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
611}
612
Radek Krejci80c10d92012-07-30 08:38:50 +0200613static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200614{
615 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
616}
617
Radek Krejci80c10d92012-07-30 08:38:50 +0200618static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200619{
620 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
621}
622
Radek Krejci80c10d92012-07-30 08:38:50 +0200623/**
624 * @return REPLY_OK: 0, *data == NULL
625 * REPLY_DATA: 0, *data != NULL
626 * REPLY_ERROR: 1, *data == NULL
627 */
628static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
629{
630 struct nc_session *session = NULL;
631 nc_reply* reply;
632 nc_rpc* rpc;
633 int retval = EXIT_SUCCESS;
Radek Krejcia332b692012-11-12 16:15:54 +0100634 NC_MSG_TYPE msgt;
635 NC_REPLY_TYPE replyt;
Radek Krejci80c10d92012-07-30 08:38:50 +0200636
637 /* create requests */
638 rpc = nc_rpc_generic(content);
639 if (rpc == NULL) {
640 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
641 return (EXIT_FAILURE);
642 }
643
Radek Krejcia332b692012-11-12 16:15:54 +0100644 if (data != NULL) {
645 *data = NULL;
646 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200647
648 /* get session where send the RPC */
649 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
650 if (session != NULL) {
651 /* send the request and get the reply */
Radek Krejcia332b692012-11-12 16:15:54 +0100652 msgt = nc_session_send_recv(session, rpc, &reply);
653 nc_rpc_free (rpc);
654
655 /* process the result of the operation */
656 switch (msgt) {
657 case NC_MSG_UNKNOWN:
Radek Krejci80c10d92012-07-30 08:38:50 +0200658 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
659 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
660 netconf_close(server, conns, session_key);
661 return (EXIT_FAILURE);
662 }
Radek Krejcia332b692012-11-12 16:15:54 +0100663 /* no break */
664 case NC_MSG_NONE:
Radek Krejci80c10d92012-07-30 08:38:50 +0200665 /* there is error handled by callback */
666 return (EXIT_FAILURE);
Radek Krejci80c10d92012-07-30 08:38:50 +0200667 break;
Radek Krejcia332b692012-11-12 16:15:54 +0100668 case NC_MSG_REPLY:
669 switch (replyt = nc_reply_get_type(reply)) {
670 case NC_REPLY_DATA:
671 if ((data != NULL) && (*data = nc_reply_get_data (reply)) == NULL) {
672 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
673 nc_reply_free(reply);
674 return (EXIT_FAILURE);
675 }
676 retval = EXIT_SUCCESS;
677 break;
678 case NC_REPLY_OK:
679 retval = EXIT_SUCCESS;
680 break;
681 default:
682 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply (%d)", replyt);
683 retval = EXIT_FAILURE;
684 break;
685 }
Radek Krejci80c10d92012-07-30 08:38:50 +0200686 break;
687 default:
Radek Krejcia332b692012-11-12 16:15:54 +0100688 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected reply message received (%d)", msgt);
Radek Krejci80c10d92012-07-30 08:38:50 +0200689 retval = EXIT_FAILURE;
690 break;
691 }
692 nc_reply_free(reply);
693
694 return (retval);
695 } else {
696 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
697 return (EXIT_FAILURE);
698 }
699}
700
Radek Krejci469aab82012-07-22 18:42:20 +0200701server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200702void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200703{
Radek Krejci7338bde2012-08-10 12:57:30 +0200704 switch (level) {
705 case NC_VERB_ERROR:
706 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
707 break;
708 case NC_VERB_WARNING:
709 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
710 break;
711 case NC_VERB_VERBOSE:
712 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
713 break;
714 case NC_VERB_DEBUG:
715 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
716 break;
717 }
Radek Krejci469aab82012-07-22 18:42:20 +0200718}
719
David Kupka8e60a372012-09-04 09:15:20 +0200720void * thread_routine (void * arg)
721{
722 void * retval = NULL;
723
724 ssize_t buffer_len;
725 struct pollfd fds;
726 int status, buffer_size, ret;
Kupka David00b9c5c2012-09-05 09:45:50 +0200727 json_object *request, *reply, *json_obj, *capabilities;
David Kupka8e60a372012-09-04 09:15:20 +0200728 int operation;
Kupka David00b9c5c2012-09-05 09:45:50 +0200729 int i, chunk_len, len = 0;
David Kupka8e60a372012-09-04 09:15:20 +0200730 char* session_key, *data;
731 const char *host, *port, *user, *pass;
732 const char *msgtext, *cpbltstr;
733 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
734 struct nc_session *session = NULL;
Kupka Davidda134a12012-09-06 14:12:16 +0200735 struct session_with_mutex * locked_session;
Kupka David00b9c5c2012-09-05 09:45:50 +0200736 struct nc_cpblts* cpblts = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200737 NC_DATASTORE ds_type_s, ds_type_t;
738 NC_EDIT_DEFOP_TYPE defop_type = 0;
739 NC_EDIT_ERROPT_TYPE erropt_type = 0;
740
741 apr_pool_t * pool = ((struct pass_to_thread*)arg)->pool;
742 apr_hash_t *netconf_sessions_list = ((struct pass_to_thread*)arg)->netconf_sessions_list;
743 server_rec * server = ((struct pass_to_thread*)arg)->server;
744 int client = ((struct pass_to_thread*)arg)->client;
745
746 char * buffer, chunk_len_str[12], *chunked_msg;
747 char c;
748
749 while (!isterminated) {
750 fds.fd = client;
751 fds.events = POLLIN;
752 fds.revents = 0;
753
754 status = poll(&fds, 1, 1000);
755
756 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
757 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
758 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
759 continue;
760 } else if (status < 0) {
761 /* 0: poll time outed
762 * close socket and ignore this request from the client, it can try it again
763 * -1: poll failed
764 * something wrong happend, close this socket and wait for another request
765 */
766 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
767 close(client);
768 break;
769 }
770 /* status > 0 */
771
772 /* check the status of the socket */
773
774 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
775 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
776 /* close client's socket (it's probably already closed by client */
777 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
778 close(client);
779 break;
780 }
781
782 /* read json in chunked framing */
783 buffer_size = 0;
784 buffer_len = 0;
785 buffer = NULL;
786 while (1) {
David Kupka1e3e4c82012-09-04 09:32:15 +0200787 /* read chunk length */
788 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '\n') {
789 free (buffer);
790 buffer = NULL;
David Kupka8e60a372012-09-04 09:15:20 +0200791 break;
792 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200793 if ((ret = recv (client, &c, 1, 0)) != 1 || c != '#') {
794 free (buffer);
795 buffer = NULL;
796 break;
797 }
798 i=0;
799 memset (chunk_len_str, 0, 12);
800 while ((ret = recv (client, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
801 if (i==0 && c == '#') {
802 if (recv (client, &c, 1, 0) != 1 || c != '\n') {
803 /* end but invalid */
804 free (buffer);
805 buffer = NULL;
806 }
807 /* end of message, double-loop break */
808 goto msg_complete;
809 }
810 chunk_len_str[i++] = c;
811 }
812 if (c != '\n') {
813 free (buffer);
814 buffer = NULL;
815 break;
816 }
817 if ((chunk_len = atoi (chunk_len_str)) == 0) {
818 free (buffer);
819 buffer = NULL;
820 break;
821 }
822 buffer_size += chunk_len+1;
823 buffer = realloc (buffer, sizeof(char)*buffer_size);
824 if ((ret = recv (client, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
825 free (buffer);
826 buffer = NULL;
827 break;
828 }
829 buffer_len += ret;
830 }
831msg_complete:
David Kupka8e60a372012-09-04 09:15:20 +0200832
833 if (buffer != NULL) {
834 request = json_tokener_parse(buffer);
835 operation = json_object_get_int(json_object_object_get(request, "type"));
836
837 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
838 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
839 if (operation != MSG_CONNECT && session_key == NULL) {
840 reply = json_object_new_object();
841 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
842 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
843 msgtext = json_object_to_json_string(reply);
844 send(client, msgtext, strlen(msgtext) + 1, 0);
845 json_object_put(reply);
846 /* there is some stupid client, so close the connection to give a chance to some other client */
847 close(client);
848 break;
849 }
850
851 /* get parameters */
852 ds_type_t = -1;
853 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
854 if (strcmp(target, "running") == 0) {
855 ds_type_t = NC_DATASTORE_RUNNING;
856 } else if (strcmp(target, "startup") == 0) {
857 ds_type_t = NC_DATASTORE_STARTUP;
858 } else if (strcmp(target, "candidate") == 0) {
859 ds_type_t = NC_DATASTORE_CANDIDATE;
860 }
861 }
862 ds_type_s = -1;
863 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
864 if (strcmp(source, "running") == 0) {
865 ds_type_s = NC_DATASTORE_RUNNING;
866 } else if (strcmp(source, "startup") == 0) {
867 ds_type_s = NC_DATASTORE_STARTUP;
868 } else if (strcmp(source, "candidate") == 0) {
869 ds_type_s = NC_DATASTORE_CANDIDATE;
870 }
871 }
872
873 /* null global JSON error-reply */
874 err_reply = NULL;
875
876 /* prepare reply envelope */
877 reply = json_object_new_object();
878
879 /* process required operation */
880 switch (operation) {
881 case MSG_CONNECT:
882 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
883
884 host = json_object_get_string(json_object_object_get(request, "host"));
885 port = json_object_get_string(json_object_object_get(request, "port"));
886 user = json_object_get_string(json_object_object_get(request, "user"));
887 pass = json_object_get_string(json_object_object_get(request, "pass"));
Tomas Cejkaf34f3912012-09-05 18:14:06 +0200888 capabilities = json_object_object_get(request, "capabilities");
889 if ((capabilities != NULL) && ((len = json_object_array_length(capabilities)) > 0)) {
Kupka David00b9c5c2012-09-05 09:45:50 +0200890 cpblts = nc_cpblts_new (NULL);
891 for (i=0; i<len; i++) {
892 nc_cpblts_add (cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
893 }
894 } else {
895 ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "no capabilities specified");
896 }
David Kupka8e60a372012-09-04 09:15:20 +0200897 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
898 if ((host == NULL) || (user == NULL)) {
899 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
900 session_key = NULL;
901 } else {
Kupka David00b9c5c2012-09-05 09:45:50 +0200902 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass, cpblts);
903 nc_cpblts_free (cpblts);
David Kupka8e60a372012-09-04 09:15:20 +0200904 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
905 }
906
907 reply = json_object_new_object();
908 if (session_key == NULL) {
909 /* negative reply */
910 if (err_reply == NULL) {
911 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
912 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
913 } else {
914 /* use filled err_reply from libnetconf's callback */
915 json_object_put(reply);
916 reply = err_reply;
917 }
918 } else {
919 /* positive reply */
920 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
921 json_object_object_add(reply, "session", json_object_new_string(session_key));
922
923 free(session_key);
924 }
925
926 break;
927 case MSG_GET:
928 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get (session %s)", session_key);
929
930 filter = json_object_get_string(json_object_object_get(request, "filter"));
931
932 //ap_log_error (APLOG_MARK, APLOG_ERR, 0, server, "get filter: %p", filter);
933
Tomas Cejkacdc274e2012-09-05 18:15:33 +0200934 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
David Kupka8e60a372012-09-04 09:15:20 +0200935 if (err_reply == NULL) {
936 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
937 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
938 } else {
939 /* use filled err_reply from libnetconf's callback */
940 json_object_put(reply);
941 reply = err_reply;
942 }
943 } else {
944 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
945 json_object_object_add(reply, "data", json_object_new_string(data));
946 }
947 break;
948 case MSG_GETCONFIG:
949 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
950
951 filter = json_object_get_string(json_object_object_get(request, "filter"));
952
953 if (ds_type_s == -1) {
954 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
955 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
956 break;
957 }
958
959 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
960 if (err_reply == NULL) {
961 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
962 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
963 } else {
964 /* use filled err_reply from libnetconf's callback */
965 json_object_put(reply);
966 reply = err_reply;
967 }
968 } else {
969 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
970 json_object_object_add(reply, "data", json_object_new_string(data));
971 }
972 break;
973 case MSG_EDITCONFIG:
974 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
975
976 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
977 if (defop != NULL) {
978 if (strcmp(defop, "merge") == 0) {
979 defop_type = NC_EDIT_DEFOP_MERGE;
980 } else if (strcmp(defop, "replace") == 0) {
981 defop_type = NC_EDIT_DEFOP_REPLACE;
982 } else if (strcmp(defop, "none") == 0) {
983 defop_type = NC_EDIT_DEFOP_NONE;
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 default-operation parameter."));
987 break;
988 }
989 } else {
990 defop_type = 0;
991 }
992
993 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
994 if (erropt != NULL) {
995 if (strcmp(erropt, "continue-on-error") == 0) {
996 erropt_type = NC_EDIT_ERROPT_CONT;
997 } else if (strcmp(erropt, "stop-on-error") == 0) {
998 erropt_type = NC_EDIT_ERROPT_STOP;
999 } else if (strcmp(erropt, "rollback-on-error") == 0) {
1000 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
1001 } else {
1002 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1003 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
1004 break;
1005 }
1006 } else {
1007 erropt_type = 0;
1008 }
1009
1010 if (ds_type_t == -1) {
1011 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1012 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1013 break;
1014 }
1015
1016 config = json_object_get_string(json_object_object_get(request, "config"));
1017 if (config == NULL) {
1018 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1019 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
1020 break;
1021 }
1022
1023 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
1024 if (err_reply == NULL) {
1025 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1026 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
1027 } else {
1028 /* use filled err_reply from libnetconf's callback */
1029 json_object_put(reply);
1030 reply = err_reply;
1031 }
1032 } else {
1033 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1034 }
1035 break;
1036 case MSG_COPYCONFIG:
1037 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
1038 config = NULL;
1039
1040 if (source == NULL) {
1041 /* no explicit source specified -> use config data */
Tomas Cejka027f3bc2012-11-10 20:28:36 +01001042 ds_type_s = NC_DATASTORE_CONFIG;
David Kupka8e60a372012-09-04 09:15:20 +02001043 config = json_object_get_string(json_object_object_get(request, "config"));
1044 } else if (ds_type_s == -1) {
1045 /* source datastore specified, but it is invalid */
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 source repository type requested."));
1048 break;
1049 }
1050
1051 if (ds_type_t == -1) {
1052 /* invalid target datastore specified */
1053 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1054 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1055 break;
1056 }
1057
1058 if (source == NULL && config == NULL) {
1059 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1060 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
1061 } else {
1062 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
1063 if (err_reply == NULL) {
1064 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1065 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
1066 } else {
1067 /* use filled err_reply from libnetconf's callback */
1068 json_object_put(reply);
1069 reply = err_reply;
1070 }
1071 } else {
1072 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1073 }
1074 }
1075 break;
1076 case MSG_DELETECONFIG:
1077 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
1078 /* no break - unifying code */
1079 case MSG_LOCK:
1080 if (operation == MSG_LOCK) {
1081 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
1082 }
1083 /* no break - unifying code */
1084 case MSG_UNLOCK:
1085 if (operation == MSG_UNLOCK) {
1086 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
1087 }
1088
1089 if (ds_type_t == -1) {
1090 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1091 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
1092 break;
1093 }
1094
1095 switch(operation) {
1096 case MSG_DELETECONFIG:
1097 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
1098 break;
1099 case MSG_LOCK:
1100 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
1101 break;
1102 case MSG_UNLOCK:
1103 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
1104 break;
1105 default:
1106 status = -1;
1107 break;
1108 }
1109
1110 if (status != EXIT_SUCCESS) {
1111 if (err_reply == 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("operation failed."));
1114 } else {
1115 /* use filled err_reply from libnetconf's callback */
1116 json_object_put(reply);
1117 reply = err_reply;
1118 }
1119 } else {
1120 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1121 }
1122 break;
1123 case MSG_KILL:
1124 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
1125
1126 sid = json_object_get_string(json_object_object_get(request, "session-id"));
1127
1128 if (sid == NULL) {
1129 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1130 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
1131 break;
1132 }
1133
1134 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
1135 if (err_reply == NULL) {
1136 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1137 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1138 } else {
1139 /* use filled err_reply from libnetconf's callback */
1140 json_object_put(reply);
1141 reply = err_reply;
1142 }
1143 } else {
1144 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1145 }
1146 break;
1147 case MSG_DISCONNECT:
1148 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
1149
1150 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
1151 if (err_reply == NULL) {
1152 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1153 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1154 } else {
1155 /* use filled err_reply from libnetconf's callback */
1156 json_object_put(reply);
1157 reply = err_reply;
1158 }
1159 } else {
1160 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1161 }
1162 break;
1163 case MSG_INFO:
1164 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1165
Kupka Davidda134a12012-09-06 14:12:16 +02001166 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1167 if (locked_session != NULL) {
1168 session = locked_session->session;
1169 } else {
1170 session = NULL;
1171 }
David Kupka8e60a372012-09-04 09:15:20 +02001172 if (session != NULL) {
1173 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
1174 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
1175 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1176 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1177 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
1178 cpblts = nc_session_get_cpblts (session);
1179 if (cpblts != NULL) {
1180 json_obj = json_object_new_array();
1181 nc_cpblts_iter_start (cpblts);
1182 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1183 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
1184 }
1185 json_object_object_add(reply, "capabilities", json_obj);
1186 }
1187 } else {
1188 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1189 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1190 }
1191
1192 break;
1193 case MSG_GENERIC:
1194 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1195
1196 config = json_object_get_string(json_object_object_get(request, "content"));
1197
1198 if (config == NULL) {
1199 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1200 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1201 break;
1202 }
1203
1204 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) {
1205 if (err_reply == NULL) {
1206 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1207 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1208 } else {
1209 /* use filled err_reply from libnetconf's callback */
1210 json_object_put(reply);
1211 reply = err_reply;
1212 }
1213 } else {
1214 if (data == NULL) {
1215 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1216 } else {
1217 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1218 json_object_object_add(reply, "data", json_object_new_string(data));
1219 }
1220 }
1221 break;
1222 default:
1223 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1224 reply = json_object_new_object();
1225 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1226 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1227 break;
1228 }
1229 json_object_put(request);
1230
David Kupka1e3e4c82012-09-04 09:32:15 +02001231 /* send reply to caller */
1232 if (reply != NULL) {
1233 msgtext = json_object_to_json_string(reply);
1234 if (asprintf (&chunked_msg, "\n#%d\n%s\n##\n", (int)strlen(msgtext), msgtext) == -1) {
1235 free (buffer);
David Kupka8e60a372012-09-04 09:15:20 +02001236 break;
1237 }
David Kupka1e3e4c82012-09-04 09:32:15 +02001238 send(client, chunked_msg, strlen(chunked_msg) + 1, 0);
1239 json_object_put(reply);
1240 free (chunked_msg);
1241 free (buffer);
1242 } else {
1243 break;
David Kupka8e60a372012-09-04 09:15:20 +02001244 }
1245 }
1246 }
1247
1248 free (arg);
1249
1250 return retval;
1251}
1252
Radek Krejcif23850c2012-07-23 16:14:17 +02001253/*
1254 * This is actually implementation of NETCONF client
1255 * - requests are received from UNIX socket in the predefined format
1256 * - results are replied through the same way
1257 * - the daemon run as a separate process, but it is started and stopped
1258 * automatically by Apache.
1259 *
1260 */
Radek Krejci469aab82012-07-22 18:42:20 +02001261static void forked_proc(apr_pool_t * pool, server_rec * server)
1262{
1263 struct sockaddr_un local, remote;
David Kupka8e60a372012-09-04 09:15:20 +02001264 int lsock, client, ret, i, pthread_count = 0;
1265 socklen_t len;
Radek Krejciae021c12012-07-25 18:03:52 +02001266 mod_netconf_cfg *cfg;
Radek Krejci469aab82012-07-22 18:42:20 +02001267 apr_hash_t *netconf_sessions_list;
David Kupka8e60a372012-09-04 09:15:20 +02001268 struct pass_to_thread * arg;
1269 pthread_t * ptids = calloc (1,sizeof(pthread_t));
1270 struct timespec maxtime;
1271 pthread_rwlockattr_t lock_attrs;
1272
1273 /* wait at most 5 secons for every thread to terminate */
1274 maxtime.tv_sec = 5;
1275 maxtime.tv_nsec = 0;
Radek Krejci469aab82012-07-22 18:42:20 +02001276
Radek Krejcif23850c2012-07-23 16:14:17 +02001277 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +02001278 unixd_setup_child();
1279
Radek Krejciae021c12012-07-25 18:03:52 +02001280 cfg = ap_get_module_config(server->module_config, &netconf_module);
1281 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001282 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
1283 return;
1284 }
Radek Krejci469aab82012-07-22 18:42:20 +02001285
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001286 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +02001287 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
1288 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
1289 return;
1290 }
1291
1292 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +02001293 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +02001294 unlink(local.sun_path);
1295 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
1296
1297 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
1298 if (errno == EADDRINUSE) {
1299 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
1300 close(lsock);
1301 exit(0);
1302 }
1303 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
1304 close(lsock);
1305 return;
1306 }
1307
1308 if (listen(lsock, MAX_SOCKET_CL) == -1) {
1309 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
1310 close(lsock);
1311 return;
1312 }
1313
1314 /* prepare internal lists */
1315 netconf_sessions_list = apr_hash_make(pool);
1316
1317 /* setup libnetconf's callbacks */
1318 nc_verbosity(NC_VERB_DEBUG);
1319 clb_print_server = server;
1320 nc_callback_print(clb_print);
1321 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
1322 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
1323 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +02001324 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +02001325
1326 /* disable publickey authentication */
1327 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
1328
David Kupka8e60a372012-09-04 09:15:20 +02001329 /* create mutex protecting session list */
1330 pthread_rwlockattr_init(&lock_attrs);
1331 /* rwlock is shared only with threads in this process */
1332 pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
1333 /* create rw lock */
1334 if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
1335 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Initialization of mutex failed: %d (%s)", errno, strerror(errno));
1336 close (lsock);
1337 return;
1338 }
1339
Radek Krejci469aab82012-07-22 18:42:20 +02001340 while (isterminated == 0) {
1341 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
1342
1343 /* open incoming connection if any */
David Kupka8e60a372012-09-04 09:15:20 +02001344 len = sizeof(remote);
1345 client = accept(lsock, (struct sockaddr *) &remote, &len);
Radek Krejci469aab82012-07-22 18:42:20 +02001346 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1347 apr_sleep(SLEEP_TIME);
1348 continue;
1349 } else if (client == -1 && (errno == EINTR)) {
1350 continue;
1351 } else if (client == -1) {
1352 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
1353 continue;
1354 }
Radek Krejci469aab82012-07-22 18:42:20 +02001355
1356 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +02001357 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +02001358
David Kupka8e60a372012-09-04 09:15:20 +02001359 arg = malloc (sizeof(struct pass_to_thread));
1360 arg->client = client;
1361 arg->pool = pool;
1362 arg->server = server;
1363 arg->netconf_sessions_list = netconf_sessions_list;
Radek Krejci469aab82012-07-22 18:42:20 +02001364
David Kupka8e60a372012-09-04 09:15:20 +02001365 /* start new thread. It will serve this particular request and then terminate */
1366 if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
1367 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating POSIX thread failed: %d\n", ret);
1368 } else {
1369 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu created", ptids[pthread_count]);
1370 pthread_count++;
1371 ptids = realloc (ptids, sizeof(pthread_t)*(pthread_count+1));
1372 ptids[pthread_count] = 0;
1373 }
Radek Krejci469aab82012-07-22 18:42:20 +02001374
David Kupka8e60a372012-09-04 09:15:20 +02001375 /* check if some thread already terminated, free some resources by joining it */
1376 for (i=0; i<pthread_count; i++) {
1377 if (pthread_tryjoin_np (ptids[i], (void**)&arg) == 0) {
1378 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Thread %lu joined with retval %p", ptids[i], arg);
1379 pthread_count--;
1380 if (pthread_count > 0) {
1381 /* place last Thread ID on the place of joined one */
1382 ptids[i] = ptids[pthread_count];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001383 }
Radek Krejci469aab82012-07-22 18:42:20 +02001384 }
1385 }
David Kupka8e60a372012-09-04 09:15:20 +02001386 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Running %d threads", pthread_count);
Radek Krejci469aab82012-07-22 18:42:20 +02001387 }
1388
David Kupka8e60a372012-09-04 09:15:20 +02001389 /* join all threads */
1390 for (i=0; i<pthread_count; i++) {
1391 pthread_timedjoin_np (ptids[i], (void**)&arg, &maxtime);
1392 }
1393 free (ptids);
1394
Radek Krejci469aab82012-07-22 18:42:20 +02001395 close(lsock);
1396
David Kupka8e60a372012-09-04 09:15:20 +02001397 /* destroy rwlock */
1398 pthread_rwlock_destroy(&session_lock);
1399 pthread_rwlockattr_destroy(&lock_attrs);
1400
Radek Krejci469aab82012-07-22 18:42:20 +02001401 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1402
1403 exit(APR_SUCCESS);
1404}
1405
Radek Krejcif23850c2012-07-23 16:14:17 +02001406static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001407{
Radek Krejcif23850c2012-07-23 16:14:17 +02001408 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1409
1410 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1411 apr_pool_create(&config->pool, pool);
1412 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001413 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001414
1415 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001416}
1417
1418static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1419 apr_pool_t * plog, server_rec * s)
1420{
Radek Krejcif23850c2012-07-23 16:14:17 +02001421 mod_netconf_cfg *config;
1422 apr_status_t res;
1423
Radek Krejci469aab82012-07-22 18:42:20 +02001424 /* These two help ensure that we only init once. */
Radek Krejcia332b692012-11-12 16:15:54 +01001425 void *data = NULL;
Radek Krejcif23850c2012-07-23 16:14:17 +02001426 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001427
1428 /*
1429 * The following checks if this routine has been called before.
1430 * This is necessary because the parent process gets initialized
1431 * a couple of times as the server starts up.
1432 */
1433 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1434 if (!data) {
1435 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1436 return (OK);
1437 }
1438
Radek Krejcif23850c2012-07-23 16:14:17 +02001439 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1440 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001441
Radek Krejcif23850c2012-07-23 16:14:17 +02001442 if (config && config->forkproc == NULL) {
1443 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1444 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001445 switch (res) {
1446 case APR_INCHILD:
1447 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001448 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001449 apr_signal(SIGTERM, signal_handler);
1450
1451 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001452 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001453
1454 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001455 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001456
Radek Krejcif23850c2012-07-23 16:14:17 +02001457 /* I never should be here, wtf?!? */
1458 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001459 exit(APR_EGENERAL);
1460 break;
1461 case APR_INPARENT:
1462 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001463 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001464 break;
1465 default:
1466 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1467 break;
1468 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001469 } else {
1470 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001471 }
1472
1473 return OK;
1474}
1475
Radek Krejci469aab82012-07-22 18:42:20 +02001476/**
1477 * Register module hooks
1478 */
1479static void mod_netconf_register_hooks(apr_pool_t * p)
1480{
Radek Krejcif23850c2012-07-23 16:14:17 +02001481 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001482}
1483
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001484static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1485{
1486 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1487 return NULL;
1488}
1489
1490static const command_rec netconf_cmds[] = {
1491 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1492 {NULL}
1493};
1494
Radek Krejci469aab82012-07-22 18:42:20 +02001495/* Dispatch list for API hooks */
1496module AP_MODULE_DECLARE_DATA netconf_module = {
1497 STANDARD20_MODULE_STUFF,
1498 NULL, /* create per-dir config structures */
1499 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001500 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001501 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001502 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001503 mod_netconf_register_hooks /* register hooks */
1504};
Radek Krejcia332b692012-11-12 16:15:54 +01001505