blob: ca62efabf60614786225dd26431d2ecd391a27cc [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>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020051
52#include <unixd.h>
53#include <httpd.h>
54#include <http_log.h>
55#include <http_config.h>
56
57#include <apr_sha1.h>
58#include <apr_hash.h>
59#include <apr_signal.h>
60#include <apr_strings.h>
Radek Krejci469aab82012-07-22 18:42:20 +020061
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020062#include <json/json.h>
63
Radek Krejci469aab82012-07-22 18:42:20 +020064#include <libnetconf.h>
Radek Krejci7b4ddd02012-07-30 08:09:58 +020065
Radek Krejci469aab82012-07-22 18:42:20 +020066
67#define MAX_PROCS 5
Radek Krejci6cb08982012-07-25 18:01:06 +020068#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci469aab82012-07-22 18:42:20 +020069#define MAX_SOCKET_CL 10
70#define BUFFER_SIZE 4096
71
72/* sleep in master process for non-blocking socket reading */
73#define SLEEP_TIME 200
74
75#ifndef offsetof
76#define offsetof(type, member) ((size_t) ((type *) 0)->member)
77#endif
78
79struct timeval timeout = { 1, 0 };
80
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020081typedef enum MSG_TYPE {
82 REPLY_OK,
83 REPLY_DATA,
84 REPLY_ERROR,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020085 REPLY_INFO,
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020086 MSG_CONNECT,
87 MSG_DISCONNECT,
88 MSG_GET,
89 MSG_GETCONFIG,
90 MSG_EDITCONFIG,
91 MSG_COPYCONFIG,
92 MSG_DELETECONFIG,
93 MSG_LOCK,
94 MSG_UNLOCK,
Radek Krejci9e04c7b2012-07-26 15:54:25 +020095 MSG_KILL,
Radek Krejci80c10d92012-07-30 08:38:50 +020096 MSG_INFO,
97 MSG_GENERIC
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020098} MSG_TYPE;
99
Radek Krejci469aab82012-07-22 18:42:20 +0200100#define MSG_OK 0
101#define MSG_OPEN 1
102#define MSG_DATA 2
103#define MSG_CLOSE 3
104#define MSG_ERROR 4
105#define MSG_UNKNOWN 5
106
Radek Krejci469aab82012-07-22 18:42:20 +0200107module AP_MODULE_DECLARE_DATA netconf_module;
108
109typedef struct {
Radek Krejci469aab82012-07-22 18:42:20 +0200110 apr_pool_t *pool;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200111 apr_proc_t *forkproc;
112 char* sockname;
Radek Krejcif23850c2012-07-23 16:14:17 +0200113} mod_netconf_cfg;
Radek Krejci469aab82012-07-22 18:42:20 +0200114
115volatile int isterminated = 0;
116
117static char* password;
118
119
120static void signal_handler(int sign)
121{
122 switch (sign) {
123 case SIGTERM:
124 isterminated = 1;
Radek Krejci469aab82012-07-22 18:42:20 +0200125 break;
126 }
127}
128
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200129static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
Radek Krejci469aab82012-07-22 18:42:20 +0200130{
Radek Krejcif23850c2012-07-23 16:14:17 +0200131 unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
132 int i;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200133 char* hash;
Radek Krejcif23850c2012-07-23 16:14:17 +0200134
Radek Krejci469aab82012-07-22 18:42:20 +0200135 apr_sha1_ctx_t sha1_ctx;
136 apr_sha1_init(&sha1_ctx);
137 apr_sha1_update(&sha1_ctx, hostname, strlen(hostname));
138 apr_sha1_update(&sha1_ctx, port, strlen(port));
139 apr_sha1_update(&sha1_ctx, sid, strlen(sid));
Radek Krejcif23850c2012-07-23 16:14:17 +0200140 apr_sha1_final(hash_raw, &sha1_ctx);
Radek Krejci469aab82012-07-22 18:42:20 +0200141
Radek Krejcif23850c2012-07-23 16:14:17 +0200142 /* convert binary hash into hex string, which is printable */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200143 hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
Radek Krejcif23850c2012-07-23 16:14:17 +0200144 for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200145 snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
Radek Krejcif23850c2012-07-23 16:14:17 +0200146 }
147 //hash[2*APR_SHA1_DIGESTSIZE] = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200148
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200149 return (hash);
Radek Krejci469aab82012-07-22 18:42:20 +0200150}
151
152int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
153{
154 /* always approve */
155 return (EXIT_SUCCESS);
156}
157
158char* netconf_callback_sshauth_password (const char* username, const char* hostname)
159{
160 char* buf;
161
162 buf = malloc ((strlen(password) + 1) * sizeof(char));
163 apr_cpystrn(buf, password, strlen(password) + 1);
164
165 return (buf);
166}
167
168void netconf_callback_sshauth_interactive (const char* name,
169 int name_len,
170 const char* instruction,
171 int instruction_len,
172 int num_prompts,
173 const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
174 LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
175 void** abstract)
176{
177 int i;
178
179 for (i=0; i<num_prompts; i++) {
180 responses[i].text = malloc ((strlen(password) + 1) * sizeof(char));
181 apr_cpystrn(responses[i].text, password, strlen(password) + 1);
182 responses[i].length = strlen(responses[i].text) + 1;
183 }
184
185 return;
186}
187
Radek Krejcic11fd862012-07-26 12:41:21 +0200188static json_object *err_reply = NULL;
189void netconf_callback_error_process(const char* tag,
190 const char* type,
191 const char* severity,
192 const char* apptag,
193 const char* path,
194 const char* message,
195 const char* attribute,
196 const char* element,
197 const char* ns,
198 const char* sid)
199{
200 err_reply = json_object_new_object();
201 json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR));
202 if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag));
203 if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type));
204 if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity));
205 if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag));
206 if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path));
207 if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message));
208 if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute));
209 if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element));
210 if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns));
211 if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid));
212}
213
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200214static 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 +0200215{
Radek Krejci469aab82012-07-22 18:42:20 +0200216 struct nc_session* session;
Radek Krejcif23850c2012-07-23 16:14:17 +0200217 char *session_key;
Radek Krejci469aab82012-07-22 18:42:20 +0200218
219 /* connect to the requested NETCONF server */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200220 password = (char*)pass;
221 session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200222
223 /* if connected successful, add session to the list */
224 if (session != NULL) {
225 /* generate hash for the session */
Radek Krejcic11fd862012-07-26 12:41:21 +0200226 session_key = gen_ncsession_hash(
227 (host==NULL) ? "localhost" : host,
228 (port==NULL) ? "830" : port,
Radek Krejcia282bed2012-07-27 14:43:45 +0200229 nc_session_get_id(session));
Radek Krejcif23850c2012-07-23 16:14:17 +0200230 apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) session);
Radek Krejci469aab82012-07-22 18:42:20 +0200231 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200232 return (session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200233 } else {
234 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200235 return (NULL);
Radek Krejci469aab82012-07-22 18:42:20 +0200236 }
237
Radek Krejci469aab82012-07-22 18:42:20 +0200238}
239
Radek Krejci80c10d92012-07-30 08:38:50 +0200240static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key)
Radek Krejci469aab82012-07-22 18:42:20 +0200241{
242 struct nc_session *ns = NULL;
Radek Krejci469aab82012-07-22 18:42:20 +0200243
Radek Krejcif23850c2012-07-23 16:14:17 +0200244 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
Radek Krejci469aab82012-07-22 18:42:20 +0200245 ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
246 if (ns != NULL) {
247 nc_session_close (ns, "NETCONF session closed by client");
248 nc_session_free (ns);
249 ns = NULL;
250
251 /* remove session from the active sessions list */
252 apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
253 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
Radek Krejcif23850c2012-07-23 16:14:17 +0200254
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200255 return (EXIT_SUCCESS);
Radek Krejci469aab82012-07-22 18:42:20 +0200256 } else {
257 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200258 return (EXIT_FAILURE);
Radek Krejci469aab82012-07-22 18:42:20 +0200259 }
260}
261
Radek Krejci80c10d92012-07-30 08:38:50 +0200262static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci469aab82012-07-22 18:42:20 +0200263{
264 struct nc_session *session = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200265 nc_reply* reply;
266 int retval = EXIT_SUCCESS;
267
Radek Krejci8e4632a2012-07-26 13:40:34 +0200268 /* check requests */
269 if (rpc == NULL) {
270 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
271 return (EXIT_FAILURE);
272 }
273
274 /* get session where send the RPC */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200275 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
276 if (session != NULL) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200277 /* send the request and get the reply */
278 nc_session_send_rpc (session, rpc);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200279 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200280 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200281 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
Radek Krejci035bf4e2012-07-25 10:59:09 +0200282 netconf_close(server, conns, session_key);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200283 return (EXIT_FAILURE);
284 }
285
286 /* there is error handled by callback */
287 return (EXIT_FAILURE);
288 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200289
290 switch (nc_reply_get_type (reply)) {
291 case NC_REPLY_OK:
292 retval = EXIT_SUCCESS;
293 break;
294 default:
295 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
296 retval = EXIT_FAILURE;
297 break;
298 }
299 nc_reply_free(reply);
300 return (retval);
301 } else {
302 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
303 return (EXIT_FAILURE);
304 }
305}
Radek Krejci80c10d92012-07-30 08:38:50 +0200306
307static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200308{
309 struct nc_session *session = NULL;
310 nc_reply* reply;
311 char* data;
312
313 /* check requests */
314 if (rpc == NULL) {
315 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created");
316 return (NULL);
317 }
318
319 /* get session where send the RPC */
320 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
321 if (session != NULL) {
322 /* send the request and get the reply */
323 nc_session_send_rpc (session, rpc);
324 if (nc_session_recv_reply (session, &reply) == 0) {
Radek Krejci8e4632a2012-07-26 13:40:34 +0200325 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
326 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
327 netconf_close(server, conns, session_key);
328 return (NULL);
329 }
330
331 /* there is error handled by callback */
332 return (NULL);
333 }
Radek Krejci8e4632a2012-07-26 13:40:34 +0200334
335 switch (nc_reply_get_type (reply)) {
336 case NC_REPLY_DATA:
337 if ((data = nc_reply_get_data (reply)) == NULL) {
338 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
339 return (NULL);
340 }
341 break;
342 default:
343 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
344 return (NULL);
345 }
346 nc_reply_free(reply);
347
348 return (data);
349 } else {
350 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
351 return (NULL);
352 }
353}
354
Radek Krejci80c10d92012-07-30 08:38:50 +0200355static 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 +0200356{
357 nc_rpc* rpc;
358 struct nc_filter *f = NULL;
359 char* data = NULL;
360
361 /* create filter if set */
362 if (filter != NULL) {
363 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
364 }
365
366 /* create requests */
367 rpc = nc_rpc_getconfig (source, f);
368 nc_filter_free(f);
369 if (rpc == NULL) {
370 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
371 return (NULL);
372 }
373
374 data = netconf_opdata(server, conns, session_key, rpc);
375 nc_rpc_free (rpc);
376 return (data);
377}
378
Radek Krejci80c10d92012-07-30 08:38:50 +0200379static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter)
Radek Krejci8e4632a2012-07-26 13:40:34 +0200380{
381 nc_rpc* rpc;
382 struct nc_filter *f = NULL;
383 char* data = NULL;
384
385 /* create filter if set */
386 if (filter != NULL) {
387 f = nc_filter_new(NC_FILTER_SUBTREE, filter);
388 }
389
390 /* create requests */
391 rpc = nc_rpc_get (f);
392 nc_filter_free(f);
393 if (rpc == NULL) {
394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
395 return (NULL);
396 }
397
398 data = netconf_opdata(server, conns, session_key, rpc);
399 nc_rpc_free (rpc);
400 return (data);
401}
402
Radek Krejci80c10d92012-07-30 08:38:50 +0200403static 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 +0200404{
405 nc_rpc* rpc;
406 int retval = EXIT_SUCCESS;
407
408 /* create requests */
409 rpc = nc_rpc_copyconfig(source, target, config);
410 if (rpc == NULL) {
411 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
412 return (EXIT_FAILURE);
413 }
414
415 retval = netconf_op(server, conns, session_key, rpc);
416 nc_rpc_free (rpc);
417 return (retval);
418}
Radek Krejci035bf4e2012-07-25 10:59:09 +0200419
Radek Krejci80c10d92012-07-30 08:38:50 +0200420static 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 +0200421{
422 nc_rpc* rpc;
423 int retval = EXIT_SUCCESS;
424
425 /* create requests */
426 rpc = nc_rpc_editconfig(target, defop, erropt, config);
427 if (rpc == NULL) {
428 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
429 return (EXIT_FAILURE);
430 }
431
432 retval = netconf_op(server, conns, session_key, rpc);
433 nc_rpc_free (rpc);
434 return (retval);
435}
436
Radek Krejci80c10d92012-07-30 08:38:50 +0200437static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid)
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200438{
439 nc_rpc* rpc;
440 int retval = EXIT_SUCCESS;
441
442 /* create requests */
443 rpc = nc_rpc_killsession(sid);
444 if (rpc == NULL) {
445 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
446 return (EXIT_FAILURE);
447 }
448
449 retval = netconf_op(server, conns, session_key, rpc);
450 nc_rpc_free (rpc);
451 return (retval);
452}
453
Radek Krejci80c10d92012-07-30 08:38:50 +0200454static 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 +0200455{
456 nc_rpc* rpc;
457 int retval = EXIT_SUCCESS;
458
459 /* create requests */
Radek Krejci5cd7d422012-07-26 14:50:29 +0200460 rpc = op_func(target);
Radek Krejci2f318372012-07-26 14:22:35 +0200461 if (rpc == NULL) {
462 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
463 return (EXIT_FAILURE);
464 }
465
466 retval = netconf_op(server, conns, session_key, rpc);
467 nc_rpc_free (rpc);
468 return (retval);
469}
470
Radek Krejci80c10d92012-07-30 08:38:50 +0200471static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200472{
473 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig));
474}
475
Radek Krejci80c10d92012-07-30 08:38:50 +0200476static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200477{
478 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock));
479}
480
Radek Krejci80c10d92012-07-30 08:38:50 +0200481static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target)
Radek Krejci5cd7d422012-07-26 14:50:29 +0200482{
483 return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock));
484}
485
Radek Krejci80c10d92012-07-30 08:38:50 +0200486/**
487 * @return REPLY_OK: 0, *data == NULL
488 * REPLY_DATA: 0, *data != NULL
489 * REPLY_ERROR: 1, *data == NULL
490 */
491static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data)
492{
493 struct nc_session *session = NULL;
494 nc_reply* reply;
495 nc_rpc* rpc;
496 int retval = EXIT_SUCCESS;
497
498 /* create requests */
499 rpc = nc_rpc_generic(content);
500 if (rpc == NULL) {
501 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
502 return (EXIT_FAILURE);
503 }
504
505 *data = NULL;
506
507 /* get session where send the RPC */
508 session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
509 if (session != NULL) {
510 /* send the request and get the reply */
511 nc_session_send_rpc (session, rpc);
512 if (nc_session_recv_reply (session, &reply) == 0) {
513 nc_rpc_free (rpc);
514 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
515 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
516 netconf_close(server, conns, session_key);
517 return (EXIT_FAILURE);
518 }
519
520 /* there is error handled by callback */
521 return (EXIT_FAILURE);
522 }
523 nc_rpc_free (rpc);
524
525 switch (nc_reply_get_type (reply)) {
526 case NC_REPLY_DATA:
527 if ((*data = nc_reply_get_data (reply)) == NULL) {
528 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
529 return (EXIT_FAILURE);
530 }
531 retval = EXIT_SUCCESS;
532 break;
533 case NC_REPLY_OK:
534 retval = EXIT_SUCCESS;
535 break;
536 default:
537 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
538 retval = EXIT_FAILURE;
539 break;
540 }
541 nc_reply_free(reply);
542
543 return (retval);
544 } else {
545 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
546 return (EXIT_FAILURE);
547 }
548}
549
Radek Krejci469aab82012-07-22 18:42:20 +0200550server_rec* clb_print_server;
Radek Krejci7338bde2012-08-10 12:57:30 +0200551void clb_print(NC_VERB_LEVEL level, const char* msg)
Radek Krejci469aab82012-07-22 18:42:20 +0200552{
Radek Krejci7338bde2012-08-10 12:57:30 +0200553 switch (level) {
554 case NC_VERB_ERROR:
555 ap_log_error(APLOG_MARK, APLOG_ERR, 0, clb_print_server, msg);
556 break;
557 case NC_VERB_WARNING:
558 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, clb_print_server, msg);
559 break;
560 case NC_VERB_VERBOSE:
561 ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg);
562 break;
563 case NC_VERB_DEBUG:
564 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, clb_print_server, msg);
565 break;
566 }
Radek Krejci469aab82012-07-22 18:42:20 +0200567}
568
Radek Krejcif23850c2012-07-23 16:14:17 +0200569/*
570 * This is actually implementation of NETCONF client
571 * - requests are received from UNIX socket in the predefined format
572 * - results are replied through the same way
573 * - the daemon run as a separate process, but it is started and stopped
574 * automatically by Apache.
575 *
576 */
Radek Krejci469aab82012-07-22 18:42:20 +0200577static void forked_proc(apr_pool_t * pool, server_rec * server)
578{
579 struct sockaddr_un local, remote;
580 int lsock, client;
581 socklen_t len, len2;
582 struct pollfd fds;
583 int status;
Radek Krejciae021c12012-07-25 18:03:52 +0200584 mod_netconf_cfg *cfg;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200585 json_object *request, *reply, *json_obj;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200586 int operation;
587 char* session_key, *data;
Radek Krejcia282bed2012-07-27 14:43:45 +0200588 const char *msgtext, *cpbltstr;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200589 const char *host, *port, *user, *pass;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200590 const char *target, *source, *filter, *config, *defop, *erropt, *sid;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200591 struct nc_session *session = NULL;
592 struct nc_cpblts* cpblts;
Radek Krejcifa891e72012-07-26 14:04:27 +0200593 NC_DATASTORE ds_type_s, ds_type_t;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200594 NC_EDIT_DEFOP_TYPE defop_type = 0;
595 NC_EDIT_ERROPT_TYPE erropt_type = 0;
Radek Krejci469aab82012-07-22 18:42:20 +0200596
597 apr_hash_t *netconf_sessions_list;
598 char buffer[BUFFER_SIZE];
Radek Krejci469aab82012-07-22 18:42:20 +0200599
Radek Krejcif23850c2012-07-23 16:14:17 +0200600 /* change uid and gid of process for security reasons */
Radek Krejci469aab82012-07-22 18:42:20 +0200601 unixd_setup_child();
602
Radek Krejciae021c12012-07-25 18:03:52 +0200603 cfg = ap_get_module_config(server->module_config, &netconf_module);
604 if (cfg == NULL) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200605 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
606 return;
607 }
Radek Krejci469aab82012-07-22 18:42:20 +0200608
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200609 /* create listening UNIX socket to accept incoming connections */
Radek Krejci469aab82012-07-22 18:42:20 +0200610 if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
611 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
612 return;
613 }
614
615 local.sun_family = AF_UNIX;
Radek Krejciae021c12012-07-25 18:03:52 +0200616 strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path));
Radek Krejci469aab82012-07-22 18:42:20 +0200617 unlink(local.sun_path);
618 len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
619
620 if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
621 if (errno == EADDRINUSE) {
622 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use");
623 close(lsock);
624 exit(0);
625 }
626 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno));
627 close(lsock);
628 return;
629 }
630
631 if (listen(lsock, MAX_SOCKET_CL) == -1) {
632 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno));
633 close(lsock);
634 return;
635 }
636
637 /* prepare internal lists */
638 netconf_sessions_list = apr_hash_make(pool);
639
640 /* setup libnetconf's callbacks */
641 nc_verbosity(NC_VERB_DEBUG);
642 clb_print_server = server;
643 nc_callback_print(clb_print);
644 nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check);
645 nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive);
646 nc_callback_sshauth_password(netconf_callback_sshauth_password);
Radek Krejcic11fd862012-07-26 12:41:21 +0200647 nc_callback_error_reply(netconf_callback_error_process);
Radek Krejci469aab82012-07-22 18:42:20 +0200648
649 /* disable publickey authentication */
650 nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1);
651
652 while (isterminated == 0) {
653 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request");
654
655 /* open incoming connection if any */
656 len2 = sizeof(remote);
657 client = accept(lsock, (struct sockaddr *) &remote, &len2);
658 if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
659 apr_sleep(SLEEP_TIME);
660 continue;
661 } else if (client == -1 && (errno == EINTR)) {
662 continue;
663 } else if (client == -1) {
664 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno));
665 continue;
666 }
667 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted.");
668
669 /* set client's socket as non-blocking */
Radek Krejcif23850c2012-07-23 16:14:17 +0200670 //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK);
Radek Krejci469aab82012-07-22 18:42:20 +0200671
672 while (1) {
673 fds.fd = client;
674 fds.events = POLLIN;
675 fds.revents = 0;
676
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200677 status = poll(&fds, 1, 1000);
Radek Krejci469aab82012-07-22 18:42:20 +0200678
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200679 if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
Radek Krejci469aab82012-07-22 18:42:20 +0200680 /* poll was interrupted - check if the isterminated is set and if not, try poll again */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200681 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
Radek Krejci469aab82012-07-22 18:42:20 +0200682 continue;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200683 } else if (status < 0) {
Radek Krejci469aab82012-07-22 18:42:20 +0200684 /* 0: poll time outed
685 * close socket and ignore this request from the client, it can try it again
686 * -1: poll failed
687 * something wrong happend, close this socket and wait for another request
688 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200689 //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 +0200690 close(client);
691 break;
692 }
693 /* status > 0 */
694
695 /* check the status of the socket */
696
697 /* if nothing to read and POLLHUP (EOF) or POLLERR set */
698 if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
699 /* close client's socket (it's probably already closed by client */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200700 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
Radek Krejci469aab82012-07-22 18:42:20 +0200701 close(client);
702 break;
703 }
704
705 if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) {
706 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno));
707 continue;
708 } else {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200709 request = json_tokener_parse(buffer);
710 operation = json_object_get_int(json_object_object_get(request, "type"));
Radek Krejci469aab82012-07-22 18:42:20 +0200711
Radek Krejci035bf4e2012-07-25 10:59:09 +0200712 session_key = (char*) json_object_get_string(json_object_object_get(request, "session"));
713 /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
714 if (operation != MSG_CONNECT && session_key == NULL) {
715 reply = json_object_new_object();
716 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
717 json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification."));
718 msgtext = json_object_to_json_string(reply);
719 send(client, msgtext, strlen(msgtext) + 1, 0);
720 json_object_put(reply);
721 /* there is some stupid client, so close the connection to give a chance to some other client */
722 close(client);
723 break;
724 }
725
Radek Krejcifa891e72012-07-26 14:04:27 +0200726 /* get parameters */
727 ds_type_t = -1;
728 if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) {
729 if (strcmp(target, "running") == 0) {
730 ds_type_t = NC_DATASTORE_RUNNING;
731 } else if (strcmp(target, "startup") == 0) {
732 ds_type_t = NC_DATASTORE_STARTUP;
733 } else if (strcmp(target, "candidate") == 0) {
734 ds_type_t = NC_DATASTORE_CANDIDATE;
735 }
736 }
737 ds_type_s = -1;
738 if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) {
739 if (strcmp(source, "running") == 0) {
740 ds_type_s = NC_DATASTORE_RUNNING;
741 } else if (strcmp(source, "startup") == 0) {
742 ds_type_s = NC_DATASTORE_STARTUP;
743 } else if (strcmp(source, "candidate") == 0) {
744 ds_type_s = NC_DATASTORE_CANDIDATE;
745 }
746 }
747
Radek Krejcic11fd862012-07-26 12:41:21 +0200748 /* null global JSON error-reply */
749 err_reply = NULL;
750
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200751 /* prepare reply envelope */
752 reply = json_object_new_object();
753
Radek Krejcic11fd862012-07-26 12:41:21 +0200754 /* process required operation */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200755 switch (operation) {
756 case MSG_CONNECT:
757 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
758
759 host = json_object_get_string(json_object_object_get(request, "host"));
760 port = json_object_get_string(json_object_object_get(request, "port"));
761 user = json_object_get_string(json_object_object_get(request, "user"));
762 pass = json_object_get_string(json_object_object_get(request, "pass"));
763 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
Tomas Cejka11dac0d2012-08-06 13:32:09 +0200764 if ((host == NULL) || (user == NULL)) {
765 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Cannot connect - insufficient input.");
766 session_key = NULL;
767 } else {
768 session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass);
769 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
770 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200771
772 reply = json_object_new_object();
773 if (session_key == NULL) {
774 /* negative reply */
Radek Krejcic11fd862012-07-26 12:41:21 +0200775 if (err_reply == NULL) {
776 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
777 json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
778 } else {
779 /* use filled err_reply from libnetconf's callback */
780 json_object_put(reply);
781 reply = err_reply;
782 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200783 } else {
784 /* positive reply */
785 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
786 json_object_object_add(reply, "session", json_object_new_string(session_key));
Radek Krejcie31ad212012-07-26 12:51:15 +0200787
788 free(session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200789 }
790
Radek Krejci469aab82012-07-22 18:42:20 +0200791 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200792 case MSG_GET:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200793 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200794
795 filter = json_object_get_string(json_object_object_get(request, "filter"));
796
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200797 if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200798 if (err_reply == NULL) {
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("get failed."));
801 } else {
802 /* use filled err_reply from libnetconf's callback */
803 json_object_put(reply);
804 reply = err_reply;
805 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200806 } else {
807 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
808 json_object_object_add(reply, "data", json_object_new_string(data));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200809 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200810 break;
811 case MSG_GETCONFIG:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200812 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200813
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200814 filter = json_object_get_string(json_object_object_get(request, "filter"));
815
Radek Krejcifa891e72012-07-26 14:04:27 +0200816 if (ds_type_s == -1) {
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200817 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejci035bf4e2012-07-25 10:59:09 +0200818 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200819 break;
820 }
821
Radek Krejcifa891e72012-07-26 14:04:27 +0200822 if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) {
Radek Krejcic11fd862012-07-26 12:41:21 +0200823 if (err_reply == NULL) {
824 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
825 json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
826 } else {
827 /* use filled err_reply from libnetconf's callback */
828 json_object_put(reply);
829 reply = err_reply;
830 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200831 } else {
832 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
833 json_object_object_add(reply, "data", json_object_new_string(data));
834 }
835 break;
Radek Krejci62ab34b2012-07-26 13:42:05 +0200836 case MSG_EDITCONFIG:
837 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key);
838
Radek Krejci62ab34b2012-07-26 13:42:05 +0200839 defop = json_object_get_string(json_object_object_get(request, "default-operation"));
840 if (defop != NULL) {
841 if (strcmp(defop, "merge") == 0) {
842 defop_type = NC_EDIT_DEFOP_MERGE;
843 } else if (strcmp(defop, "replace") == 0) {
844 defop_type = NC_EDIT_DEFOP_REPLACE;
845 } else if (strcmp(defop, "none") == 0) {
846 defop_type = NC_EDIT_DEFOP_NONE;
847 } else {
848 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
849 json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter."));
850 break;
851 }
852 } else {
853 defop_type = 0;
854 }
855
856 erropt = json_object_get_string(json_object_object_get(request, "error-option"));
857 if (erropt != NULL) {
858 if (strcmp(erropt, "continue-on-error") == 0) {
859 erropt_type = NC_EDIT_ERROPT_CONT;
860 } else if (strcmp(erropt, "stop-on-error") == 0) {
861 erropt_type = NC_EDIT_ERROPT_STOP;
862 } else if (strcmp(erropt, "rollback-on-error") == 0) {
863 erropt_type = NC_EDIT_ERROPT_ROLLBACK;
864 } else {
865 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
866 json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter."));
867 break;
868 }
869 } else {
870 erropt_type = 0;
871 }
872
Radek Krejcifa891e72012-07-26 14:04:27 +0200873 if (ds_type_t == -1) {
Radek Krejci62ab34b2012-07-26 13:42:05 +0200874 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
875 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
876 break;
877 }
878
879 config = json_object_get_string(json_object_object_get(request, "config"));
880 if (config == NULL) {
881 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
882 json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter."));
883 break;
884 }
885
Radek Krejcifa891e72012-07-26 14:04:27 +0200886 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 +0200887 if (err_reply == NULL) {
888 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
889 json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed."));
890 } else {
891 /* use filled err_reply from libnetconf's callback */
892 json_object_put(reply);
893 reply = err_reply;
894 }
895 } else {
896 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
897 }
898 break;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200899 case MSG_COPYCONFIG:
900 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key);
Radek Krejcicebb7af2012-07-26 14:58:12 +0200901 config = NULL;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200902
Radek Krejcifa891e72012-07-26 14:04:27 +0200903 if (source == NULL) {
904 /* no explicit source specified -> use config data */
905 ds_type_s = NC_DATASTORE_NONE;
Radek Krejciae021c12012-07-25 18:03:52 +0200906 config = json_object_get_string(json_object_object_get(request, "config"));
Radek Krejcifa891e72012-07-26 14:04:27 +0200907 } else if (ds_type_s == -1) {
908 /* source datastore specified, but it is invalid */
909 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
910 json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested."));
911 break;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200912 }
Radek Krejciae021c12012-07-25 18:03:52 +0200913
Radek Krejcifa891e72012-07-26 14:04:27 +0200914 if (ds_type_t == -1) {
915 /* invalid target datastore specified */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200916 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
917 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200918 break;
919 }
920
Radek Krejcifa891e72012-07-26 14:04:27 +0200921 if (source == NULL && config == NULL) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200922 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejcifa891e72012-07-26 14:04:27 +0200923 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 +0200924 } else {
Radek Krejcifa891e72012-07-26 14:04:27 +0200925 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 +0200926 if (err_reply == NULL) {
927 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
928 json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed."));
929 } else {
930 /* use filled err_reply from libnetconf's callback */
931 json_object_put(reply);
932 reply = err_reply;
933 }
Radek Krejciae021c12012-07-25 18:03:52 +0200934 } else {
935 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
936 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200937 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200938 break;
Radek Krejci2f318372012-07-26 14:22:35 +0200939 case MSG_DELETECONFIG:
940 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key);
Radek Krejci5cd7d422012-07-26 14:50:29 +0200941 /* no break - unifying code */
942 case MSG_LOCK:
943 if (operation == MSG_LOCK) {
944 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key);
945 }
946 /* no break - unifying code */
947 case MSG_UNLOCK:
948 if (operation == MSG_UNLOCK) {
949 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key);
950 }
Radek Krejci2f318372012-07-26 14:22:35 +0200951
Radek Krejci2f318372012-07-26 14:22:35 +0200952 if (ds_type_t == -1) {
953 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
954 json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
955 break;
956 }
957
Radek Krejci5cd7d422012-07-26 14:50:29 +0200958 switch(operation) {
959 case MSG_DELETECONFIG:
960 status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t);
961 break;
962 case MSG_LOCK:
963 status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t);
964 break;
965 case MSG_UNLOCK:
966 status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t);
967 break;
968 default:
969 status = -1;
970 break;
971 }
972
973 if (status != EXIT_SUCCESS) {
Radek Krejci2f318372012-07-26 14:22:35 +0200974 if (err_reply == NULL) {
975 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
Radek Krejci5cd7d422012-07-26 14:50:29 +0200976 json_object_object_add(reply, "error-message", json_object_new_string("operation failed."));
Radek Krejci2f318372012-07-26 14:22:35 +0200977 } else {
978 /* use filled err_reply from libnetconf's callback */
979 json_object_put(reply);
980 reply = err_reply;
981 }
982 } else {
983 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
984 }
985 break;
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200986 case MSG_KILL:
987 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key);
988
989 sid = json_object_get_string(json_object_object_get(request, "session-id"));
990
Radek Krejcie34d3eb2012-07-26 15:05:53 +0200991 if (sid == NULL) {
992 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
993 json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter."));
994 break;
995 }
996
997 if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) {
998 if (err_reply == NULL) {
999 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1000 json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed."));
1001 } else {
1002 /* use filled err_reply from libnetconf's callback */
1003 json_object_put(reply);
1004 reply = err_reply;
1005 }
1006 } else {
1007 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1008 }
1009 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001010 case MSG_DISCONNECT:
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001011 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001012
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001013 if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
Radek Krejcic11fd862012-07-26 12:41:21 +02001014 if (err_reply == NULL) {
1015 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1016 json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
1017 } else {
1018 /* use filled err_reply from libnetconf's callback */
1019 json_object_put(reply);
1020 reply = err_reply;
1021 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001022 } else {
1023 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1024 }
1025 break;
Radek Krejci9e04c7b2012-07-26 15:54:25 +02001026 case MSG_INFO:
1027 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key);
1028
1029 session = (struct nc_session *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
1030 if (session != NULL) {
Radek Krejcia282bed2012-07-27 14:43:45 +02001031 json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session)));
Radek Krejci9e04c7b2012-07-26 15:54:25 +02001032 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 +02001033 json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session)));
1034 json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session)));
1035 json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session)));
Radek Krejci9e04c7b2012-07-26 15:54:25 +02001036 cpblts = nc_session_get_cpblts (session);
1037 if (cpblts != NULL) {
1038 json_obj = json_object_new_array();
1039 nc_cpblts_iter_start (cpblts);
Radek Krejcia282bed2012-07-27 14:43:45 +02001040 while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) {
1041 json_object_array_add(json_obj, json_object_new_string(cpbltstr));
Radek Krejci9e04c7b2012-07-26 15:54:25 +02001042 }
1043 json_object_object_add(reply, "capabilities", json_obj);
1044 }
1045 } else {
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 session identifier."));
1048 }
1049
1050 break;
Radek Krejci80c10d92012-07-30 08:38:50 +02001051 case MSG_GENERIC:
1052 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key);
1053
1054 config = json_object_get_string(json_object_object_get(request, "content"));
1055
1056 if (config == NULL) {
1057 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1058 json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter."));
1059 break;
1060 }
1061
1062 if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != 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("kill-session 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 if (data == NULL) {
1073 json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
1074 } else {
1075 json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
1076 json_object_object_add(reply, "data", json_object_new_string(data));
1077 }
1078 }
1079 break;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001080 default:
1081 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
1082 reply = json_object_new_object();
1083 json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
1084 json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
1085 break;
1086 }
1087 json_object_put(request);
1088
1089 /* send reply to caller */
1090 if (reply != NULL) {
1091 msgtext = json_object_to_json_string(reply);
1092 send(client, msgtext, strlen(msgtext) + 1, 0);
1093 json_object_put(reply);
1094 } else {
1095 break;
1096 }
Radek Krejci469aab82012-07-22 18:42:20 +02001097 }
1098 }
1099 }
1100
1101 close(lsock);
1102
1103 ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon");
1104
1105 exit(APR_SUCCESS);
1106}
1107
Radek Krejcif23850c2012-07-23 16:14:17 +02001108static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s)
Radek Krejci469aab82012-07-22 18:42:20 +02001109{
Radek Krejcif23850c2012-07-23 16:14:17 +02001110 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config");
1111
1112 mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
1113 apr_pool_create(&config->pool, pool);
1114 config->forkproc = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001115 config->sockname = SOCKET_FILENAME;
Radek Krejcif23850c2012-07-23 16:14:17 +02001116
1117 return (void *)config;
Radek Krejci469aab82012-07-22 18:42:20 +02001118}
1119
1120static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp,
1121 apr_pool_t * plog, server_rec * s)
1122{
Radek Krejcif23850c2012-07-23 16:14:17 +02001123 mod_netconf_cfg *config;
1124 apr_status_t res;
1125
Radek Krejci469aab82012-07-22 18:42:20 +02001126 /* These two help ensure that we only init once. */
1127 void *data;
Radek Krejcif23850c2012-07-23 16:14:17 +02001128 const char *userdata_key = "netconf_ipc_init";
Radek Krejci469aab82012-07-22 18:42:20 +02001129
1130 /*
1131 * The following checks if this routine has been called before.
1132 * This is necessary because the parent process gets initialized
1133 * a couple of times as the server starts up.
1134 */
1135 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
1136 if (!data) {
1137 apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
1138 return (OK);
1139 }
1140
Radek Krejcif23850c2012-07-23 16:14:17 +02001141 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon");
1142 config = ap_get_module_config(s->module_config, &netconf_module);
Radek Krejcidfaa6ea2012-07-23 09:04:43 +02001143
Radek Krejcif23850c2012-07-23 16:14:17 +02001144 if (config && config->forkproc == NULL) {
1145 config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t));
1146 res = apr_proc_fork(config->forkproc, config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001147 switch (res) {
1148 case APR_INCHILD:
1149 /* set signal handler */
Radek Krejcif23850c2012-07-23 16:14:17 +02001150 apr_signal_init(config->pool);
Radek Krejci469aab82012-07-22 18:42:20 +02001151 apr_signal(SIGTERM, signal_handler);
1152
1153 /* log start of the separated NETCONF communication process */
Radek Krejcif23850c2012-07-23 16:14:17 +02001154 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid());
Radek Krejci469aab82012-07-22 18:42:20 +02001155
1156 /* start main loop providing NETCONF communication */
Radek Krejcif23850c2012-07-23 16:14:17 +02001157 forked_proc(config->pool, s);
Radek Krejci469aab82012-07-22 18:42:20 +02001158
Radek Krejcif23850c2012-07-23 16:14:17 +02001159 /* I never should be here, wtf?!? */
1160 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped");
Radek Krejci469aab82012-07-22 18:42:20 +02001161 exit(APR_EGENERAL);
1162 break;
1163 case APR_INPARENT:
1164 /* register child to be killed (SIGTERM) when the module config's pool dies */
Radek Krejcif23850c2012-07-23 16:14:17 +02001165 apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
Radek Krejci469aab82012-07-22 18:42:20 +02001166 break;
1167 default:
1168 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed");
1169 break;
1170 }
Radek Krejcif23850c2012-07-23 16:14:17 +02001171 } else {
1172 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure");
Radek Krejci469aab82012-07-22 18:42:20 +02001173 }
1174
1175 return OK;
1176}
1177
Radek Krejci469aab82012-07-22 18:42:20 +02001178/**
1179 * Register module hooks
1180 */
1181static void mod_netconf_register_hooks(apr_pool_t * p)
1182{
Radek Krejcif23850c2012-07-23 16:14:17 +02001183 ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
Radek Krejci469aab82012-07-22 18:42:20 +02001184}
1185
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001186static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
1187{
1188 ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
1189 return NULL;
1190}
1191
1192static const command_rec netconf_cmds[] = {
1193 AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
1194 {NULL}
1195};
1196
Radek Krejci469aab82012-07-22 18:42:20 +02001197/* Dispatch list for API hooks */
1198module AP_MODULE_DECLARE_DATA netconf_module = {
1199 STANDARD20_MODULE_STUFF,
1200 NULL, /* create per-dir config structures */
1201 NULL, /* merge per-dir config structures */
Radek Krejcif23850c2012-07-23 16:14:17 +02001202 mod_netconf_create_conf, /* create per-server config structures */
Radek Krejci469aab82012-07-22 18:42:20 +02001203 NULL, /* merge per-server config structures */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001204 netconf_cmds, /* table of config file commands */
Radek Krejci469aab82012-07-22 18:42:20 +02001205 mod_netconf_register_hooks /* register hooks */
1206};