blob: 0609068316aec1f9dc4ca3bc87c64d7b0a5e3b35 [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
46#include "httpd.h"
47#include "http_config.h"
48#include "http_protocol.h"
49#include "http_request.h"
50#include "ap_config.h"
51#include "http_log.h"
52#include "apu.h"
53#include "apr_general.h"
54#include "apr_sha1.h"
55#include "apr_file_io.h"
56
57#include <unixd.h>
58#include <apr_base64.h>
59#include <apr_pools.h>
60#include <apr_general.h>
61#include <apr_hash.h>
62#include <apr_strings.h>
63#include <apr_thread_proc.h>
64#include <apr_signal.h>
65
66#include <sys/types.h>
67#include <sys/socket.h>
68#include <sys/un.h>
69#include <stdlib.h>
70#include <stdio.h>
71#include <poll.h>
72#include <unistd.h>
73#include <string.h>
74#include <errno.h>
75
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020076#include <json/json.h>
77
Radek Krejci469aab82012-07-22 18:42:20 +020078#include <libnetconf.h>
79#include <libxml/tree.h>
80#include <libxml/parser.h>
81
82#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020083#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020084#define MAX_SOCKET_CL 10
85#define BUFFER_SIZE 4096
86
87/* sleep in master process for non-blocking socket reading */
88#define SLEEP_TIME 200
89
90#ifndef offsetof
91#define offsetof(type, member) ((size_t) ((type *) 0)->member)
92#endif
93
94struct timeval timeout = { 1, 0 };
95
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020096typedef enum MSG_TYPE {
97 REPLY_OK,
98 REPLY_DATA,
99 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200100 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200101 MSG_CONNECT,
102 MSG_DISCONNECT,
103 MSG_GET,
104 MSG_GETCONFIG,
105 MSG_EDITCONFIG,
106 MSG_COPYCONFIG,
107 MSG_DELETECONFIG,
108 MSG_LOCK,
109 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200110 MSG_KILL,
111 MSG_INFO
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200112} MSG_TYPE;
113
Radek Krejci469aab82012-07-22 18:42:20 +0200114#define MSG_OK 0
115#define MSG_OPEN 1
116#define MSG_DATA 2
117#define MSG_CLOSE 3
118#define MSG_ERROR 4
119#define MSG_UNKNOWN 5
120
Radek Krejci469aab82012-07-22 18:42:20 +0200121module AP_MODULE_DECLARE_DATA netconf_module;
122
123typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200124 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200125 apr_proc_t *forkproc;
126 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200127} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200128
129volatile int isterminated = 0;
130
131static char* password;
132
133
134static void signal_handler(int sign)
135{
136 switch (sign) {
137 case SIGTERM:
138 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200139 break;
140 }
141}
142
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200144{
Radek Krejcif23850c2012-07-23 16:14:17 +0200145 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
146 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200147 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200148
Radek Krejci469aab82012-07-22 18:42:20 +0200149 apr_sha1_ctx_t sha1_ctx;
150 apr_sha1_init(&sha1_ctx);
151 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
152 apr_sha1_update(&sha1_ctx, port, strlen(port));
153 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200154 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200155
Radek Krejcif23850c2012-07-23 16:14:17 +0200156 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200157 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200158 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200159 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200160 }
161 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200162
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200163 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200164}
165
166int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
167{
168 /* always approve */
169 return (EXIT_SUCCESS);
170}
171
172char* netconf_callback_sshauth_password (const char* username, const char* hostname)
173{
174 char* buf;
175
176 buf = malloc ((strlen(password) + 1) * sizeof(char));
177 apr_cpystrn(buf, password, strlen(password) + 1);
178
179 return (buf);
180}
181
182void netconf_callback_sshauth_interactive (const char* name,
183 int name_len,
184 const char* instruction,
185 int instruction_len,
186 int num_prompts,
187 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
188 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
189 void** abstract)
190{
191 int i;
192
193 for (i=0; i<num_prompts; i++) {
194 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
195 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
196 responses[i].length = strlen(responses[i].text) + 1;
197 }
198
199 return;
200}
201
Radek Krejcic11fd862012-07-26 12:41:21 +0200202static json_object *err_reply = NULL;
203void netconf_callback_error_process(const char* tag,
204 const char* type,
205 const char* severity,
206 const char* apptag,
207 const char* path,
208 const char* message,
209 const char* attribute,
210 const char* element,
211 const char* ns,
212 const char* sid)
213{
214 err_reply = json_object_new_object();
215 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
216 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
217 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
218 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
219 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
220 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
221 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
222 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
223 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
224 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
225 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
226}
227
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200228static 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)
Radek Krejci469aab82012-07-22 18:42:20 +0200229{
Radek Krejci469aab82012-07-22 18:42:20 +0200230 struct nc_session* session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200231 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200232
233 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200234 password = (char*)pass;
235 session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200236
237 /* if connected successful, add session to the list */
238 if (session != NULL) {
239 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200240 session_key = gen_ncsession_hash(
241 (host==NULL) ? "localhost" : host,
242 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200243 nc_session_get_id(session));
Radek Krejcif23850c2012-07-23 16:14:17 +0200244 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) session);
Radek Krejci469aab82012-07-22 18:42:20 +0200245 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200246 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200247 } else {
248 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200249 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200250 }
251
Radek Krejci469aab82012-07-22 18:42:20 +0200252}
253
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200254static int netconf_close(server_rec* server, apr_hash_t* conns, char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200255{
256 struct nc_session *ns = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200257
Radek Krejcif23850c2012-07-23 16:14:17 +0200258 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200259 ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
260 if (ns != NULL) {
261 nc_session_close (ns, "NETCONF session closed by client");
262 nc_session_free (ns);
263 ns = NULL;
264
265 /* remove session from the active sessions list */
266 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
267 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200268
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200269 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200270 } else {
271 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200272 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200273 }
274}
275
Radek Krejci8e4632a2012-07-26 13:40:34 +0200276static int netconf_op(server_rec* server, apr_hash_t* conns, char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200277{
278 struct nc_session *session = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200279 nc_reply* reply;
280 int retval = EXIT_SUCCESS;
281
Radek Krejci8e4632a2012-07-26 13:40:34 +0200282 /* check requests */
283 if (rpc == NULL) {
284 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
285 return (EXIT_FAILURE);
286 }
287
288 /* get session where send the RPC */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200289 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
290 if (session != NULL) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200291 /* send the request and get the reply */
292 nc_session_send_rpc (session, rpc);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200293 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200294 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200295 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Radek Krejci035bf4e2012-07-25 10:59:09 +0200296 netconf_close(server, conns, session_key);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200297 return (EXIT_FAILURE);
298 }
299
300 /* there is error handled by callback */
301 return (EXIT_FAILURE);
302 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200303
304 switch (nc_reply_get_type (reply)) {
305 case NC_REPLY_OK:
306 retval = EXIT_SUCCESS;
307 break;
308 default:
309 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
310 retval = EXIT_FAILURE;
311 break;
312 }
313 nc_reply_free(reply);
314 return (retval);
315 } else {
316 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
317 return (EXIT_FAILURE);
318 }
319}
Radek Krejci8e4632a2012-07-26 13:40:34 +0200320static char* netconf_opdata(server_rec* server, apr_hash_t* conns, char* session_key, nc_rpc* rpc)
321{
322 struct nc_session *session = NULL;
323 nc_reply* reply;
324 char* data;
325
326 /* check requests */
327 if (rpc == NULL) {
328 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
329 return (NULL);
330 }
331
332 /* get session where send the RPC */
333 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
334 if (session != NULL) {
335 /* send the request and get the reply */
336 nc_session_send_rpc (session, rpc);
337 if (nc_session_recv_reply (session, &reply) == 0) {
338 nc_rpc_free (rpc);
339 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
340 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
341 netconf_close(server, conns, session_key);
342 return (NULL);
343 }
344
345 /* there is error handled by callback */
346 return (NULL);
347 }
348 nc_rpc_free (rpc);
349
350 switch (nc_reply_get_type (reply)) {
351 case NC_REPLY_DATA:
352 if ((data = nc_reply_get_data (reply)) == NULL) {
353 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
354 return (NULL);
355 }
356 break;
357 default:
358 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
359 return (NULL);
360 }
361 nc_reply_free(reply);
362
363 return (data);
364 } else {
365 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
366 return (NULL);
367 }
368}
369
370static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, const char* filter)
371{
372 nc_rpc* rpc;
373 struct nc_filter *f = NULL;
374 char* data = NULL;
375
376 /* create filter if set */
377 if (filter != NULL) {
378 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
379 }
380
381 /* create requests */
382 rpc = nc_rpc_getconfig (source, f);
383 nc_filter_free(f);
384 if (rpc == NULL) {
385 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
386 return (NULL);
387 }
388
389 data = netconf_opdata(server, conns, session_key, rpc);
390 nc_rpc_free (rpc);
391 return (data);
392}
393
394static char* netconf_get(server_rec* server, apr_hash_t* conns, char* session_key, const char* filter)
395{
396 nc_rpc* rpc;
397 struct nc_filter *f = NULL;
398 char* data = NULL;
399
400 /* create filter if set */
401 if (filter != NULL) {
402 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
403 }
404
405 /* create requests */
406 rpc = nc_rpc_get (f);
407 nc_filter_free(f);
408 if (rpc == NULL) {
409 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
410 return (NULL);
411 }
412
413 data = netconf_opdata(server, conns, session_key, rpc);
414 nc_rpc_free (rpc);
415 return (data);
416}
417
418static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config)
419{
420 nc_rpc* rpc;
421 int retval = EXIT_SUCCESS;
422
423 /* create requests */
424 rpc = nc_rpc_copyconfig(source, target, config);
425 if (rpc == NULL) {
426 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
427 return (EXIT_FAILURE);
428 }
429
430 retval = netconf_op(server, conns, session_key, rpc);
431 nc_rpc_free (rpc);
432 return (retval);
433}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200434
Radek Krejci62ab34b2012-07-26 13:42:05 +0200435static int netconf_editconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, const char* config)
436{
437 nc_rpc* rpc;
438 int retval = EXIT_SUCCESS;
439
440 /* create requests */
441 rpc = nc_rpc_editconfig(target, defop, erropt, config);
442 if (rpc == NULL) {
443 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
444 return (EXIT_FAILURE);
445 }
446
447 retval = netconf_op(server, conns, session_key, rpc);
448 nc_rpc_free (rpc);
449 return (retval);
450}
451
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200452static int netconf_killsession(server_rec* server, apr_hash_t* conns, char* session_key, const char* sid)
453{
454 nc_rpc* rpc;
455 int retval = EXIT_SUCCESS;
456
457 /* create requests */
458 rpc = nc_rpc_killsession(sid);
459 if (rpc == NULL) {
460 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
461 return (EXIT_FAILURE);
462 }
463
464 retval = netconf_op(server, conns, session_key, rpc);
465 nc_rpc_free (rpc);
466 return (retval);
467}
468
Radek Krejci5cd7d422012-07-26 14:50:29 +0200469static int netconf_onlytargetop(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE))
Radek Krejci2f318372012-07-26 14:22:35 +0200470{
471 nc_rpc* rpc;
472 int retval = EXIT_SUCCESS;
473
474 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200475 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200476 if (rpc == NULL) {
477 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
478 return (EXIT_FAILURE);
479 }
480
481 retval = netconf_op(server, conns, session_key, rpc);
482 nc_rpc_free (rpc);
483 return (retval);
484}
485
Radek Krejci5cd7d422012-07-26 14:50:29 +0200486static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target)
487{
488 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
489}
490
491static int netconf_lock(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target)
492{
493 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
494}
495
496static int netconf_unlock(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target)
497{
498 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
499}
500
Radek Krejci469aab82012-07-22 18:42:20 +0200501server_rec* clb_print_server;
502int clb_print(const char* msg)
503{
504 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
505 return (0);
506}
507
Radek Krejcif23850c2012-07-23 16:14:17 +0200508/*
509 * This is actually implementation of NETCONF client
510 * - requests are received from UNIX socket in the predefined format
511 * - results are replied through the same way
512 * - the daemon run as a separate process, but it is started and stopped
513 * automatically by Apache.
514 *
515 */
Radek Krejci469aab82012-07-22 18:42:20 +0200516static void forked_proc(apr_pool_t * pool, server_rec * server)
517{
518 struct sockaddr_un local, remote;
519 int lsock, client;
520 socklen_t len, len2;
521 struct pollfd fds;
522 int status;
Radek Krejciae021c12012-07-25 18:03:52 +0200523 mod_netconf_cfg *cfg;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200524 json_object *request, *reply, *json_obj;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200525 int operation;
526 char* session_key, *data;
Radek Krejcia282bed2012-07-27 14:43:45 +0200527 const char *msgtext, *cpbltstr;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200528 const char *host, *port, *user, *pass;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200529 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200530 struct nc_session *session = NULL;
531 struct nc_cpblts* cpblts;
Radek Krejcifa891e72012-07-26 14:04:27 +0200532 NC_DATASTORE ds_type_s, ds_type_t;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200533 NC_EDIT_DEFOP_TYPE defop_type = 0;
534 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200535
536 apr_hash_t *netconf_sessions_list;
537 char buffer[BUFFER_SIZE];
Radek Krejci469aab82012-07-22 18:42:20 +0200538
Radek Krejcif23850c2012-07-23 16:14:17 +0200539 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +0200540 unixd_setup_child();
541
Radek Krejciae021c12012-07-25 18:03:52 +0200542 cfg = ap_get_module_config(server->module_config, &netconf_module);
543 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200544 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
545 return;
546 }
Radek Krejci469aab82012-07-22 18:42:20 +0200547
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200548 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +0200549 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
550 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
551 return;
552 }
553
554 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +0200555 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +0200556 unlink(local.sun_path);
557 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
558
559 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
560 if (errno == EADDRINUSE) {
561 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
562 close(lsock);
563 exit(0);
564 }
565 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
566 close(lsock);
567 return;
568 }
569
570 if (listen(lsock, MAX_SOCKET_CL) == -1) {
571 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
572 close(lsock);
573 return;
574 }
575
576 /* prepare internal lists */
577 netconf_sessions_list = apr_hash_make(pool);
578
579 /* setup libnetconf's callbacks */
580 nc_verbosity(NC_VERB_DEBUG);
581 clb_print_server = server;
582 nc_callback_print(clb_print);
583 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
584 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
585 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +0200586 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +0200587
588 /* disable publickey authentication */
589 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
590
591 while (isterminated == 0) {
592 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
593
594 /* open incoming connection if any */
595 len2 = sizeof(remote);
596 client = accept(lsock, (struct sockaddr *) &remote, &len2);
597 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
598 apr_sleep(SLEEP_TIME);
599 continue;
600 } else if (client == -1 && (errno == EINTR)) {
601 continue;
602 } else if (client == -1) {
603 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
604 continue;
605 }
606 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted.");
607
608 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +0200609 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +0200610
611 while (1) {
612 fds.fd = client;
613 fds.events = POLLIN;
614 fds.revents = 0;
615
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200616 status = poll(&fds, 1, 1000);
Radek Krejci469aab82012-07-22 18:42:20 +0200617
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200618 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
Radek Krejci469aab82012-07-22 18:42:20 +0200619 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200620 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
Radek Krejci469aab82012-07-22 18:42:20 +0200621 continue;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200622 } else if (status < 0) {
Radek Krejci469aab82012-07-22 18:42:20 +0200623 /* 0: poll time outed
624 * close socket and ignore this request from the client, it can try it again
625 * -1: poll failed
626 * something wrong happend, close this socket and wait for another request
627 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200628 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
Radek Krejci469aab82012-07-22 18:42:20 +0200629 close(client);
630 break;
631 }
632 /* status > 0 */
633
634 /* check the status of the socket */
635
636 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
637 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
638 /* close client's socket (it's probably already closed by client */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200639 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
Radek Krejci469aab82012-07-22 18:42:20 +0200640 close(client);
641 break;
642 }
643
644 if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) {
645 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno));
646 continue;
647 } else {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200648 request = json_tokener_parse(buffer);
649 operation = json_object_get_int(json_object_object_get(request, "type"));
Radek Krejci469aab82012-07-22 18:42:20 +0200650
Radek Krejci035bf4e2012-07-25 10:59:09 +0200651 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
652 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
653 if (operation != MSG_CONNECT && session_key == NULL) {
654 reply = json_object_new_object();
655 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
656 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
657 msgtext = json_object_to_json_string(reply);
658 send(client, msgtext, strlen(msgtext) + 1, 0);
659 json_object_put(reply);
660 /* there is some stupid client, so close the connection to give a chance to some other client */
661 close(client);
662 break;
663 }
664
Radek Krejcifa891e72012-07-26 14:04:27 +0200665 /* get parameters */
666 ds_type_t = -1;
667 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
668 if (strcmp(target, "running") == 0) {
669 ds_type_t = NC_DATASTORE_RUNNING;
670 } else if (strcmp(target, "startup") == 0) {
671 ds_type_t = NC_DATASTORE_STARTUP;
672 } else if (strcmp(target, "candidate") == 0) {
673 ds_type_t = NC_DATASTORE_CANDIDATE;
674 }
675 }
676 ds_type_s = -1;
677 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
678 if (strcmp(source, "running") == 0) {
679 ds_type_s = NC_DATASTORE_RUNNING;
680 } else if (strcmp(source, "startup") == 0) {
681 ds_type_s = NC_DATASTORE_STARTUP;
682 } else if (strcmp(source, "candidate") == 0) {
683 ds_type_s = NC_DATASTORE_CANDIDATE;
684 }
685 }
686
Radek Krejcic11fd862012-07-26 12:41:21 +0200687 /* null global JSON error-reply */
688 err_reply = NULL;
689
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200690 /* prepare reply envelope */
691 reply = json_object_new_object();
692
Radek Krejcic11fd862012-07-26 12:41:21 +0200693 /* process required operation */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200694 switch (operation) {
695 case MSG_CONNECT:
696 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
697
698 host = json_object_get_string(json_object_object_get(request, "host"));
699 port = json_object_get_string(json_object_object_get(request, "port"));
700 user = json_object_get_string(json_object_object_get(request, "user"));
701 pass = json_object_get_string(json_object_object_get(request, "pass"));
702 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
703 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass);
704 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
705
706 reply = json_object_new_object();
707 if (session_key == NULL) {
708 /* negative reply */
Radek Krejcic11fd862012-07-26 12:41:21 +0200709 if (err_reply == NULL) {
710 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
711 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
712 } else {
713 /* use filled err_reply from libnetconf's callback */
714 json_object_put(reply);
715 reply = err_reply;
716 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200717 } else {
718 /* positive reply */
719 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
720 json_object_object_add(reply, "session", json_object_new_string(session_key));
Radek Krejcie31ad212012-07-26 12:51:15 +0200721
722 free(session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200723 }
724
Radek Krejci469aab82012-07-22 18:42:20 +0200725 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200726 case MSG_GET:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200727 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200728
729 filter = json_object_get_string(json_object_object_get(request, "filter"));
730
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200731 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200732 if (err_reply == NULL) {
733 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
734 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
735 } else {
736 /* use filled err_reply from libnetconf's callback */
737 json_object_put(reply);
738 reply = err_reply;
739 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200740 } else {
741 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
742 json_object_object_add(reply, "data", json_object_new_string(data));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200743 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200744 break;
745 case MSG_GETCONFIG:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200746 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200747
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200748 filter = json_object_get_string(json_object_object_get(request, "filter"));
749
Radek Krejcifa891e72012-07-26 14:04:27 +0200750 if (ds_type_s == -1) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200751 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200752 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200753 break;
754 }
755
Radek Krejcifa891e72012-07-26 14:04:27 +0200756 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200757 if (err_reply == NULL) {
758 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
759 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
760 } else {
761 /* use filled err_reply from libnetconf's callback */
762 json_object_put(reply);
763 reply = err_reply;
764 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200765 } else {
766 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
767 json_object_object_add(reply, "data", json_object_new_string(data));
768 }
769 break;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200770 case MSG_EDITCONFIG:
771 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
772
Radek Krejci62ab34b2012-07-26 13:42:05 +0200773 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
774 if (defop != NULL) {
775 if (strcmp(defop, "merge") == 0) {
776 defop_type = NC_EDIT_DEFOP_MERGE;
777 } else if (strcmp(defop, "replace") == 0) {
778 defop_type = NC_EDIT_DEFOP_REPLACE;
779 } else if (strcmp(defop, "none") == 0) {
780 defop_type = NC_EDIT_DEFOP_NONE;
781 } else {
782 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
783 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
784 break;
785 }
786 } else {
787 defop_type = 0;
788 }
789
790 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
791 if (erropt != NULL) {
792 if (strcmp(erropt, "continue-on-error") == 0) {
793 erropt_type = NC_EDIT_ERROPT_CONT;
794 } else if (strcmp(erropt, "stop-on-error") == 0) {
795 erropt_type = NC_EDIT_ERROPT_STOP;
796 } else if (strcmp(erropt, "rollback-on-error") == 0) {
797 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
798 } else {
799 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
800 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
801 break;
802 }
803 } else {
804 erropt_type = 0;
805 }
806
Radek Krejcifa891e72012-07-26 14:04:27 +0200807 if (ds_type_t == -1) {
Radek Krejci62ab34b2012-07-26 13:42:05 +0200808 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
809 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
810 break;
811 }
812
813 config = json_object_get_string(json_object_object_get(request, "config"));
814 if (config == NULL) {
815 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
816 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
817 break;
818 }
819
Radek Krejcifa891e72012-07-26 14:04:27 +0200820 if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) {
Radek Krejci62ab34b2012-07-26 13:42:05 +0200821 if (err_reply == NULL) {
822 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
823 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
824 } else {
825 /* use filled err_reply from libnetconf's callback */
826 json_object_put(reply);
827 reply = err_reply;
828 }
829 } else {
830 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
831 }
832 break;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200833 case MSG_COPYCONFIG:
834 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
Radek Krejcicebb7af2012-07-26 14:58:12 +0200835 config = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200836
Radek Krejcifa891e72012-07-26 14:04:27 +0200837 if (source == NULL) {
838 /* no explicit source specified -> use config data */
839 ds_type_s = NC_DATASTORE_NONE;
Radek Krejciae021c12012-07-25 18:03:52 +0200840 config = json_object_get_string(json_object_object_get(request, "config"));
Radek Krejcifa891e72012-07-26 14:04:27 +0200841 } else if (ds_type_s == -1) {
842 /* source datastore specified, but it is invalid */
843 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
844 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
845 break;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200846 }
Radek Krejciae021c12012-07-25 18:03:52 +0200847
Radek Krejcifa891e72012-07-26 14:04:27 +0200848 if (ds_type_t == -1) {
849 /* invalid target datastore specified */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200850 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
851 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200852 break;
853 }
854
Radek Krejcifa891e72012-07-26 14:04:27 +0200855 if (source == NULL && config == NULL) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200856 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejcifa891e72012-07-26 14:04:27 +0200857 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required."));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200858 } else {
Radek Krejcifa891e72012-07-26 14:04:27 +0200859 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200860 if (err_reply == NULL) {
861 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
862 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
863 } else {
864 /* use filled err_reply from libnetconf's callback */
865 json_object_put(reply);
866 reply = err_reply;
867 }
Radek Krejciae021c12012-07-25 18:03:52 +0200868 } else {
869 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
870 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200871 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200872 break;
Radek Krejci2f318372012-07-26 14:22:35 +0200873 case MSG_DELETECONFIG:
874 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200875 /* no break - unifying code */
876 case MSG_LOCK:
877 if (operation == MSG_LOCK) {
878 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
879 }
880 /* no break - unifying code */
881 case MSG_UNLOCK:
882 if (operation == MSG_UNLOCK) {
883 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
884 }
Radek Krejci2f318372012-07-26 14:22:35 +0200885
Radek Krejci2f318372012-07-26 14:22:35 +0200886 if (ds_type_t == -1) {
887 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
888 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
889 break;
890 }
891
Radek Krejci5cd7d422012-07-26 14:50:29 +0200892 switch(operation) {
893 case MSG_DELETECONFIG:
894 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
895 break;
896 case MSG_LOCK:
897 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
898 break;
899 case MSG_UNLOCK:
900 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
901 break;
902 default:
903 status = -1;
904 break;
905 }
906
907 if (status != EXIT_SUCCESS) {
Radek Krejci2f318372012-07-26 14:22:35 +0200908 if (err_reply == NULL) {
909 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200910 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
Radek Krejci2f318372012-07-26 14:22:35 +0200911 } else {
912 /* use filled err_reply from libnetconf's callback */
913 json_object_put(reply);
914 reply = err_reply;
915 }
916 } else {
917 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
918 }
919 break;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200920 case MSG_KILL:
921 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
922
923 sid = json_object_get_string(json_object_object_get(request, "session-id"));
924
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200925 if (sid == NULL) {
926 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
927 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
928 break;
929 }
930
931 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
932 if (err_reply == NULL) {
933 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
934 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
935 } else {
936 /* use filled err_reply from libnetconf's callback */
937 json_object_put(reply);
938 reply = err_reply;
939 }
940 } else {
941 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
942 }
943 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200944 case MSG_DISCONNECT:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200945 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200946
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200947 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200948 if (err_reply == NULL) {
949 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
950 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
951 } else {
952 /* use filled err_reply from libnetconf's callback */
953 json_object_put(reply);
954 reply = err_reply;
955 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200956 } else {
957 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
958 }
959 break;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200960 case MSG_INFO:
961 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
962
963 session = (struct nc_session *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
964 if (session != NULL) {
Radek Krejcia282bed2012-07-27 14:43:45 +0200965 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200966 json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1"));
Radek Krejcia282bed2012-07-27 14:43:45 +0200967 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
968 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
969 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200970 cpblts = nc_session_get_cpblts (session);
971 if (cpblts != NULL) {
972 json_obj = json_object_new_array();
973 nc_cpblts_iter_start (cpblts);
Radek Krejcia282bed2012-07-27 14:43:45 +0200974 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
975 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200976 }
977 json_object_object_add(reply, "capabilities", json_obj);
978 }
979 } else {
980 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
981 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
982 }
983
984 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200985 default:
986 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
987 reply = json_object_new_object();
988 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
989 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
990 break;
991 }
992 json_object_put(request);
993
994 /* send reply to caller */
995 if (reply != NULL) {
996 msgtext = json_object_to_json_string(reply);
997 send(client, msgtext, strlen(msgtext) + 1, 0);
998 json_object_put(reply);
999 } else {
1000 break;
1001 }
Radek Krejci469aab82012-07-22 18:42:20 +02001002 }
1003 }
1004 }
1005
1006 close(lsock);
1007
1008 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1009
1010 exit(APR_SUCCESS);
1011}
1012
Radek Krejcif23850c2012-07-23 16:14:17 +02001013static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001014{
Radek Krejcif23850c2012-07-23 16:14:17 +02001015 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1016
1017 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1018 apr_pool_create(&config->pool, pool);
1019 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001020 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001021
1022 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001023}
1024
1025static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1026 apr_pool_t * plog, server_rec * s)
1027{
Radek Krejcif23850c2012-07-23 16:14:17 +02001028 mod_netconf_cfg *config;
1029 apr_status_t res;
1030
Radek Krejci469aab82012-07-22 18:42:20 +02001031 /* These two help ensure that we only init once. */
1032 void *data;
Radek Krejcif23850c2012-07-23 16:14:17 +02001033 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001034
1035 /*
1036 * The following checks if this routine has been called before.
1037 * This is necessary because the parent process gets initialized
1038 * a couple of times as the server starts up.
1039 */
1040 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1041 if (!data) {
1042 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1043 return (OK);
1044 }
1045
Radek Krejcif23850c2012-07-23 16:14:17 +02001046 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1047 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001048
Radek Krejcif23850c2012-07-23 16:14:17 +02001049 if (config && config->forkproc == NULL) {
1050 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1051 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001052 switch (res) {
1053 case APR_INCHILD:
1054 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001055 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001056 apr_signal(SIGTERM, signal_handler);
1057
1058 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001059 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001060
1061 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001062 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001063
Radek Krejcif23850c2012-07-23 16:14:17 +02001064 /* I never should be here, wtf?!? */
1065 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001066 exit(APR_EGENERAL);
1067 break;
1068 case APR_INPARENT:
1069 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001070 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001071 break;
1072 default:
1073 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1074 break;
1075 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001076 } else {
1077 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001078 }
1079
1080 return OK;
1081}
1082
Radek Krejci469aab82012-07-22 18:42:20 +02001083/**
1084 * Register module hooks
1085 */
1086static void mod_netconf_register_hooks(apr_pool_t * p)
1087{
Radek Krejcif23850c2012-07-23 16:14:17 +02001088 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001089}
1090
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001091static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1092{
1093 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1094 return NULL;
1095}
1096
1097static const command_rec netconf_cmds[] = {
1098 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1099 {NULL}
1100};
1101
Radek Krejci469aab82012-07-22 18:42:20 +02001102/* Dispatch list for API hooks */
1103module AP_MODULE_DECLARE_DATA netconf_module = {
1104 STANDARD20_MODULE_STUFF,
1105 NULL, /* create per-dir config structures */
1106 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001107 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001108 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001109 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001110 mod_netconf_register_hooks /* register hooks */
1111};