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 | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 277 | static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, const char* filter) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 278 | { |
| 279 | struct nc_session *session = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 280 | nc_rpc* rpc; |
| 281 | nc_reply* reply; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 282 | char* data; |
| 283 | struct nc_filter *f = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 284 | |
| 285 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 286 | if (session != NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 287 | /* create filter if set */ |
| 288 | if (filter != NULL) { |
| 289 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 290 | } |
| 291 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 292 | /* create requests */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 293 | rpc = nc_rpc_getconfig (source, f); |
| 294 | nc_filter_free(f); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 295 | if (rpc == NULL) { |
| 296 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 297 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 298 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 299 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 300 | /* send the request and get the reply */ |
| 301 | nc_session_send_rpc (session, rpc); |
| 302 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 303 | nc_rpc_free (rpc); |
| 304 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 305 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 306 | netconf_close(server, conns, session_key); |
| 307 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 308 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 309 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 310 | /* there is error handled by callback */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 311 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 312 | } |
| 313 | nc_rpc_free (rpc); |
| 314 | |
| 315 | switch (nc_reply_get_type (reply)) { |
| 316 | case NC_REPLY_DATA: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 317 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 318 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 319 | return (NULL); |
| 320 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 321 | break; |
| 322 | default: |
| 323 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 324 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 325 | } |
| 326 | nc_reply_free(reply); |
| 327 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 328 | return (data); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 329 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 330 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 331 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 332 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 335 | static char* netconf_get(server_rec* server, apr_hash_t* conns, char* session_key, const char* filter) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 336 | { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 337 | struct nc_session *session = NULL; |
| 338 | nc_rpc* rpc; |
| 339 | nc_reply* reply; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 340 | char* data = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 341 | struct nc_filter *f = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 342 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 343 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 344 | if (session != NULL) { |
| 345 | /* create filter if set */ |
| 346 | if (filter != NULL) { |
| 347 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 348 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 349 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 350 | /* create requests */ |
| 351 | rpc = nc_rpc_get (f); |
| 352 | nc_filter_free(f); |
| 353 | if (rpc == NULL) { |
| 354 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 355 | return (NULL); |
| 356 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 357 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 358 | /* send the request and get the reply */ |
| 359 | nc_session_send_rpc (session, rpc); |
| 360 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 361 | nc_rpc_free (rpc); |
| 362 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 363 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 364 | netconf_close(server, conns, session_key); |
| 365 | return (NULL); |
| 366 | } |
| 367 | |
| 368 | /* there is error handled by callback */ |
| 369 | return (NULL); |
| 370 | } |
| 371 | nc_rpc_free (rpc); |
| 372 | |
| 373 | switch (nc_reply_get_type (reply)) { |
| 374 | case NC_REPLY_DATA: |
| 375 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 376 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 377 | return (NULL); |
| 378 | } |
| 379 | break; |
| 380 | default: |
| 381 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 382 | data = NULL; /* error return code */ |
| 383 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 384 | } |
| 385 | nc_reply_free(reply); |
| 386 | |
| 387 | return (data); |
| 388 | } else { |
| 389 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 390 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 394 | static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config) |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 395 | { |
| 396 | struct nc_session *session = NULL; |
| 397 | nc_rpc* rpc; |
| 398 | nc_reply* reply; |
| 399 | int retval = EXIT_SUCCESS; |
| 400 | |
| 401 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 402 | if (session != NULL) { |
| 403 | /* create requests */ |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 404 | rpc = nc_rpc_copyconfig(source, target, config); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 405 | if (rpc == NULL) { |
| 406 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 407 | return (EXIT_FAILURE); |
| 408 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 409 | |
| 410 | /* send the request and get the reply */ |
| 411 | nc_session_send_rpc (session, rpc); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 412 | if (nc_session_recv_reply (session, &reply) == 0) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 413 | nc_rpc_free (rpc); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 414 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 415 | 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] | 416 | netconf_close(server, conns, session_key); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 417 | return (EXIT_FAILURE); |
| 418 | } |
| 419 | |
| 420 | /* there is error handled by callback */ |
| 421 | return (EXIT_FAILURE); |
| 422 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 423 | nc_rpc_free (rpc); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 424 | |
| 425 | switch (nc_reply_get_type (reply)) { |
| 426 | case NC_REPLY_OK: |
| 427 | retval = EXIT_SUCCESS; |
| 428 | break; |
| 429 | default: |
| 430 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 431 | retval = EXIT_FAILURE; |
| 432 | break; |
| 433 | } |
| 434 | nc_reply_free(reply); |
| 435 | return (retval); |
| 436 | } else { |
| 437 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 438 | return (EXIT_FAILURE); |
| 439 | } |
| 440 | } |
| 441 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 442 | server_rec* clb_print_server; |
| 443 | int clb_print(const char* msg) |
| 444 | { |
| 445 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 446 | return (0); |
| 447 | } |
| 448 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 449 | /* |
| 450 | * This is actually implementation of NETCONF client |
| 451 | * - requests are received from UNIX socket in the predefined format |
| 452 | * - results are replied through the same way |
| 453 | * - the daemon run as a separate process, but it is started and stopped |
| 454 | * automatically by Apache. |
| 455 | * |
| 456 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 457 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 458 | { |
| 459 | struct sockaddr_un local, remote; |
| 460 | int lsock, client; |
| 461 | socklen_t len, len2; |
| 462 | struct pollfd fds; |
| 463 | int status; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 464 | mod_netconf_cfg *cfg; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 465 | json_object *request, *reply; |
| 466 | int operation; |
| 467 | char* session_key, *data; |
| 468 | const char *msgtext; |
| 469 | const char *host, *port, *user, *pass; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 470 | const char *target, *source, *filter, *config; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 471 | NC_DATASTORE ds_type1, ds_type2; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 472 | |
| 473 | apr_hash_t *netconf_sessions_list; |
| 474 | char buffer[BUFFER_SIZE]; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 475 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 476 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 477 | unixd_setup_child(); |
| 478 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 479 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 480 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 481 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 482 | return; |
| 483 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 485 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 486 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 487 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 492 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 493 | unlink(local.sun_path); |
| 494 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 495 | |
| 496 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 497 | if (errno == EADDRINUSE) { |
| 498 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 499 | close(lsock); |
| 500 | exit(0); |
| 501 | } |
| 502 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 503 | close(lsock); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 508 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 509 | close(lsock); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | /* prepare internal lists */ |
| 514 | netconf_sessions_list = apr_hash_make(pool); |
| 515 | |
| 516 | /* setup libnetconf's callbacks */ |
| 517 | nc_verbosity(NC_VERB_DEBUG); |
| 518 | clb_print_server = server; |
| 519 | nc_callback_print(clb_print); |
| 520 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 521 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 522 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 523 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 524 | |
| 525 | /* disable publickey authentication */ |
| 526 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 527 | |
| 528 | while (isterminated == 0) { |
| 529 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 530 | |
| 531 | /* open incoming connection if any */ |
| 532 | len2 = sizeof(remote); |
| 533 | client = accept(lsock, (struct sockaddr *) &remote, &len2); |
| 534 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 535 | apr_sleep(SLEEP_TIME); |
| 536 | continue; |
| 537 | } else if (client == -1 && (errno == EINTR)) { |
| 538 | continue; |
| 539 | } else if (client == -1) { |
| 540 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 541 | continue; |
| 542 | } |
| 543 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted."); |
| 544 | |
| 545 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 546 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 547 | |
| 548 | while (1) { |
| 549 | fds.fd = client; |
| 550 | fds.events = POLLIN; |
| 551 | fds.revents = 0; |
| 552 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 553 | status = poll(&fds, 1, 1000); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 554 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 555 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 556 | /* 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] | 557 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 558 | continue; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 559 | } else if (status < 0) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 560 | /* 0: poll time outed |
| 561 | * close socket and ignore this request from the client, it can try it again |
| 562 | * -1: poll failed |
| 563 | * something wrong happend, close this socket and wait for another request |
| 564 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 565 | //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] | 566 | close(client); |
| 567 | break; |
| 568 | } |
| 569 | /* status > 0 */ |
| 570 | |
| 571 | /* check the status of the socket */ |
| 572 | |
| 573 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 574 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 575 | /* close client's socket (it's probably already closed by client */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 576 | //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] | 577 | close(client); |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) { |
| 582 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno)); |
| 583 | continue; |
| 584 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 585 | request = json_tokener_parse(buffer); |
| 586 | operation = json_object_get_int(json_object_object_get(request, "type")); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 587 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 588 | session_key = (char*) json_object_get_string(json_object_object_get(request, "session")); |
| 589 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 590 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 591 | reply = json_object_new_object(); |
| 592 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 593 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 594 | msgtext = json_object_to_json_string(reply); |
| 595 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 596 | json_object_put(reply); |
| 597 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 598 | close(client); |
| 599 | break; |
| 600 | } |
| 601 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 602 | /* null global JSON error-reply */ |
| 603 | err_reply = NULL; |
| 604 | |
| 605 | /* process required operation */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 606 | switch (operation) { |
| 607 | case MSG_CONNECT: |
| 608 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 609 | |
| 610 | host = json_object_get_string(json_object_object_get(request, "host")); |
| 611 | port = json_object_get_string(json_object_object_get(request, "port")); |
| 612 | user = json_object_get_string(json_object_object_get(request, "user")); |
| 613 | pass = json_object_get_string(json_object_object_get(request, "pass")); |
| 614 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 615 | session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass); |
| 616 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key); |
| 617 | |
| 618 | reply = json_object_new_object(); |
| 619 | if (session_key == NULL) { |
| 620 | /* negative reply */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 621 | if (err_reply == NULL) { |
| 622 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 623 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 624 | } else { |
| 625 | /* use filled err_reply from libnetconf's callback */ |
| 626 | json_object_put(reply); |
| 627 | reply = err_reply; |
| 628 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 629 | } else { |
| 630 | /* positive reply */ |
| 631 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 632 | json_object_object_add(reply, "session", json_object_new_string(session_key)); |
Radek Krejci | e31ad21 | 2012-07-26 12:51:15 +0200 | [diff] [blame^] | 633 | |
| 634 | free(session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 637 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 638 | case MSG_GET: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 639 | 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] | 640 | |
| 641 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 642 | |
| 643 | reply = json_object_new_object(); |
| 644 | |
| 645 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 646 | if (err_reply == NULL) { |
| 647 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 648 | json_object_object_add(reply, "error-message", json_object_new_string("get failed.")); |
| 649 | } else { |
| 650 | /* use filled err_reply from libnetconf's callback */ |
| 651 | json_object_put(reply); |
| 652 | reply = err_reply; |
| 653 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 654 | } else { |
| 655 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 656 | json_object_object_add(reply, "data", json_object_new_string(data)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 657 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 658 | break; |
| 659 | case MSG_GETCONFIG: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 660 | 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] | 661 | |
| 662 | source = json_object_get_string(json_object_object_get(request, "source")); |
| 663 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 664 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 665 | reply = json_object_new_object(); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 666 | |
| 667 | if (strcmp(source, "running") == 0) { |
| 668 | ds_type1 = NC_DATASTORE_RUNNING; |
| 669 | } else if (strcmp(source, "startup") == 0) { |
| 670 | ds_type1 = NC_DATASTORE_STARTUP; |
| 671 | } else if (strcmp(source, "candidate") == 0) { |
| 672 | ds_type1 = NC_DATASTORE_CANDIDATE; |
| 673 | } else { |
| 674 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 675 | 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] | 676 | break; |
| 677 | } |
| 678 | |
| 679 | if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type1, filter)) == NULL) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 680 | if (err_reply == NULL) { |
| 681 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 682 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 683 | } else { |
| 684 | /* use filled err_reply from libnetconf's callback */ |
| 685 | json_object_put(reply); |
| 686 | reply = err_reply; |
| 687 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 688 | } else { |
| 689 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 690 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 691 | } |
| 692 | break; |
| 693 | case MSG_COPYCONFIG: |
| 694 | 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] | 695 | source = target = config = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 696 | |
| 697 | source = json_object_get_string(json_object_object_get(request, "source")); |
| 698 | target = json_object_get_string(json_object_object_get(request, "target")); |
| 699 | |
| 700 | reply = json_object_new_object(); |
| 701 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 702 | if (source != NULL) { |
| 703 | if (strcmp(source, "running") == 0) { |
| 704 | ds_type1 = NC_DATASTORE_RUNNING; |
| 705 | } else if (strcmp(source, "startup") == 0) { |
| 706 | ds_type1 = NC_DATASTORE_STARTUP; |
| 707 | } else if (strcmp(source, "candidate") == 0) { |
| 708 | ds_type1 = NC_DATASTORE_CANDIDATE; |
| 709 | } else { |
| 710 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 711 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 712 | break; |
| 713 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 714 | } else { |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 715 | ds_type1 = NC_DATASTORE_NONE; |
| 716 | config = json_object_get_string(json_object_object_get(request, "config")); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 717 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 718 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 719 | if (strcmp(target, "running") == 0) { |
| 720 | ds_type2 = NC_DATASTORE_RUNNING; |
| 721 | } else if (strcmp(target, "startup") == 0) { |
| 722 | ds_type2 = NC_DATASTORE_STARTUP; |
| 723 | } else if (strcmp(target, "candidate") == 0) { |
| 724 | ds_type2 = NC_DATASTORE_CANDIDATE; |
| 725 | } else { |
| 726 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 727 | 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] | 728 | break; |
| 729 | } |
| 730 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 731 | if (target == NULL || (source == NULL && config == NULL)) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 732 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 733 | json_object_object_add(reply, "error-message", json_object_new_string("invalid input parameters.")); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 734 | } else { |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 735 | if (netconf_copyconfig(server, netconf_sessions_list, session_key, ds_type1, ds_type2, config) != EXIT_SUCCESS) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 736 | if (err_reply == NULL) { |
| 737 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 738 | json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed.")); |
| 739 | } else { |
| 740 | /* use filled err_reply from libnetconf's callback */ |
| 741 | json_object_put(reply); |
| 742 | reply = err_reply; |
| 743 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 744 | } else { |
| 745 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 746 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 747 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 748 | break; |
| 749 | case MSG_DISCONNECT: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 750 | 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] | 751 | |
| 752 | reply = json_object_new_object(); |
| 753 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 754 | if (err_reply == NULL) { |
| 755 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 756 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 757 | } else { |
| 758 | /* use filled err_reply from libnetconf's callback */ |
| 759 | json_object_put(reply); |
| 760 | reply = err_reply; |
| 761 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 762 | } else { |
| 763 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 764 | } |
| 765 | break; |
| 766 | default: |
| 767 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
| 768 | reply = json_object_new_object(); |
| 769 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 770 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 771 | break; |
| 772 | } |
| 773 | json_object_put(request); |
| 774 | |
| 775 | /* send reply to caller */ |
| 776 | if (reply != NULL) { |
| 777 | msgtext = json_object_to_json_string(reply); |
| 778 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 779 | json_object_put(reply); |
| 780 | } else { |
| 781 | break; |
| 782 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | close(lsock); |
| 788 | |
| 789 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 790 | |
| 791 | exit(APR_SUCCESS); |
| 792 | } |
| 793 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 794 | 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] | 795 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 796 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 797 | |
| 798 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 799 | apr_pool_create(&config->pool, pool); |
| 800 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 801 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 802 | |
| 803 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 807 | apr_pool_t * plog, server_rec * s) |
| 808 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 809 | mod_netconf_cfg *config; |
| 810 | apr_status_t res; |
| 811 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 812 | /* These two help ensure that we only init once. */ |
| 813 | void *data; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 814 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * The following checks if this routine has been called before. |
| 818 | * This is necessary because the parent process gets initialized |
| 819 | * a couple of times as the server starts up. |
| 820 | */ |
| 821 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 822 | if (!data) { |
| 823 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 824 | return (OK); |
| 825 | } |
| 826 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 827 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 828 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 829 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 830 | if (config && config->forkproc == NULL) { |
| 831 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 832 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 833 | switch (res) { |
| 834 | case APR_INCHILD: |
| 835 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 836 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 837 | apr_signal(SIGTERM, signal_handler); |
| 838 | |
| 839 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 840 | 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] | 841 | |
| 842 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 843 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 844 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 845 | /* I never should be here, wtf?!? */ |
| 846 | 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] | 847 | exit(APR_EGENERAL); |
| 848 | break; |
| 849 | case APR_INPARENT: |
| 850 | /* 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] | 851 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 852 | break; |
| 853 | default: |
| 854 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 855 | break; |
| 856 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 857 | } else { |
| 858 | 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] | 859 | } |
| 860 | |
| 861 | return OK; |
| 862 | } |
| 863 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 864 | /** |
| 865 | * Register module hooks |
| 866 | */ |
| 867 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 868 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 869 | 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] | 870 | } |
| 871 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 872 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 873 | { |
| 874 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 875 | return NULL; |
| 876 | } |
| 877 | |
| 878 | static const command_rec netconf_cmds[] = { |
| 879 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 880 | {NULL} |
| 881 | }; |
| 882 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 883 | /* Dispatch list for API hooks */ |
| 884 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 885 | STANDARD20_MODULE_STUFF, |
| 886 | NULL, /* create per-dir config structures */ |
| 887 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 888 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 889 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 890 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 891 | mod_netconf_register_hooks /* register hooks */ |
| 892 | }; |