Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1 | /*! |
| 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 Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 76 | #include <json/json.h> |
| 77 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 78 | #include <libnetconf.h> |
| 79 | #include <libxml/tree.h> |
| 80 | #include <libxml/parser.h> |
| 81 | |
| 82 | #define MAX_PROCS 5 |
Radek Krejci | 6cb0898 | 2012-07-25 18:01:06 +0200 | [diff] [blame] | 83 | #define SOCKET_FILENAME "/tmp/mod_netconf.sock" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 84 | #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 | |
| 94 | struct timeval timeout = { 1, 0 }; |
| 95 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 96 | typedef 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 Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 112 | #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 Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 119 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 120 | |
| 121 | typedef struct { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 122 | apr_pool_t *pool; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 123 | apr_proc_t *forkproc; |
| 124 | char* sockname; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 125 | } mod_netconf_cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 126 | |
| 127 | volatile int isterminated = 0; |
| 128 | |
| 129 | static char* password; |
| 130 | |
| 131 | |
| 132 | static void signal_handler(int sign) |
| 133 | { |
| 134 | switch (sign) { |
| 135 | case SIGTERM: |
| 136 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 141 | static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 142 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 143 | unsigned char hash_raw[APR_SHA1_DIGESTSIZE]; |
| 144 | int i; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 145 | char* hash; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 147 | 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 Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 152 | apr_sha1_final(hash_raw, &sha1_ctx); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 153 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 154 | /* convert binary hash into hex string, which is printable */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 155 | hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 156 | for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 157 | snprintf(hash + (2*i), 3, "%02x", hash_raw[i]); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 158 | } |
| 159 | //hash[2*APR_SHA1_DIGESTSIZE] = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 160 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 161 | return (hash); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint) |
| 165 | { |
| 166 | /* always approve */ |
| 167 | return (EXIT_SUCCESS); |
| 168 | } |
| 169 | |
| 170 | char* 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 | |
| 180 | void 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 Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 200 | static json_object *err_reply = NULL; |
| 201 | void 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 Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 226 | static 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 Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 227 | { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 228 | struct nc_session* session; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 229 | char *sid; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 230 | char *session_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 231 | |
| 232 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 233 | password = (char*)pass; |
| 234 | session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 235 | |
| 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 Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 240 | session_key = gen_ncsession_hash( |
| 241 | (host==NULL) ? "localhost" : host, |
| 242 | (port==NULL) ? "830" : port, |
| 243 | sid); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 244 | free(sid); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 245 | apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) session); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 246 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 247 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 248 | } else { |
| 249 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 250 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 255 | static int netconf_close(server_rec* server, apr_hash_t* conns, char* session_key) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 256 | { |
| 257 | struct nc_session *ns = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 258 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 259 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 260 | 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 Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 269 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 270 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 271 | } else { |
| 272 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 273 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 277 | static int netconf_op(server_rec* server, apr_hash_t* conns, char* session_key, nc_rpc* rpc) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 278 | { |
| 279 | struct nc_session *session = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 280 | nc_reply* reply; |
| 281 | int retval = EXIT_SUCCESS; |
| 282 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 283 | /* 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 Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 290 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 291 | if (session != NULL) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 292 | /* send the request and get the reply */ |
| 293 | nc_session_send_rpc (session, rpc); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 294 | if (nc_session_recv_reply (session, &reply) == 0) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 295 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 296 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 297 | netconf_close(server, conns, session_key); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 298 | return (EXIT_FAILURE); |
| 299 | } |
| 300 | |
| 301 | /* there is error handled by callback */ |
| 302 | return (EXIT_FAILURE); |
| 303 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 304 | |
| 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 Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 321 | static 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 | |
| 371 | static 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 | |
| 395 | static 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 | |
| 419 | static 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 Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 435 | |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 436 | static int netconf_editconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, const char* config) |
| 437 | { |
| 438 | nc_rpc* rpc; |
| 439 | int retval = EXIT_SUCCESS; |
| 440 | |
| 441 | /* create requests */ |
| 442 | rpc = nc_rpc_editconfig(target, defop, erropt, config); |
| 443 | if (rpc == NULL) { |
| 444 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 445 | return (EXIT_FAILURE); |
| 446 | } |
| 447 | |
| 448 | retval = netconf_op(server, conns, session_key, rpc); |
| 449 | nc_rpc_free (rpc); |
| 450 | return (retval); |
| 451 | } |
| 452 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 453 | server_rec* clb_print_server; |
| 454 | int clb_print(const char* msg) |
| 455 | { |
| 456 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 457 | return (0); |
| 458 | } |
| 459 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 460 | /* |
| 461 | * This is actually implementation of NETCONF client |
| 462 | * - requests are received from UNIX socket in the predefined format |
| 463 | * - results are replied through the same way |
| 464 | * - the daemon run as a separate process, but it is started and stopped |
| 465 | * automatically by Apache. |
| 466 | * |
| 467 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 468 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 469 | { |
| 470 | struct sockaddr_un local, remote; |
| 471 | int lsock, client; |
| 472 | socklen_t len, len2; |
| 473 | struct pollfd fds; |
| 474 | int status; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 475 | mod_netconf_cfg *cfg; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 476 | json_object *request, *reply; |
| 477 | int operation; |
| 478 | char* session_key, *data; |
| 479 | const char *msgtext; |
| 480 | const char *host, *port, *user, *pass; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 481 | const char *target, *source, *filter, *config, *defop, *erropt; |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 482 | NC_DATASTORE ds_type_s, ds_type_t; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 483 | NC_EDIT_DEFOP_TYPE defop_type = 0; |
| 484 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 485 | |
| 486 | apr_hash_t *netconf_sessions_list; |
| 487 | char buffer[BUFFER_SIZE]; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 488 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 489 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 490 | unixd_setup_child(); |
| 491 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 492 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 493 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 494 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 495 | return; |
| 496 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 497 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 498 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 499 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 500 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 505 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 506 | unlink(local.sun_path); |
| 507 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 508 | |
| 509 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 510 | if (errno == EADDRINUSE) { |
| 511 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 512 | close(lsock); |
| 513 | exit(0); |
| 514 | } |
| 515 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 516 | close(lsock); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 521 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 522 | close(lsock); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | /* prepare internal lists */ |
| 527 | netconf_sessions_list = apr_hash_make(pool); |
| 528 | |
| 529 | /* setup libnetconf's callbacks */ |
| 530 | nc_verbosity(NC_VERB_DEBUG); |
| 531 | clb_print_server = server; |
| 532 | nc_callback_print(clb_print); |
| 533 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 534 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 535 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 536 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 537 | |
| 538 | /* disable publickey authentication */ |
| 539 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 540 | |
| 541 | while (isterminated == 0) { |
| 542 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 543 | |
| 544 | /* open incoming connection if any */ |
| 545 | len2 = sizeof(remote); |
| 546 | client = accept(lsock, (struct sockaddr *) &remote, &len2); |
| 547 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 548 | apr_sleep(SLEEP_TIME); |
| 549 | continue; |
| 550 | } else if (client == -1 && (errno == EINTR)) { |
| 551 | continue; |
| 552 | } else if (client == -1) { |
| 553 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 554 | continue; |
| 555 | } |
| 556 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted."); |
| 557 | |
| 558 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 559 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 560 | |
| 561 | while (1) { |
| 562 | fds.fd = client; |
| 563 | fds.events = POLLIN; |
| 564 | fds.revents = 0; |
| 565 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 566 | status = poll(&fds, 1, 1000); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 567 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 568 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 569 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 570 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 571 | continue; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 572 | } else if (status < 0) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 573 | /* 0: poll time outed |
| 574 | * close socket and ignore this request from the client, it can try it again |
| 575 | * -1: poll failed |
| 576 | * something wrong happend, close this socket and wait for another request |
| 577 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 578 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 579 | close(client); |
| 580 | break; |
| 581 | } |
| 582 | /* status > 0 */ |
| 583 | |
| 584 | /* check the status of the socket */ |
| 585 | |
| 586 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 587 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 588 | /* close client's socket (it's probably already closed by client */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 589 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 590 | close(client); |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) { |
| 595 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno)); |
| 596 | continue; |
| 597 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 598 | request = json_tokener_parse(buffer); |
| 599 | operation = json_object_get_int(json_object_object_get(request, "type")); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 600 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 601 | session_key = (char*) json_object_get_string(json_object_object_get(request, "session")); |
| 602 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 603 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 604 | reply = json_object_new_object(); |
| 605 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 606 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 607 | msgtext = json_object_to_json_string(reply); |
| 608 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 609 | json_object_put(reply); |
| 610 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 611 | close(client); |
| 612 | break; |
| 613 | } |
| 614 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 615 | /* get parameters */ |
| 616 | ds_type_t = -1; |
| 617 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 618 | if (strcmp(target, "running") == 0) { |
| 619 | ds_type_t = NC_DATASTORE_RUNNING; |
| 620 | } else if (strcmp(target, "startup") == 0) { |
| 621 | ds_type_t = NC_DATASTORE_STARTUP; |
| 622 | } else if (strcmp(target, "candidate") == 0) { |
| 623 | ds_type_t = NC_DATASTORE_CANDIDATE; |
| 624 | } |
| 625 | } |
| 626 | ds_type_s = -1; |
| 627 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 628 | if (strcmp(source, "running") == 0) { |
| 629 | ds_type_s = NC_DATASTORE_RUNNING; |
| 630 | } else if (strcmp(source, "startup") == 0) { |
| 631 | ds_type_s = NC_DATASTORE_STARTUP; |
| 632 | } else if (strcmp(source, "candidate") == 0) { |
| 633 | ds_type_s = NC_DATASTORE_CANDIDATE; |
| 634 | } |
| 635 | } |
| 636 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 637 | /* null global JSON error-reply */ |
| 638 | err_reply = NULL; |
| 639 | |
| 640 | /* process required operation */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 641 | switch (operation) { |
| 642 | case MSG_CONNECT: |
| 643 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 644 | |
| 645 | host = json_object_get_string(json_object_object_get(request, "host")); |
| 646 | port = json_object_get_string(json_object_object_get(request, "port")); |
| 647 | user = json_object_get_string(json_object_object_get(request, "user")); |
| 648 | pass = json_object_get_string(json_object_object_get(request, "pass")); |
| 649 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 650 | session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass); |
| 651 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key); |
| 652 | |
| 653 | reply = json_object_new_object(); |
| 654 | if (session_key == NULL) { |
| 655 | /* negative reply */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 656 | if (err_reply == NULL) { |
| 657 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 658 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 659 | } else { |
| 660 | /* use filled err_reply from libnetconf's callback */ |
| 661 | json_object_put(reply); |
| 662 | reply = err_reply; |
| 663 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 664 | } else { |
| 665 | /* positive reply */ |
| 666 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 667 | json_object_object_add(reply, "session", json_object_new_string(session_key)); |
Radek Krejci | e31ad21 | 2012-07-26 12:51:15 +0200 | [diff] [blame] | 668 | |
| 669 | free(session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 672 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 673 | case MSG_GET: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 674 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 675 | |
| 676 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 677 | |
| 678 | reply = json_object_new_object(); |
| 679 | |
| 680 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 681 | if (err_reply == NULL) { |
| 682 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 683 | json_object_object_add(reply, "error-message", json_object_new_string("get failed.")); |
| 684 | } else { |
| 685 | /* use filled err_reply from libnetconf's callback */ |
| 686 | json_object_put(reply); |
| 687 | reply = err_reply; |
| 688 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 689 | } else { |
| 690 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 691 | json_object_object_add(reply, "data", json_object_new_string(data)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 692 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 693 | break; |
| 694 | case MSG_GETCONFIG: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 695 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 696 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 697 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 698 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 699 | reply = json_object_new_object(); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 700 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 701 | if (ds_type_s == -1) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 702 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 703 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 704 | break; |
| 705 | } |
| 706 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 707 | if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type_s, filter)) == NULL) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 708 | if (err_reply == NULL) { |
| 709 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 710 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 711 | } else { |
| 712 | /* use filled err_reply from libnetconf's callback */ |
| 713 | json_object_put(reply); |
| 714 | reply = err_reply; |
| 715 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 716 | } else { |
| 717 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 718 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 719 | } |
| 720 | break; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 721 | case MSG_EDITCONFIG: |
| 722 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key); |
| 723 | |
| 724 | reply = json_object_new_object(); |
| 725 | |
| 726 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 727 | if (defop != NULL) { |
| 728 | if (strcmp(defop, "merge") == 0) { |
| 729 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 730 | } else if (strcmp(defop, "replace") == 0) { |
| 731 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 732 | } else if (strcmp(defop, "none") == 0) { |
| 733 | defop_type = NC_EDIT_DEFOP_NONE; |
| 734 | } else { |
| 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("Invalid default-operation parameter.")); |
| 737 | break; |
| 738 | } |
| 739 | } else { |
| 740 | defop_type = 0; |
| 741 | } |
| 742 | |
| 743 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 744 | if (erropt != NULL) { |
| 745 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 746 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 747 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 748 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 749 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 750 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 751 | } else { |
| 752 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 753 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter.")); |
| 754 | break; |
| 755 | } |
| 756 | } else { |
| 757 | erropt_type = 0; |
| 758 | } |
| 759 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 760 | if (ds_type_t == -1) { |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 761 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 762 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 767 | if (config == NULL) { |
| 768 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 769 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter.")); |
| 770 | break; |
| 771 | } |
| 772 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 773 | if (netconf_editconfig(server, netconf_sessions_list, session_key, ds_type_t, defop_type, erropt_type, config) != EXIT_SUCCESS) { |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 774 | if (err_reply == NULL) { |
| 775 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 776 | json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed.")); |
| 777 | } else { |
| 778 | /* use filled err_reply from libnetconf's callback */ |
| 779 | json_object_put(reply); |
| 780 | reply = err_reply; |
| 781 | } |
| 782 | } else { |
| 783 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 784 | } |
| 785 | break; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 786 | case MSG_COPYCONFIG: |
| 787 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 788 | source = target = config = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 789 | |
| 790 | source = json_object_get_string(json_object_object_get(request, "source")); |
| 791 | target = json_object_get_string(json_object_object_get(request, "target")); |
| 792 | |
| 793 | reply = json_object_new_object(); |
| 794 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 795 | if (source == NULL) { |
| 796 | /* no explicit source specified -> use config data */ |
| 797 | ds_type_s = NC_DATASTORE_NONE; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 798 | config = json_object_get_string(json_object_object_get(request, "config")); |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 799 | } else if (ds_type_s == -1) { |
| 800 | /* source datastore specified, but it is invalid */ |
| 801 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 802 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 803 | break; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 804 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 805 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 806 | if (ds_type_t == -1) { |
| 807 | /* invalid target datastore specified */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 808 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 809 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 813 | if (source == NULL && config == NULL) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 814 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 815 | json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters - one of source and config is required.")); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 816 | } else { |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame^] | 817 | if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type_s, ds_type_t, config) != EXIT_SUCCESS) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 818 | if (err_reply == NULL) { |
| 819 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 820 | json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed.")); |
| 821 | } else { |
| 822 | /* use filled err_reply from libnetconf's callback */ |
| 823 | json_object_put(reply); |
| 824 | reply = err_reply; |
| 825 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 826 | } else { |
| 827 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 828 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 829 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 830 | break; |
| 831 | case MSG_DISCONNECT: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 832 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 833 | |
| 834 | reply = json_object_new_object(); |
| 835 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 836 | if (err_reply == NULL) { |
| 837 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 838 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 839 | } else { |
| 840 | /* use filled err_reply from libnetconf's callback */ |
| 841 | json_object_put(reply); |
| 842 | reply = err_reply; |
| 843 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 844 | } else { |
| 845 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 846 | } |
| 847 | break; |
| 848 | default: |
| 849 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
| 850 | reply = json_object_new_object(); |
| 851 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 852 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 853 | break; |
| 854 | } |
| 855 | json_object_put(request); |
| 856 | |
| 857 | /* send reply to caller */ |
| 858 | if (reply != NULL) { |
| 859 | msgtext = json_object_to_json_string(reply); |
| 860 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 861 | json_object_put(reply); |
| 862 | } else { |
| 863 | break; |
| 864 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | close(lsock); |
| 870 | |
| 871 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 872 | |
| 873 | exit(APR_SUCCESS); |
| 874 | } |
| 875 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 876 | static void *mod_netconf_create_conf(apr_pool_t * pool, server_rec * s) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 877 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 878 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 879 | |
| 880 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 881 | apr_pool_create(&config->pool, pool); |
| 882 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 883 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 884 | |
| 885 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 889 | apr_pool_t * plog, server_rec * s) |
| 890 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 891 | mod_netconf_cfg *config; |
| 892 | apr_status_t res; |
| 893 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 894 | /* These two help ensure that we only init once. */ |
| 895 | void *data; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 896 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * The following checks if this routine has been called before. |
| 900 | * This is necessary because the parent process gets initialized |
| 901 | * a couple of times as the server starts up. |
| 902 | */ |
| 903 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 904 | if (!data) { |
| 905 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 906 | return (OK); |
| 907 | } |
| 908 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 909 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 910 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 911 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 912 | if (config && config->forkproc == NULL) { |
| 913 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 914 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 915 | switch (res) { |
| 916 | case APR_INCHILD: |
| 917 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 918 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 919 | apr_signal(SIGTERM, signal_handler); |
| 920 | |
| 921 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 922 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf daemon started (PID %d)", getpid()); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 923 | |
| 924 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 925 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 926 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 927 | /* I never should be here, wtf?!? */ |
| 928 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf daemon unexpectedly stopped"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 929 | exit(APR_EGENERAL); |
| 930 | break; |
| 931 | case APR_INPARENT: |
| 932 | /* register child to be killed (SIGTERM) when the module config's pool dies */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 933 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 934 | break; |
| 935 | default: |
| 936 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 937 | break; |
| 938 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 939 | } else { |
| 940 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "mod_netconf misses configuration structure"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | return OK; |
| 944 | } |
| 945 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 946 | /** |
| 947 | * Register module hooks |
| 948 | */ |
| 949 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 950 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 951 | ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 954 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 955 | { |
| 956 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 957 | return NULL; |
| 958 | } |
| 959 | |
| 960 | static const command_rec netconf_cmds[] = { |
| 961 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 962 | {NULL} |
| 963 | }; |
| 964 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 965 | /* Dispatch list for API hooks */ |
| 966 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 967 | STANDARD20_MODULE_STUFF, |
| 968 | NULL, /* create per-dir config structures */ |
| 969 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 970 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 971 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 972 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 973 | mod_netconf_register_hooks /* register hooks */ |
| 974 | }; |