blob: 7b27b541002999975974adad0befaa4a912076f4 [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,
100 MSG_CONNECT,
101 MSG_DISCONNECT,
102 MSG_GET,
103 MSG_GETCONFIG,
104 MSG_EDITCONFIG,
105 MSG_COPYCONFIG,
106 MSG_DELETECONFIG,
107 MSG_LOCK,
108 MSG_UNLOCK,
109 MSG_KILL
110} MSG_TYPE;
111
Radek Krejci469aab82012-07-22 18:42:20 +0200112#define MSG_OK 0
113#define MSG_OPEN 1
114#define MSG_DATA 2
115#define MSG_CLOSE 3
116#define MSG_ERROR 4
117#define MSG_UNKNOWN 5
118
Radek Krejci469aab82012-07-22 18:42:20 +0200119module AP_MODULE_DECLARE_DATA netconf_module;
120
121typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200122 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200123 apr_proc_t *forkproc;
124 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200125} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200126
127volatile int isterminated = 0;
128
129static char* password;
130
131
132static void signal_handler(int sign)
133{
134 switch (sign) {
135 case SIGTERM:
136 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200137 break;
138 }
139}
140
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200141static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200142{
Radek Krejcif23850c2012-07-23 16:14:17 +0200143 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
144 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200146
Radek Krejci469aab82012-07-22 18:42:20 +0200147 apr_sha1_ctx_t sha1_ctx;
148 apr_sha1_init(&sha1_ctx);
149 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
150 apr_sha1_update(&sha1_ctx, port, strlen(port));
151 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200152 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200153
Radek Krejcif23850c2012-07-23 16:14:17 +0200154 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200155 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200156 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200157 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200158 }
159 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200160
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200161 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200162}
163
164int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
165{
166 /* always approve */
167 return (EXIT_SUCCESS);
168}
169
170char* netconf_callback_sshauth_password (const char* username, const char* hostname)
171{
172 char* buf;
173
174 buf = malloc ((strlen(password) + 1) * sizeof(char));
175 apr_cpystrn(buf, password, strlen(password) + 1);
176
177 return (buf);
178}
179
180void netconf_callback_sshauth_interactive (const char* name,
181 int name_len,
182 const char* instruction,
183 int instruction_len,
184 int num_prompts,
185 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
186 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
187 void** abstract)
188{
189 int i;
190
191 for (i=0; i<num_prompts; i++) {
192 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
193 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
194 responses[i].length = strlen(responses[i].text) + 1;
195 }
196
197 return;
198}
199
Radek Krejcic11fd862012-07-26 12:41:21 +0200200static json_object *err_reply = NULL;
201void netconf_callback_error_process(const char* tag,
202 const char* type,
203 const char* severity,
204 const char* apptag,
205 const char* path,
206 const char* message,
207 const char* attribute,
208 const char* element,
209 const char* ns,
210 const char* sid)
211{
212 err_reply = json_object_new_object();
213 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
214 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
215 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
216 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
217 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
218 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
219 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
220 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
221 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
222 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
223 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
224}
225
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200226static 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 +0200227{
Radek Krejci469aab82012-07-22 18:42:20 +0200228 struct nc_session* session;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200229 char *sid;
Radek Krejcif23850c2012-07-23 16:14:17 +0200230 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200231
232 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200233 password = (char*)pass;
234 session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200235
236 /* if connected successful, add session to the list */
237 if (session != NULL) {
238 /* generate hash for the session */
239 sid = nc_session_get_id(session);
Radek Krejcic11fd862012-07-26 12:41:21 +0200240 session_key = gen_ncsession_hash(
241 (host==NULL) ? "localhost" : host,
242 (port==NULL) ? "830" : port,
243 sid);
Radek Krejci469aab82012-07-22 18:42:20 +0200244 free(sid);
Radek Krejcif23850c2012-07-23 16:14:17 +0200245 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) session);
Radek Krejci469aab82012-07-22 18:42:20 +0200246 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200247 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200248 } else {
249 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200250 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200251 }
252
Radek Krejci469aab82012-07-22 18:42:20 +0200253}
254
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200255static int netconf_close(server_rec* server, apr_hash_t* conns, char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200256{
257 struct nc_session *ns = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200258
Radek Krejcif23850c2012-07-23 16:14:17 +0200259 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200260 ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
261 if (ns != NULL) {
262 nc_session_close (ns, "NETCONF session closed by client");
263 nc_session_free (ns);
264 ns = NULL;
265
266 /* remove session from the active sessions list */
267 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
268 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200269
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200270 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200271 } else {
272 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200273 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200274 }
275}
276
Radek Krejci8e4632a2012-07-26 13:40:34 +0200277static int netconf_op(server_rec* server, apr_hash_t* conns, char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200278{
279 struct nc_session *session = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200280 nc_reply* reply;
281 int retval = EXIT_SUCCESS;
282
Radek Krejci8e4632a2012-07-26 13:40:34 +0200283 /* check requests */
284 if (rpc == NULL) {
285 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
286 return (EXIT_FAILURE);
287 }
288
289 /* get session where send the RPC */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200290 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
291 if (session != NULL) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200292 /* send the request and get the reply */
293 nc_session_send_rpc (session, rpc);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200294 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200295 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200296 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Radek Krejci035bf4e2012-07-25 10:59:09 +0200297 netconf_close(server, conns, session_key);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200298 return (EXIT_FAILURE);
299 }
300
301 /* there is error handled by callback */
302 return (EXIT_FAILURE);
303 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200304
305 switch (nc_reply_get_type (reply)) {
306 case NC_REPLY_OK:
307 retval = EXIT_SUCCESS;
308 break;
309 default:
310 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
311 retval = EXIT_FAILURE;
312 break;
313 }
314 nc_reply_free(reply);
315 return (retval);
316 } else {
317 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
318 return (EXIT_FAILURE);
319 }
320}
Radek Krejci8e4632a2012-07-26 13:40:34 +0200321static char* netconf_opdata(server_rec* server, apr_hash_t* conns, char* session_key, nc_rpc* rpc)
322{
323 struct nc_session *session = NULL;
324 nc_reply* reply;
325 char* data;
326
327 /* check requests */
328 if (rpc == NULL) {
329 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
330 return (NULL);
331 }
332
333 /* get session where send the RPC */
334 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
335 if (session != NULL) {
336 /* send the request and get the reply */
337 nc_session_send_rpc (session, rpc);
338 if (nc_session_recv_reply (session, &reply) == 0) {
339 nc_rpc_free (rpc);
340 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
341 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
342 netconf_close(server, conns, session_key);
343 return (NULL);
344 }
345
346 /* there is error handled by callback */
347 return (NULL);
348 }
349 nc_rpc_free (rpc);
350
351 switch (nc_reply_get_type (reply)) {
352 case NC_REPLY_DATA:
353 if ((data = nc_reply_get_data (reply)) == NULL) {
354 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
355 return (NULL);
356 }
357 break;
358 default:
359 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
360 return (NULL);
361 }
362 nc_reply_free(reply);
363
364 return (data);
365 } else {
366 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
367 return (NULL);
368 }
369}
370
371static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, const char* filter)
372{
373 nc_rpc* rpc;
374 struct nc_filter *f = NULL;
375 char* data = NULL;
376
377 /* create filter if set */
378 if (filter != NULL) {
379 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
380 }
381
382 /* create requests */
383 rpc = nc_rpc_getconfig (source, f);
384 nc_filter_free(f);
385 if (rpc == NULL) {
386 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
387 return (NULL);
388 }
389
390 data = netconf_opdata(server, conns, session_key, rpc);
391 nc_rpc_free (rpc);
392 return (data);
393}
394
395static char* netconf_get(server_rec* server, apr_hash_t* conns, char* session_key, const char* filter)
396{
397 nc_rpc* rpc;
398 struct nc_filter *f = NULL;
399 char* data = NULL;
400
401 /* create filter if set */
402 if (filter != NULL) {
403 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
404 }
405
406 /* create requests */
407 rpc = nc_rpc_get (f);
408 nc_filter_free(f);
409 if (rpc == NULL) {
410 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
411 return (NULL);
412 }
413
414 data = netconf_opdata(server, conns, session_key, rpc);
415 nc_rpc_free (rpc);
416 return (data);
417}
418
419static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config)
420{
421 nc_rpc* rpc;
422 int retval = EXIT_SUCCESS;
423
424 /* create requests */
425 rpc = nc_rpc_copyconfig(source, target, config);
426 if (rpc == NULL) {
427 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
428 return (EXIT_FAILURE);
429 }
430
431 retval = netconf_op(server, conns, session_key, rpc);
432 nc_rpc_free (rpc);
433 return (retval);
434}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200435
Radek Krejci469aab82012-07-22 18:42:20 +0200436server_rec* clb_print_server;
437int clb_print(const char* msg)
438{
439 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
440 return (0);
441}
442
Radek Krejcif23850c2012-07-23 16:14:17 +0200443/*
444 * This is actually implementation of NETCONF client
445 * - requests are received from UNIX socket in the predefined format
446 * - results are replied through the same way
447 * - the daemon run as a separate process, but it is started and stopped
448 * automatically by Apache.
449 *
450 */
Radek Krejci469aab82012-07-22 18:42:20 +0200451static void forked_proc(apr_pool_t * pool, server_rec * server)
452{
453 struct sockaddr_un local, remote;
454 int lsock, client;
455 socklen_t len, len2;
456 struct pollfd fds;
457 int status;
Radek Krejciae021c12012-07-25 18:03:52 +0200458 mod_netconf_cfg *cfg;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200459 json_object *request, *reply;
460 int operation;
461 char* session_key, *data;
462 const char *msgtext;
463 const char *host, *port, *user, *pass;
Radek Krejciae021c12012-07-25 18:03:52 +0200464 const char *target, *source, *filter, *config;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200465 NC_DATASTORE ds_type1, ds_type2;
Radek Krejci469aab82012-07-22 18:42:20 +0200466
467 apr_hash_t *netconf_sessions_list;
468 char buffer[BUFFER_SIZE];
Radek Krejci469aab82012-07-22 18:42:20 +0200469
Radek Krejcif23850c2012-07-23 16:14:17 +0200470 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +0200471 unixd_setup_child();
472
Radek Krejciae021c12012-07-25 18:03:52 +0200473 cfg = ap_get_module_config(server->module_config, &netconf_module);
474 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200475 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
476 return;
477 }
Radek Krejci469aab82012-07-22 18:42:20 +0200478
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200479 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +0200480 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
481 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
482 return;
483 }
484
485 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +0200486 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +0200487 unlink(local.sun_path);
488 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
489
490 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
491 if (errno == EADDRINUSE) {
492 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
493 close(lsock);
494 exit(0);
495 }
496 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
497 close(lsock);
498 return;
499 }
500
501 if (listen(lsock, MAX_SOCKET_CL) == -1) {
502 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
503 close(lsock);
504 return;
505 }
506
507 /* prepare internal lists */
508 netconf_sessions_list = apr_hash_make(pool);
509
510 /* setup libnetconf's callbacks */
511 nc_verbosity(NC_VERB_DEBUG);
512 clb_print_server = server;
513 nc_callback_print(clb_print);
514 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
515 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
516 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +0200517 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +0200518
519 /* disable publickey authentication */
520 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
521
522 while (isterminated == 0) {
523 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
524
525 /* open incoming connection if any */
526 len2 = sizeof(remote);
527 client = accept(lsock, (struct sockaddr *) &remote, &len2);
528 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
529 apr_sleep(SLEEP_TIME);
530 continue;
531 } else if (client == -1 && (errno == EINTR)) {
532 continue;
533 } else if (client == -1) {
534 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
535 continue;
536 }
537 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted.");
538
539 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +0200540 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +0200541
542 while (1) {
543 fds.fd = client;
544 fds.events = POLLIN;
545 fds.revents = 0;
546
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200547 status = poll(&fds, 1, 1000);
Radek Krejci469aab82012-07-22 18:42:20 +0200548
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200549 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
Radek Krejci469aab82012-07-22 18:42:20 +0200550 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200551 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
Radek Krejci469aab82012-07-22 18:42:20 +0200552 continue;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200553 } else if (status < 0) {
Radek Krejci469aab82012-07-22 18:42:20 +0200554 /* 0: poll time outed
555 * close socket and ignore this request from the client, it can try it again
556 * -1: poll failed
557 * something wrong happend, close this socket and wait for another request
558 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200559 //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 +0200560 close(client);
561 break;
562 }
563 /* status > 0 */
564
565 /* check the status of the socket */
566
567 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
568 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
569 /* close client's socket (it's probably already closed by client */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200570 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
Radek Krejci469aab82012-07-22 18:42:20 +0200571 close(client);
572 break;
573 }
574
575 if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) {
576 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno));
577 continue;
578 } else {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200579 request = json_tokener_parse(buffer);
580 operation = json_object_get_int(json_object_object_get(request, "type"));
Radek Krejci469aab82012-07-22 18:42:20 +0200581
Radek Krejci035bf4e2012-07-25 10:59:09 +0200582 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
583 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
584 if (operation != MSG_CONNECT && session_key == NULL) {
585 reply = json_object_new_object();
586 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
587 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
588 msgtext = json_object_to_json_string(reply);
589 send(client, msgtext, strlen(msgtext) + 1, 0);
590 json_object_put(reply);
591 /* there is some stupid client, so close the connection to give a chance to some other client */
592 close(client);
593 break;
594 }
595
Radek Krejcic11fd862012-07-26 12:41:21 +0200596 /* null global JSON error-reply */
597 err_reply = NULL;
598
599 /* process required operation */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200600 switch (operation) {
601 case MSG_CONNECT:
602 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
603
604 host = json_object_get_string(json_object_object_get(request, "host"));
605 port = json_object_get_string(json_object_object_get(request, "port"));
606 user = json_object_get_string(json_object_object_get(request, "user"));
607 pass = json_object_get_string(json_object_object_get(request, "pass"));
608 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
609 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass);
610 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
611
612 reply = json_object_new_object();
613 if (session_key == NULL) {
614 /* negative reply */
Radek Krejcic11fd862012-07-26 12:41:21 +0200615 if (err_reply == NULL) {
616 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
617 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
618 } else {
619 /* use filled err_reply from libnetconf's callback */
620 json_object_put(reply);
621 reply = err_reply;
622 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200623 } else {
624 /* positive reply */
625 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
626 json_object_object_add(reply, "session", json_object_new_string(session_key));
Radek Krejcie31ad212012-07-26 12:51:15 +0200627
628 free(session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200629 }
630
Radek Krejci469aab82012-07-22 18:42:20 +0200631 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200632 case MSG_GET:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200633 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200634
635 filter = json_object_get_string(json_object_object_get(request, "filter"));
636
637 reply = json_object_new_object();
638
639 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200640 if (err_reply == NULL) {
641 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
642 json_object_object_add(reply, "error-message", json_object_new_string("get failed."));
643 } else {
644 /* use filled err_reply from libnetconf's callback */
645 json_object_put(reply);
646 reply = err_reply;
647 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200648 } else {
649 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
650 json_object_object_add(reply, "data", json_object_new_string(data));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200651 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200652 break;
653 case MSG_GETCONFIG:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200654 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200655
656 source = json_object_get_string(json_object_object_get(request, "source"));
657 filter = json_object_get_string(json_object_object_get(request, "filter"));
658
Radek Krejci035bf4e2012-07-25 10:59:09 +0200659 reply = json_object_new_object();
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200660
Radek Krejci5d076db2012-07-26 12:56:59 +0200661 /* if source is NULL, set valid string for strcmp, that is invalid for the following test */
662 source = (source == NULL) ? "": source;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200663 if (strcmp(source, "running") == 0) {
664 ds_type1 = NC_DATASTORE_RUNNING;
665 } else if (strcmp(source, "startup") == 0) {
666 ds_type1 = NC_DATASTORE_STARTUP;
667 } else if (strcmp(source, "candidate") == 0) {
668 ds_type1 = NC_DATASTORE_CANDIDATE;
669 } else {
670 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200671 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200672 break;
673 }
674
675 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type1, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200676 if (err_reply == NULL) {
677 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
678 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
679 } else {
680 /* use filled err_reply from libnetconf's callback */
681 json_object_put(reply);
682 reply = err_reply;
683 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200684 } else {
685 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
686 json_object_object_add(reply, "data", json_object_new_string(data));
687 }
688 break;
689 case MSG_COPYCONFIG:
690 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
Radek Krejciae021c12012-07-25 18:03:52 +0200691 source = target = config = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200692
693 source = json_object_get_string(json_object_object_get(request, "source"));
694 target = json_object_get_string(json_object_object_get(request, "target"));
695
696 reply = json_object_new_object();
697
Radek Krejciae021c12012-07-25 18:03:52 +0200698 if (source != NULL) {
699 if (strcmp(source, "running") == 0) {
700 ds_type1 = NC_DATASTORE_RUNNING;
701 } else if (strcmp(source, "startup") == 0) {
702 ds_type1 = NC_DATASTORE_STARTUP;
703 } else if (strcmp(source, "candidate") == 0) {
704 ds_type1 = NC_DATASTORE_CANDIDATE;
705 } else {
706 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
707 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
708 break;
709 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200710 } else {
Radek Krejciae021c12012-07-25 18:03:52 +0200711 ds_type1 = NC_DATASTORE_NONE;
712 config = json_object_get_string(json_object_object_get(request, "config"));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200713 }
Radek Krejciae021c12012-07-25 18:03:52 +0200714
Radek Krejci5d076db2012-07-26 12:56:59 +0200715 /* if target is NULL, set valid string for strcmp, that is invalid for the following test */
716 target = (target == NULL) ? "": target;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200717 if (strcmp(target, "running") == 0) {
718 ds_type2 = NC_DATASTORE_RUNNING;
719 } else if (strcmp(target, "startup") == 0) {
720 ds_type2 = NC_DATASTORE_STARTUP;
721 } else if (strcmp(target, "candidate") == 0) {
722 ds_type2 = NC_DATASTORE_CANDIDATE;
723 } else {
724 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
725 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200726 break;
727 }
728
Radek Krejciae021c12012-07-25 18:03:52 +0200729 if (target == NULL || (source == NULL && config == NULL)) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200730 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejciae021c12012-07-25 18:03:52 +0200731 json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters."));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200732 } else {
Radek Krejciae021c12012-07-25 18:03:52 +0200733 if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type1, ds_type2, config) != EXIT_SUCCESS) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200734 if (err_reply == NULL) {
735 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
736 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
737 } else {
738 /* use filled err_reply from libnetconf's callback */
739 json_object_put(reply);
740 reply = err_reply;
741 }
Radek Krejciae021c12012-07-25 18:03:52 +0200742 } else {
743 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
744 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200745 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200746 break;
747 case MSG_DISCONNECT:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200748 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200749
750 reply = json_object_new_object();
751 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200752 if (err_reply == NULL) {
753 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
754 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
755 } else {
756 /* use filled err_reply from libnetconf's callback */
757 json_object_put(reply);
758 reply = err_reply;
759 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200760 } else {
761 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
762 }
763 break;
764 default:
765 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
766 reply = json_object_new_object();
767 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
768 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
769 break;
770 }
771 json_object_put(request);
772
773 /* send reply to caller */
774 if (reply != NULL) {
775 msgtext = json_object_to_json_string(reply);
776 send(client, msgtext, strlen(msgtext) + 1, 0);
777 json_object_put(reply);
778 } else {
779 break;
780 }
Radek Krejci469aab82012-07-22 18:42:20 +0200781 }
782 }
783 }
784
785 close(lsock);
786
787 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
788
789 exit(APR_SUCCESS);
790}
791
Radek Krejcif23850c2012-07-23 16:14:17 +0200792static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +0200793{
Radek Krejcif23850c2012-07-23 16:14:17 +0200794 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
795
796 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
797 apr_pool_create(&config->pool, pool);
798 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200799 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +0200800
801 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +0200802}
803
804static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
805 apr_pool_t * plog, server_rec * s)
806{
Radek Krejcif23850c2012-07-23 16:14:17 +0200807 mod_netconf_cfg *config;
808 apr_status_t res;
809
Radek Krejci469aab82012-07-22 18:42:20 +0200810 /* These two help ensure that we only init once. */
811 void *data;
Radek Krejcif23850c2012-07-23 16:14:17 +0200812 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +0200813
814 /*
815 * The following checks if this routine has been called before.
816 * This is necessary because the parent process gets initialized
817 * a couple of times as the server starts up.
818 */
819 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
820 if (!data) {
821 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
822 return (OK);
823 }
824
Radek Krejcif23850c2012-07-23 16:14:17 +0200825 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
826 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +0200827
Radek Krejcif23850c2012-07-23 16:14:17 +0200828 if (config && config->forkproc == NULL) {
829 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
830 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +0200831 switch (res) {
832 case APR_INCHILD:
833 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +0200834 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +0200835 apr_signal(SIGTERM, signal_handler);
836
837 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +0200838 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +0200839
840 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +0200841 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +0200842
Radek Krejcif23850c2012-07-23 16:14:17 +0200843 /* I never should be here, wtf?!? */
844 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +0200845 exit(APR_EGENERAL);
846 break;
847 case APR_INPARENT:
848 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +0200849 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +0200850 break;
851 default:
852 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
853 break;
854 }
Radek Krejcif23850c2012-07-23 16:14:17 +0200855 } else {
856 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +0200857 }
858
859 return OK;
860}
861
Radek Krejci469aab82012-07-22 18:42:20 +0200862/**
863 * Register module hooks
864 */
865static void mod_netconf_register_hooks(apr_pool_t * p)
866{
Radek Krejcif23850c2012-07-23 16:14:17 +0200867 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +0200868}
869
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200870static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
871{
872 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
873 return NULL;
874}
875
876static const command_rec netconf_cmds[] = {
877 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
878 {NULL}
879};
880
Radek Krejci469aab82012-07-22 18:42:20 +0200881/* Dispatch list for API hooks */
882module AP_MODULE_DECLARE_DATA netconf_module = {
883 STANDARD20_MODULE_STUFF,
884 NULL, /* create per-dir config structures */
885 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +0200886 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +0200887 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200888 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +0200889 mod_netconf_register_hooks /* register hooks */
890};